AP_License::ap_product_license()

Description #

AnsPress license form.

Source #

File: admin/license.php

	 * AnsPress license form.
	 */
	public static function ap_product_license() {
		if ( ! current_user_can( 'manage_options' ) || ! ap_verify_nonce( 'ap_licenses_nonce' ) ) {
			return;
		}

		$licenses = get_option( 'anspress_license', array() );
		$fields = ap_product_license_fields();

		if ( empty( $fields ) ) {
			return;
		}

		if ( ap_isset_post_value( 'save_licenses' ) ) {
			foreach ( (array) $fields as $slug => $prod ) {
				$prod_license = ap_isset_post_value( 'ap_license_' . $slug, '' );
				if ( ! empty( $prod_license ) && ! isset( $licenses[ $slug ] ) || $prod_license !== $licenses[ $slug ]['key'] ) {
					$licenses[ $slug ] = array(
						'key'    => trim( ap_sanitize_unslash( 'ap_license_' . $slug, 'g', '' ) ),
						'status' => false,
					);

					update_option( 'anspress_license', $licenses );
				}
			}
		}

		foreach ( (array) $fields as $slug => $prod ) {

			// Data to send in our API request.
			$api_params = array(
				'license' 	=> $licenses[ $slug ]['key'],
				'item_name' => rawurlencode( $prod['name'] ),
				'url'       => home_url(),
				'anspress_ver' => AP_VERSION,
			);

			// Check if activate is clicked.
			if ( ap_isset_post_value( 'ap_license_activate_' . $slug ) ) {
				$api_params['edd_action'] = 'activate_license';

				// Call the custom API.
				$response = wp_remote_post( 'https://anspress.io', array( 'timeout' => 15, 'sslverify' => true, 'body' => $api_params ) );

				// Make sure the response came back okay.
				if ( ! is_wp_error( $response ) ) {
					// Decode the license data.
					$license_data = json_decode( wp_remote_retrieve_body( $response ) );

					$licenses[ $slug ]['status'] = sanitize_text_field( $license_data->license );
					update_option( 'anspress_license', $licenses );
				}
			}

			// Check if deactivate is clicked.
			if ( ap_isset_post_value( 'ap_license_deactivate_' . $slug ) ) {
				$api_params['edd_action'] = 'deactivate_license';

				// Call the custom API.
				$response = wp_remote_post( 'https://anspress.io', array( 'timeout' => 15, 'sslverify' => true, 'body' => $api_params ) );

				// Make sure the response came back okay.
				if ( ! is_wp_error( $response ) ) {
					// Decode the license data.
					$license_data = json_decode( wp_remote_retrieve_body( $response ) );
					$licenses[ $slug ]['status'] = sanitize_text_field( $license_data->license );
					update_option( 'anspress_license', $licenses );
				}
			}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment