Completed
Push — develop ( c4b7f8...6c1f8e )
by David
01:00
created
src/includes/class-wordlift-key-validation-service.php 2 patches
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -20,190 +20,190 @@
 block discarded – undo
20 20
  */
21 21
 class Wordlift_Key_Validation_Service {
22 22
 
23
-	/**
24
-	 * A {@link Wordlift_Log_Service} instance.
25
-	 *
26
-	 * @since  3.14.0
27
-	 * @access private
28
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
-	 */
30
-	private $log;
31
-
32
-	/**
33
-	 * The {@link Wordlift_Configuration_Service} instance.
34
-	 *
35
-	 * @since  3.14.0
36
-	 * @access private
37
-	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
38
-	 */
39
-	private $configuration_service;
40
-
41
-	/**
42
-	 * @var Ttl_Cache
43
-	 */
44
-	private $ttl_cache_service;
45
-
46
-	/**
47
-	 * Create a {@link Wordlift_Key_Validation_Service} instance.
48
-	 *
49
-	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
50
-	 *
51
-	 * @since 3.14.0
52
-	 */
53
-	public function __construct( $configuration_service ) {
54
-
55
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
56
-
57
-		$this->configuration_service = $configuration_service;
58
-
59
-		add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
60
-		/**
61
-		 * Filter: wl_feature__enable__notices.
62
-		 *
63
-		 * @param bool whether the notices needs to be enabled or not.
64
-		 *
65
-		 * @return bool
66
-		 * @since 3.27.6
67
-		 */
68
-		if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
69
-			add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
70
-		}
71
-
72
-		$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification');
73
-
74
-	}
75
-
76
-	/**
77
-	 * Validate the provided key.
78
-	 *
79
-	 * @param string $key WordLift's key to validate.
80
-	 *
81
-	 * @return WP_Error|array The response or WP_Error on failure.
82
-	 * @since 3.9.0
83
-	 *
84
-	 */
85
-	public function get_account_info( $key ) {
86
-
87
-		$this->log->debug( 'Validating key...' );
88
-
89
-		return Default_Api_Service::get_instance()->get( '/accounts/info', array(
90
-			'Authorization' => "Key $key",
91
-		) )->get_response();
92
-	}
93
-
94
-	/**
95
-	 * Check if key is valid
96
-	 *
97
-	 * @param $key string
98
-	 *
99
-	 * @return bool
100
-	 */
101
-	public function is_key_valid( $key ) {
102
-
103
-		$response = $this->get_account_info( $key );
104
-
105
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
106
-			return false;
107
-		}
108
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
109
-
110
-		$url = $res_body['url'];
111
-
112
-		// Considering that production URL may be filtered.
113
-		$home_url = get_option( 'home' );
114
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
115
-		if ( is_null( $url ) || $url === $site_url ) {
116
-			return true;
117
-		}
118
-
119
-		return false;
120
-	}
121
-
122
-	/**
123
-	 * This function is hooked to the `wl_validate_key` AJAX call.
124
-	 *
125
-	 * @since 3.9.0
126
-	 */
127
-	public function validate_key() {
128
-
129
-		// Ensure we don't have garbage before us.
130
-		ob_clean();
131
-
132
-		// Check if we have a key.
133
-		if ( ! isset( $_POST['key'] ) ) {
134
-			wp_send_json_error( 'The key parameter is required.' );
135
-		}
136
-
137
-		$response = $this->get_account_info( (string) $_POST['key'] );
138
-
139
-		// If we got an error, return invalid.
140
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
141
-			wp_send_json_success( array( 'valid' => false, 'message' => '' , 'response' => $response,
142
-                                         'api_url' => Default_Api_Service::get_instance()->get_base_url() ) );
143
-		}
144
-
145
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
146
-
147
-		// The URL stored in WLS. If this is the initial install the URL may be null.
148
-		$url = $res_body['url'];
149
-
150
-		// Considering that production URL may be filtered.
151
-		$home_url = get_option( 'home' );
152
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
153
-
154
-		// If the URL isn't set or matches, then it's valid.
155
-		if ( is_null( $url ) || $url === $site_url ) {
156
-		    // Invalidate the cache key
157
-			$this->ttl_cache_service->delete('is_key_valid' );
158
-			wp_send_json_success( array( 'valid' => true, 'message' => '' ) );
159
-		}
160
-
161
-		// If the URL doesn't match it means that this key has been configured elsewhere already.
162
-		if ( $url !== $site_url ) {
163
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
164
-			wp_send_json_success( array(
165
-				'valid'   => false,
166
-				'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
167
-			) );
168
-		}
169
-
170
-		// Set a response with valid set to true or false according to the key validity with message.
171
-		wp_send_json_success( array(
172
-			'valid'   => false,
173
-			'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
174
-		) );
175
-	}
176
-
177
-	/**
178
-	 * This function is hooked `admin_init` to check _wl_blog_url.
179
-	 *
180
-	 */
181
-	public function wl_load_plugin() {
182
-
183
-		$wl_blog_url = get_option( '_wl_blog_url' );
184
-		$home_url    = get_option( 'home' );
185
-
186
-		if ( ! $wl_blog_url ) {
187
-			update_option( '_wl_blog_url', $home_url, true );
188
-		} else if ( $wl_blog_url !== $home_url ) {
189
-			update_option( '_wl_blog_url', $home_url, true );
190
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
191
-			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 );
192
-		}
193
-
194
-	}
195
-
196
-	/**
197
-	 * This function is hooked to the `admin_notices` to show admin notification.
198
-	 *
199
-	 */
200
-	public function wl_key_update_notice() {
201
-		if ( get_transient( 'wl-key-error-msg' ) ) {
202
-			?>
23
+    /**
24
+     * A {@link Wordlift_Log_Service} instance.
25
+     *
26
+     * @since  3.14.0
27
+     * @access private
28
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
+     */
30
+    private $log;
31
+
32
+    /**
33
+     * The {@link Wordlift_Configuration_Service} instance.
34
+     *
35
+     * @since  3.14.0
36
+     * @access private
37
+     * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
38
+     */
39
+    private $configuration_service;
40
+
41
+    /**
42
+     * @var Ttl_Cache
43
+     */
44
+    private $ttl_cache_service;
45
+
46
+    /**
47
+     * Create a {@link Wordlift_Key_Validation_Service} instance.
48
+     *
49
+     * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
50
+     *
51
+     * @since 3.14.0
52
+     */
53
+    public function __construct( $configuration_service ) {
54
+
55
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
56
+
57
+        $this->configuration_service = $configuration_service;
58
+
59
+        add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
60
+        /**
61
+         * Filter: wl_feature__enable__notices.
62
+         *
63
+         * @param bool whether the notices needs to be enabled or not.
64
+         *
65
+         * @return bool
66
+         * @since 3.27.6
67
+         */
68
+        if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
69
+            add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
70
+        }
71
+
72
+        $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification');
73
+
74
+    }
75
+
76
+    /**
77
+     * Validate the provided key.
78
+     *
79
+     * @param string $key WordLift's key to validate.
80
+     *
81
+     * @return WP_Error|array The response or WP_Error on failure.
82
+     * @since 3.9.0
83
+     *
84
+     */
85
+    public function get_account_info( $key ) {
86
+
87
+        $this->log->debug( 'Validating key...' );
88
+
89
+        return Default_Api_Service::get_instance()->get( '/accounts/info', array(
90
+            'Authorization' => "Key $key",
91
+        ) )->get_response();
92
+    }
93
+
94
+    /**
95
+     * Check if key is valid
96
+     *
97
+     * @param $key string
98
+     *
99
+     * @return bool
100
+     */
101
+    public function is_key_valid( $key ) {
102
+
103
+        $response = $this->get_account_info( $key );
104
+
105
+        if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
106
+            return false;
107
+        }
108
+        $res_body = json_decode( wp_remote_retrieve_body( $response ), true );
109
+
110
+        $url = $res_body['url'];
111
+
112
+        // Considering that production URL may be filtered.
113
+        $home_url = get_option( 'home' );
114
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
115
+        if ( is_null( $url ) || $url === $site_url ) {
116
+            return true;
117
+        }
118
+
119
+        return false;
120
+    }
121
+
122
+    /**
123
+     * This function is hooked to the `wl_validate_key` AJAX call.
124
+     *
125
+     * @since 3.9.0
126
+     */
127
+    public function validate_key() {
128
+
129
+        // Ensure we don't have garbage before us.
130
+        ob_clean();
131
+
132
+        // Check if we have a key.
133
+        if ( ! isset( $_POST['key'] ) ) {
134
+            wp_send_json_error( 'The key parameter is required.' );
135
+        }
136
+
137
+        $response = $this->get_account_info( (string) $_POST['key'] );
138
+
139
+        // If we got an error, return invalid.
140
+        if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
141
+            wp_send_json_success( array( 'valid' => false, 'message' => '' , 'response' => $response,
142
+                                            'api_url' => Default_Api_Service::get_instance()->get_base_url() ) );
143
+        }
144
+
145
+        $res_body = json_decode( wp_remote_retrieve_body( $response ), true );
146
+
147
+        // The URL stored in WLS. If this is the initial install the URL may be null.
148
+        $url = $res_body['url'];
149
+
150
+        // Considering that production URL may be filtered.
151
+        $home_url = get_option( 'home' );
152
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
153
+
154
+        // If the URL isn't set or matches, then it's valid.
155
+        if ( is_null( $url ) || $url === $site_url ) {
156
+            // Invalidate the cache key
157
+            $this->ttl_cache_service->delete('is_key_valid' );
158
+            wp_send_json_success( array( 'valid' => true, 'message' => '' ) );
159
+        }
160
+
161
+        // If the URL doesn't match it means that this key has been configured elsewhere already.
162
+        if ( $url !== $site_url ) {
163
+            Wordlift_Configuration_Service::get_instance()->set_key( '' );
164
+            wp_send_json_success( array(
165
+                'valid'   => false,
166
+                'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
167
+            ) );
168
+        }
169
+
170
+        // Set a response with valid set to true or false according to the key validity with message.
171
+        wp_send_json_success( array(
172
+            'valid'   => false,
173
+            'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
174
+        ) );
175
+    }
176
+
177
+    /**
178
+     * This function is hooked `admin_init` to check _wl_blog_url.
179
+     *
180
+     */
181
+    public function wl_load_plugin() {
182
+
183
+        $wl_blog_url = get_option( '_wl_blog_url' );
184
+        $home_url    = get_option( 'home' );
185
+
186
+        if ( ! $wl_blog_url ) {
187
+            update_option( '_wl_blog_url', $home_url, true );
188
+        } else if ( $wl_blog_url !== $home_url ) {
189
+            update_option( '_wl_blog_url', $home_url, true );
190
+            Wordlift_Configuration_Service::get_instance()->set_key( '' );
191
+            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 );
192
+        }
193
+
194
+    }
195
+
196
+    /**
197
+     * This function is hooked to the `admin_notices` to show admin notification.
198
+     *
199
+     */
200
+    public function wl_key_update_notice() {
201
+        if ( get_transient( 'wl-key-error-msg' ) ) {
202
+            ?>
203 203
             <div class="updated notice is-dismissible error">
204 204
                 <p><?php esc_html_e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p>
205 205
             </div>
206 206
 			<?php
207
-		}
208
-	}
207
+        }
208
+    }
209 209
 }
Please login to merge, or discard this patch.
Spacing   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -50,13 +50,13 @@  discard block
 block discarded – undo
50 50
 	 *
51 51
 	 * @since 3.14.0
52 52
 	 */
53
-	public function __construct( $configuration_service ) {
53
+	public function __construct($configuration_service) {
54 54
 
55
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
55
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service');
56 56
 
57 57
 		$this->configuration_service = $configuration_service;
58 58
 
59
-		add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
59
+		add_action('admin_init', array($this, 'wl_load_plugin'));
60 60
 		/**
61 61
 		 * Filter: wl_feature__enable__notices.
62 62
 		 *
@@ -65,11 +65,11 @@  discard block
 block discarded – undo
65 65
 		 * @return bool
66 66
 		 * @since 3.27.6
67 67
 		 */
68
-		if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
69
-			add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
68
+		if (apply_filters('wl_feature__enable__notices', true)) {
69
+			add_action('admin_notices', array($this, 'wl_key_update_notice'));
70 70
 		}
71 71
 
72
-		$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification');
72
+		$this->ttl_cache_service = new Ttl_Cache('key-validation-notification');
73 73
 
74 74
 	}
75 75
 
@@ -82,13 +82,13 @@  discard block
 block discarded – undo
82 82
 	 * @since 3.9.0
83 83
 	 *
84 84
 	 */
85
-	public function get_account_info( $key ) {
85
+	public function get_account_info($key) {
86 86
 
87
-		$this->log->debug( 'Validating key...' );
87
+		$this->log->debug('Validating key...');
88 88
 
89
-		return Default_Api_Service::get_instance()->get( '/accounts/info', array(
89
+		return Default_Api_Service::get_instance()->get('/accounts/info', array(
90 90
 			'Authorization' => "Key $key",
91
-		) )->get_response();
91
+		))->get_response();
92 92
 	}
93 93
 
94 94
 	/**
@@ -98,21 +98,21 @@  discard block
 block discarded – undo
98 98
 	 *
99 99
 	 * @return bool
100 100
 	 */
101
-	public function is_key_valid( $key ) {
101
+	public function is_key_valid($key) {
102 102
 
103
-		$response = $this->get_account_info( $key );
103
+		$response = $this->get_account_info($key);
104 104
 
105
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
105
+		if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) {
106 106
 			return false;
107 107
 		}
108
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
108
+		$res_body = json_decode(wp_remote_retrieve_body($response), true);
109 109
 
110 110
 		$url = $res_body['url'];
111 111
 
112 112
 		// Considering that production URL may be filtered.
113
-		$home_url = get_option( 'home' );
114
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
115
-		if ( is_null( $url ) || $url === $site_url ) {
113
+		$home_url = get_option('home');
114
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
115
+		if (is_null($url) || $url === $site_url) {
116 116
 			return true;
117 117
 		}
118 118
 
@@ -130,48 +130,48 @@  discard block
 block discarded – undo
130 130
 		ob_clean();
131 131
 
132 132
 		// Check if we have a key.
133
-		if ( ! isset( $_POST['key'] ) ) {
134
-			wp_send_json_error( 'The key parameter is required.' );
133
+		if ( ! isset($_POST['key'])) {
134
+			wp_send_json_error('The key parameter is required.');
135 135
 		}
136 136
 
137
-		$response = $this->get_account_info( (string) $_POST['key'] );
137
+		$response = $this->get_account_info((string) $_POST['key']);
138 138
 
139 139
 		// If we got an error, return invalid.
140
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
141
-			wp_send_json_success( array( 'valid' => false, 'message' => '' , 'response' => $response,
142
-                                         'api_url' => Default_Api_Service::get_instance()->get_base_url() ) );
140
+		if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) {
141
+			wp_send_json_success(array('valid' => false, 'message' => '', 'response' => $response,
142
+                                         'api_url' => Default_Api_Service::get_instance()->get_base_url()));
143 143
 		}
144 144
 
145
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
145
+		$res_body = json_decode(wp_remote_retrieve_body($response), true);
146 146
 
147 147
 		// The URL stored in WLS. If this is the initial install the URL may be null.
148 148
 		$url = $res_body['url'];
149 149
 
150 150
 		// Considering that production URL may be filtered.
151
-		$home_url = get_option( 'home' );
152
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
151
+		$home_url = get_option('home');
152
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
153 153
 
154 154
 		// If the URL isn't set or matches, then it's valid.
155
-		if ( is_null( $url ) || $url === $site_url ) {
155
+		if (is_null($url) || $url === $site_url) {
156 156
 		    // Invalidate the cache key
157
-			$this->ttl_cache_service->delete('is_key_valid' );
158
-			wp_send_json_success( array( 'valid' => true, 'message' => '' ) );
157
+			$this->ttl_cache_service->delete('is_key_valid');
158
+			wp_send_json_success(array('valid' => true, 'message' => ''));
159 159
 		}
160 160
 
161 161
 		// If the URL doesn't match it means that this key has been configured elsewhere already.
162
-		if ( $url !== $site_url ) {
163
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
164
-			wp_send_json_success( array(
162
+		if ($url !== $site_url) {
163
+			Wordlift_Configuration_Service::get_instance()->set_key('');
164
+			wp_send_json_success(array(
165 165
 				'valid'   => false,
166
-				'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
167
-			) );
166
+				'message' => __('The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift'),
167
+			));
168 168
 		}
169 169
 
170 170
 		// Set a response with valid set to true or false according to the key validity with message.
171
-		wp_send_json_success( array(
171
+		wp_send_json_success(array(
172 172
 			'valid'   => false,
173
-			'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
174
-		) );
173
+			'message' => __('An error occurred, please contact us at [email protected]', 'wordlift'),
174
+		));
175 175
 	}
176 176
 
177 177
 	/**
@@ -180,15 +180,15 @@  discard block
 block discarded – undo
180 180
 	 */
181 181
 	public function wl_load_plugin() {
182 182
 
183
-		$wl_blog_url = get_option( '_wl_blog_url' );
184
-		$home_url    = get_option( 'home' );
183
+		$wl_blog_url = get_option('_wl_blog_url');
184
+		$home_url    = get_option('home');
185 185
 
186
-		if ( ! $wl_blog_url ) {
187
-			update_option( '_wl_blog_url', $home_url, true );
188
-		} else if ( $wl_blog_url !== $home_url ) {
189
-			update_option( '_wl_blog_url', $home_url, true );
190
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
191
-			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 );
186
+		if ( ! $wl_blog_url) {
187
+			update_option('_wl_blog_url', $home_url, true);
188
+		} else if ($wl_blog_url !== $home_url) {
189
+			update_option('_wl_blog_url', $home_url, true);
190
+			Wordlift_Configuration_Service::get_instance()->set_key('');
191
+			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);
192 192
 		}
193 193
 
194 194
 	}
@@ -198,10 +198,10 @@  discard block
 block discarded – undo
198 198
 	 *
199 199
 	 */
200 200
 	public function wl_key_update_notice() {
201
-		if ( get_transient( 'wl-key-error-msg' ) ) {
201
+		if (get_transient('wl-key-error-msg')) {
202 202
 			?>
203 203
             <div class="updated notice is-dismissible error">
204
-                <p><?php esc_html_e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p>
204
+                <p><?php esc_html_e(get_transient('wl-key-error-msg'), 'wordlift'); ?></p>
205 205
             </div>
206 206
 			<?php
207 207
 		}
Please login to merge, or discard this patch.