@@ -4,35 +4,35 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Options_Cache implements Cache { |
| 6 | 6 | |
| 7 | - private $namespace; |
|
| 7 | + private $namespace; |
|
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Options_Cache constructor. |
|
| 11 | - * |
|
| 12 | - * @param $namespace |
|
| 13 | - */ |
|
| 14 | - public function __construct( $namespace ) { |
|
| 15 | - $this->namespace = $namespace; |
|
| 16 | - } |
|
| 9 | + /** |
|
| 10 | + * Options_Cache constructor. |
|
| 11 | + * |
|
| 12 | + * @param $namespace |
|
| 13 | + */ |
|
| 14 | + public function __construct( $namespace ) { |
|
| 15 | + $this->namespace = $namespace; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function get( $cache_key ) { |
|
| 18 | + public function get( $cache_key ) { |
|
| 19 | 19 | |
| 20 | - return get_option( $this->namespace . '__' . $cache_key, false ); |
|
| 20 | + return get_option( $this->namespace . '__' . $cache_key, false ); |
|
| 21 | 21 | |
| 22 | - } |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function put( $cache_key, $value ) { |
|
| 24 | + public function put( $cache_key, $value ) { |
|
| 25 | 25 | |
| 26 | - return update_option( $this->namespace . '__' . $cache_key, $value, false ); |
|
| 26 | + return update_option( $this->namespace . '__' . $cache_key, $value, false ); |
|
| 27 | 27 | |
| 28 | - } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - public function flush_all() { |
|
| 31 | - if ( '' !== $this->namespace ) { |
|
| 32 | - global $wpdb; |
|
| 33 | - $namespace_esc = $wpdb->esc_like( $this->namespace ) . '__%'; |
|
| 34 | - $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s", $namespace_esc ) ); |
|
| 35 | - } |
|
| 36 | - } |
|
| 30 | + public function flush_all() { |
|
| 31 | + if ( '' !== $this->namespace ) { |
|
| 32 | + global $wpdb; |
|
| 33 | + $namespace_esc = $wpdb->esc_like( $this->namespace ) . '__%'; |
|
| 34 | + $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s", $namespace_esc ) ); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | } |
@@ -8,101 +8,101 @@ |
||
| 8 | 8 | use Wordlift\Task\Task; |
| 9 | 9 | |
| 10 | 10 | class Background_Task extends Action_Scheduler_Background_Process implements Background_Route_Task { |
| 11 | - /** |
|
| 12 | - * The option prefix to store state. |
|
| 13 | - * |
|
| 14 | - * @var string $option_prefix |
|
| 15 | - */ |
|
| 16 | - private $option_prefix; |
|
| 17 | - /** |
|
| 18 | - * @var Task |
|
| 19 | - */ |
|
| 20 | - private $task; |
|
| 21 | - |
|
| 22 | - const STATE_STARTED = 'started'; |
|
| 23 | - const STATE_STOPPED = 'stopped'; |
|
| 24 | - /** |
|
| 25 | - * @var int |
|
| 26 | - */ |
|
| 27 | - private $batch_size; |
|
| 28 | - |
|
| 29 | - public function __construct( $hook, $group, $task, $option_prefix, $batch_size = 5 ) { |
|
| 30 | - parent::__construct( $hook, $group ); |
|
| 31 | - $this->task = $task; |
|
| 32 | - $this->option_prefix = $option_prefix; |
|
| 33 | - $this->batch_size = $batch_size; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 37 | - public function do_task( $args ) { |
|
| 38 | - if ( self::STATE_STOPPED === $this->get_process_state() ) { |
|
| 39 | - return State::complete(); |
|
| 40 | - } |
|
| 41 | - $context = $this->get_context(); |
|
| 42 | - $this->task->tick( null, $context->get_data() + array( 'batch_size' => $this->batch_size ) ); |
|
| 43 | - |
|
| 44 | - if ( ( $context->get_count() - $context->get_offset() ) >= 0 ) { |
|
| 45 | - $context->set_offset( $context->get_offset() + $this->batch_size )->set_updated( time() ); |
|
| 46 | - $this->set_info( $context ); |
|
| 47 | - return State::items_in_queue(); |
|
| 48 | - } else { |
|
| 49 | - $this->set_process_state( self::STATE_STOPPED ); |
|
| 50 | - return State::complete(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function start() { |
|
| 56 | - $this->delete_info(); |
|
| 57 | - $this->set_process_state( self::STATE_STARTED ); |
|
| 58 | - $this->schedule(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function stop() { |
|
| 62 | - $this->set_process_state( self::STATE_STOPPED ); |
|
| 63 | - $this->unschedule(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function resume() { |
|
| 67 | - $this->set_process_state( self::STATE_STARTED ); |
|
| 68 | - $this->schedule(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function get_info() { |
|
| 72 | - return $this->get_context()->get_data() + array( 'state' => $this->get_process_state() ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function get_context() { |
|
| 76 | - $data = get_option( |
|
| 77 | - "{$this->option_prefix}_state", |
|
| 78 | - null |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - if ( null === $data ) { |
|
| 82 | - return Context::from( (int) $this->task->starting() ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return Context::from_data( $data ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param $context Context |
|
| 90 | - * |
|
| 91 | - * @return void |
|
| 92 | - */ |
|
| 93 | - public function set_info( $context ) { |
|
| 94 | - update_option( "{$this->option_prefix}_state", $context->get_data(), false ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - private function delete_info() { |
|
| 98 | - delete_option( "{$this->option_prefix}_state" ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - private function get_process_state() { |
|
| 102 | - return get_option( "{$this->option_prefix}_action_scheduler_state", self::STATE_STOPPED ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - private function set_process_state( $state ) { |
|
| 106 | - update_option( "{$this->option_prefix}_action_scheduler_state", $state ); |
|
| 107 | - } |
|
| 11 | + /** |
|
| 12 | + * The option prefix to store state. |
|
| 13 | + * |
|
| 14 | + * @var string $option_prefix |
|
| 15 | + */ |
|
| 16 | + private $option_prefix; |
|
| 17 | + /** |
|
| 18 | + * @var Task |
|
| 19 | + */ |
|
| 20 | + private $task; |
|
| 21 | + |
|
| 22 | + const STATE_STARTED = 'started'; |
|
| 23 | + const STATE_STOPPED = 'stopped'; |
|
| 24 | + /** |
|
| 25 | + * @var int |
|
| 26 | + */ |
|
| 27 | + private $batch_size; |
|
| 28 | + |
|
| 29 | + public function __construct( $hook, $group, $task, $option_prefix, $batch_size = 5 ) { |
|
| 30 | + parent::__construct( $hook, $group ); |
|
| 31 | + $this->task = $task; |
|
| 32 | + $this->option_prefix = $option_prefix; |
|
| 33 | + $this->batch_size = $batch_size; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 37 | + public function do_task( $args ) { |
|
| 38 | + if ( self::STATE_STOPPED === $this->get_process_state() ) { |
|
| 39 | + return State::complete(); |
|
| 40 | + } |
|
| 41 | + $context = $this->get_context(); |
|
| 42 | + $this->task->tick( null, $context->get_data() + array( 'batch_size' => $this->batch_size ) ); |
|
| 43 | + |
|
| 44 | + if ( ( $context->get_count() - $context->get_offset() ) >= 0 ) { |
|
| 45 | + $context->set_offset( $context->get_offset() + $this->batch_size )->set_updated( time() ); |
|
| 46 | + $this->set_info( $context ); |
|
| 47 | + return State::items_in_queue(); |
|
| 48 | + } else { |
|
| 49 | + $this->set_process_state( self::STATE_STOPPED ); |
|
| 50 | + return State::complete(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function start() { |
|
| 56 | + $this->delete_info(); |
|
| 57 | + $this->set_process_state( self::STATE_STARTED ); |
|
| 58 | + $this->schedule(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function stop() { |
|
| 62 | + $this->set_process_state( self::STATE_STOPPED ); |
|
| 63 | + $this->unschedule(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function resume() { |
|
| 67 | + $this->set_process_state( self::STATE_STARTED ); |
|
| 68 | + $this->schedule(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function get_info() { |
|
| 72 | + return $this->get_context()->get_data() + array( 'state' => $this->get_process_state() ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function get_context() { |
|
| 76 | + $data = get_option( |
|
| 77 | + "{$this->option_prefix}_state", |
|
| 78 | + null |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + if ( null === $data ) { |
|
| 82 | + return Context::from( (int) $this->task->starting() ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return Context::from_data( $data ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param $context Context |
|
| 90 | + * |
|
| 91 | + * @return void |
|
| 92 | + */ |
|
| 93 | + public function set_info( $context ) { |
|
| 94 | + update_option( "{$this->option_prefix}_state", $context->get_data(), false ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + private function delete_info() { |
|
| 98 | + delete_option( "{$this->option_prefix}_state" ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + private function get_process_state() { |
|
| 102 | + return get_option( "{$this->option_prefix}_action_scheduler_state", self::STATE_STOPPED ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + private function set_process_state( $state ) { |
|
| 106 | + update_option( "{$this->option_prefix}_action_scheduler_state", $state ); |
|
| 107 | + } |
|
| 108 | 108 | } |
@@ -21,186 +21,186 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Key_Validation_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * A {@link Wordlift_Log_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.14.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $log; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var Ttl_Cache |
|
| 35 | - */ |
|
| 36 | - private $ttl_cache_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | - * |
|
| 41 | - * @since 3.14.0 |
|
| 42 | - */ |
|
| 43 | - public function __construct() { |
|
| 44 | - |
|
| 45 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 46 | - |
|
| 47 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 48 | - /** |
|
| 49 | - * Filter: wl_feature__enable__notices. |
|
| 50 | - * |
|
| 51 | - * @param bool whether notices need to be enabled or not. |
|
| 52 | - * |
|
| 53 | - * @return bool |
|
| 54 | - * @since 3.27.6 |
|
| 55 | - */ |
|
| 56 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 57 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Validate the provided key. |
|
| 66 | - * |
|
| 67 | - * @param string $key WordLift's key to validate. |
|
| 68 | - * |
|
| 69 | - * @return WP_Error|array The response or WP_Error on failure. |
|
| 70 | - * @since 3.9.0 |
|
| 71 | - */ |
|
| 72 | - public function get_account_info( $key ) { |
|
| 73 | - |
|
| 74 | - $this->log->debug( 'Validating key...' ); |
|
| 75 | - |
|
| 76 | - $response = Default_Api_Service::get_instance()->get( |
|
| 77 | - '/accounts/info', |
|
| 78 | - array( |
|
| 79 | - 'Authorization' => "Key $key", |
|
| 80 | - ) |
|
| 81 | - ); |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @since 3.38.5 |
|
| 85 | - * This action is fired when the key is validated. |
|
| 86 | - * @param $response \Wordlift\Api\Response |
|
| 87 | - */ |
|
| 88 | - do_action( 'wl_key_validation_response', $response ); |
|
| 89 | - |
|
| 90 | - return $response->get_response(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - private function key_validation_request( $key ) { |
|
| 94 | - $response = $this->get_account_info( $key ); |
|
| 95 | - |
|
| 96 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 97 | - throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 101 | - |
|
| 102 | - $url = $res_body['url']; |
|
| 103 | - |
|
| 104 | - $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
| 105 | - $plugin_features = array( Entity_Type_Setter::STARTER_PLAN, Entity_Type_Setter::PROFESSIONAL_PLAN, Entity_Type_Setter::BUSINESS_PLAN ); |
|
| 106 | - |
|
| 107 | - if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
| 108 | - throw new \Exception( __( 'This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift' ) ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // Considering that production URL may be filtered. |
|
| 112 | - $home_url = get_option( 'home' ); |
|
| 113 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 114 | - |
|
| 115 | - if ( $url !== $site_url ) { |
|
| 116 | - throw new \Exception( __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ) ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return true; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Check if key is valid |
|
| 124 | - * |
|
| 125 | - * @param $key string |
|
| 126 | - * |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function is_key_valid( $key ) { |
|
| 130 | - try { |
|
| 131 | - $this->key_validation_request( $key ); |
|
| 132 | - return true; |
|
| 133 | - } catch ( \Exception $e ) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 140 | - * |
|
| 141 | - * @since 3.9.0 |
|
| 142 | - */ |
|
| 143 | - public function validate_key() { |
|
| 144 | - |
|
| 145 | - // Ensure we don't have garbage before us. |
|
| 146 | - ob_clean(); |
|
| 147 | - |
|
| 148 | - // Check if we have a key. |
|
| 149 | - if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 150 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 154 | - |
|
| 155 | - try { |
|
| 156 | - $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 157 | - wp_send_json_success( |
|
| 158 | - array( |
|
| 159 | - 'valid' => true, |
|
| 160 | - 'message' => '', |
|
| 161 | - ) |
|
| 162 | - ); |
|
| 163 | - |
|
| 164 | - } catch ( \Exception $e ) { |
|
| 165 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 166 | - wp_send_json_success( |
|
| 167 | - array( |
|
| 168 | - 'valid' => false, |
|
| 169 | - 'message' => $e->getMessage(), |
|
| 170 | - 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
| 171 | - ) |
|
| 172 | - ); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 178 | - */ |
|
| 179 | - public function wl_load_plugin() { |
|
| 180 | - |
|
| 181 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 182 | - $home_url = get_option( 'home' ); |
|
| 183 | - |
|
| 184 | - if ( ! $wl_blog_url ) { |
|
| 185 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 186 | - } elseif ( $wl_blog_url !== $home_url ) { |
|
| 187 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 188 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 189 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * This function is hooked to the `admin_notices` to show admin notification. |
|
| 196 | - */ |
|
| 197 | - public function wl_key_update_notice() { |
|
| 198 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 199 | - ?> |
|
| 24 | + /** |
|
| 25 | + * A {@link Wordlift_Log_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.14.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $log; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var Ttl_Cache |
|
| 35 | + */ |
|
| 36 | + private $ttl_cache_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | + * |
|
| 41 | + * @since 3.14.0 |
|
| 42 | + */ |
|
| 43 | + public function __construct() { |
|
| 44 | + |
|
| 45 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 46 | + |
|
| 47 | + add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 48 | + /** |
|
| 49 | + * Filter: wl_feature__enable__notices. |
|
| 50 | + * |
|
| 51 | + * @param bool whether notices need to be enabled or not. |
|
| 52 | + * |
|
| 53 | + * @return bool |
|
| 54 | + * @since 3.27.6 |
|
| 55 | + */ |
|
| 56 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 57 | + add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Validate the provided key. |
|
| 66 | + * |
|
| 67 | + * @param string $key WordLift's key to validate. |
|
| 68 | + * |
|
| 69 | + * @return WP_Error|array The response or WP_Error on failure. |
|
| 70 | + * @since 3.9.0 |
|
| 71 | + */ |
|
| 72 | + public function get_account_info( $key ) { |
|
| 73 | + |
|
| 74 | + $this->log->debug( 'Validating key...' ); |
|
| 75 | + |
|
| 76 | + $response = Default_Api_Service::get_instance()->get( |
|
| 77 | + '/accounts/info', |
|
| 78 | + array( |
|
| 79 | + 'Authorization' => "Key $key", |
|
| 80 | + ) |
|
| 81 | + ); |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @since 3.38.5 |
|
| 85 | + * This action is fired when the key is validated. |
|
| 86 | + * @param $response \Wordlift\Api\Response |
|
| 87 | + */ |
|
| 88 | + do_action( 'wl_key_validation_response', $response ); |
|
| 89 | + |
|
| 90 | + return $response->get_response(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + private function key_validation_request( $key ) { |
|
| 94 | + $response = $this->get_account_info( $key ); |
|
| 95 | + |
|
| 96 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 97 | + throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 101 | + |
|
| 102 | + $url = $res_body['url']; |
|
| 103 | + |
|
| 104 | + $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
| 105 | + $plugin_features = array( Entity_Type_Setter::STARTER_PLAN, Entity_Type_Setter::PROFESSIONAL_PLAN, Entity_Type_Setter::BUSINESS_PLAN ); |
|
| 106 | + |
|
| 107 | + if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
| 108 | + throw new \Exception( __( 'This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift' ) ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // Considering that production URL may be filtered. |
|
| 112 | + $home_url = get_option( 'home' ); |
|
| 113 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 114 | + |
|
| 115 | + if ( $url !== $site_url ) { |
|
| 116 | + throw new \Exception( __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ) ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return true; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Check if key is valid |
|
| 124 | + * |
|
| 125 | + * @param $key string |
|
| 126 | + * |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function is_key_valid( $key ) { |
|
| 130 | + try { |
|
| 131 | + $this->key_validation_request( $key ); |
|
| 132 | + return true; |
|
| 133 | + } catch ( \Exception $e ) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 140 | + * |
|
| 141 | + * @since 3.9.0 |
|
| 142 | + */ |
|
| 143 | + public function validate_key() { |
|
| 144 | + |
|
| 145 | + // Ensure we don't have garbage before us. |
|
| 146 | + ob_clean(); |
|
| 147 | + |
|
| 148 | + // Check if we have a key. |
|
| 149 | + if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 150 | + wp_send_json_error( 'The key parameter is required.' ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 154 | + |
|
| 155 | + try { |
|
| 156 | + $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 157 | + wp_send_json_success( |
|
| 158 | + array( |
|
| 159 | + 'valid' => true, |
|
| 160 | + 'message' => '', |
|
| 161 | + ) |
|
| 162 | + ); |
|
| 163 | + |
|
| 164 | + } catch ( \Exception $e ) { |
|
| 165 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 166 | + wp_send_json_success( |
|
| 167 | + array( |
|
| 168 | + 'valid' => false, |
|
| 169 | + 'message' => $e->getMessage(), |
|
| 170 | + 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
| 171 | + ) |
|
| 172 | + ); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 178 | + */ |
|
| 179 | + public function wl_load_plugin() { |
|
| 180 | + |
|
| 181 | + $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 182 | + $home_url = get_option( 'home' ); |
|
| 183 | + |
|
| 184 | + if ( ! $wl_blog_url ) { |
|
| 185 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 186 | + } elseif ( $wl_blog_url !== $home_url ) { |
|
| 187 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 188 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 189 | + set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * This function is hooked to the `admin_notices` to show admin notification. |
|
| 196 | + */ |
|
| 197 | + public function wl_key_update_notice() { |
|
| 198 | + if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 199 | + ?> |
|
| 200 | 200 | <div class="updated notice is-dismissible error"> |
| 201 | 201 | <p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p> |
| 202 | 202 | </div> |
| 203 | 203 | <?php |
| 204 | - } |
|
| 205 | - } |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | 206 | } |
@@ -4,39 +4,39 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Api_Headers_Service { |
| 6 | 6 | |
| 7 | - private static $instance = null; |
|
| 8 | - |
|
| 9 | - protected function __construct() { |
|
| 10 | - |
|
| 11 | - } |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * This function is used to append WordPress endpoint data to every request made. |
|
| 15 | - * |
|
| 16 | - * @return array |
|
| 17 | - */ |
|
| 18 | - public function get_wp_headers() { |
|
| 19 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 20 | - $is_plugin_subscription = apply_filters( 'wl_feature__enable__entity-types-professional', false ) || |
|
| 21 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 22 | - apply_filters( 'wl_feature__enable__entity-types-business', false ) || |
|
| 23 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 24 | - apply_filters( 'wl_feature__enable__entity-types-starter', false ); |
|
| 25 | - return $is_plugin_subscription ? array( |
|
| 26 | - 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
| 27 | - 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
| 28 | - ) : array(); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @return Api_Headers_Service |
|
| 33 | - */ |
|
| 34 | - public static function get_instance() { |
|
| 35 | - if ( null === self::$instance ) { |
|
| 36 | - self::$instance = new Api_Headers_Service(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - return self::$instance; |
|
| 40 | - } |
|
| 7 | + private static $instance = null; |
|
| 8 | + |
|
| 9 | + protected function __construct() { |
|
| 10 | + |
|
| 11 | + } |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * This function is used to append WordPress endpoint data to every request made. |
|
| 15 | + * |
|
| 16 | + * @return array |
|
| 17 | + */ |
|
| 18 | + public function get_wp_headers() { |
|
| 19 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 20 | + $is_plugin_subscription = apply_filters( 'wl_feature__enable__entity-types-professional', false ) || |
|
| 21 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 22 | + apply_filters( 'wl_feature__enable__entity-types-business', false ) || |
|
| 23 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 24 | + apply_filters( 'wl_feature__enable__entity-types-starter', false ); |
|
| 25 | + return $is_plugin_subscription ? array( |
|
| 26 | + 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
| 27 | + 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
| 28 | + ) : array(); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @return Api_Headers_Service |
|
| 33 | + */ |
|
| 34 | + public static function get_instance() { |
|
| 35 | + if ( null === self::$instance ) { |
|
| 36 | + self::$instance = new Api_Headers_Service(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + return self::$instance; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | } |