Completed
Push — master ( e3c7ae...1fddf6 )
by David
01:09
created
src/includes/class-wordlift-key-validation-service.php 1 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.