@@ -21,190 +21,190 @@ |
||
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 | - * @return bool |
|
52 | - * @since 3.27.6 |
|
53 | - */ |
|
54 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
55 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
56 | - } |
|
57 | - |
|
58 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
59 | - |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Validate the provided key. |
|
64 | - * |
|
65 | - * @param string $key WordLift's key to validate. |
|
66 | - * |
|
67 | - * @return WP_Error|array The response or WP_Error on failure. |
|
68 | - * @since 3.9.0 |
|
69 | - */ |
|
70 | - public function get_account_info( $key ) { |
|
71 | - |
|
72 | - $this->log->debug( 'Validating key...' ); |
|
73 | - |
|
74 | - $response = Default_Api_Service::get_instance()->get( |
|
75 | - '/accounts/info', |
|
76 | - array( |
|
77 | - 'Authorization' => "Key $key", |
|
78 | - ) |
|
79 | - ); |
|
80 | - |
|
81 | - /** |
|
82 | - * @param $response \Wordlift\Api\Response |
|
83 | - * |
|
84 | - * @since 3.38.5 |
|
85 | - * This action is fired when the key is validated. |
|
86 | - */ |
|
87 | - do_action( 'wl_key_validation_response', $response ); |
|
88 | - |
|
89 | - return $response->get_response(); |
|
90 | - } |
|
91 | - |
|
92 | - private function key_validation_request( $key ) { |
|
93 | - $response = $this->get_account_info( $key ); |
|
94 | - |
|
95 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
96 | - throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
97 | - } |
|
98 | - |
|
99 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
100 | - |
|
101 | - $url = $res_body['url']; |
|
102 | - |
|
103 | - $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
104 | - $plugin_features = array( |
|
105 | - Entity_Type_Setter::STARTER_PLAN, |
|
106 | - Entity_Type_Setter::PROFESSIONAL_PLAN, |
|
107 | - Entity_Type_Setter::BUSINESS_PLAN, |
|
108 | - ); |
|
109 | - |
|
110 | - if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
111 | - 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' ) ); |
|
112 | - } |
|
113 | - |
|
114 | - // Considering that production URL may be filtered. |
|
115 | - $home_url = get_option( 'home' ); |
|
116 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
117 | - |
|
118 | - if ( empty( $url ) && $url !== $site_url ) { |
|
119 | - 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' ) ); |
|
120 | - } |
|
121 | - |
|
122 | - return true; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Check if key is valid |
|
127 | - * |
|
128 | - * @param $key string |
|
129 | - * |
|
130 | - * @return bool |
|
131 | - */ |
|
132 | - public function is_key_valid( $key ) { |
|
133 | - try { |
|
134 | - $this->key_validation_request( $key ); |
|
135 | - |
|
136 | - return true; |
|
137 | - } catch ( \Exception $e ) { |
|
138 | - return false; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * This function is hooked to the `wl_validate_key` AJAX call. |
|
144 | - * |
|
145 | - * @since 3.9.0 |
|
146 | - */ |
|
147 | - public function validate_key() { |
|
148 | - |
|
149 | - // Ensure we don't have garbage before us. |
|
150 | - ob_clean(); |
|
151 | - |
|
152 | - // Check if we have a key. |
|
153 | - if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
154 | - wp_send_json_error( 'The key parameter is required.' ); |
|
155 | - } |
|
156 | - |
|
157 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
158 | - |
|
159 | - try { |
|
160 | - $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
161 | - wp_send_json_success( |
|
162 | - array( |
|
163 | - 'valid' => true, |
|
164 | - 'message' => '', |
|
165 | - ) |
|
166 | - ); |
|
167 | - |
|
168 | - } catch ( \Exception $e ) { |
|
169 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
170 | - wp_send_json_success( |
|
171 | - array( |
|
172 | - 'valid' => false, |
|
173 | - 'message' => $e->getMessage(), |
|
174 | - 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
175 | - ) |
|
176 | - ); |
|
177 | - } |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * This function is hooked `admin_init` to check _wl_blog_url. |
|
182 | - */ |
|
183 | - public function wl_load_plugin() { |
|
184 | - |
|
185 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
186 | - $home_url = get_option( 'home' ); |
|
187 | - |
|
188 | - if ( ! $wl_blog_url ) { |
|
189 | - update_option( '_wl_blog_url', $home_url, true ); |
|
190 | - } elseif ( $wl_blog_url !== $home_url ) { |
|
191 | - update_option( '_wl_blog_url', $home_url, true ); |
|
192 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
193 | - 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 ); |
|
194 | - } |
|
195 | - |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * This function is hooked to the `admin_notices` to show admin notification. |
|
200 | - */ |
|
201 | - public function wl_key_update_notice() { |
|
202 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
203 | - ?> |
|
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 | + * @return bool |
|
52 | + * @since 3.27.6 |
|
53 | + */ |
|
54 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
55 | + add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
56 | + } |
|
57 | + |
|
58 | + $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
59 | + |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Validate the provided key. |
|
64 | + * |
|
65 | + * @param string $key WordLift's key to validate. |
|
66 | + * |
|
67 | + * @return WP_Error|array The response or WP_Error on failure. |
|
68 | + * @since 3.9.0 |
|
69 | + */ |
|
70 | + public function get_account_info( $key ) { |
|
71 | + |
|
72 | + $this->log->debug( 'Validating key...' ); |
|
73 | + |
|
74 | + $response = Default_Api_Service::get_instance()->get( |
|
75 | + '/accounts/info', |
|
76 | + array( |
|
77 | + 'Authorization' => "Key $key", |
|
78 | + ) |
|
79 | + ); |
|
80 | + |
|
81 | + /** |
|
82 | + * @param $response \Wordlift\Api\Response |
|
83 | + * |
|
84 | + * @since 3.38.5 |
|
85 | + * This action is fired when the key is validated. |
|
86 | + */ |
|
87 | + do_action( 'wl_key_validation_response', $response ); |
|
88 | + |
|
89 | + return $response->get_response(); |
|
90 | + } |
|
91 | + |
|
92 | + private function key_validation_request( $key ) { |
|
93 | + $response = $this->get_account_info( $key ); |
|
94 | + |
|
95 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
96 | + throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
97 | + } |
|
98 | + |
|
99 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
100 | + |
|
101 | + $url = $res_body['url']; |
|
102 | + |
|
103 | + $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
104 | + $plugin_features = array( |
|
105 | + Entity_Type_Setter::STARTER_PLAN, |
|
106 | + Entity_Type_Setter::PROFESSIONAL_PLAN, |
|
107 | + Entity_Type_Setter::BUSINESS_PLAN, |
|
108 | + ); |
|
109 | + |
|
110 | + if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
111 | + 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' ) ); |
|
112 | + } |
|
113 | + |
|
114 | + // Considering that production URL may be filtered. |
|
115 | + $home_url = get_option( 'home' ); |
|
116 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
117 | + |
|
118 | + if ( empty( $url ) && $url !== $site_url ) { |
|
119 | + 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' ) ); |
|
120 | + } |
|
121 | + |
|
122 | + return true; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Check if key is valid |
|
127 | + * |
|
128 | + * @param $key string |
|
129 | + * |
|
130 | + * @return bool |
|
131 | + */ |
|
132 | + public function is_key_valid( $key ) { |
|
133 | + try { |
|
134 | + $this->key_validation_request( $key ); |
|
135 | + |
|
136 | + return true; |
|
137 | + } catch ( \Exception $e ) { |
|
138 | + return false; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * This function is hooked to the `wl_validate_key` AJAX call. |
|
144 | + * |
|
145 | + * @since 3.9.0 |
|
146 | + */ |
|
147 | + public function validate_key() { |
|
148 | + |
|
149 | + // Ensure we don't have garbage before us. |
|
150 | + ob_clean(); |
|
151 | + |
|
152 | + // Check if we have a key. |
|
153 | + if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
154 | + wp_send_json_error( 'The key parameter is required.' ); |
|
155 | + } |
|
156 | + |
|
157 | + $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
158 | + |
|
159 | + try { |
|
160 | + $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
161 | + wp_send_json_success( |
|
162 | + array( |
|
163 | + 'valid' => true, |
|
164 | + 'message' => '', |
|
165 | + ) |
|
166 | + ); |
|
167 | + |
|
168 | + } catch ( \Exception $e ) { |
|
169 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
170 | + wp_send_json_success( |
|
171 | + array( |
|
172 | + 'valid' => false, |
|
173 | + 'message' => $e->getMessage(), |
|
174 | + 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
175 | + ) |
|
176 | + ); |
|
177 | + } |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * This function is hooked `admin_init` to check _wl_blog_url. |
|
182 | + */ |
|
183 | + public function wl_load_plugin() { |
|
184 | + |
|
185 | + $wl_blog_url = get_option( '_wl_blog_url' ); |
|
186 | + $home_url = get_option( 'home' ); |
|
187 | + |
|
188 | + if ( ! $wl_blog_url ) { |
|
189 | + update_option( '_wl_blog_url', $home_url, true ); |
|
190 | + } elseif ( $wl_blog_url !== $home_url ) { |
|
191 | + update_option( '_wl_blog_url', $home_url, true ); |
|
192 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
193 | + 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 ); |
|
194 | + } |
|
195 | + |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * This function is hooked to the `admin_notices` to show admin notification. |
|
200 | + */ |
|
201 | + public function wl_key_update_notice() { |
|
202 | + if ( get_transient( 'wl-key-error-msg' ) ) { |
|
203 | + ?> |
|
204 | 204 | <div class="updated notice is-dismissible error"> |
205 | 205 | <p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p> |
206 | 206 | </div> |
207 | 207 | <?php |
208 | - } |
|
209 | - } |
|
208 | + } |
|
209 | + } |
|
210 | 210 | } |