Completed
Pull Request — develop (#1271)
by
unknown
03:05
created
src/includes/class-wordlift-configuration-service.php 2 patches
Indentation   +713 added lines, -713 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 use Wordlift\Api\Default_Api_Service;
14 14
 
15 15
 if ( ! defined( 'ABSPATH' ) ) {
16
-	exit;
16
+    exit;
17 17
 }
18 18
 
19 19
 /**
@@ -23,721 +23,721 @@  discard block
 block discarded – undo
23 23
  */
24 24
 class Wordlift_Configuration_Service {
25 25
 
26
-	/**
27
-	 * The entity base path option name.
28
-	 *
29
-	 * @since 3.6.0
30
-	 */
31
-	const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
-
33
-	/**
34
-	 * The skip wizard (admin installation wizard) option name.
35
-	 *
36
-	 * @since 3.9.0
37
-	 */
38
-	const SKIP_WIZARD = 'wl_skip_wizard';
39
-
40
-	/**
41
-	 * WordLift's key option name.
42
-	 *
43
-	 * @since 3.9.0
44
-	 */
45
-	const KEY = 'key';
46
-
47
-	/**
48
-	 * WordLift's configured language option name.
49
-	 *
50
-	 * @since 3.9.0
51
-	 */
52
-	const LANGUAGE = 'site_language';
53
-
54
-	/**
55
-	 * WordLift's configured country code.
56
-	 *
57
-	 * @since 3.18.0
58
-	 */
59
-	const COUNTRY_CODE = 'country_code';
60
-
61
-	/**
62
-	 * The publisher entity post ID option name.
63
-	 *
64
-	 * @since 3.9.0
65
-	 */
66
-	const PUBLISHER_ID = 'publisher_id';
67
-
68
-	/**
69
-	 * The dataset URI option name
70
-	 *
71
-	 * @since 3.10.0
72
-	 */
73
-	const DATASET_URI = 'redlink_dataset_uri';
74
-
75
-	/**
76
-	 * The link by default option name.
77
-	 *
78
-	 * @since 3.11.0
79
-	 */
80
-	const LINK_BY_DEFAULT = 'link_by_default';
81
-
82
-	/**
83
-	 * The analytics enable option.
84
-	 *
85
-	 * @since 3.21.0
86
-	 */
87
-	const ANALYTICS_ENABLE = 'analytics_enable';
88
-
89
-	/**
90
-	 * The analytics entity uri dimension option.
91
-	 *
92
-	 * @since 3.21.0
93
-	 */
94
-	const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
-
96
-	/**
97
-	 * The analytics entity type dimension option.
98
-	 *
99
-	 * @since 3.21.0
100
-	 */
101
-	const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
-
103
-	/**
104
-	 * The user preferences about sharing data option.
105
-	 *
106
-	 * @since 3.19.0
107
-	 */
108
-	const SEND_DIAGNOSTIC = 'send_diagnostic';
109
-
110
-	/**
111
-	 * The package type configuration key.
112
-	 *
113
-	 * @since 3.20.0
114
-	 */
115
-	const PACKAGE_TYPE = 'package_type';
116
-
117
-	/**
118
-	 * The {@link Wordlift_Log_Service} instance.
119
-	 *
120
-	 * @since 3.16.0
121
-	 *
122
-	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
123
-	 */
124
-	private $log;
125
-
126
-	/**
127
-	 * The Wordlift_Configuration_Service's singleton instance.
128
-	 *
129
-	 * @since  3.6.0
130
-	 *
131
-	 * @access private
132
-	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
133
-	 */
134
-	private static $instance;
135
-
136
-	/**
137
-	 * Create a Wordlift_Configuration_Service's instance.
138
-	 *
139
-	 * @since 3.6.0
140
-	 */
141
-	public function __construct() {
142
-
143
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
144
-
145
-		self::$instance = $this;
146
-
147
-	}
148
-
149
-	/**
150
-	 * Get the singleton instance.
151
-	 *
152
-	 * @return \Wordlift_Configuration_Service
153
-	 * @since 3.6.0
154
-	 *
155
-	 */
156
-	public static function get_instance() {
157
-
158
-		return self::$instance;
159
-	}
160
-
161
-	/**
162
-	 * Get a configuration given the option name and a key. The option value is
163
-	 * expected to be an array.
164
-	 *
165
-	 * @param string $option The option name.
166
-	 * @param string $key A key in the option value array.
167
-	 * @param string $default The default value in case the key is not found (by default an empty string).
168
-	 *
169
-	 * @return mixed The configuration value or the default value if not found.
170
-	 * @since 3.6.0
171
-	 *
172
-	 */
173
-	private function get( $option, $key, $default = '' ) {
174
-
175
-		$options = get_option( $option, array() );
176
-
177
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
178
-	}
179
-
180
-	/**
181
-	 * Set a configuration parameter.
182
-	 *
183
-	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
184
-	 * @param string $key The value key.
185
-	 * @param mixed $value The value.
186
-	 *
187
-	 * @since 3.9.0
188
-	 *
189
-	 */
190
-	private function set( $option, $key, $value ) {
191
-
192
-		$values         = get_option( $option );
193
-		$values         = isset( $values ) ? $values : array();
194
-		$values[ $key ] = $value;
195
-		update_option( $option, $values );
196
-
197
-	}
198
-
199
-	/**
200
-	 * Get the entity base path, by default 'entity'.
201
-	 *
202
-	 * @return string The entity base path.
203
-	 * @since 3.6.0
204
-	 *
205
-	 */
206
-	public function get_entity_base_path() {
207
-
208
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
209
-	}
210
-
211
-	/**
212
-	 * Get the entity base path.
213
-	 *
214
-	 * @param string $value The entity base path.
215
-	 *
216
-	 * @since 3.9.0
217
-	 *
218
-	 */
219
-	public function set_entity_base_path( $value ) {
220
-
221
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
222
-
223
-	}
224
-
225
-	/**
226
-	 * Whether the installation skip wizard should be skipped.
227
-	 *
228
-	 * @return bool True if it should be skipped otherwise false.
229
-	 * @since 3.9.0
230
-	 *
231
-	 */
232
-	public function is_skip_wizard() {
233
-
234
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
235
-	}
236
-
237
-	/**
238
-	 * Set the skip wizard parameter.
239
-	 *
240
-	 * @param bool $value True to skip the wizard. We expect a boolean value.
241
-	 *
242
-	 * @since 3.9.0
243
-	 *
244
-	 */
245
-	public function set_skip_wizard( $value ) {
246
-
247
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
248
-
249
-	}
250
-
251
-	/**
252
-	 * Get WordLift's key.
253
-	 *
254
-	 * @return string WordLift's key or an empty string if not set.
255
-	 * @since 3.9.0
256
-	 *
257
-	 */
258
-	public function get_key() {
259
-
260
-		return $this->get( 'wl_general_settings', self::KEY, '' );
261
-	}
262
-
263
-	/**
264
-	 * Set WordLift's key.
265
-	 *
266
-	 * @param string $value WordLift's key.
267
-	 *
268
-	 * @since 3.9.0
269
-	 *
270
-	 */
271
-	public function set_key( $value ) {
272
-
273
-		$this->set( 'wl_general_settings', self::KEY, $value );
274
-	}
275
-
276
-	/**
277
-	 * Get WordLift's configured language, by default 'en'.
278
-	 *
279
-	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
280
-	 *
281
-	 * @return string WordLift's configured language code ('en' by default).
282
-	 * @since 3.9.0
283
-	 *
284
-	 */
285
-	public function get_language_code() {
286
-
287
-		return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
288
-	}
289
-
290
-	/**
291
-	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
292
-	 *
293
-	 * @param string $value WordLift's language code.
294
-	 *
295
-	 * @since 3.9.0
296
-	 *
297
-	 */
298
-	public function set_language_code( $value ) {
299
-
300
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
301
-
302
-	}
303
-
304
-	/**
305
-	 * Set the user preferences about sharing diagnostic with us.
306
-	 *
307
-	 * @param string $value The user preferences(yes/no).
308
-	 *
309
-	 * @since 3.19.0
310
-	 *
311
-	 */
312
-	public function set_diagnostic_preferences( $value ) {
313
-
314
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
315
-
316
-	}
317
-
318
-	/**
319
-	 * Get the user preferences about sharing diagnostic.
320
-	 *
321
-	 * @since 3.19.0
322
-	 */
323
-	public function get_diagnostic_preferences() {
324
-
325
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
326
-	}
327
-
328
-	/**
329
-	 * Get WordLift's configured country code, by default 'us'.
330
-	 *
331
-	 * @return string WordLift's configured country code ('us' by default).
332
-	 * @since 3.18.0
333
-	 *
334
-	 */
335
-	public function get_country_code() {
336
-
337
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
338
-	}
339
-
340
-	/**
341
-	 * Set WordLift's country code.
342
-	 *
343
-	 * @param string $value WordLift's country code.
344
-	 *
345
-	 * @since 3.18.0
346
-	 *
347
-	 */
348
-	public function set_country_code( $value ) {
349
-
350
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
351
-
352
-	}
353
-
354
-	/**
355
-	 * Get the publisher entity post id.
356
-	 *
357
-	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
358
-	 * Article markup.
359
-	 *
360
-	 * @return int|NULL The publisher entity post id or NULL if not set.
361
-	 * @since 3.9.0
362
-	 *
363
-	 */
364
-	public function get_publisher_id() {
365
-
366
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
367
-	}
368
-
369
-	/**
370
-	 * Set the publisher entity post id.
371
-	 *
372
-	 * @param int $value The publisher entity post id.
373
-	 *
374
-	 * @since 3.9.0
375
-	 *
376
-	 */
377
-	public function set_publisher_id( $value ) {
378
-
379
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
380
-
381
-	}
382
-
383
-	/**
384
-	 * Get the dataset URI.
385
-	 *
386
-	 * @return string The dataset URI or an empty string if not set.
387
-	 * @since 3.10.0
388
-	 * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
389
-	 *
390
-	 */
391
-	public function get_dataset_uri() {
392
-
393
-		if ( apply_filters( 'wl_features__enable__dataset', true ) ) {
394
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
395
-		} else {
396
-			return null;
397
-		}
398
-	}
399
-
400
-	/**
401
-	 * Set the dataset URI.
402
-	 *
403
-	 * @param string $value The dataset URI.
404
-	 *
405
-	 * @since 3.10.0
406
-	 *
407
-	 */
408
-	public function set_dataset_uri( $value ) {
409
-
410
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
411
-	}
412
-
413
-	/**
414
-	 * Get the package type.
415
-	 *
416
-	 * @return string The package type or an empty string if not set.
417
-	 * @since 3.20.0
418
-	 *
419
-	 */
420
-	public function get_package_type() {
421
-
422
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
423
-	}
424
-
425
-	/**
426
-	 * Set the package type.
427
-	 *
428
-	 * @param string $value The package type.
429
-	 *
430
-	 * @since 3.20.0
431
-	 *
432
-	 */
433
-	public function set_package_type( $value ) {
434
-
435
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
436
-	}
437
-
438
-	/**
439
-	 * Intercept the change of the WordLift key in order to set the dataset URI.
440
-	 *
441
-	 *
442
-	 * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
443
-	 *               store the configuration parameters such as country or language.
444
-	 * @since 3.11.0
445
-	 *
446
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/761
447
-	 *
448
-	 * @param array $old_value The old settings.
449
-	 * @param array $new_value The new settings.
450
-	 */
451
-	public function update_key( $old_value, $new_value ) {
452
-
453
-		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
454
-		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
455
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
456
-
457
-		// If the key hasn't changed, don't do anything.
458
-		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
459
-		//		if ( $old_key === $new_key ) {
460
-		//			return;
461
-		//		}
462
-
463
-		// If the key is empty, empty the dataset URI.
464
-		if ( '' === $new_key ) {
465
-			$this->set_dataset_uri( '' );
466
-		}
467
-
468
-		// make the request to the remote server.
469
-		$this->get_remote_dataset_uri( $new_key );
470
-
471
-	}
472
-
473
-	/**
474
-	 * Handle retrieving the dataset uri from the remote server.
475
-	 *
476
-	 * If a valid dataset uri is returned it is stored in the appropriate option,
477
-	 * otherwise the option is set to empty string.
478
-	 *
479
-	 * @param string $key The key to be used.
480
-	 *
481
-	 * @since 3.12.0
482
-	 *
483
-	 * @since 3.17.0 send the site URL and get the dataset URI.
484
-	 */
485
-	public function get_remote_dataset_uri( $key ) {
486
-
487
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
488
-
489
-		if ( empty( $key ) ) {
490
-			$this->log->warn( 'Key set to empty value.' );
491
-
492
-			$this->set_dataset_uri( '' );
493
-			$this->set_package_type( null );
494
-
495
-			return;
496
-		}
497
-
498
-		/**
499
-		 * Allow 3rd parties to change the site_url.
500
-		 *
501
-		 * @param string $site_url The site url.
502
-		 *
503
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/850
504
-		 *
505
-		 * @since 3.20.0
506
-		 *
507
-		 */
508
-		$home_url = get_option( 'home' );
509
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
510
-
511
-		// Build the URL.
512
-		$url = '/accounts'
513
-		       . '?key=' . rawurlencode( $key )
514
-		       . '&url=' . rawurlencode( $site_url )
515
-		       . '&country=' . $this->get_country_code()
516
-		       . '&language=' . $this->get_language_code();
517
-
518
-		$api_service = Default_Api_Service::get_instance();
519
-		/**
520
-		 * @since 3.27.7.1
521
-		 * The Key should be passed to headers, otherwise api would return null.
522
-		 */
523
-		$headers     = array(
524
-			'Authorization' => "Key $key",
525
-		);
526
-		$response    = $api_service->request( 'PUT', $url, $headers )->get_response();
527
-
528
-		// The response is an error.
529
-		if ( is_wp_error( $response ) ) {
530
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
531
-
532
-			$this->set_dataset_uri( '' );
533
-			$this->set_package_type( null );
534
-
535
-			return;
536
-		}
537
-
538
-		// The response is not OK.
539
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
540
-			$base_url = $api_service->get_base_url();
541
-
542
-			if ( ! is_array( $response ) ) {
543
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
544
-			} else {
545
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
546
-			}
547
-
548
-
549
-			$this->set_dataset_uri( '' );
550
-			$this->set_package_type( null );
551
-
552
-			return;
553
-		}
554
-
555
-		/*
26
+    /**
27
+     * The entity base path option name.
28
+     *
29
+     * @since 3.6.0
30
+     */
31
+    const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
+
33
+    /**
34
+     * The skip wizard (admin installation wizard) option name.
35
+     *
36
+     * @since 3.9.0
37
+     */
38
+    const SKIP_WIZARD = 'wl_skip_wizard';
39
+
40
+    /**
41
+     * WordLift's key option name.
42
+     *
43
+     * @since 3.9.0
44
+     */
45
+    const KEY = 'key';
46
+
47
+    /**
48
+     * WordLift's configured language option name.
49
+     *
50
+     * @since 3.9.0
51
+     */
52
+    const LANGUAGE = 'site_language';
53
+
54
+    /**
55
+     * WordLift's configured country code.
56
+     *
57
+     * @since 3.18.0
58
+     */
59
+    const COUNTRY_CODE = 'country_code';
60
+
61
+    /**
62
+     * The publisher entity post ID option name.
63
+     *
64
+     * @since 3.9.0
65
+     */
66
+    const PUBLISHER_ID = 'publisher_id';
67
+
68
+    /**
69
+     * The dataset URI option name
70
+     *
71
+     * @since 3.10.0
72
+     */
73
+    const DATASET_URI = 'redlink_dataset_uri';
74
+
75
+    /**
76
+     * The link by default option name.
77
+     *
78
+     * @since 3.11.0
79
+     */
80
+    const LINK_BY_DEFAULT = 'link_by_default';
81
+
82
+    /**
83
+     * The analytics enable option.
84
+     *
85
+     * @since 3.21.0
86
+     */
87
+    const ANALYTICS_ENABLE = 'analytics_enable';
88
+
89
+    /**
90
+     * The analytics entity uri dimension option.
91
+     *
92
+     * @since 3.21.0
93
+     */
94
+    const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
+
96
+    /**
97
+     * The analytics entity type dimension option.
98
+     *
99
+     * @since 3.21.0
100
+     */
101
+    const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
+
103
+    /**
104
+     * The user preferences about sharing data option.
105
+     *
106
+     * @since 3.19.0
107
+     */
108
+    const SEND_DIAGNOSTIC = 'send_diagnostic';
109
+
110
+    /**
111
+     * The package type configuration key.
112
+     *
113
+     * @since 3.20.0
114
+     */
115
+    const PACKAGE_TYPE = 'package_type';
116
+
117
+    /**
118
+     * The {@link Wordlift_Log_Service} instance.
119
+     *
120
+     * @since 3.16.0
121
+     *
122
+     * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
123
+     */
124
+    private $log;
125
+
126
+    /**
127
+     * The Wordlift_Configuration_Service's singleton instance.
128
+     *
129
+     * @since  3.6.0
130
+     *
131
+     * @access private
132
+     * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
133
+     */
134
+    private static $instance;
135
+
136
+    /**
137
+     * Create a Wordlift_Configuration_Service's instance.
138
+     *
139
+     * @since 3.6.0
140
+     */
141
+    public function __construct() {
142
+
143
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
144
+
145
+        self::$instance = $this;
146
+
147
+    }
148
+
149
+    /**
150
+     * Get the singleton instance.
151
+     *
152
+     * @return \Wordlift_Configuration_Service
153
+     * @since 3.6.0
154
+     *
155
+     */
156
+    public static function get_instance() {
157
+
158
+        return self::$instance;
159
+    }
160
+
161
+    /**
162
+     * Get a configuration given the option name and a key. The option value is
163
+     * expected to be an array.
164
+     *
165
+     * @param string $option The option name.
166
+     * @param string $key A key in the option value array.
167
+     * @param string $default The default value in case the key is not found (by default an empty string).
168
+     *
169
+     * @return mixed The configuration value or the default value if not found.
170
+     * @since 3.6.0
171
+     *
172
+     */
173
+    private function get( $option, $key, $default = '' ) {
174
+
175
+        $options = get_option( $option, array() );
176
+
177
+        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
178
+    }
179
+
180
+    /**
181
+     * Set a configuration parameter.
182
+     *
183
+     * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
184
+     * @param string $key The value key.
185
+     * @param mixed $value The value.
186
+     *
187
+     * @since 3.9.0
188
+     *
189
+     */
190
+    private function set( $option, $key, $value ) {
191
+
192
+        $values         = get_option( $option );
193
+        $values         = isset( $values ) ? $values : array();
194
+        $values[ $key ] = $value;
195
+        update_option( $option, $values );
196
+
197
+    }
198
+
199
+    /**
200
+     * Get the entity base path, by default 'entity'.
201
+     *
202
+     * @return string The entity base path.
203
+     * @since 3.6.0
204
+     *
205
+     */
206
+    public function get_entity_base_path() {
207
+
208
+        return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
209
+    }
210
+
211
+    /**
212
+     * Get the entity base path.
213
+     *
214
+     * @param string $value The entity base path.
215
+     *
216
+     * @since 3.9.0
217
+     *
218
+     */
219
+    public function set_entity_base_path( $value ) {
220
+
221
+        $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
222
+
223
+    }
224
+
225
+    /**
226
+     * Whether the installation skip wizard should be skipped.
227
+     *
228
+     * @return bool True if it should be skipped otherwise false.
229
+     * @since 3.9.0
230
+     *
231
+     */
232
+    public function is_skip_wizard() {
233
+
234
+        return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
235
+    }
236
+
237
+    /**
238
+     * Set the skip wizard parameter.
239
+     *
240
+     * @param bool $value True to skip the wizard. We expect a boolean value.
241
+     *
242
+     * @since 3.9.0
243
+     *
244
+     */
245
+    public function set_skip_wizard( $value ) {
246
+
247
+        $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
248
+
249
+    }
250
+
251
+    /**
252
+     * Get WordLift's key.
253
+     *
254
+     * @return string WordLift's key or an empty string if not set.
255
+     * @since 3.9.0
256
+     *
257
+     */
258
+    public function get_key() {
259
+
260
+        return $this->get( 'wl_general_settings', self::KEY, '' );
261
+    }
262
+
263
+    /**
264
+     * Set WordLift's key.
265
+     *
266
+     * @param string $value WordLift's key.
267
+     *
268
+     * @since 3.9.0
269
+     *
270
+     */
271
+    public function set_key( $value ) {
272
+
273
+        $this->set( 'wl_general_settings', self::KEY, $value );
274
+    }
275
+
276
+    /**
277
+     * Get WordLift's configured language, by default 'en'.
278
+     *
279
+     * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
280
+     *
281
+     * @return string WordLift's configured language code ('en' by default).
282
+     * @since 3.9.0
283
+     *
284
+     */
285
+    public function get_language_code() {
286
+
287
+        return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
288
+    }
289
+
290
+    /**
291
+     * Set WordLift's language code, used when storing strings to the Linked Data dataset.
292
+     *
293
+     * @param string $value WordLift's language code.
294
+     *
295
+     * @since 3.9.0
296
+     *
297
+     */
298
+    public function set_language_code( $value ) {
299
+
300
+        $this->set( 'wl_general_settings', self::LANGUAGE, $value );
301
+
302
+    }
303
+
304
+    /**
305
+     * Set the user preferences about sharing diagnostic with us.
306
+     *
307
+     * @param string $value The user preferences(yes/no).
308
+     *
309
+     * @since 3.19.0
310
+     *
311
+     */
312
+    public function set_diagnostic_preferences( $value ) {
313
+
314
+        $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
315
+
316
+    }
317
+
318
+    /**
319
+     * Get the user preferences about sharing diagnostic.
320
+     *
321
+     * @since 3.19.0
322
+     */
323
+    public function get_diagnostic_preferences() {
324
+
325
+        return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
326
+    }
327
+
328
+    /**
329
+     * Get WordLift's configured country code, by default 'us'.
330
+     *
331
+     * @return string WordLift's configured country code ('us' by default).
332
+     * @since 3.18.0
333
+     *
334
+     */
335
+    public function get_country_code() {
336
+
337
+        return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
338
+    }
339
+
340
+    /**
341
+     * Set WordLift's country code.
342
+     *
343
+     * @param string $value WordLift's country code.
344
+     *
345
+     * @since 3.18.0
346
+     *
347
+     */
348
+    public function set_country_code( $value ) {
349
+
350
+        $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
351
+
352
+    }
353
+
354
+    /**
355
+     * Get the publisher entity post id.
356
+     *
357
+     * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
358
+     * Article markup.
359
+     *
360
+     * @return int|NULL The publisher entity post id or NULL if not set.
361
+     * @since 3.9.0
362
+     *
363
+     */
364
+    public function get_publisher_id() {
365
+
366
+        return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
367
+    }
368
+
369
+    /**
370
+     * Set the publisher entity post id.
371
+     *
372
+     * @param int $value The publisher entity post id.
373
+     *
374
+     * @since 3.9.0
375
+     *
376
+     */
377
+    public function set_publisher_id( $value ) {
378
+
379
+        $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
380
+
381
+    }
382
+
383
+    /**
384
+     * Get the dataset URI.
385
+     *
386
+     * @return string The dataset URI or an empty string if not set.
387
+     * @since 3.10.0
388
+     * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
389
+     *
390
+     */
391
+    public function get_dataset_uri() {
392
+
393
+        if ( apply_filters( 'wl_features__enable__dataset', true ) ) {
394
+            return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
395
+        } else {
396
+            return null;
397
+        }
398
+    }
399
+
400
+    /**
401
+     * Set the dataset URI.
402
+     *
403
+     * @param string $value The dataset URI.
404
+     *
405
+     * @since 3.10.0
406
+     *
407
+     */
408
+    public function set_dataset_uri( $value ) {
409
+
410
+        $this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
411
+    }
412
+
413
+    /**
414
+     * Get the package type.
415
+     *
416
+     * @return string The package type or an empty string if not set.
417
+     * @since 3.20.0
418
+     *
419
+     */
420
+    public function get_package_type() {
421
+
422
+        return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
423
+    }
424
+
425
+    /**
426
+     * Set the package type.
427
+     *
428
+     * @param string $value The package type.
429
+     *
430
+     * @since 3.20.0
431
+     *
432
+     */
433
+    public function set_package_type( $value ) {
434
+
435
+        $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
436
+    }
437
+
438
+    /**
439
+     * Intercept the change of the WordLift key in order to set the dataset URI.
440
+     *
441
+     *
442
+     * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
443
+     *               store the configuration parameters such as country or language.
444
+     * @since 3.11.0
445
+     *
446
+     * @see https://github.com/insideout10/wordlift-plugin/issues/761
447
+     *
448
+     * @param array $old_value The old settings.
449
+     * @param array $new_value The new settings.
450
+     */
451
+    public function update_key( $old_value, $new_value ) {
452
+
453
+        // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
454
+        // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
455
+        $new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
456
+
457
+        // If the key hasn't changed, don't do anything.
458
+        // WARN The 'update_option' hook is fired only if the new and old value are not equal.
459
+        //		if ( $old_key === $new_key ) {
460
+        //			return;
461
+        //		}
462
+
463
+        // If the key is empty, empty the dataset URI.
464
+        if ( '' === $new_key ) {
465
+            $this->set_dataset_uri( '' );
466
+        }
467
+
468
+        // make the request to the remote server.
469
+        $this->get_remote_dataset_uri( $new_key );
470
+
471
+    }
472
+
473
+    /**
474
+     * Handle retrieving the dataset uri from the remote server.
475
+     *
476
+     * If a valid dataset uri is returned it is stored in the appropriate option,
477
+     * otherwise the option is set to empty string.
478
+     *
479
+     * @param string $key The key to be used.
480
+     *
481
+     * @since 3.12.0
482
+     *
483
+     * @since 3.17.0 send the site URL and get the dataset URI.
484
+     */
485
+    public function get_remote_dataset_uri( $key ) {
486
+
487
+        $this->log->trace( 'Getting the remote dataset URI and package type...' );
488
+
489
+        if ( empty( $key ) ) {
490
+            $this->log->warn( 'Key set to empty value.' );
491
+
492
+            $this->set_dataset_uri( '' );
493
+            $this->set_package_type( null );
494
+
495
+            return;
496
+        }
497
+
498
+        /**
499
+         * Allow 3rd parties to change the site_url.
500
+         *
501
+         * @param string $site_url The site url.
502
+         *
503
+         * @see https://github.com/insideout10/wordlift-plugin/issues/850
504
+         *
505
+         * @since 3.20.0
506
+         *
507
+         */
508
+        $home_url = get_option( 'home' );
509
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
510
+
511
+        // Build the URL.
512
+        $url = '/accounts'
513
+                . '?key=' . rawurlencode( $key )
514
+                . '&url=' . rawurlencode( $site_url )
515
+                . '&country=' . $this->get_country_code()
516
+                . '&language=' . $this->get_language_code();
517
+
518
+        $api_service = Default_Api_Service::get_instance();
519
+        /**
520
+         * @since 3.27.7.1
521
+         * The Key should be passed to headers, otherwise api would return null.
522
+         */
523
+        $headers     = array(
524
+            'Authorization' => "Key $key",
525
+        );
526
+        $response    = $api_service->request( 'PUT', $url, $headers )->get_response();
527
+
528
+        // The response is an error.
529
+        if ( is_wp_error( $response ) ) {
530
+            $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
531
+
532
+            $this->set_dataset_uri( '' );
533
+            $this->set_package_type( null );
534
+
535
+            return;
536
+        }
537
+
538
+        // The response is not OK.
539
+        if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
540
+            $base_url = $api_service->get_base_url();
541
+
542
+            if ( ! is_array( $response ) ) {
543
+                $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
544
+            } else {
545
+                $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
546
+            }
547
+
548
+
549
+            $this->set_dataset_uri( '' );
550
+            $this->set_package_type( null );
551
+
552
+            return;
553
+        }
554
+
555
+        /*
556 556
 		 * We also store the package type.
557 557
 		 *
558 558
 		 * @since 3.20.0
559 559
 		 */
560
-		$json = json_decode( $response['body'] );
561
-		/**
562
-		 * @since 3.27.7
563
-		 * Remove the trailing slash returned from the new platform api.
564
-		 */
565
-		$dataset_uri  = untrailingslashit( $json->datasetURI );
566
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
567
-
568
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
569
-
570
-		$this->set_dataset_uri( $dataset_uri );
571
-		$this->set_package_type( $package_type );
572
-	}
573
-
574
-	/**
575
-	 * Handle the edge case where a user submits the same key again
576
-	 * when he does not have the dataset uri to regain it.
577
-	 *
578
-	 * This can not be handled in the normal option update hook because
579
-	 * it is not being triggered when the save value equals to the one already
580
-	 * in the DB.
581
-	 *
582
-	 * @param mixed $value The new, unserialized option value.
583
-	 * @param mixed $old_value The old option value.
584
-	 *
585
-	 * @return mixed The same value in the $value parameter
586
-	 * @since 3.12.0
587
-	 *
588
-	 */
589
-	function maybe_update_dataset_uri( $value, $old_value ) {
590
-
591
-		// Check the old key value and the new one. Here we're only handling the
592
-		// case where the key hasn't changed and the dataset URI isn't set. The
593
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
594
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
595
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
596
-
597
-		$dataset_uri = $this->get_dataset_uri();
598
-
599
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
600
-
601
-			// make the request to the remote server to try to get the dataset uri.
602
-			$this->get_remote_dataset_uri( $new_key );
603
-		}
604
-
605
-		return $value;
606
-	}
607
-
608
-	/**
609
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
610
-	 *
611
-	 * @param string $key The WordLift key to use.
612
-	 *
613
-	 * @return string The API URI.
614
-	 * @since 3.11.0
615
-	 *
616
-	 */
617
-	public function get_accounts_by_key_dataset_uri( $key ) {
618
-
619
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
620
-	}
621
-
622
-	/**
623
-	 * Get the `accounts` end point.
624
-	 *
625
-	 * @return string The `accounts` end point.
626
-	 * @since 3.16.0
627
-	 *
628
-	 */
629
-	public function get_accounts() {
630
-
631
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
632
-	}
633
-
634
-	/**
635
-	 * Get the `link by default` option.
636
-	 *
637
-	 * @return bool True if entities must be linked by default otherwise false.
638
-	 * @since 3.13.0
639
-	 *
640
-	 */
641
-	public function is_link_by_default() {
642
-
643
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
644
-	}
645
-
646
-	/**
647
-	 * Set the `link by default` option.
648
-	 *
649
-	 * @param bool $value True to enabling linking by default, otherwise false.
650
-	 *
651
-	 * @since 3.13.0
652
-	 *
653
-	 */
654
-	public function set_link_by_default( $value ) {
655
-
656
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
657
-	}
658
-
659
-	/**
660
-	 * Get the 'analytics-enable' option.
661
-	 *
662
-	 * @return string 'no' or 'yes' representing bool.
663
-	 * @since 3.21.0
664
-	 *
665
-	 */
666
-	public function is_analytics_enable() {
667
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
668
-	}
669
-
670
-	/**
671
-	 * Set the `analytics-enable` option.
672
-	 *
673
-	 * @param bool $value True to enabling analytics, otherwise false.
674
-	 *
675
-	 * @since 3.21.0
676
-	 *
677
-	 */
678
-	public function set_is_analytics_enable( $value ) {
679
-
680
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
681
-	}
682
-
683
-	/**
684
-	 * Get the 'analytics-entity-uri-dimention' option.
685
-	 *
686
-	 * @return int
687
-	 * @since 3.21.0
688
-	 *
689
-	 */
690
-	public function get_analytics_entity_uri_dimension() {
691
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
692
-	}
693
-
694
-	/**
695
-	 * Get the 'analytics-entity-type-dimension' option.
696
-	 *
697
-	 * @return int
698
-	 * @since 3.21.0
699
-	 *
700
-	 */
701
-	public function get_analytics_entity_type_dimension() {
702
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
703
-	}
704
-
705
-	/**
706
-	 * Get the URL to perform autocomplete request.
707
-	 *
708
-	 * @return string The URL to call to perform the autocomplete request.
709
-	 * @since 3.15.0
710
-	 *
711
-	 */
712
-	public function get_autocomplete_url() {
713
-
714
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
715
-
716
-	}
717
-
718
-	/**
719
-	 * Get the URL to perform feedback deactivation request.
720
-	 *
721
-	 * @return string The URL to call to perform the feedback deactivation request.
722
-	 * @since 3.19.0
723
-	 *
724
-	 */
725
-	public function get_deactivation_feedback_url() {
726
-
727
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
728
-
729
-	}
730
-
731
-	/**
732
-	 * Get the base API URL.
733
-	 *
734
-	 * @return string The base API URL.
735
-	 * @since 3.20.0
736
-	 *
737
-	 */
738
-	public function get_api_url() {
739
-
740
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
741
-	}
560
+        $json = json_decode( $response['body'] );
561
+        /**
562
+         * @since 3.27.7
563
+         * Remove the trailing slash returned from the new platform api.
564
+         */
565
+        $dataset_uri  = untrailingslashit( $json->datasetURI );
566
+        $package_type = isset( $json->packageType ) ? $json->packageType : null;
567
+
568
+        $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
569
+
570
+        $this->set_dataset_uri( $dataset_uri );
571
+        $this->set_package_type( $package_type );
572
+    }
573
+
574
+    /**
575
+     * Handle the edge case where a user submits the same key again
576
+     * when he does not have the dataset uri to regain it.
577
+     *
578
+     * This can not be handled in the normal option update hook because
579
+     * it is not being triggered when the save value equals to the one already
580
+     * in the DB.
581
+     *
582
+     * @param mixed $value The new, unserialized option value.
583
+     * @param mixed $old_value The old option value.
584
+     *
585
+     * @return mixed The same value in the $value parameter
586
+     * @since 3.12.0
587
+     *
588
+     */
589
+    function maybe_update_dataset_uri( $value, $old_value ) {
590
+
591
+        // Check the old key value and the new one. Here we're only handling the
592
+        // case where the key hasn't changed and the dataset URI isn't set. The
593
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
594
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
595
+        $new_key = isset( $value['key'] ) ? $value['key'] : '';
596
+
597
+        $dataset_uri = $this->get_dataset_uri();
598
+
599
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
600
+
601
+            // make the request to the remote server to try to get the dataset uri.
602
+            $this->get_remote_dataset_uri( $new_key );
603
+        }
604
+
605
+        return $value;
606
+    }
607
+
608
+    /**
609
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
610
+     *
611
+     * @param string $key The WordLift key to use.
612
+     *
613
+     * @return string The API URI.
614
+     * @since 3.11.0
615
+     *
616
+     */
617
+    public function get_accounts_by_key_dataset_uri( $key ) {
618
+
619
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
620
+    }
621
+
622
+    /**
623
+     * Get the `accounts` end point.
624
+     *
625
+     * @return string The `accounts` end point.
626
+     * @since 3.16.0
627
+     *
628
+     */
629
+    public function get_accounts() {
630
+
631
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
632
+    }
633
+
634
+    /**
635
+     * Get the `link by default` option.
636
+     *
637
+     * @return bool True if entities must be linked by default otherwise false.
638
+     * @since 3.13.0
639
+     *
640
+     */
641
+    public function is_link_by_default() {
642
+
643
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
644
+    }
645
+
646
+    /**
647
+     * Set the `link by default` option.
648
+     *
649
+     * @param bool $value True to enabling linking by default, otherwise false.
650
+     *
651
+     * @since 3.13.0
652
+     *
653
+     */
654
+    public function set_link_by_default( $value ) {
655
+
656
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
657
+    }
658
+
659
+    /**
660
+     * Get the 'analytics-enable' option.
661
+     *
662
+     * @return string 'no' or 'yes' representing bool.
663
+     * @since 3.21.0
664
+     *
665
+     */
666
+    public function is_analytics_enable() {
667
+        return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
668
+    }
669
+
670
+    /**
671
+     * Set the `analytics-enable` option.
672
+     *
673
+     * @param bool $value True to enabling analytics, otherwise false.
674
+     *
675
+     * @since 3.21.0
676
+     *
677
+     */
678
+    public function set_is_analytics_enable( $value ) {
679
+
680
+        $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
681
+    }
682
+
683
+    /**
684
+     * Get the 'analytics-entity-uri-dimention' option.
685
+     *
686
+     * @return int
687
+     * @since 3.21.0
688
+     *
689
+     */
690
+    public function get_analytics_entity_uri_dimension() {
691
+        return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
692
+    }
693
+
694
+    /**
695
+     * Get the 'analytics-entity-type-dimension' option.
696
+     *
697
+     * @return int
698
+     * @since 3.21.0
699
+     *
700
+     */
701
+    public function get_analytics_entity_type_dimension() {
702
+        return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
703
+    }
704
+
705
+    /**
706
+     * Get the URL to perform autocomplete request.
707
+     *
708
+     * @return string The URL to call to perform the autocomplete request.
709
+     * @since 3.15.0
710
+     *
711
+     */
712
+    public function get_autocomplete_url() {
713
+
714
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
715
+
716
+    }
717
+
718
+    /**
719
+     * Get the URL to perform feedback deactivation request.
720
+     *
721
+     * @return string The URL to call to perform the feedback deactivation request.
722
+     * @since 3.19.0
723
+     *
724
+     */
725
+    public function get_deactivation_feedback_url() {
726
+
727
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
728
+
729
+    }
730
+
731
+    /**
732
+     * Get the base API URL.
733
+     *
734
+     * @return string The base API URL.
735
+     * @since 3.20.0
736
+     *
737
+     */
738
+    public function get_api_url() {
739
+
740
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
741
+    }
742 742
 
743 743
 }
Please login to merge, or discard this patch.
Spacing   +90 added lines, -90 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
 
13 13
 use Wordlift\Api\Default_Api_Service;
14 14
 
15
-if ( ! defined( 'ABSPATH' ) ) {
15
+if ( ! defined('ABSPATH')) {
16 16
 	exit;
17 17
 }
18 18
 
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 	 */
141 141
 	public function __construct() {
142 142
 
143
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
143
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
144 144
 
145 145
 		self::$instance = $this;
146 146
 
@@ -170,11 +170,11 @@  discard block
 block discarded – undo
170 170
 	 * @since 3.6.0
171 171
 	 *
172 172
 	 */
173
-	private function get( $option, $key, $default = '' ) {
173
+	private function get($option, $key, $default = '') {
174 174
 
175
-		$options = get_option( $option, array() );
175
+		$options = get_option($option, array());
176 176
 
177
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
177
+		return isset($options[$key]) ? $options[$key] : $default;
178 178
 	}
179 179
 
180 180
 	/**
@@ -187,12 +187,12 @@  discard block
 block discarded – undo
187 187
 	 * @since 3.9.0
188 188
 	 *
189 189
 	 */
190
-	private function set( $option, $key, $value ) {
190
+	private function set($option, $key, $value) {
191 191
 
192
-		$values         = get_option( $option );
193
-		$values         = isset( $values ) ? $values : array();
194
-		$values[ $key ] = $value;
195
-		update_option( $option, $values );
192
+		$values         = get_option($option);
193
+		$values         = isset($values) ? $values : array();
194
+		$values[$key] = $value;
195
+		update_option($option, $values);
196 196
 
197 197
 	}
198 198
 
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
 	 */
206 206
 	public function get_entity_base_path() {
207 207
 
208
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
208
+		return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity');
209 209
 	}
210 210
 
211 211
 	/**
@@ -216,9 +216,9 @@  discard block
 block discarded – undo
216 216
 	 * @since 3.9.0
217 217
 	 *
218 218
 	 */
219
-	public function set_entity_base_path( $value ) {
219
+	public function set_entity_base_path($value) {
220 220
 
221
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
221
+		$this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value);
222 222
 
223 223
 	}
224 224
 
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
 	 */
232 232
 	public function is_skip_wizard() {
233 233
 
234
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
234
+		return $this->get('wl_general_settings', self::SKIP_WIZARD, false);
235 235
 	}
236 236
 
237 237
 	/**
@@ -242,9 +242,9 @@  discard block
 block discarded – undo
242 242
 	 * @since 3.9.0
243 243
 	 *
244 244
 	 */
245
-	public function set_skip_wizard( $value ) {
245
+	public function set_skip_wizard($value) {
246 246
 
247
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
247
+		$this->set('wl_general_settings', self::SKIP_WIZARD, true === $value);
248 248
 
249 249
 	}
250 250
 
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
 	 */
258 258
 	public function get_key() {
259 259
 
260
-		return $this->get( 'wl_general_settings', self::KEY, '' );
260
+		return $this->get('wl_general_settings', self::KEY, '');
261 261
 	}
262 262
 
263 263
 	/**
@@ -268,9 +268,9 @@  discard block
 block discarded – undo
268 268
 	 * @since 3.9.0
269 269
 	 *
270 270
 	 */
271
-	public function set_key( $value ) {
271
+	public function set_key($value) {
272 272
 
273
-		$this->set( 'wl_general_settings', self::KEY, $value );
273
+		$this->set('wl_general_settings', self::KEY, $value);
274 274
 	}
275 275
 
276 276
 	/**
@@ -284,7 +284,7 @@  discard block
 block discarded – undo
284 284
 	 */
285 285
 	public function get_language_code() {
286 286
 
287
-		return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
287
+		return $this->get('wl_general_settings', self::LANGUAGE, 'en');
288 288
 	}
289 289
 
290 290
 	/**
@@ -295,9 +295,9 @@  discard block
 block discarded – undo
295 295
 	 * @since 3.9.0
296 296
 	 *
297 297
 	 */
298
-	public function set_language_code( $value ) {
298
+	public function set_language_code($value) {
299 299
 
300
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
300
+		$this->set('wl_general_settings', self::LANGUAGE, $value);
301 301
 
302 302
 	}
303 303
 
@@ -309,9 +309,9 @@  discard block
 block discarded – undo
309 309
 	 * @since 3.19.0
310 310
 	 *
311 311
 	 */
312
-	public function set_diagnostic_preferences( $value ) {
312
+	public function set_diagnostic_preferences($value) {
313 313
 
314
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
314
+		$this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value);
315 315
 
316 316
 	}
317 317
 
@@ -322,7 +322,7 @@  discard block
 block discarded – undo
322 322
 	 */
323 323
 	public function get_diagnostic_preferences() {
324 324
 
325
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
325
+		return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no');
326 326
 	}
327 327
 
328 328
 	/**
@@ -334,7 +334,7 @@  discard block
 block discarded – undo
334 334
 	 */
335 335
 	public function get_country_code() {
336 336
 
337
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
337
+		return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us');
338 338
 	}
339 339
 
340 340
 	/**
@@ -345,9 +345,9 @@  discard block
 block discarded – undo
345 345
 	 * @since 3.18.0
346 346
 	 *
347 347
 	 */
348
-	public function set_country_code( $value ) {
348
+	public function set_country_code($value) {
349 349
 
350
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
350
+		$this->set('wl_general_settings', self::COUNTRY_CODE, $value);
351 351
 
352 352
 	}
353 353
 
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 	 */
364 364
 	public function get_publisher_id() {
365 365
 
366
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
366
+		return $this->get('wl_general_settings', self::PUBLISHER_ID, null);
367 367
 	}
368 368
 
369 369
 	/**
@@ -374,9 +374,9 @@  discard block
 block discarded – undo
374 374
 	 * @since 3.9.0
375 375
 	 *
376 376
 	 */
377
-	public function set_publisher_id( $value ) {
377
+	public function set_publisher_id($value) {
378 378
 
379
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
379
+		$this->set('wl_general_settings', self::PUBLISHER_ID, $value);
380 380
 
381 381
 	}
382 382
 
@@ -390,8 +390,8 @@  discard block
 block discarded – undo
390 390
 	 */
391 391
 	public function get_dataset_uri() {
392 392
 
393
-		if ( apply_filters( 'wl_features__enable__dataset', true ) ) {
394
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
393
+		if (apply_filters('wl_features__enable__dataset', true)) {
394
+			return $this->get('wl_advanced_settings', self::DATASET_URI, null);
395 395
 		} else {
396 396
 			return null;
397 397
 		}
@@ -405,9 +405,9 @@  discard block
 block discarded – undo
405 405
 	 * @since 3.10.0
406 406
 	 *
407 407
 	 */
408
-	public function set_dataset_uri( $value ) {
408
+	public function set_dataset_uri($value) {
409 409
 
410
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
410
+		$this->set('wl_advanced_settings', self::DATASET_URI, $value);
411 411
 	}
412 412
 
413 413
 	/**
@@ -419,7 +419,7 @@  discard block
 block discarded – undo
419 419
 	 */
420 420
 	public function get_package_type() {
421 421
 
422
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
422
+		return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null);
423 423
 	}
424 424
 
425 425
 	/**
@@ -430,9 +430,9 @@  discard block
 block discarded – undo
430 430
 	 * @since 3.20.0
431 431
 	 *
432 432
 	 */
433
-	public function set_package_type( $value ) {
433
+	public function set_package_type($value) {
434 434
 
435
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
435
+		$this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value);
436 436
 	}
437 437
 
438 438
 	/**
@@ -448,11 +448,11 @@  discard block
 block discarded – undo
448 448
 	 * @param array $old_value The old settings.
449 449
 	 * @param array $new_value The new settings.
450 450
 	 */
451
-	public function update_key( $old_value, $new_value ) {
451
+	public function update_key($old_value, $new_value) {
452 452
 
453 453
 		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
454 454
 		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
455
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
455
+		$new_key = isset($new_value['key']) ? $new_value['key'] : '';
456 456
 
457 457
 		// If the key hasn't changed, don't do anything.
458 458
 		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
@@ -461,12 +461,12 @@  discard block
 block discarded – undo
461 461
 		//		}
462 462
 
463 463
 		// If the key is empty, empty the dataset URI.
464
-		if ( '' === $new_key ) {
465
-			$this->set_dataset_uri( '' );
464
+		if ('' === $new_key) {
465
+			$this->set_dataset_uri('');
466 466
 		}
467 467
 
468 468
 		// make the request to the remote server.
469
-		$this->get_remote_dataset_uri( $new_key );
469
+		$this->get_remote_dataset_uri($new_key);
470 470
 
471 471
 	}
472 472
 
@@ -482,15 +482,15 @@  discard block
 block discarded – undo
482 482
 	 *
483 483
 	 * @since 3.17.0 send the site URL and get the dataset URI.
484 484
 	 */
485
-	public function get_remote_dataset_uri( $key ) {
485
+	public function get_remote_dataset_uri($key) {
486 486
 
487
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
487
+		$this->log->trace('Getting the remote dataset URI and package type...');
488 488
 
489
-		if ( empty( $key ) ) {
490
-			$this->log->warn( 'Key set to empty value.' );
489
+		if (empty($key)) {
490
+			$this->log->warn('Key set to empty value.');
491 491
 
492
-			$this->set_dataset_uri( '' );
493
-			$this->set_package_type( null );
492
+			$this->set_dataset_uri('');
493
+			$this->set_package_type(null);
494 494
 
495 495
 			return;
496 496
 		}
@@ -505,15 +505,15 @@  discard block
 block discarded – undo
505 505
 		 * @since 3.20.0
506 506
 		 *
507 507
 		 */
508
-		$home_url = get_option( 'home' );
509
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
508
+		$home_url = get_option('home');
509
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
510 510
 
511 511
 		// Build the URL.
512 512
 		$url = '/accounts'
513
-		       . '?key=' . rawurlencode( $key )
514
-		       . '&url=' . rawurlencode( $site_url )
515
-		       . '&country=' . $this->get_country_code()
516
-		       . '&language=' . $this->get_language_code();
513
+		       . '?key='.rawurlencode($key)
514
+		       . '&url='.rawurlencode($site_url)
515
+		       . '&country='.$this->get_country_code()
516
+		       . '&language='.$this->get_language_code();
517 517
 
518 518
 		$api_service = Default_Api_Service::get_instance();
519 519
 		/**
@@ -523,31 +523,31 @@  discard block
 block discarded – undo
523 523
 		$headers     = array(
524 524
 			'Authorization' => "Key $key",
525 525
 		);
526
-		$response    = $api_service->request( 'PUT', $url, $headers )->get_response();
526
+		$response    = $api_service->request('PUT', $url, $headers)->get_response();
527 527
 
528 528
 		// The response is an error.
529
-		if ( is_wp_error( $response ) ) {
530
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
529
+		if (is_wp_error($response)) {
530
+			$this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message());
531 531
 
532
-			$this->set_dataset_uri( '' );
533
-			$this->set_package_type( null );
532
+			$this->set_dataset_uri('');
533
+			$this->set_package_type(null);
534 534
 
535 535
 			return;
536 536
 		}
537 537
 
538 538
 		// The response is not OK.
539
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
539
+		if ( ! is_array($response) || 200 !== (int) $response['response']['code']) {
540 540
 			$base_url = $api_service->get_base_url();
541 541
 
542
-			if ( ! is_array( $response ) ) {
543
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
542
+			if ( ! is_array($response)) {
543
+				$this->log->error("Unexpected response when opening URL $base_url$url: ".var_export($response, true));
544 544
 			} else {
545
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
545
+				$this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true));
546 546
 			}
547 547
 
548 548
 
549
-			$this->set_dataset_uri( '' );
550
-			$this->set_package_type( null );
549
+			$this->set_dataset_uri('');
550
+			$this->set_package_type(null);
551 551
 
552 552
 			return;
553 553
 		}
@@ -557,18 +557,18 @@  discard block
 block discarded – undo
557 557
 		 *
558 558
 		 * @since 3.20.0
559 559
 		 */
560
-		$json = json_decode( $response['body'] );
560
+		$json = json_decode($response['body']);
561 561
 		/**
562 562
 		 * @since 3.27.7
563 563
 		 * Remove the trailing slash returned from the new platform api.
564 564
 		 */
565
-		$dataset_uri  = untrailingslashit( $json->datasetURI );
566
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
565
+		$dataset_uri  = untrailingslashit($json->datasetURI);
566
+		$package_type = isset($json->packageType) ? $json->packageType : null;
567 567
 
568
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
568
+		$this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]...");
569 569
 
570
-		$this->set_dataset_uri( $dataset_uri );
571
-		$this->set_package_type( $package_type );
570
+		$this->set_dataset_uri($dataset_uri);
571
+		$this->set_package_type($package_type);
572 572
 	}
573 573
 
574 574
 	/**
@@ -586,20 +586,20 @@  discard block
 block discarded – undo
586 586
 	 * @since 3.12.0
587 587
 	 *
588 588
 	 */
589
-	function maybe_update_dataset_uri( $value, $old_value ) {
589
+	function maybe_update_dataset_uri($value, $old_value) {
590 590
 
591 591
 		// Check the old key value and the new one. Here we're only handling the
592 592
 		// case where the key hasn't changed and the dataset URI isn't set. The
593 593
 		// other case, i.e. a new key is inserted, is handled at `update_key`.
594
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
595
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
594
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
595
+		$new_key = isset($value['key']) ? $value['key'] : '';
596 596
 
597 597
 		$dataset_uri = $this->get_dataset_uri();
598 598
 
599
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
599
+		if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) {
600 600
 
601 601
 			// make the request to the remote server to try to get the dataset uri.
602
-			$this->get_remote_dataset_uri( $new_key );
602
+			$this->get_remote_dataset_uri($new_key);
603 603
 		}
604 604
 
605 605
 		return $value;
@@ -614,9 +614,9 @@  discard block
 block discarded – undo
614 614
 	 * @since 3.11.0
615 615
 	 *
616 616
 	 */
617
-	public function get_accounts_by_key_dataset_uri( $key ) {
617
+	public function get_accounts_by_key_dataset_uri($key) {
618 618
 
619
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
619
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri";
620 620
 	}
621 621
 
622 622
 	/**
@@ -628,7 +628,7 @@  discard block
 block discarded – undo
628 628
 	 */
629 629
 	public function get_accounts() {
630 630
 
631
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
631
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts';
632 632
 	}
633 633
 
634 634
 	/**
@@ -640,7 +640,7 @@  discard block
 block discarded – undo
640 640
 	 */
641 641
 	public function is_link_by_default() {
642 642
 
643
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
643
+		return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes');
644 644
 	}
645 645
 
646 646
 	/**
@@ -651,9 +651,9 @@  discard block
 block discarded – undo
651 651
 	 * @since 3.13.0
652 652
 	 *
653 653
 	 */
654
-	public function set_link_by_default( $value ) {
654
+	public function set_link_by_default($value) {
655 655
 
656
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
656
+		$this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no');
657 657
 	}
658 658
 
659 659
 	/**
@@ -664,7 +664,7 @@  discard block
 block discarded – undo
664 664
 	 *
665 665
 	 */
666 666
 	public function is_analytics_enable() {
667
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
667
+		return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no');
668 668
 	}
669 669
 
670 670
 	/**
@@ -675,9 +675,9 @@  discard block
 block discarded – undo
675 675
 	 * @since 3.21.0
676 676
 	 *
677 677
 	 */
678
-	public function set_is_analytics_enable( $value ) {
678
+	public function set_is_analytics_enable($value) {
679 679
 
680
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
680
+		$this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no');
681 681
 	}
682 682
 
683 683
 	/**
@@ -688,7 +688,7 @@  discard block
 block discarded – undo
688 688
 	 *
689 689
 	 */
690 690
 	public function get_analytics_entity_uri_dimension() {
691
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
691
+		return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1);
692 692
 	}
693 693
 
694 694
 	/**
@@ -699,7 +699,7 @@  discard block
 block discarded – undo
699 699
 	 *
700 700
 	 */
701 701
 	public function get_analytics_entity_type_dimension() {
702
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
702
+		return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2);
703 703
 	}
704 704
 
705 705
 	/**
@@ -711,7 +711,7 @@  discard block
 block discarded – undo
711 711
 	 */
712 712
 	public function get_autocomplete_url() {
713 713
 
714
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
714
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete';
715 715
 
716 716
 	}
717 717
 
@@ -724,7 +724,7 @@  discard block
 block discarded – undo
724 724
 	 */
725 725
 	public function get_deactivation_feedback_url() {
726 726
 
727
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
727
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks';
728 728
 
729 729
 	}
730 730
 
Please login to merge, or discard this patch.
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 = 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 = 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 = 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 = 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 = 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 = 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 = get_option( 'home' );
143
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
142
+		$home_url = 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.