Completed
Push — master ( c336cf...9cad87 )
by David
30:41
created

set_diagnostic_preferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Wordlift_Configuration_Service class.
4
 *
5
 * The {@link Wordlift_Configuration_Service} class provides helper functions to get configuration parameter values.
6
 *
7
 * @link       https://wordlift.io
8
 *
9
 * @package    Wordlift
10
 * @since      3.6.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Get WordLift's configuration settings stored in WordPress database.
19
 *
20
 * @since 3.6.0
21
 */
22
class Wordlift_Configuration_Service {
23
24
	/**
25
	 * The entity base path option name.
26
	 *
27
	 * @since 3.6.0
28
	 */
29
	const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
30
31
	/**
32
	 * The skip wizard (admin installation wizard) option name.
33
	 *
34
	 * @since 3.9.0
35
	 */
36
	const SKIP_WIZARD = 'wl_skip_wizard';
37
38
	/**
39
	 * WordLift's key option name.
40
	 *
41
	 * @since 3.9.0
42
	 */
43
	const KEY = 'key';
44
45
	/**
46
	 * WordLift's configured language option name.
47
	 *
48
	 * @since 3.9.0
49
	 */
50
	const LANGUAGE = 'site_language';
51
52
	/**
53
	 * The publisher entity post ID option name.
54
	 *
55
	 * @since 3.9.0
56
	 */
57
	const PUBLISHER_ID = 'publisher_id';
58
59
	/**
60
	 * The dataset URI option name
61
	 *
62
	 * @since 3.10.0
63
	 */
64
	const DATASET_URI = 'redlink_dataset_uri';
65
66
	/**
67
	 * The link by default option name.
68
	 *
69
	 * @since 3.11.0
70
	 */
71
	const LINK_BY_DEFAULT = 'link_by_default';
72
73
	/**
74
	 * The user preferences about sharing data option.
75
	 *
76
	 * @since 3.19.0
77
	 */
78
	const SEND_DIAGNOSTIC = 'send_diagnostic';
79
80
	/**
81
	 * The {@link Wordlift_Log_Service} instance.
82
	 *
83
	 * @since 3.16.0
84
	 *
85
	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
86
	 */
87
	private $log;
88
89
	/**
90
	 * The Wordlift_Configuration_Service's singleton instance.
91
	 *
92
	 * @since  3.6.0
93
	 *
94
	 * @access private
95
	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
96
	 */
97
	private static $instance;
98
99
	/**
100
	 * Create a Wordlift_Configuration_Service's instance.
101
	 *
102
	 * @since 3.6.0
103
	 */
104
	public function __construct() {
105
106
		$this->log = Wordlift_Log_Service::get_logger( get_class() );
107
108
		self::$instance = $this;
109
110
	}
111
112
	/**
113
	 * Get the singleton instance.
114
	 *
115
	 * @since 3.6.0
116
	 *
117
	 * @return \Wordlift_Configuration_Service
118
	 */
119
	public static function get_instance() {
120
121
		return self::$instance;
122
	}
123
124
	/**
125
	 * Get a configuration given the option name and a key. The option value is
126
	 * expected to be an array.
127
	 *
128
	 * @since 3.6.0
129
	 *
130
	 * @param string $option  The option name.
131
	 * @param string $key     A key in the option value array.
132
	 * @param string $default The default value in case the key is not found (by default an empty string).
133
	 *
134
	 * @return mixed The configuration value or the default value if not found.
135
	 */
136
	private function get( $option, $key, $default = '' ) {
137
138
		$options = get_option( $option, array() );
139
140
		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
141
	}
142
143
	/**
144
	 * Set a configuration parameter.
145
	 *
146
	 * @since 3.9.0
147
	 *
148
	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
149
	 * @param string $key    The value key.
150
	 * @param mixed  $value  The value.
151
	 */
152
	private function set( $option, $key, $value ) {
153
154
		$values         = get_option( $option );
155
		$values         = isset( $values ) ? $values : array();
156
		$values[ $key ] = $value;
157
		update_option( $option, $values );
158
159
	}
160
161
	/**
162
	 * Get the entity base path, by default 'entity'.
163
	 *
164
	 * @since 3.6.0
165
	 *
166
	 * @return string The entity base path.
167
	 */
168
	public function get_entity_base_path() {
169
170
		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
171
	}
172
173
	/**
174
	 * Get the entity base path.
175
	 *
176
	 * @since 3.9.0
177
	 *
178
	 * @param string $value The entity base path.
179
	 */
180
	public function set_entity_base_path( $value ) {
181
182
		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
183
184
	}
185
186
	/**
187
	 * Whether the installation skip wizard should be skipped.
188
	 *
189
	 * @since 3.9.0
190
	 *
191
	 * @return bool True if it should be skipped otherwise false.
192
	 */
193
	public function is_skip_wizard() {
194
195
		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
196
	}
197
198
	/**
199
	 * Set the skip wizard parameter.
200
	 *
201
	 * @since 3.9.0
202
	 *
203
	 * @param bool $value True to skip the wizard. We expect a boolean value.
204
	 */
205
	public function set_skip_wizard( $value ) {
206
207
		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
208
209
	}
210
211
	/**
212
	 * Get WordLift's key.
213
	 *
214
	 * @since 3.9.0
215
	 *
216
	 * @return string WordLift's key or an empty string if not set.
217
	 */
218
	public function get_key() {
219
220
		return $this->get( 'wl_general_settings', self::KEY, '' );
221
	}
222
223
	/**
224
	 * Set WordLift's key.
225
	 *
226
	 * @since 3.9.0
227
	 *
228
	 * @param string $value WordLift's key.
229
	 */
230
	public function set_key( $value ) {
231
232
		$this->set( 'wl_general_settings', self::KEY, $value );
233
	}
234
235
	/**
236
	 * Get WordLift's configured language, by default 'en'.
237
	 *
238
	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
239
	 *
240
	 * @since 3.9.0
241
	 *
242
	 * @return string WordLift's configured language code ('en' by default).
243
	 */
244
	public function get_language_code() {
245
246
		return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' );
247
	}
248
249
	/**
250
	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
251
	 *
252
	 * @since 3.9.0
253
	 *
254
	 * @param string $value WordLift's language code.
255
	 */
256
	public function set_language_code( $value ) {
257
258
		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
259
260
	}
261
262
	/**
263
	 * Set the user preferences about sharing diagnostic with us.
264
	 *
265
	 * @since 3.19.0
266
	 *
267
	 * @param string $value The user preferences(yes/no).
268
	 */
269
	public function set_diagnostic_preferences( $value ) {
270
271
		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
272
273
	}
274
275
	/**
276
	 * Get the user preferences about sharing diagnostic.
277
	 *
278
	 * @since 3.19.0
279
	 */
280
	public function get_diagnostic_preferences() {
281
282
		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
283
284
	}
285
286
	/**
287
	 * Get the publisher entity post id.
288
	 *
289
	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
290
	 * Article markup.
291
	 *
292
	 * @since 3.9.0
293
	 *
294
	 * @return int|NULL The publisher entity post id or NULL if not set.
295
	 */
296
	public function get_publisher_id() {
297
298
		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
299
	}
300
301
	/**
302
	 * Set the publisher entity post id.
303
	 *
304
	 * @since 3.9.0
305
	 *
306
	 * @param int $value The publisher entity post id.
307
	 */
308
	public function set_publisher_id( $value ) {
309
310
		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
311
312
	}
313
314
	/**
315
	 * Get the dataset URI.
316
	 *
317
	 * @since 3.10.0
318
	 *
319
	 * @return string The dataset URI or an empty string if not set.
320
	 */
321
	public function get_dataset_uri() {
322
323
		return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
324
	}
325
326
	/**
327
	 * Set the dataset URI.
328
	 *
329
	 * @since 3.10.0
330
	 *
331
	 * @param string $value The dataset URI.
332
	 */
333
	public function set_dataset_uri( $value ) {
334
335
		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
336
	}
337
338
	/**
339
	 * Intercept the change of the WordLift key in order to set the dataset URI.
340
	 *
341
	 * @since 3.11.0
342
	 *
343
	 * @param array $old_value The old settings.
344
	 * @param array $new_value The new settings.
345
	 */
346
	public function update_key( $old_value, $new_value ) {
347
348
		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
349
		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
350
		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
351
352
		// If the key hasn't changed, don't do anything.
353
		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
354
		if ( $old_key === $new_key ) {
355
			return;
356
		}
357
358
		// If the key is empty, empty the dataset URI.
359
		if ( '' === $new_key ) {
360
			$this->set_dataset_uri( '' );
361
		}
362
363
		// make the request to the remote server.
364
		$this->get_remote_dataset_uri( $new_key );
365
	}
366
367
	/**
368
	 * Handle retrieving the dataset uri from the remote server.
369
	 *
370
	 * If a valid dataset uri is returned it is stored in the appropriate option,
371
	 * otherwise the option is set to empty string.
372
	 *
373
	 * @since 3.17.0 send the site URL and get the dataset URI.
374
	 * @since 3.12.0
375
	 *
376
	 * @param string $key The key to be used.
377
	 */
378
	public function get_remote_dataset_uri( $key ) {
379
380
		$this->log->trace( 'Getting the remote dataset URI...' );
381
382
		// Build the URL.
383
		$url = $this->get_accounts()
384
			   . '?key=' . rawurlencode( $key )
385
			   . '&url=' . rawurlencode( site_url() );
386
387
		$args     = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array(
388
			'method' => 'PUT',
389
		) );
390
		$response = wp_remote_request( $url, $args );
391
392
		// The response is an error.
393
		if ( is_wp_error( $response ) ) {
394
			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
395
396
			$this->set_dataset_uri( '' );
397
398
			return;
399
		}
400
401
		// The response is not OK.
402
		if ( 200 !== (int) $response['response']['code'] ) {
403
			$this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] );
404
405
			$this->set_dataset_uri( '' );
406
407
			return;
408
		}
409
410
		$json        = json_decode( $response['body'] );
411
		$dataset_uri = $json->datasetURI;
412
413
		$this->log->info( "Setting the dataset URI to $dataset_uri..." );
414
415
		$this->set_dataset_uri( $dataset_uri );
416
417
	}
418
419
	/**
420
	 * Handle the edge case where a user submits the same key again
421
	 * when he does not have the dataset uri to regain it.
422
	 *
423
	 * This can not be handled in the normal option update hook because
424
	 * it is not being triggered when the save value equals to the one already
425
	 * in the DB.
426
	 *
427
	 * @since 3.12.0
428
	 *
429
	 * @param mixed $value     The new, unserialized option value.
430
	 * @param mixed $old_value The old option value.
431
	 *
432
	 * @return mixed The same value in the $value parameter
433
	 */
434
	function maybe_update_dataset_uri( $value, $old_value ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
435
436
		// Check the old key value and the new one. Here we're only handling the
437
		// case where the key hasn't changed and the dataset URI isn't set. The
438
		// other case, i.e. a new key is inserted, is handled at `update_key`.
439
		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
440
		$new_key = isset( $value['key'] ) ? $value['key'] : '';
441
442
		$dataset_uri = $this->get_dataset_uri();
443
444
		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
445
446
			// make the request to the remote server to try to get the dataset uri.
447
			$this->get_remote_dataset_uri( $new_key );
448
		}
449
450
		return $value;
451
	}
452
453
	/**
454
	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
455
	 *
456
	 * @since 3.11.0
457
	 *
458
	 * @param string $key The WordLift key to use.
459
	 *
460
	 * @return string The API URI.
461
	 */
462
	public function get_accounts_by_key_dataset_uri( $key ) {
463
464
		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
465
	}
466
467
	/**
468
	 * Get the `accounts` end point.
469
	 *
470
	 * @since 3.16.0
471
	 *
472
	 * @return string The `accounts` end point.
473
	 */
474
	public function get_accounts() {
475
476
		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
477
	}
478
479
	/**
480
	 * Get the `link by default` option.
481
	 *
482
	 * @since 3.13.0
483
	 *
484
	 * @return bool True if entities must be linked by default otherwise false.
485
	 */
486
	public function is_link_by_default() {
487
488
		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
489
	}
490
491
	/**
492
	 * Set the `link by default` option.
493
	 *
494
	 * @since 3.13.0
495
	 *
496
	 * @param bool $value True to enabling linking by default, otherwise false.
497
	 */
498
	public function set_link_by_default( $value ) {
499
500
		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
501
	}
502
503
	/**
504
	 * Get the URL to perform batch analyses.
505
	 *
506
	 * @since 3.14.0
507
	 *
508
	 * @return string The URL to call to perform the batch analyzes.
509
	 */
510
	public function get_batch_analysis_url() {
511
512
		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'batch-analyses';
513
514
	}
515
516
	/**
517
	 * Get the URL to perform autocomplete request.
518
	 *
519
	 * @since 3.15.0
520
	 *
521
	 * @return string The URL to call to perform the batch analyzes.
522
	 */
523
	public function get_autocomplete_url() {
524
525
		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
526
527
	}
528
529
	/**
530
	 * Get the URL to perform feedback deactivation request.
531
	 *
532
	 * @since 3.19.0
533
	 *
534
	 * @return string The URL to call to perform the feedback deactivation request.
535
	 */
536
	public function get_deactivation_feedback_url() {
537
538
		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
539
540
	}
541
542
}
543