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