Tribe__PUE__Checker::validate_key( string $key, bool $network = false )

Checks for the license key status with MT servers.


Parameters

$key

(string) (Required) The license key to check.

$network

(bool) (Optional) Whether the key to check for is a network one or not.

Default value: false


Top ↑

Return

(array) An associative array containing the license status response.


Top ↑

Source

File: src/Tribe/PUE/Checker.php

		public function validate_key( $key, $network = false ) {
			$response           = array();
			$response['status'] = 0;

			if ( ! $key ) {
				$response['message'] = sprintf( esc_html__( 'Hmmm... something\'s wrong with this validator. Please contact %ssupport%s.', 'tribe-common' ), '<a href="https://m.tri.be/1u">', '</a>' );
				return $response;
			}

			$query_args = $this->get_validate_query();

			$query_args['key'] = sanitize_text_field( $key );

			// This method is primarily used during when validating keys by ajax, before they are
			// formally committed or saved by the user: for that reason we call request_info()
			// rather than license_key_status() as at this stage invalid or missing keys should
			// not result in admin notices being generated
			$plugin_info = $this->request_info( $query_args );
			$expiration = isset( $plugin_info->expiration ) ? $plugin_info->expiration : __( 'unknown date', 'tribe-common' );

			$pue_notices = Tribe__Main::instance()->pue_notices();
			$plugin_name = $this->get_plugin_name();

			if ( empty( $plugin_info ) ) {
				$response['message'] = __( 'Sorry, key validation server is not available.', 'tribe-common' );
			} elseif ( isset( $plugin_info->api_expired ) && 1 === (int) $plugin_info->api_expired ) {
				$response['message'] = $this->get_license_expired_message();
				$response['api_expired'] = true;
			} elseif ( isset( $plugin_info->api_upgrade ) && 1 === (int) $plugin_info->api_upgrade ) {
				$response['message'] = $this->get_api_message( $plugin_info );
				$response['api_upgrade'] = true;
			} elseif ( isset( $plugin_info->api_invalid ) && 1 === (int) $plugin_info->api_invalid ) {
				$response['message'] = $this->get_api_message( $plugin_info );
				$response['api_invalid'] = true;
			} else {
				$key_type = 'local';

				if ( $network ) {
					$key_type = 'network';
				}

				$current_install_key = $this->get_key( $key_type );

				if ( $current_install_key && $current_install_key === $query_args['key'] ) {
					$default_success_msg = esc_html( sprintf( __( 'Valid Key! Expires on %s', 'tribe-common' ), $expiration ) );
				} else {
					// Set the key
					$this->update_key( $query_args['key'], $key_type );

					$default_success_msg = esc_html( sprintf( __( 'Thanks for setting up a valid key. It will expire on %s', 'tribe-common' ), $expiration ) );

					//Set SysInfo Key on Tec.com After Successful Validation of License
					$optin_key = get_option( 'tribe_systeminfo_optin' );
					if ( $optin_key ) {
						Tribe__Support::send_sysinfo_key( $optin_key, $query_args['domain'], false, true );
					}
				}

				$pue_notices->clear_notices( $plugin_name );

				$response['status']     = isset( $plugin_info->api_message ) ? 2 : 1;
				$response['message']    = isset( $plugin_info->api_message ) ? $plugin_info->api_message : $default_success_msg;
				$response['expiration'] = esc_html( $expiration );

				if ( isset( $plugin_info->daily_limit ) ) {
					$response['daily_limit'] = intval( $plugin_info->daily_limit );
				}
			}

			$response['message'] = wp_kses( $response['message'], 'data' );

			return $response;
		}