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