Completed
Push — master ( e3c7ae...1fddf6 )
by David
01:09
created
src/includes/class-wordlift-key-validation-service.php 2 patches
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -21,190 +21,190 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -42,20 +42,20 @@  discard block
 block discarded – undo
42 42
 	 */
43 43
 	public function __construct() {
44 44
 
45
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
45
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service');
46 46
 
47
-		add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
47
+		add_action('admin_init', array($this, 'wl_load_plugin'));
48 48
 		/**
49 49
 		 * Filter: wl_feature__enable__notices.
50 50
 		 *
51 51
 		 * @return bool
52 52
 		 * @since 3.27.6
53 53
 		 */
54
-		if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
55
-			add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
54
+		if (apply_filters('wl_feature__enable__notices', true)) {
55
+			add_action('admin_notices', array($this, 'wl_key_update_notice'));
56 56
 		}
57 57
 
58
-		$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' );
58
+		$this->ttl_cache_service = new Ttl_Cache('key-validation-notification');
59 59
 
60 60
 	}
61 61
 
@@ -67,9 +67,9 @@  discard block
 block discarded – undo
67 67
 	 * @return WP_Error|array The response or WP_Error on failure.
68 68
 	 * @since 3.9.0
69 69
 	 */
70
-	public function get_account_info( $key ) {
70
+	public function get_account_info($key) {
71 71
 
72
-		$this->log->debug( 'Validating key...' );
72
+		$this->log->debug('Validating key...');
73 73
 
74 74
 		$response = Default_Api_Service::get_instance()->get(
75 75
 			'/accounts/info',
@@ -84,39 +84,39 @@  discard block
 block discarded – undo
84 84
 		 * @since 3.38.5
85 85
 		 * This action is fired when the key is validated.
86 86
 		 */
87
-		do_action( 'wl_key_validation_response', $response );
87
+		do_action('wl_key_validation_response', $response);
88 88
 
89 89
 		return $response->get_response();
90 90
 	}
91 91
 
92
-	private function key_validation_request( $key ) {
93
-		$response = $this->get_account_info( $key );
92
+	private function key_validation_request($key) {
93
+		$response = $this->get_account_info($key);
94 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' ) );
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 97
 		}
98 98
 
99
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
99
+		$res_body = json_decode(wp_remote_retrieve_body($response), true);
100 100
 
101 101
 		$url = $res_body['url'];
102 102
 
103
-		$enabled_features = array_keys( array_filter( $res_body['features'] ) );
103
+		$enabled_features = array_keys(array_filter($res_body['features']));
104 104
 		$plugin_features  = array(
105 105
 			Entity_Type_Setter::STARTER_PLAN,
106 106
 			Entity_Type_Setter::PROFESSIONAL_PLAN,
107 107
 			Entity_Type_Setter::BUSINESS_PLAN,
108 108
 		);
109 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' ) );
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 112
 		}
113 113
 
114 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 ) );
115
+		$home_url = get_option('home');
116
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
117 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' ) );
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 120
 		}
121 121
 
122 122
 		return true;
@@ -129,12 +129,12 @@  discard block
 block discarded – undo
129 129
 	 *
130 130
 	 * @return bool
131 131
 	 */
132
-	public function is_key_valid( $key ) {
132
+	public function is_key_valid($key) {
133 133
 		try {
134
-			$this->key_validation_request( $key );
134
+			$this->key_validation_request($key);
135 135
 
136 136
 			return true;
137
-		} catch ( \Exception $e ) {
137
+		} catch (\Exception $e) {
138 138
 			return false;
139 139
 		}
140 140
 	}
@@ -150,14 +150,14 @@  discard block
 block discarded – undo
150 150
 		ob_clean();
151 151
 
152 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.' );
153
+		if ( ! isset($_POST['key'])) {  //phpcs:ignore WordPress.Security.NonceVerification.Missing
154
+			wp_send_json_error('The key parameter is required.');
155 155
 		}
156 156
 
157
-		$this->ttl_cache_service->delete( 'is_key_valid' );
157
+		$this->ttl_cache_service->delete('is_key_valid');
158 158
 
159 159
 		try {
160
-			$this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing
160
+			$this->key_validation_request(sanitize_text_field(wp_unslash((string) $_POST['key']))); //phpcs:ignore WordPress.Security.NonceVerification.Missing
161 161
 			wp_send_json_success(
162 162
 				array(
163 163
 					'valid'   => true,
@@ -165,8 +165,8 @@  discard block
 block discarded – undo
165 165
 				)
166 166
 			);
167 167
 
168
-		} catch ( \Exception $e ) {
169
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
168
+		} catch (\Exception $e) {
169
+			Wordlift_Configuration_Service::get_instance()->set_key('');
170 170
 			wp_send_json_success(
171 171
 				array(
172 172
 					'valid'   => false,
@@ -182,15 +182,15 @@  discard block
 block discarded – undo
182 182
 	 */
183 183
 	public function wl_load_plugin() {
184 184
 
185
-		$wl_blog_url = get_option( '_wl_blog_url' );
186
-		$home_url    = get_option( 'home' );
185
+		$wl_blog_url = get_option('_wl_blog_url');
186
+		$home_url    = get_option('home');
187 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 );
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 194
 		}
195 195
 
196 196
 	}
@@ -199,10 +199,10 @@  discard block
 block discarded – undo
199 199
 	 * This function is hooked to the `admin_notices` to show admin notification.
200 200
 	 */
201 201
 	public function wl_key_update_notice() {
202
-		if ( get_transient( 'wl-key-error-msg' ) ) {
202
+		if (get_transient('wl-key-error-msg')) {
203 203
 			?>
204 204
 		  <div class="updated notice is-dismissible error">
205
-			<p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p>
205
+			<p><?php esc_html(get_transient('wl-key-error-msg')); ?></p>
206 206
 		  </div>
207 207
 			<?php
208 208
 		}
Please login to merge, or discard this patch.