Completed
Push — develop ( 583f49...4fc599 )
by
unknown
03:26
created
src/includes/class-wordlift-key-validation-service.php 2 patches
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -19,146 +19,146 @@
 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' )->get_response();
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
-			?>
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' )->get_response();
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
@@ -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,11 +74,11 @@  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' )->get_response();
81
+		return Default_Api_Service::get_instance()->get('/accounts/info')->get_response();
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.