Completed
Push — develop ( b5ae2b...45137e )
by Naveen
02:52
created
src/includes/class-wordlift-configuration-service.php 2 patches
Indentation   +706 added lines, -706 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,714 +23,714 @@  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 = defined( 'WP_HOME' ) ? WP_HOME : 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
-		$response    = $api_service->request( 'PUT', $url )->get_response();
520
-
521
-		// The response is an error.
522
-		if ( is_wp_error( $response ) ) {
523
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
524
-
525
-			$this->set_dataset_uri( '' );
526
-			$this->set_package_type( null );
527
-
528
-			return;
529
-		}
530
-
531
-		// The response is not OK.
532
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
533
-			$base_url = $api_service->get_base_url();
534
-
535
-			if ( ! is_array( $response ) ) {
536
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
537
-			} else {
538
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
539
-			}
540
-
541
-
542
-			$this->set_dataset_uri( '' );
543
-			$this->set_package_type( null );
544
-
545
-			return;
546
-		}
547
-
548
-		/*
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 = defined( 'WP_HOME' ) ? WP_HOME : 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
+        $response    = $api_service->request( 'PUT', $url )->get_response();
520
+
521
+        // The response is an error.
522
+        if ( is_wp_error( $response ) ) {
523
+            $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
524
+
525
+            $this->set_dataset_uri( '' );
526
+            $this->set_package_type( null );
527
+
528
+            return;
529
+        }
530
+
531
+        // The response is not OK.
532
+        if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
533
+            $base_url = $api_service->get_base_url();
534
+
535
+            if ( ! is_array( $response ) ) {
536
+                $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
537
+            } else {
538
+                $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
539
+            }
540
+
541
+
542
+            $this->set_dataset_uri( '' );
543
+            $this->set_package_type( null );
544
+
545
+            return;
546
+        }
547
+
548
+        /*
549 549
 		 * We also store the package type.
550 550
 		 *
551 551
 		 * @since 3.20.0
552 552
 		 */
553
-		$json         = json_decode( $response['body'] );
554
-		/**
555
-		 * @since 3.27.7
556
-		 * Remove the trailing slash returned from the new platform api.
557
-		 */
558
-		$dataset_uri  = untrailingslashit( $json->datasetURI );
559
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
560
-
561
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
562
-
563
-		$this->set_dataset_uri( $dataset_uri );
564
-		$this->set_package_type( $package_type );
565
-	}
566
-
567
-	/**
568
-	 * Handle the edge case where a user submits the same key again
569
-	 * when he does not have the dataset uri to regain it.
570
-	 *
571
-	 * This can not be handled in the normal option update hook because
572
-	 * it is not being triggered when the save value equals to the one already
573
-	 * in the DB.
574
-	 *
575
-	 * @param mixed $value The new, unserialized option value.
576
-	 * @param mixed $old_value The old option value.
577
-	 *
578
-	 * @return mixed The same value in the $value parameter
579
-	 * @since 3.12.0
580
-	 *
581
-	 */
582
-	function maybe_update_dataset_uri( $value, $old_value ) {
583
-
584
-		// Check the old key value and the new one. Here we're only handling the
585
-		// case where the key hasn't changed and the dataset URI isn't set. The
586
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
587
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
588
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
589
-
590
-		$dataset_uri = $this->get_dataset_uri();
591
-
592
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
593
-
594
-			// make the request to the remote server to try to get the dataset uri.
595
-			$this->get_remote_dataset_uri( $new_key );
596
-		}
597
-
598
-		return $value;
599
-	}
600
-
601
-	/**
602
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
603
-	 *
604
-	 * @param string $key The WordLift key to use.
605
-	 *
606
-	 * @return string The API URI.
607
-	 * @since 3.11.0
608
-	 *
609
-	 */
610
-	public function get_accounts_by_key_dataset_uri( $key ) {
611
-
612
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
613
-	}
614
-
615
-	/**
616
-	 * Get the `accounts` end point.
617
-	 *
618
-	 * @return string The `accounts` end point.
619
-	 * @since 3.16.0
620
-	 *
621
-	 */
622
-	public function get_accounts() {
623
-
624
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
625
-	}
626
-
627
-	/**
628
-	 * Get the `link by default` option.
629
-	 *
630
-	 * @return bool True if entities must be linked by default otherwise false.
631
-	 * @since 3.13.0
632
-	 *
633
-	 */
634
-	public function is_link_by_default() {
635
-
636
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
637
-	}
638
-
639
-	/**
640
-	 * Set the `link by default` option.
641
-	 *
642
-	 * @param bool $value True to enabling linking by default, otherwise false.
643
-	 *
644
-	 * @since 3.13.0
645
-	 *
646
-	 */
647
-	public function set_link_by_default( $value ) {
648
-
649
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
650
-	}
651
-
652
-	/**
653
-	 * Get the 'analytics-enable' option.
654
-	 *
655
-	 * @return string 'no' or 'yes' representing bool.
656
-	 * @since 3.21.0
657
-	 *
658
-	 */
659
-	public function is_analytics_enable() {
660
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
661
-	}
662
-
663
-	/**
664
-	 * Set the `analytics-enable` option.
665
-	 *
666
-	 * @param bool $value True to enabling analytics, otherwise false.
667
-	 *
668
-	 * @since 3.21.0
669
-	 *
670
-	 */
671
-	public function set_is_analytics_enable( $value ) {
672
-
673
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
674
-	}
675
-
676
-	/**
677
-	 * Get the 'analytics-entity-uri-dimention' option.
678
-	 *
679
-	 * @return int
680
-	 * @since 3.21.0
681
-	 *
682
-	 */
683
-	public function get_analytics_entity_uri_dimension() {
684
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
685
-	}
686
-
687
-	/**
688
-	 * Get the 'analytics-entity-type-dimension' option.
689
-	 *
690
-	 * @return int
691
-	 * @since 3.21.0
692
-	 *
693
-	 */
694
-	public function get_analytics_entity_type_dimension() {
695
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
696
-	}
697
-
698
-	/**
699
-	 * Get the URL to perform autocomplete request.
700
-	 *
701
-	 * @return string The URL to call to perform the autocomplete request.
702
-	 * @since 3.15.0
703
-	 *
704
-	 */
705
-	public function get_autocomplete_url() {
706
-
707
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
708
-
709
-	}
710
-
711
-	/**
712
-	 * Get the URL to perform feedback deactivation request.
713
-	 *
714
-	 * @return string The URL to call to perform the feedback deactivation request.
715
-	 * @since 3.19.0
716
-	 *
717
-	 */
718
-	public function get_deactivation_feedback_url() {
719
-
720
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
721
-
722
-	}
723
-
724
-	/**
725
-	 * Get the base API URL.
726
-	 *
727
-	 * @return string The base API URL.
728
-	 * @since 3.20.0
729
-	 *
730
-	 */
731
-	public function get_api_url() {
732
-
733
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
734
-	}
553
+        $json         = json_decode( $response['body'] );
554
+        /**
555
+         * @since 3.27.7
556
+         * Remove the trailing slash returned from the new platform api.
557
+         */
558
+        $dataset_uri  = untrailingslashit( $json->datasetURI );
559
+        $package_type = isset( $json->packageType ) ? $json->packageType : null;
560
+
561
+        $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
562
+
563
+        $this->set_dataset_uri( $dataset_uri );
564
+        $this->set_package_type( $package_type );
565
+    }
566
+
567
+    /**
568
+     * Handle the edge case where a user submits the same key again
569
+     * when he does not have the dataset uri to regain it.
570
+     *
571
+     * This can not be handled in the normal option update hook because
572
+     * it is not being triggered when the save value equals to the one already
573
+     * in the DB.
574
+     *
575
+     * @param mixed $value The new, unserialized option value.
576
+     * @param mixed $old_value The old option value.
577
+     *
578
+     * @return mixed The same value in the $value parameter
579
+     * @since 3.12.0
580
+     *
581
+     */
582
+    function maybe_update_dataset_uri( $value, $old_value ) {
583
+
584
+        // Check the old key value and the new one. Here we're only handling the
585
+        // case where the key hasn't changed and the dataset URI isn't set. The
586
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
587
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
588
+        $new_key = isset( $value['key'] ) ? $value['key'] : '';
589
+
590
+        $dataset_uri = $this->get_dataset_uri();
591
+
592
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
593
+
594
+            // make the request to the remote server to try to get the dataset uri.
595
+            $this->get_remote_dataset_uri( $new_key );
596
+        }
597
+
598
+        return $value;
599
+    }
600
+
601
+    /**
602
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
603
+     *
604
+     * @param string $key The WordLift key to use.
605
+     *
606
+     * @return string The API URI.
607
+     * @since 3.11.0
608
+     *
609
+     */
610
+    public function get_accounts_by_key_dataset_uri( $key ) {
611
+
612
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
613
+    }
614
+
615
+    /**
616
+     * Get the `accounts` end point.
617
+     *
618
+     * @return string The `accounts` end point.
619
+     * @since 3.16.0
620
+     *
621
+     */
622
+    public function get_accounts() {
623
+
624
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
625
+    }
626
+
627
+    /**
628
+     * Get the `link by default` option.
629
+     *
630
+     * @return bool True if entities must be linked by default otherwise false.
631
+     * @since 3.13.0
632
+     *
633
+     */
634
+    public function is_link_by_default() {
635
+
636
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
637
+    }
638
+
639
+    /**
640
+     * Set the `link by default` option.
641
+     *
642
+     * @param bool $value True to enabling linking by default, otherwise false.
643
+     *
644
+     * @since 3.13.0
645
+     *
646
+     */
647
+    public function set_link_by_default( $value ) {
648
+
649
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
650
+    }
651
+
652
+    /**
653
+     * Get the 'analytics-enable' option.
654
+     *
655
+     * @return string 'no' or 'yes' representing bool.
656
+     * @since 3.21.0
657
+     *
658
+     */
659
+    public function is_analytics_enable() {
660
+        return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
661
+    }
662
+
663
+    /**
664
+     * Set the `analytics-enable` option.
665
+     *
666
+     * @param bool $value True to enabling analytics, otherwise false.
667
+     *
668
+     * @since 3.21.0
669
+     *
670
+     */
671
+    public function set_is_analytics_enable( $value ) {
672
+
673
+        $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
674
+    }
675
+
676
+    /**
677
+     * Get the 'analytics-entity-uri-dimention' option.
678
+     *
679
+     * @return int
680
+     * @since 3.21.0
681
+     *
682
+     */
683
+    public function get_analytics_entity_uri_dimension() {
684
+        return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
685
+    }
686
+
687
+    /**
688
+     * Get the 'analytics-entity-type-dimension' option.
689
+     *
690
+     * @return int
691
+     * @since 3.21.0
692
+     *
693
+     */
694
+    public function get_analytics_entity_type_dimension() {
695
+        return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
696
+    }
697
+
698
+    /**
699
+     * Get the URL to perform autocomplete request.
700
+     *
701
+     * @return string The URL to call to perform the autocomplete request.
702
+     * @since 3.15.0
703
+     *
704
+     */
705
+    public function get_autocomplete_url() {
706
+
707
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
708
+
709
+    }
710
+
711
+    /**
712
+     * Get the URL to perform feedback deactivation request.
713
+     *
714
+     * @return string The URL to call to perform the feedback deactivation request.
715
+     * @since 3.19.0
716
+     *
717
+     */
718
+    public function get_deactivation_feedback_url() {
719
+
720
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
721
+
722
+    }
723
+
724
+    /**
725
+     * Get the base API URL.
726
+     *
727
+     * @return string The base API URL.
728
+     * @since 3.20.0
729
+     *
730
+     */
731
+    public function get_api_url() {
732
+
733
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
734
+    }
735 735
 
736 736
 }
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,42 +505,42 @@  discard block
 block discarded – undo
505 505
 		 * @since 3.20.0
506 506
 		 *
507 507
 		 */
508
-		$home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' );
509
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
508
+		$home_url = defined('WP_HOME') ? WP_HOME : 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
-		$response    = $api_service->request( 'PUT', $url )->get_response();
519
+		$response    = $api_service->request('PUT', $url)->get_response();
520 520
 
521 521
 		// The response is an error.
522
-		if ( is_wp_error( $response ) ) {
523
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
522
+		if (is_wp_error($response)) {
523
+			$this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message());
524 524
 
525
-			$this->set_dataset_uri( '' );
526
-			$this->set_package_type( null );
525
+			$this->set_dataset_uri('');
526
+			$this->set_package_type(null);
527 527
 
528 528
 			return;
529 529
 		}
530 530
 
531 531
 		// The response is not OK.
532
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
532
+		if ( ! is_array($response) || 200 !== (int) $response['response']['code']) {
533 533
 			$base_url = $api_service->get_base_url();
534 534
 
535
-			if ( ! is_array( $response ) ) {
536
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
535
+			if ( ! is_array($response)) {
536
+				$this->log->error("Unexpected response when opening URL $base_url$url: ".var_export($response, true));
537 537
 			} else {
538
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
538
+				$this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true));
539 539
 			}
540 540
 
541 541
 
542
-			$this->set_dataset_uri( '' );
543
-			$this->set_package_type( null );
542
+			$this->set_dataset_uri('');
543
+			$this->set_package_type(null);
544 544
 
545 545
 			return;
546 546
 		}
@@ -550,18 +550,18 @@  discard block
 block discarded – undo
550 550
 		 *
551 551
 		 * @since 3.20.0
552 552
 		 */
553
-		$json         = json_decode( $response['body'] );
553
+		$json = json_decode($response['body']);
554 554
 		/**
555 555
 		 * @since 3.27.7
556 556
 		 * Remove the trailing slash returned from the new platform api.
557 557
 		 */
558
-		$dataset_uri  = untrailingslashit( $json->datasetURI );
559
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
558
+		$dataset_uri  = untrailingslashit($json->datasetURI);
559
+		$package_type = isset($json->packageType) ? $json->packageType : null;
560 560
 
561
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
561
+		$this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]...");
562 562
 
563
-		$this->set_dataset_uri( $dataset_uri );
564
-		$this->set_package_type( $package_type );
563
+		$this->set_dataset_uri($dataset_uri);
564
+		$this->set_package_type($package_type);
565 565
 	}
566 566
 
567 567
 	/**
@@ -579,20 +579,20 @@  discard block
 block discarded – undo
579 579
 	 * @since 3.12.0
580 580
 	 *
581 581
 	 */
582
-	function maybe_update_dataset_uri( $value, $old_value ) {
582
+	function maybe_update_dataset_uri($value, $old_value) {
583 583
 
584 584
 		// Check the old key value and the new one. Here we're only handling the
585 585
 		// case where the key hasn't changed and the dataset URI isn't set. The
586 586
 		// other case, i.e. a new key is inserted, is handled at `update_key`.
587
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
588
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
587
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
588
+		$new_key = isset($value['key']) ? $value['key'] : '';
589 589
 
590 590
 		$dataset_uri = $this->get_dataset_uri();
591 591
 
592
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
592
+		if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) {
593 593
 
594 594
 			// make the request to the remote server to try to get the dataset uri.
595
-			$this->get_remote_dataset_uri( $new_key );
595
+			$this->get_remote_dataset_uri($new_key);
596 596
 		}
597 597
 
598 598
 		return $value;
@@ -607,9 +607,9 @@  discard block
 block discarded – undo
607 607
 	 * @since 3.11.0
608 608
 	 *
609 609
 	 */
610
-	public function get_accounts_by_key_dataset_uri( $key ) {
610
+	public function get_accounts_by_key_dataset_uri($key) {
611 611
 
612
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
612
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri";
613 613
 	}
614 614
 
615 615
 	/**
@@ -621,7 +621,7 @@  discard block
 block discarded – undo
621 621
 	 */
622 622
 	public function get_accounts() {
623 623
 
624
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
624
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts';
625 625
 	}
626 626
 
627 627
 	/**
@@ -633,7 +633,7 @@  discard block
 block discarded – undo
633 633
 	 */
634 634
 	public function is_link_by_default() {
635 635
 
636
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
636
+		return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes');
637 637
 	}
638 638
 
639 639
 	/**
@@ -644,9 +644,9 @@  discard block
 block discarded – undo
644 644
 	 * @since 3.13.0
645 645
 	 *
646 646
 	 */
647
-	public function set_link_by_default( $value ) {
647
+	public function set_link_by_default($value) {
648 648
 
649
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
649
+		$this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no');
650 650
 	}
651 651
 
652 652
 	/**
@@ -657,7 +657,7 @@  discard block
 block discarded – undo
657 657
 	 *
658 658
 	 */
659 659
 	public function is_analytics_enable() {
660
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
660
+		return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no');
661 661
 	}
662 662
 
663 663
 	/**
@@ -668,9 +668,9 @@  discard block
 block discarded – undo
668 668
 	 * @since 3.21.0
669 669
 	 *
670 670
 	 */
671
-	public function set_is_analytics_enable( $value ) {
671
+	public function set_is_analytics_enable($value) {
672 672
 
673
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
673
+		$this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no');
674 674
 	}
675 675
 
676 676
 	/**
@@ -681,7 +681,7 @@  discard block
 block discarded – undo
681 681
 	 *
682 682
 	 */
683 683
 	public function get_analytics_entity_uri_dimension() {
684
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
684
+		return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1);
685 685
 	}
686 686
 
687 687
 	/**
@@ -692,7 +692,7 @@  discard block
 block discarded – undo
692 692
 	 *
693 693
 	 */
694 694
 	public function get_analytics_entity_type_dimension() {
695
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
695
+		return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2);
696 696
 	}
697 697
 
698 698
 	/**
@@ -704,7 +704,7 @@  discard block
 block discarded – undo
704 704
 	 */
705 705
 	public function get_autocomplete_url() {
706 706
 
707
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
707
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete';
708 708
 
709 709
 	}
710 710
 
@@ -717,7 +717,7 @@  discard block
 block discarded – undo
717 717
 	 */
718 718
 	public function get_deactivation_feedback_url() {
719 719
 
720
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
720
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks';
721 721
 
722 722
 	}
723 723
 
Please login to merge, or discard this patch.