Completed
Push — develop ( e6790c...3d54a6 )
by David
01:32
created
src/includes/class-wordlift-configuration-service.php 2 patches
Indentation   +798 added lines, -798 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,806 +23,806 @@  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
-	 * The skip installation notice.
42
-	 *
43
-	 * @since 3.40.3
44
-	 */
45
-	const SKIP_INSTALLATION_NOTICE = 'wl_skip_installation_notice';
46
-
47
-	/**
48
-	 * WordLift's key option name.
49
-	 *
50
-	 * @since 3.9.0
51
-	 */
52
-	const KEY = 'key';
53
-
54
-	/**
55
-	 * WordLift's configured language option name.
56
-	 *
57
-	 * @since 3.9.0
58
-	 */
59
-	const LANGUAGE = 'site_language';
60
-
61
-	/**
62
-	 * WordLift's configured country code.
63
-	 *
64
-	 * @since 3.18.0
65
-	 */
66
-	const COUNTRY_CODE = 'country_code';
67
-
68
-	/**
69
-	 * The alternateName option name.
70
-	 *
71
-	 * @since 3.38.6
72
-	 */
73
-	const ALTERNATE_NAME = 'wl-alternate-name';
74
-
75
-	/**
76
-	 * The publisher entity post ID option name.
77
-	 *
78
-	 * @since 3.9.0
79
-	 */
80
-	const PUBLISHER_ID = 'publisher_id';
81
-
82
-	/**
83
-	 * The dataset URI option name
84
-	 *
85
-	 * @since 3.10.0
86
-	 */
87
-	const DATASET_URI = 'redlink_dataset_uri';
88
-
89
-	/**
90
-	 * The link by default option name.
91
-	 *
92
-	 * @since 3.11.0
93
-	 */
94
-	const LINK_BY_DEFAULT = 'link_by_default';
95
-
96
-	/**
97
-	 * The analytics enable option.
98
-	 *
99
-	 * @since 3.21.0
100
-	 */
101
-	const ANALYTICS_ENABLE = 'analytics_enable';
102
-
103
-	/**
104
-	 * The analytics entity uri dimension option.
105
-	 *
106
-	 * @since 3.21.0
107
-	 */
108
-	const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
109
-
110
-	/**
111
-	 * The analytics entity type dimension option.
112
-	 *
113
-	 * @since 3.21.0
114
-	 */
115
-	const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
116
-
117
-	/**
118
-	 * The user preferences about sharing data option.
119
-	 *
120
-	 * @since 3.19.0
121
-	 */
122
-	const SEND_DIAGNOSTIC = 'send_diagnostic';
123
-
124
-	/**
125
-	 * The package type configuration key.
126
-	 *
127
-	 * @since 3.20.0
128
-	 */
129
-	const PACKAGE_TYPE = 'package_type';
130
-	/**
131
-	 * The dataset ids connected to the current key
132
-	 *
133
-	 * @since 3.38.5
134
-	 */
135
-	const NETWORK_DATASET_IDS = 'network_dataset_ids';
136
-
137
-	const OVERRIDE_WEBSITE_URL = 'wl-override-website-url';
138
-
139
-	/**
140
-	 * The {@link Wordlift_Log_Service} instance.
141
-	 *
142
-	 * @since 3.16.0
143
-	 *
144
-	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
145
-	 */
146
-	private $log;
147
-
148
-	/**
149
-	 * Create a Wordlift_Configuration_Service's instance.
150
-	 *
151
-	 * @since 3.6.0
152
-	 */
153
-	protected function __construct() {
154
-
155
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
156
-
157
-		// Sync some configuration properties when key is validated.
158
-		add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
159
-
160
-	}
161
-
162
-	/**
163
-	 * @param $response \Wordlift\Api\Response
164
-	 *
165
-	 * @return void
166
-	 */
167
-	public function sync( $response ) {
168
-		if ( ! $response->is_success() ) {
169
-			return;
170
-		}
171
-		$data = json_decode( $response->get_body(), true );
172
-		if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
173
-			return;
174
-		}
175
-		$this->set_network_dataset_ids( $data['networkDatasetId'] );
176
-	}
177
-
178
-	/**
179
-	 * The Wordlift_Configuration_Service's singleton instance.
180
-	 *
181
-	 * @since  3.6.0
182
-	 *
183
-	 * @access private
184
-	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
185
-	 */
186
-	private static $instance = null;
187
-
188
-	/**
189
-	 * Get the singleton instance.
190
-	 *
191
-	 * @return \Wordlift_Configuration_Service
192
-	 * @since 3.6.0
193
-	 */
194
-	public static function get_instance() {
195
-
196
-		if ( ! isset( self::$instance ) ) {
197
-			self::$instance = new self();
198
-		}
199
-
200
-		return self::$instance;
201
-	}
202
-
203
-	/**
204
-	 * Get a configuration given the option name and a key. The option value is
205
-	 * expected to be an array.
206
-	 *
207
-	 * @param string $option The option name.
208
-	 * @param string $key A key in the option value array.
209
-	 * @param string $default The default value in case the key is not found (by default an empty string).
210
-	 *
211
-	 * @return mixed The configuration value or the default value if not found.
212
-	 * @since 3.6.0
213
-	 */
214
-	private function get( $option, $key, $default = '' ) {
215
-
216
-		$options = get_option( $option, array() );
217
-
218
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
219
-	}
220
-
221
-	/**
222
-	 * Set a configuration parameter.
223
-	 *
224
-	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
225
-	 * @param string $key The value key.
226
-	 * @param mixed  $value The value.
227
-	 *
228
-	 * @since 3.9.0
229
-	 */
230
-	private function set( $option, $key, $value ) {
231
-
232
-		$values         = get_option( $option );
233
-		$values         = isset( $values ) ? $values : array();
234
-		$values[ $key ] = $value;
235
-		update_option( $option, $values );
236
-
237
-	}
238
-
239
-	/**
240
-	 * Get the entity base path, by default 'entity'.
241
-	 *
242
-	 * @return string The entity base path.
243
-	 * @since 3.6.0
244
-	 */
245
-	public function get_entity_base_path() {
246
-
247
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
248
-	}
249
-
250
-	/**
251
-	 * Get the entity base path.
252
-	 *
253
-	 * @param string $value The entity base path.
254
-	 *
255
-	 * @since 3.9.0
256
-	 */
257
-	public function set_entity_base_path( $value ) {
258
-
259
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
260
-
261
-	}
262
-
263
-	/**
264
-	 * Whether the installation skip wizard should be skipped.
265
-	 *
266
-	 * @return bool True if it should be skipped otherwise false.
267
-	 * @since 3.9.0
268
-	 */
269
-	public function is_skip_wizard() {
270
-
271
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
272
-	}
273
-
274
-	/**
275
-	 * Set the skip wizard parameter.
276
-	 *
277
-	 * @param bool $value True to skip the wizard. We expect a boolean value.
278
-	 *
279
-	 * @since 3.9.0
280
-	 */
281
-	public function set_skip_wizard( $value ) {
282
-
283
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
284
-
285
-	}
286
-
287
-	/**
288
-	 * Get WordLift's key.
289
-	 *
290
-	 * @return string WordLift's key or an empty string if not set.
291
-	 * @since 3.9.0
292
-	 */
293
-	public function get_key() {
294
-
295
-		return $this->get( 'wl_general_settings', self::KEY, '' );
296
-	}
297
-
298
-	/**
299
-	 * Set WordLift's key.
300
-	 *
301
-	 * @param string $value WordLift's key.
302
-	 *
303
-	 * @since 3.9.0
304
-	 */
305
-	public function set_key( $value ) {
306
-
307
-		$this->set( 'wl_general_settings', self::KEY, $value );
308
-	}
309
-
310
-	/**
311
-	 * Get WordLift's configured language, by default 'en'.
312
-	 *
313
-	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
314
-	 *
315
-	 * @return string WordLift's configured language code ('en' by default).
316
-	 * @since 3.9.0
317
-	 */
318
-	public function get_language_code() {
319
-
320
-		$language = get_locale();
321
-		if ( ! $language ) {
322
-			return 'en';
323
-		}
324
-
325
-		return substr( $language, 0, 2 );
326
-	}
327
-
328
-	/**
329
-	 * @param string $value WordLift's language code.
330
-	 *
331
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1466
332
-	 *
333
-	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
334
-	 *
335
-	 * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
336
-	 * language code form WordPress directly.
337
-	 *
338
-	 * @since 3.9.0
339
-	 */
340
-	public function set_language_code( $value ) {
341
-
342
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
343
-
344
-	}
345
-
346
-	/**
347
-	 * Set the user preferences about sharing diagnostic with us.
348
-	 *
349
-	 * @param string $value The user preferences(yes/no).
350
-	 *
351
-	 * @since 3.19.0
352
-	 */
353
-	public function set_diagnostic_preferences( $value ) {
354
-
355
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
356
-
357
-	}
358
-
359
-	/**
360
-	 * Get the user preferences about sharing diagnostic.
361
-	 *
362
-	 * @since 3.19.0
363
-	 */
364
-	public function get_diagnostic_preferences() {
365
-
366
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
367
-	}
368
-
369
-	/**
370
-	 * Get WordLift's configured country code, by default 'us'.
371
-	 *
372
-	 * @return string WordLift's configured country code ('us' by default).
373
-	 * @since 3.18.0
374
-	 */
375
-	public function get_country_code() {
376
-
377
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
378
-	}
379
-
380
-	/**
381
-	 * Set WordLift's country code.
382
-	 *
383
-	 * @param string $value WordLift's country code.
384
-	 *
385
-	 * @since 3.18.0
386
-	 */
387
-	public function set_country_code( $value ) {
388
-
389
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
390
-
391
-	}
392
-
393
-	/**
394
-	 * Get the alternateName.
395
-	 *
396
-	 * Website markup alternateName
397
-	 *
398
-	 * @return string|NULL alternateName or NULL if not set.
399
-	 * @since 3.38.6
400
-	 */
401
-	public function get_alternate_name() {
402
-		return $this->get( 'wl_general_settings', self::ALTERNATE_NAME );
403
-	}
404
-
405
-	/**
406
-	 * Set the alternateName.
407
-	 *
408
-	 * @param int $value The alternateName value.
409
-	 *
410
-	 * @since 3.38.6
411
-	 */
412
-	public function set_alternate_name( $value ) {
413
-
414
-		$this->set( 'wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags( $value ) );
415
-
416
-	}
417
-
418
-	/**
419
-	 * Get the publisher entity post id.
420
-	 *
421
-	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
422
-	 * Article markup.
423
-	 *
424
-	 * @return int|NULL The publisher entity post id or NULL if not set.
425
-	 * @since 3.9.0
426
-	 */
427
-	public function get_publisher_id() {
428
-
429
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
430
-	}
431
-
432
-	/**
433
-	 * Set the publisher entity post id.
434
-	 *
435
-	 * @param int $value The publisher entity post id.
436
-	 *
437
-	 * @since 3.9.0
438
-	 */
439
-	public function set_publisher_id( $value ) {
440
-
441
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
442
-
443
-	}
444
-
445
-	/**
446
-	 * Get the dataset URI.
447
-	 *
448
-	 * @return string The dataset URI or an empty string if not set.
449
-	 * @since 3.10.0
450
-	 * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
451
-	 */
452
-	public function get_dataset_uri() {
453
-
454
-		if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
455
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
456
-		} else {
457
-			return null;
458
-		}
459
-	}
460
-
461
-	/**
462
-	 * Set the dataset URI.
463
-	 *
464
-	 * @param string $value The dataset URI.
465
-	 *
466
-	 * @since 3.10.0
467
-	 */
468
-	public function set_dataset_uri( $value ) {
469
-
470
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
471
-	}
472
-
473
-	/**
474
-	 * Get the package type.
475
-	 *
476
-	 * @return string The package type or an empty string if not set.
477
-	 * @since 3.20.0
478
-	 */
479
-	public function get_package_type() {
480
-
481
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
482
-	}
483
-
484
-	/**
485
-	 * Set the package type.
486
-	 *
487
-	 * @param string $value The package type.
488
-	 *
489
-	 * @since 3.20.0
490
-	 */
491
-	public function set_package_type( $value ) {
492
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
493
-	}
494
-
495
-	/**
496
-	 * Intercept the change of the WordLift key in order to set the dataset URI.
497
-	 *
498
-	 * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
499
-	 *               store the configuration parameters such as country or language.
500
-	 * @since 3.11.0
501
-	 *
502
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/761
503
-	 *
504
-	 * @param array $old_value The old settings.
505
-	 * @param array $new_value The new settings.
506
-	 */
507
-	public function update_key( $old_value, $new_value ) {
508
-
509
-		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
510
-		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
511
-		$new_key = isset( $new_value['key'] ) ? trim( $new_value['key'] ) : '';
512
-
513
-		// If the key hasn't changed, don't do anything.
514
-		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
515
-		// if ( $old_key === $new_key ) {
516
-		// return;
517
-		// }
518
-
519
-		// If the key is empty, empty the dataset URI.
520
-		if ( '' === $new_key ) {
521
-			$this->set_dataset_uri( '' );
522
-		}
523
-
524
-		// make the request to the remote server.
525
-		$this->get_remote_dataset_uri( $new_key );
526
-
527
-		do_action( 'wl_key_updated' );
528
-
529
-	}
530
-
531
-	/**
532
-	 * Handle retrieving the dataset uri from the remote server.
533
-	 *
534
-	 * If a valid dataset uri is returned it is stored in the appropriate option,
535
-	 * otherwise the option is set to empty string.
536
-	 *
537
-	 * @param string $key The key to be used.
538
-	 *
539
-	 * @since 3.12.0
540
-	 *
541
-	 * @since 3.17.0 send the site URL and get the dataset URI.
542
-	 */
543
-	public function get_remote_dataset_uri( $key ) {
544
-
545
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
546
-
547
-		if ( empty( $key ) ) {
548
-			$this->log->warn( 'Key set to empty value.' );
549
-
550
-			$this->set_dataset_uri( '' );
551
-			$this->set_package_type( null );
552
-
553
-			return;
554
-		}
555
-
556
-		/**
557
-		 * Allow 3rd parties to change the site_url.
558
-		 *
559
-		 * @param string $site_url The site url.
560
-		 *
561
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/850
562
-		 *
563
-		 * @since 3.20.0
564
-		 */
565
-		$home_url = get_option( 'home' );
566
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
567
-
568
-		// Build the URL.
569
-		$url = '/accounts'
570
-			   . '?key=' . rawurlencode( $key )
571
-			   . '&url=' . rawurlencode( $site_url )
572
-			   . '&country=' . $this->get_country_code()
573
-			   . '&language=' . $this->get_language_code();
574
-
575
-		$api_service = Default_Api_Service::get_instance();
576
-		/**
577
-		 * @since 3.27.7.1
578
-		 * The Key should be passed to headers, otherwise api would return null.
579
-		 */
580
-		$headers  = array(
581
-			'Authorization' => "Key $key",
582
-		);
583
-		$response = $api_service->request( 'PUT', $url, $headers )->get_response();
584
-
585
-		// The response is an error.
586
-		if ( is_wp_error( $response ) ) {
587
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
588
-
589
-			$this->set_dataset_uri( '' );
590
-			$this->set_package_type( null );
591
-
592
-			return;
593
-		}
594
-
595
-		// The response is not OK.
596
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
597
-			$base_url = $api_service->get_base_url();
598
-
599
-			if ( ! is_array( $response ) ) {
600
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
601
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
602
-			} else {
603
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
604
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
605
-			}
606
-
607
-			$this->set_dataset_uri( '' );
608
-			$this->set_package_type( null );
609
-
610
-			return;
611
-		}
612
-
613
-		/*
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
+     * The skip installation notice.
42
+     *
43
+     * @since 3.40.3
44
+     */
45
+    const SKIP_INSTALLATION_NOTICE = 'wl_skip_installation_notice';
46
+
47
+    /**
48
+     * WordLift's key option name.
49
+     *
50
+     * @since 3.9.0
51
+     */
52
+    const KEY = 'key';
53
+
54
+    /**
55
+     * WordLift's configured language option name.
56
+     *
57
+     * @since 3.9.0
58
+     */
59
+    const LANGUAGE = 'site_language';
60
+
61
+    /**
62
+     * WordLift's configured country code.
63
+     *
64
+     * @since 3.18.0
65
+     */
66
+    const COUNTRY_CODE = 'country_code';
67
+
68
+    /**
69
+     * The alternateName option name.
70
+     *
71
+     * @since 3.38.6
72
+     */
73
+    const ALTERNATE_NAME = 'wl-alternate-name';
74
+
75
+    /**
76
+     * The publisher entity post ID option name.
77
+     *
78
+     * @since 3.9.0
79
+     */
80
+    const PUBLISHER_ID = 'publisher_id';
81
+
82
+    /**
83
+     * The dataset URI option name
84
+     *
85
+     * @since 3.10.0
86
+     */
87
+    const DATASET_URI = 'redlink_dataset_uri';
88
+
89
+    /**
90
+     * The link by default option name.
91
+     *
92
+     * @since 3.11.0
93
+     */
94
+    const LINK_BY_DEFAULT = 'link_by_default';
95
+
96
+    /**
97
+     * The analytics enable option.
98
+     *
99
+     * @since 3.21.0
100
+     */
101
+    const ANALYTICS_ENABLE = 'analytics_enable';
102
+
103
+    /**
104
+     * The analytics entity uri dimension option.
105
+     *
106
+     * @since 3.21.0
107
+     */
108
+    const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
109
+
110
+    /**
111
+     * The analytics entity type dimension option.
112
+     *
113
+     * @since 3.21.0
114
+     */
115
+    const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
116
+
117
+    /**
118
+     * The user preferences about sharing data option.
119
+     *
120
+     * @since 3.19.0
121
+     */
122
+    const SEND_DIAGNOSTIC = 'send_diagnostic';
123
+
124
+    /**
125
+     * The package type configuration key.
126
+     *
127
+     * @since 3.20.0
128
+     */
129
+    const PACKAGE_TYPE = 'package_type';
130
+    /**
131
+     * The dataset ids connected to the current key
132
+     *
133
+     * @since 3.38.5
134
+     */
135
+    const NETWORK_DATASET_IDS = 'network_dataset_ids';
136
+
137
+    const OVERRIDE_WEBSITE_URL = 'wl-override-website-url';
138
+
139
+    /**
140
+     * The {@link Wordlift_Log_Service} instance.
141
+     *
142
+     * @since 3.16.0
143
+     *
144
+     * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
145
+     */
146
+    private $log;
147
+
148
+    /**
149
+     * Create a Wordlift_Configuration_Service's instance.
150
+     *
151
+     * @since 3.6.0
152
+     */
153
+    protected function __construct() {
154
+
155
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
156
+
157
+        // Sync some configuration properties when key is validated.
158
+        add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
159
+
160
+    }
161
+
162
+    /**
163
+     * @param $response \Wordlift\Api\Response
164
+     *
165
+     * @return void
166
+     */
167
+    public function sync( $response ) {
168
+        if ( ! $response->is_success() ) {
169
+            return;
170
+        }
171
+        $data = json_decode( $response->get_body(), true );
172
+        if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
173
+            return;
174
+        }
175
+        $this->set_network_dataset_ids( $data['networkDatasetId'] );
176
+    }
177
+
178
+    /**
179
+     * The Wordlift_Configuration_Service's singleton instance.
180
+     *
181
+     * @since  3.6.0
182
+     *
183
+     * @access private
184
+     * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
185
+     */
186
+    private static $instance = null;
187
+
188
+    /**
189
+     * Get the singleton instance.
190
+     *
191
+     * @return \Wordlift_Configuration_Service
192
+     * @since 3.6.0
193
+     */
194
+    public static function get_instance() {
195
+
196
+        if ( ! isset( self::$instance ) ) {
197
+            self::$instance = new self();
198
+        }
199
+
200
+        return self::$instance;
201
+    }
202
+
203
+    /**
204
+     * Get a configuration given the option name and a key. The option value is
205
+     * expected to be an array.
206
+     *
207
+     * @param string $option The option name.
208
+     * @param string $key A key in the option value array.
209
+     * @param string $default The default value in case the key is not found (by default an empty string).
210
+     *
211
+     * @return mixed The configuration value or the default value if not found.
212
+     * @since 3.6.0
213
+     */
214
+    private function get( $option, $key, $default = '' ) {
215
+
216
+        $options = get_option( $option, array() );
217
+
218
+        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
219
+    }
220
+
221
+    /**
222
+     * Set a configuration parameter.
223
+     *
224
+     * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
225
+     * @param string $key The value key.
226
+     * @param mixed  $value The value.
227
+     *
228
+     * @since 3.9.0
229
+     */
230
+    private function set( $option, $key, $value ) {
231
+
232
+        $values         = get_option( $option );
233
+        $values         = isset( $values ) ? $values : array();
234
+        $values[ $key ] = $value;
235
+        update_option( $option, $values );
236
+
237
+    }
238
+
239
+    /**
240
+     * Get the entity base path, by default 'entity'.
241
+     *
242
+     * @return string The entity base path.
243
+     * @since 3.6.0
244
+     */
245
+    public function get_entity_base_path() {
246
+
247
+        return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
248
+    }
249
+
250
+    /**
251
+     * Get the entity base path.
252
+     *
253
+     * @param string $value The entity base path.
254
+     *
255
+     * @since 3.9.0
256
+     */
257
+    public function set_entity_base_path( $value ) {
258
+
259
+        $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
260
+
261
+    }
262
+
263
+    /**
264
+     * Whether the installation skip wizard should be skipped.
265
+     *
266
+     * @return bool True if it should be skipped otherwise false.
267
+     * @since 3.9.0
268
+     */
269
+    public function is_skip_wizard() {
270
+
271
+        return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
272
+    }
273
+
274
+    /**
275
+     * Set the skip wizard parameter.
276
+     *
277
+     * @param bool $value True to skip the wizard. We expect a boolean value.
278
+     *
279
+     * @since 3.9.0
280
+     */
281
+    public function set_skip_wizard( $value ) {
282
+
283
+        $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
284
+
285
+    }
286
+
287
+    /**
288
+     * Get WordLift's key.
289
+     *
290
+     * @return string WordLift's key or an empty string if not set.
291
+     * @since 3.9.0
292
+     */
293
+    public function get_key() {
294
+
295
+        return $this->get( 'wl_general_settings', self::KEY, '' );
296
+    }
297
+
298
+    /**
299
+     * Set WordLift's key.
300
+     *
301
+     * @param string $value WordLift's key.
302
+     *
303
+     * @since 3.9.0
304
+     */
305
+    public function set_key( $value ) {
306
+
307
+        $this->set( 'wl_general_settings', self::KEY, $value );
308
+    }
309
+
310
+    /**
311
+     * Get WordLift's configured language, by default 'en'.
312
+     *
313
+     * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
314
+     *
315
+     * @return string WordLift's configured language code ('en' by default).
316
+     * @since 3.9.0
317
+     */
318
+    public function get_language_code() {
319
+
320
+        $language = get_locale();
321
+        if ( ! $language ) {
322
+            return 'en';
323
+        }
324
+
325
+        return substr( $language, 0, 2 );
326
+    }
327
+
328
+    /**
329
+     * @param string $value WordLift's language code.
330
+     *
331
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1466
332
+     *
333
+     * Set WordLift's language code, used when storing strings to the Linked Data dataset.
334
+     *
335
+     * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
336
+     * language code form WordPress directly.
337
+     *
338
+     * @since 3.9.0
339
+     */
340
+    public function set_language_code( $value ) {
341
+
342
+        $this->set( 'wl_general_settings', self::LANGUAGE, $value );
343
+
344
+    }
345
+
346
+    /**
347
+     * Set the user preferences about sharing diagnostic with us.
348
+     *
349
+     * @param string $value The user preferences(yes/no).
350
+     *
351
+     * @since 3.19.0
352
+     */
353
+    public function set_diagnostic_preferences( $value ) {
354
+
355
+        $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
356
+
357
+    }
358
+
359
+    /**
360
+     * Get the user preferences about sharing diagnostic.
361
+     *
362
+     * @since 3.19.0
363
+     */
364
+    public function get_diagnostic_preferences() {
365
+
366
+        return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
367
+    }
368
+
369
+    /**
370
+     * Get WordLift's configured country code, by default 'us'.
371
+     *
372
+     * @return string WordLift's configured country code ('us' by default).
373
+     * @since 3.18.0
374
+     */
375
+    public function get_country_code() {
376
+
377
+        return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
378
+    }
379
+
380
+    /**
381
+     * Set WordLift's country code.
382
+     *
383
+     * @param string $value WordLift's country code.
384
+     *
385
+     * @since 3.18.0
386
+     */
387
+    public function set_country_code( $value ) {
388
+
389
+        $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
390
+
391
+    }
392
+
393
+    /**
394
+     * Get the alternateName.
395
+     *
396
+     * Website markup alternateName
397
+     *
398
+     * @return string|NULL alternateName or NULL if not set.
399
+     * @since 3.38.6
400
+     */
401
+    public function get_alternate_name() {
402
+        return $this->get( 'wl_general_settings', self::ALTERNATE_NAME );
403
+    }
404
+
405
+    /**
406
+     * Set the alternateName.
407
+     *
408
+     * @param int $value The alternateName value.
409
+     *
410
+     * @since 3.38.6
411
+     */
412
+    public function set_alternate_name( $value ) {
413
+
414
+        $this->set( 'wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags( $value ) );
415
+
416
+    }
417
+
418
+    /**
419
+     * Get the publisher entity post id.
420
+     *
421
+     * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
422
+     * Article markup.
423
+     *
424
+     * @return int|NULL The publisher entity post id or NULL if not set.
425
+     * @since 3.9.0
426
+     */
427
+    public function get_publisher_id() {
428
+
429
+        return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
430
+    }
431
+
432
+    /**
433
+     * Set the publisher entity post id.
434
+     *
435
+     * @param int $value The publisher entity post id.
436
+     *
437
+     * @since 3.9.0
438
+     */
439
+    public function set_publisher_id( $value ) {
440
+
441
+        $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
442
+
443
+    }
444
+
445
+    /**
446
+     * Get the dataset URI.
447
+     *
448
+     * @return string The dataset URI or an empty string if not set.
449
+     * @since 3.10.0
450
+     * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
451
+     */
452
+    public function get_dataset_uri() {
453
+
454
+        if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
455
+            return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
456
+        } else {
457
+            return null;
458
+        }
459
+    }
460
+
461
+    /**
462
+     * Set the dataset URI.
463
+     *
464
+     * @param string $value The dataset URI.
465
+     *
466
+     * @since 3.10.0
467
+     */
468
+    public function set_dataset_uri( $value ) {
469
+
470
+        $this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
471
+    }
472
+
473
+    /**
474
+     * Get the package type.
475
+     *
476
+     * @return string The package type or an empty string if not set.
477
+     * @since 3.20.0
478
+     */
479
+    public function get_package_type() {
480
+
481
+        return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
482
+    }
483
+
484
+    /**
485
+     * Set the package type.
486
+     *
487
+     * @param string $value The package type.
488
+     *
489
+     * @since 3.20.0
490
+     */
491
+    public function set_package_type( $value ) {
492
+        $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
493
+    }
494
+
495
+    /**
496
+     * Intercept the change of the WordLift key in order to set the dataset URI.
497
+     *
498
+     * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
499
+     *               store the configuration parameters such as country or language.
500
+     * @since 3.11.0
501
+     *
502
+     * @see https://github.com/insideout10/wordlift-plugin/issues/761
503
+     *
504
+     * @param array $old_value The old settings.
505
+     * @param array $new_value The new settings.
506
+     */
507
+    public function update_key( $old_value, $new_value ) {
508
+
509
+        // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
510
+        // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
511
+        $new_key = isset( $new_value['key'] ) ? trim( $new_value['key'] ) : '';
512
+
513
+        // If the key hasn't changed, don't do anything.
514
+        // WARN The 'update_option' hook is fired only if the new and old value are not equal.
515
+        // if ( $old_key === $new_key ) {
516
+        // return;
517
+        // }
518
+
519
+        // If the key is empty, empty the dataset URI.
520
+        if ( '' === $new_key ) {
521
+            $this->set_dataset_uri( '' );
522
+        }
523
+
524
+        // make the request to the remote server.
525
+        $this->get_remote_dataset_uri( $new_key );
526
+
527
+        do_action( 'wl_key_updated' );
528
+
529
+    }
530
+
531
+    /**
532
+     * Handle retrieving the dataset uri from the remote server.
533
+     *
534
+     * If a valid dataset uri is returned it is stored in the appropriate option,
535
+     * otherwise the option is set to empty string.
536
+     *
537
+     * @param string $key The key to be used.
538
+     *
539
+     * @since 3.12.0
540
+     *
541
+     * @since 3.17.0 send the site URL and get the dataset URI.
542
+     */
543
+    public function get_remote_dataset_uri( $key ) {
544
+
545
+        $this->log->trace( 'Getting the remote dataset URI and package type...' );
546
+
547
+        if ( empty( $key ) ) {
548
+            $this->log->warn( 'Key set to empty value.' );
549
+
550
+            $this->set_dataset_uri( '' );
551
+            $this->set_package_type( null );
552
+
553
+            return;
554
+        }
555
+
556
+        /**
557
+         * Allow 3rd parties to change the site_url.
558
+         *
559
+         * @param string $site_url The site url.
560
+         *
561
+         * @see https://github.com/insideout10/wordlift-plugin/issues/850
562
+         *
563
+         * @since 3.20.0
564
+         */
565
+        $home_url = get_option( 'home' );
566
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
567
+
568
+        // Build the URL.
569
+        $url = '/accounts'
570
+                . '?key=' . rawurlencode( $key )
571
+                . '&url=' . rawurlencode( $site_url )
572
+                . '&country=' . $this->get_country_code()
573
+                . '&language=' . $this->get_language_code();
574
+
575
+        $api_service = Default_Api_Service::get_instance();
576
+        /**
577
+         * @since 3.27.7.1
578
+         * The Key should be passed to headers, otherwise api would return null.
579
+         */
580
+        $headers  = array(
581
+            'Authorization' => "Key $key",
582
+        );
583
+        $response = $api_service->request( 'PUT', $url, $headers )->get_response();
584
+
585
+        // The response is an error.
586
+        if ( is_wp_error( $response ) ) {
587
+            $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
588
+
589
+            $this->set_dataset_uri( '' );
590
+            $this->set_package_type( null );
591
+
592
+            return;
593
+        }
594
+
595
+        // The response is not OK.
596
+        if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
597
+            $base_url = $api_service->get_base_url();
598
+
599
+            if ( ! is_array( $response ) ) {
600
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
601
+                $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
602
+            } else {
603
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
604
+                $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
605
+            }
606
+
607
+            $this->set_dataset_uri( '' );
608
+            $this->set_package_type( null );
609
+
610
+            return;
611
+        }
612
+
613
+        /*
614 614
 		 * We also store the package type.
615 615
 		 *
616 616
 		 * @since 3.20.0
617 617
 		 */
618
-		$json = json_decode( $response['body'] );
619
-		/**
620
-		 * @since 3.27.7
621
-		 * Remove the trailing slash returned from the new platform api.
622
-		 */
623
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
624
-		$dataset_uri = untrailingslashit( $json->datasetURI );
625
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
626
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
627
-
628
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
629
-
630
-		$this->set_dataset_uri( $dataset_uri );
631
-		$this->set_package_type( $package_type );
632
-	}
633
-
634
-	/**
635
-	 * Handle the edge case where a user submits the same key again
636
-	 * when he does not have the dataset uri to regain it.
637
-	 *
638
-	 * This can not be handled in the normal option update hook because
639
-	 * it is not being triggered when the save value equals to the one already
640
-	 * in the DB.
641
-	 *
642
-	 * @param mixed $value The new, unserialized option value.
643
-	 * @param mixed $old_value The old option value.
644
-	 *
645
-	 * @return mixed The same value in the $value parameter
646
-	 * @since 3.12.0
647
-	 */
648
-	public function maybe_update_dataset_uri( $value, $old_value ) {
649
-
650
-		// Check the old key value and the new one. Here we're only handling the
651
-		// case where the key hasn't changed and the dataset URI isn't set. The
652
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
653
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
654
-		$new_key = isset( $value['key'] ) ? trim( $value['key'] ) : '';
655
-
656
-		$dataset_uri = $this->get_dataset_uri();
657
-
658
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
659
-
660
-			// make the request to the remote server to try to get the dataset uri.
661
-			$this->get_remote_dataset_uri( $new_key );
662
-		}
663
-
664
-		return $value;
665
-	}
666
-
667
-	/**
668
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
669
-	 *
670
-	 * @param string $key The WordLift key to use.
671
-	 *
672
-	 * @return string The API URI.
673
-	 * @since 3.11.0
674
-	 */
675
-	public function get_accounts_by_key_dataset_uri( $key ) {
676
-
677
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
678
-	}
679
-
680
-	/**
681
-	 * Get the `accounts` end point.
682
-	 *
683
-	 * @return string The `accounts` end point.
684
-	 * @since 3.16.0
685
-	 */
686
-	public function get_accounts() {
687
-
688
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
689
-	}
690
-
691
-	/**
692
-	 * Get the `link by default` option.
693
-	 *
694
-	 * @return bool True if entities must be linked by default otherwise false.
695
-	 * @since 3.13.0
696
-	 */
697
-	public function is_link_by_default() {
698
-
699
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
700
-	}
701
-
702
-	/**
703
-	 * Set the `link by default` option.
704
-	 *
705
-	 * @param bool $value True to enabling linking by default, otherwise false.
706
-	 *
707
-	 * @since 3.13.0
708
-	 */
709
-	public function set_link_by_default( $value ) {
710
-
711
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
712
-	}
713
-
714
-	/**
715
-	 * Get the 'analytics-enable' option.
716
-	 *
717
-	 * @return string 'no' or 'yes' representing bool.
718
-	 * @since 3.21.0
719
-	 */
720
-	public function is_analytics_enable() {
721
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
722
-	}
723
-
724
-	/**
725
-	 * Set the `analytics-enable` option.
726
-	 *
727
-	 * @param bool $value True to enabling analytics, otherwise false.
728
-	 *
729
-	 * @since 3.21.0
730
-	 */
731
-	public function set_is_analytics_enable( $value ) {
732
-
733
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
734
-	}
735
-
736
-	/**
737
-	 * Get the 'analytics-entity-uri-dimention' option.
738
-	 *
739
-	 * @return int
740
-	 * @since 3.21.0
741
-	 */
742
-	public function get_analytics_entity_uri_dimension() {
743
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
744
-	}
745
-
746
-	/**
747
-	 * Get the 'analytics-entity-type-dimension' option.
748
-	 *
749
-	 * @return int
750
-	 * @since 3.21.0
751
-	 */
752
-	public function get_analytics_entity_type_dimension() {
753
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
754
-	}
755
-
756
-	/**
757
-	 * Get the URL to perform autocomplete request.
758
-	 *
759
-	 * @return string The URL to call to perform the autocomplete request.
760
-	 * @since 3.15.0
761
-	 */
762
-	public function get_autocomplete_url() {
763
-
764
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
765
-
766
-	}
767
-
768
-	/**
769
-	 * Get the URL to perform feedback deactivation request.
770
-	 *
771
-	 * @return string The URL to call to perform the feedback deactivation request.
772
-	 * @since 3.19.0
773
-	 */
774
-	public function get_deactivation_feedback_url() {
775
-
776
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
777
-
778
-	}
779
-
780
-	/**
781
-	 * Get the base API URL.
782
-	 *
783
-	 * @return string The base API URL.
784
-	 * @since 3.20.0
785
-	 */
786
-	public function get_api_url() {
787
-
788
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
789
-	}
790
-
791
-	public function get_network_dataset_ids() {
792
-		return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
793
-	}
794
-
795
-	public function set_network_dataset_ids( $network_dataset_ids ) {
796
-		$this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
797
-	}
798
-
799
-	public function get_skip_installation_notice() {
800
-
801
-		return $this->get( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false );
802
-	}
803
-
804
-	public function set_skip_installation_notice( $value ) {
805
-
806
-		$this->set( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value );
807
-
808
-	}
809
-
810
-	/**
811
-	 * The override URL or false if not set.
812
-	 *
813
-	 * @return false|string
814
-	 */
815
-	public function get_override_website_url() {
816
-		$value = $this->get( 'wl_general_settings', self::OVERRIDE_WEBSITE_URL, false );
817
-		if ( empty( $value ) ) {
818
-			return false;
819
-		}
820
-
821
-		return untrailingslashit( $value );
822
-	}
823
-
824
-	public function set_override_website_url( $value ) {
825
-		$this->set( 'wl_general_settings', self::OVERRIDE_WEBSITE_URL, $value );
826
-	}
618
+        $json = json_decode( $response['body'] );
619
+        /**
620
+         * @since 3.27.7
621
+         * Remove the trailing slash returned from the new platform api.
622
+         */
623
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
624
+        $dataset_uri = untrailingslashit( $json->datasetURI );
625
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
626
+        $package_type = isset( $json->packageType ) ? $json->packageType : null;
627
+
628
+        $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
629
+
630
+        $this->set_dataset_uri( $dataset_uri );
631
+        $this->set_package_type( $package_type );
632
+    }
633
+
634
+    /**
635
+     * Handle the edge case where a user submits the same key again
636
+     * when he does not have the dataset uri to regain it.
637
+     *
638
+     * This can not be handled in the normal option update hook because
639
+     * it is not being triggered when the save value equals to the one already
640
+     * in the DB.
641
+     *
642
+     * @param mixed $value The new, unserialized option value.
643
+     * @param mixed $old_value The old option value.
644
+     *
645
+     * @return mixed The same value in the $value parameter
646
+     * @since 3.12.0
647
+     */
648
+    public function maybe_update_dataset_uri( $value, $old_value ) {
649
+
650
+        // Check the old key value and the new one. Here we're only handling the
651
+        // case where the key hasn't changed and the dataset URI isn't set. The
652
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
653
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
654
+        $new_key = isset( $value['key'] ) ? trim( $value['key'] ) : '';
655
+
656
+        $dataset_uri = $this->get_dataset_uri();
657
+
658
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
659
+
660
+            // make the request to the remote server to try to get the dataset uri.
661
+            $this->get_remote_dataset_uri( $new_key );
662
+        }
663
+
664
+        return $value;
665
+    }
666
+
667
+    /**
668
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
669
+     *
670
+     * @param string $key The WordLift key to use.
671
+     *
672
+     * @return string The API URI.
673
+     * @since 3.11.0
674
+     */
675
+    public function get_accounts_by_key_dataset_uri( $key ) {
676
+
677
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
678
+    }
679
+
680
+    /**
681
+     * Get the `accounts` end point.
682
+     *
683
+     * @return string The `accounts` end point.
684
+     * @since 3.16.0
685
+     */
686
+    public function get_accounts() {
687
+
688
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
689
+    }
690
+
691
+    /**
692
+     * Get the `link by default` option.
693
+     *
694
+     * @return bool True if entities must be linked by default otherwise false.
695
+     * @since 3.13.0
696
+     */
697
+    public function is_link_by_default() {
698
+
699
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
700
+    }
701
+
702
+    /**
703
+     * Set the `link by default` option.
704
+     *
705
+     * @param bool $value True to enabling linking by default, otherwise false.
706
+     *
707
+     * @since 3.13.0
708
+     */
709
+    public function set_link_by_default( $value ) {
710
+
711
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
712
+    }
713
+
714
+    /**
715
+     * Get the 'analytics-enable' option.
716
+     *
717
+     * @return string 'no' or 'yes' representing bool.
718
+     * @since 3.21.0
719
+     */
720
+    public function is_analytics_enable() {
721
+        return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
722
+    }
723
+
724
+    /**
725
+     * Set the `analytics-enable` option.
726
+     *
727
+     * @param bool $value True to enabling analytics, otherwise false.
728
+     *
729
+     * @since 3.21.0
730
+     */
731
+    public function set_is_analytics_enable( $value ) {
732
+
733
+        $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
734
+    }
735
+
736
+    /**
737
+     * Get the 'analytics-entity-uri-dimention' option.
738
+     *
739
+     * @return int
740
+     * @since 3.21.0
741
+     */
742
+    public function get_analytics_entity_uri_dimension() {
743
+        return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
744
+    }
745
+
746
+    /**
747
+     * Get the 'analytics-entity-type-dimension' option.
748
+     *
749
+     * @return int
750
+     * @since 3.21.0
751
+     */
752
+    public function get_analytics_entity_type_dimension() {
753
+        return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
754
+    }
755
+
756
+    /**
757
+     * Get the URL to perform autocomplete request.
758
+     *
759
+     * @return string The URL to call to perform the autocomplete request.
760
+     * @since 3.15.0
761
+     */
762
+    public function get_autocomplete_url() {
763
+
764
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
765
+
766
+    }
767
+
768
+    /**
769
+     * Get the URL to perform feedback deactivation request.
770
+     *
771
+     * @return string The URL to call to perform the feedback deactivation request.
772
+     * @since 3.19.0
773
+     */
774
+    public function get_deactivation_feedback_url() {
775
+
776
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
777
+
778
+    }
779
+
780
+    /**
781
+     * Get the base API URL.
782
+     *
783
+     * @return string The base API URL.
784
+     * @since 3.20.0
785
+     */
786
+    public function get_api_url() {
787
+
788
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
789
+    }
790
+
791
+    public function get_network_dataset_ids() {
792
+        return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
793
+    }
794
+
795
+    public function set_network_dataset_ids( $network_dataset_ids ) {
796
+        $this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
797
+    }
798
+
799
+    public function get_skip_installation_notice() {
800
+
801
+        return $this->get( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false );
802
+    }
803
+
804
+    public function set_skip_installation_notice( $value ) {
805
+
806
+        $this->set( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value );
807
+
808
+    }
809
+
810
+    /**
811
+     * The override URL or false if not set.
812
+     *
813
+     * @return false|string
814
+     */
815
+    public function get_override_website_url() {
816
+        $value = $this->get( 'wl_general_settings', self::OVERRIDE_WEBSITE_URL, false );
817
+        if ( empty( $value ) ) {
818
+            return false;
819
+        }
820
+
821
+        return untrailingslashit( $value );
822
+    }
823
+
824
+    public function set_override_website_url( $value ) {
825
+        $this->set( 'wl_general_settings', self::OVERRIDE_WEBSITE_URL, $value );
826
+    }
827 827
 
828 828
 }
Please login to merge, or discard this patch.
Spacing   +113 added lines, -113 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
 
@@ -152,10 +152,10 @@  discard block
 block discarded – undo
152 152
 	 */
153 153
 	protected function __construct() {
154 154
 
155
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
155
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
156 156
 
157 157
 		// Sync some configuration properties when key is validated.
158
-		add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
158
+		add_action('wl_key_validation_response', array($this, 'sync'));
159 159
 
160 160
 	}
161 161
 
@@ -164,15 +164,15 @@  discard block
 block discarded – undo
164 164
 	 *
165 165
 	 * @return void
166 166
 	 */
167
-	public function sync( $response ) {
168
-		if ( ! $response->is_success() ) {
167
+	public function sync($response) {
168
+		if ( ! $response->is_success()) {
169 169
 			return;
170 170
 		}
171
-		$data = json_decode( $response->get_body(), true );
172
-		if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
171
+		$data = json_decode($response->get_body(), true);
172
+		if ( ! is_array($data) || ! array_key_exists('networkDatasetId', $data)) {
173 173
 			return;
174 174
 		}
175
-		$this->set_network_dataset_ids( $data['networkDatasetId'] );
175
+		$this->set_network_dataset_ids($data['networkDatasetId']);
176 176
 	}
177 177
 
178 178
 	/**
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
 	 */
194 194
 	public static function get_instance() {
195 195
 
196
-		if ( ! isset( self::$instance ) ) {
196
+		if ( ! isset(self::$instance)) {
197 197
 			self::$instance = new self();
198 198
 		}
199 199
 
@@ -211,11 +211,11 @@  discard block
 block discarded – undo
211 211
 	 * @return mixed The configuration value or the default value if not found.
212 212
 	 * @since 3.6.0
213 213
 	 */
214
-	private function get( $option, $key, $default = '' ) {
214
+	private function get($option, $key, $default = '') {
215 215
 
216
-		$options = get_option( $option, array() );
216
+		$options = get_option($option, array());
217 217
 
218
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
218
+		return isset($options[$key]) ? $options[$key] : $default;
219 219
 	}
220 220
 
221 221
 	/**
@@ -227,12 +227,12 @@  discard block
 block discarded – undo
227 227
 	 *
228 228
 	 * @since 3.9.0
229 229
 	 */
230
-	private function set( $option, $key, $value ) {
230
+	private function set($option, $key, $value) {
231 231
 
232
-		$values         = get_option( $option );
233
-		$values         = isset( $values ) ? $values : array();
234
-		$values[ $key ] = $value;
235
-		update_option( $option, $values );
232
+		$values         = get_option($option);
233
+		$values         = isset($values) ? $values : array();
234
+		$values[$key] = $value;
235
+		update_option($option, $values);
236 236
 
237 237
 	}
238 238
 
@@ -244,7 +244,7 @@  discard block
 block discarded – undo
244 244
 	 */
245 245
 	public function get_entity_base_path() {
246 246
 
247
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
247
+		return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity');
248 248
 	}
249 249
 
250 250
 	/**
@@ -254,9 +254,9 @@  discard block
 block discarded – undo
254 254
 	 *
255 255
 	 * @since 3.9.0
256 256
 	 */
257
-	public function set_entity_base_path( $value ) {
257
+	public function set_entity_base_path($value) {
258 258
 
259
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
259
+		$this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value);
260 260
 
261 261
 	}
262 262
 
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
 	 */
269 269
 	public function is_skip_wizard() {
270 270
 
271
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
271
+		return $this->get('wl_general_settings', self::SKIP_WIZARD, false);
272 272
 	}
273 273
 
274 274
 	/**
@@ -278,9 +278,9 @@  discard block
 block discarded – undo
278 278
 	 *
279 279
 	 * @since 3.9.0
280 280
 	 */
281
-	public function set_skip_wizard( $value ) {
281
+	public function set_skip_wizard($value) {
282 282
 
283
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
283
+		$this->set('wl_general_settings', self::SKIP_WIZARD, true === $value);
284 284
 
285 285
 	}
286 286
 
@@ -292,7 +292,7 @@  discard block
 block discarded – undo
292 292
 	 */
293 293
 	public function get_key() {
294 294
 
295
-		return $this->get( 'wl_general_settings', self::KEY, '' );
295
+		return $this->get('wl_general_settings', self::KEY, '');
296 296
 	}
297 297
 
298 298
 	/**
@@ -302,9 +302,9 @@  discard block
 block discarded – undo
302 302
 	 *
303 303
 	 * @since 3.9.0
304 304
 	 */
305
-	public function set_key( $value ) {
305
+	public function set_key($value) {
306 306
 
307
-		$this->set( 'wl_general_settings', self::KEY, $value );
307
+		$this->set('wl_general_settings', self::KEY, $value);
308 308
 	}
309 309
 
310 310
 	/**
@@ -318,11 +318,11 @@  discard block
 block discarded – undo
318 318
 	public function get_language_code() {
319 319
 
320 320
 		$language = get_locale();
321
-		if ( ! $language ) {
321
+		if ( ! $language) {
322 322
 			return 'en';
323 323
 		}
324 324
 
325
-		return substr( $language, 0, 2 );
325
+		return substr($language, 0, 2);
326 326
 	}
327 327
 
328 328
 	/**
@@ -337,9 +337,9 @@  discard block
 block discarded – undo
337 337
 	 *
338 338
 	 * @since 3.9.0
339 339
 	 */
340
-	public function set_language_code( $value ) {
340
+	public function set_language_code($value) {
341 341
 
342
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
342
+		$this->set('wl_general_settings', self::LANGUAGE, $value);
343 343
 
344 344
 	}
345 345
 
@@ -350,9 +350,9 @@  discard block
 block discarded – undo
350 350
 	 *
351 351
 	 * @since 3.19.0
352 352
 	 */
353
-	public function set_diagnostic_preferences( $value ) {
353
+	public function set_diagnostic_preferences($value) {
354 354
 
355
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
355
+		$this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value);
356 356
 
357 357
 	}
358 358
 
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 	 */
364 364
 	public function get_diagnostic_preferences() {
365 365
 
366
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
366
+		return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no');
367 367
 	}
368 368
 
369 369
 	/**
@@ -374,7 +374,7 @@  discard block
 block discarded – undo
374 374
 	 */
375 375
 	public function get_country_code() {
376 376
 
377
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
377
+		return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us');
378 378
 	}
379 379
 
380 380
 	/**
@@ -384,9 +384,9 @@  discard block
 block discarded – undo
384 384
 	 *
385 385
 	 * @since 3.18.0
386 386
 	 */
387
-	public function set_country_code( $value ) {
387
+	public function set_country_code($value) {
388 388
 
389
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
389
+		$this->set('wl_general_settings', self::COUNTRY_CODE, $value);
390 390
 
391 391
 	}
392 392
 
@@ -399,7 +399,7 @@  discard block
 block discarded – undo
399 399
 	 * @since 3.38.6
400 400
 	 */
401 401
 	public function get_alternate_name() {
402
-		return $this->get( 'wl_general_settings', self::ALTERNATE_NAME );
402
+		return $this->get('wl_general_settings', self::ALTERNATE_NAME);
403 403
 	}
404 404
 
405 405
 	/**
@@ -409,9 +409,9 @@  discard block
 block discarded – undo
409 409
 	 *
410 410
 	 * @since 3.38.6
411 411
 	 */
412
-	public function set_alternate_name( $value ) {
412
+	public function set_alternate_name($value) {
413 413
 
414
-		$this->set( 'wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags( $value ) );
414
+		$this->set('wl_general_settings', self::ALTERNATE_NAME, wp_strip_all_tags($value));
415 415
 
416 416
 	}
417 417
 
@@ -426,7 +426,7 @@  discard block
 block discarded – undo
426 426
 	 */
427 427
 	public function get_publisher_id() {
428 428
 
429
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
429
+		return $this->get('wl_general_settings', self::PUBLISHER_ID, null);
430 430
 	}
431 431
 
432 432
 	/**
@@ -436,9 +436,9 @@  discard block
 block discarded – undo
436 436
 	 *
437 437
 	 * @since 3.9.0
438 438
 	 */
439
-	public function set_publisher_id( $value ) {
439
+	public function set_publisher_id($value) {
440 440
 
441
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
441
+		$this->set('wl_general_settings', self::PUBLISHER_ID, $value);
442 442
 
443 443
 	}
444 444
 
@@ -451,8 +451,8 @@  discard block
 block discarded – undo
451 451
 	 */
452 452
 	public function get_dataset_uri() {
453 453
 
454
-		if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
455
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
454
+		if (apply_filters('wl_feature__enable__dataset', true)) {
455
+			return $this->get('wl_advanced_settings', self::DATASET_URI, null);
456 456
 		} else {
457 457
 			return null;
458 458
 		}
@@ -465,9 +465,9 @@  discard block
 block discarded – undo
465 465
 	 *
466 466
 	 * @since 3.10.0
467 467
 	 */
468
-	public function set_dataset_uri( $value ) {
468
+	public function set_dataset_uri($value) {
469 469
 
470
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
470
+		$this->set('wl_advanced_settings', self::DATASET_URI, $value);
471 471
 	}
472 472
 
473 473
 	/**
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
 	 */
479 479
 	public function get_package_type() {
480 480
 
481
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
481
+		return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null);
482 482
 	}
483 483
 
484 484
 	/**
@@ -488,8 +488,8 @@  discard block
 block discarded – undo
488 488
 	 *
489 489
 	 * @since 3.20.0
490 490
 	 */
491
-	public function set_package_type( $value ) {
492
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
491
+	public function set_package_type($value) {
492
+		$this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value);
493 493
 	}
494 494
 
495 495
 	/**
@@ -504,11 +504,11 @@  discard block
 block discarded – undo
504 504
 	 * @param array $old_value The old settings.
505 505
 	 * @param array $new_value The new settings.
506 506
 	 */
507
-	public function update_key( $old_value, $new_value ) {
507
+	public function update_key($old_value, $new_value) {
508 508
 
509 509
 		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
510 510
 		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
511
-		$new_key = isset( $new_value['key'] ) ? trim( $new_value['key'] ) : '';
511
+		$new_key = isset($new_value['key']) ? trim($new_value['key']) : '';
512 512
 
513 513
 		// If the key hasn't changed, don't do anything.
514 514
 		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
@@ -517,14 +517,14 @@  discard block
 block discarded – undo
517 517
 		// }
518 518
 
519 519
 		// If the key is empty, empty the dataset URI.
520
-		if ( '' === $new_key ) {
521
-			$this->set_dataset_uri( '' );
520
+		if ('' === $new_key) {
521
+			$this->set_dataset_uri('');
522 522
 		}
523 523
 
524 524
 		// make the request to the remote server.
525
-		$this->get_remote_dataset_uri( $new_key );
525
+		$this->get_remote_dataset_uri($new_key);
526 526
 
527
-		do_action( 'wl_key_updated' );
527
+		do_action('wl_key_updated');
528 528
 
529 529
 	}
530 530
 
@@ -540,15 +540,15 @@  discard block
 block discarded – undo
540 540
 	 *
541 541
 	 * @since 3.17.0 send the site URL and get the dataset URI.
542 542
 	 */
543
-	public function get_remote_dataset_uri( $key ) {
543
+	public function get_remote_dataset_uri($key) {
544 544
 
545
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
545
+		$this->log->trace('Getting the remote dataset URI and package type...');
546 546
 
547
-		if ( empty( $key ) ) {
548
-			$this->log->warn( 'Key set to empty value.' );
547
+		if (empty($key)) {
548
+			$this->log->warn('Key set to empty value.');
549 549
 
550
-			$this->set_dataset_uri( '' );
551
-			$this->set_package_type( null );
550
+			$this->set_dataset_uri('');
551
+			$this->set_package_type(null);
552 552
 
553 553
 			return;
554 554
 		}
@@ -562,15 +562,15 @@  discard block
 block discarded – undo
562 562
 		 *
563 563
 		 * @since 3.20.0
564 564
 		 */
565
-		$home_url = get_option( 'home' );
566
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
565
+		$home_url = get_option('home');
566
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
567 567
 
568 568
 		// Build the URL.
569 569
 		$url = '/accounts'
570
-			   . '?key=' . rawurlencode( $key )
571
-			   . '&url=' . rawurlencode( $site_url )
572
-			   . '&country=' . $this->get_country_code()
573
-			   . '&language=' . $this->get_language_code();
570
+			   . '?key='.rawurlencode($key)
571
+			   . '&url='.rawurlencode($site_url)
572
+			   . '&country='.$this->get_country_code()
573
+			   . '&language='.$this->get_language_code();
574 574
 
575 575
 		$api_service = Default_Api_Service::get_instance();
576 576
 		/**
@@ -580,32 +580,32 @@  discard block
 block discarded – undo
580 580
 		$headers  = array(
581 581
 			'Authorization' => "Key $key",
582 582
 		);
583
-		$response = $api_service->request( 'PUT', $url, $headers )->get_response();
583
+		$response = $api_service->request('PUT', $url, $headers)->get_response();
584 584
 
585 585
 		// The response is an error.
586
-		if ( is_wp_error( $response ) ) {
587
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
586
+		if (is_wp_error($response)) {
587
+			$this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message());
588 588
 
589
-			$this->set_dataset_uri( '' );
590
-			$this->set_package_type( null );
589
+			$this->set_dataset_uri('');
590
+			$this->set_package_type(null);
591 591
 
592 592
 			return;
593 593
 		}
594 594
 
595 595
 		// The response is not OK.
596
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
596
+		if ( ! is_array($response) || 200 !== (int) $response['response']['code']) {
597 597
 			$base_url = $api_service->get_base_url();
598 598
 
599
-			if ( ! is_array( $response ) ) {
599
+			if ( ! is_array($response)) {
600 600
 				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
601
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
601
+				$this->log->error("Unexpected response when opening URL $base_url$url: ".var_export($response, true));
602 602
 			} else {
603 603
 				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
604
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
604
+				$this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true));
605 605
 			}
606 606
 
607
-			$this->set_dataset_uri( '' );
608
-			$this->set_package_type( null );
607
+			$this->set_dataset_uri('');
608
+			$this->set_package_type(null);
609 609
 
610 610
 			return;
611 611
 		}
@@ -615,20 +615,20 @@  discard block
 block discarded – undo
615 615
 		 *
616 616
 		 * @since 3.20.0
617 617
 		 */
618
-		$json = json_decode( $response['body'] );
618
+		$json = json_decode($response['body']);
619 619
 		/**
620 620
 		 * @since 3.27.7
621 621
 		 * Remove the trailing slash returned from the new platform api.
622 622
 		 */
623 623
 		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
624
-		$dataset_uri = untrailingslashit( $json->datasetURI );
624
+		$dataset_uri = untrailingslashit($json->datasetURI);
625 625
 		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
626
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
626
+		$package_type = isset($json->packageType) ? $json->packageType : null;
627 627
 
628
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
628
+		$this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]...");
629 629
 
630
-		$this->set_dataset_uri( $dataset_uri );
631
-		$this->set_package_type( $package_type );
630
+		$this->set_dataset_uri($dataset_uri);
631
+		$this->set_package_type($package_type);
632 632
 	}
633 633
 
634 634
 	/**
@@ -645,20 +645,20 @@  discard block
 block discarded – undo
645 645
 	 * @return mixed The same value in the $value parameter
646 646
 	 * @since 3.12.0
647 647
 	 */
648
-	public function maybe_update_dataset_uri( $value, $old_value ) {
648
+	public function maybe_update_dataset_uri($value, $old_value) {
649 649
 
650 650
 		// Check the old key value and the new one. Here we're only handling the
651 651
 		// case where the key hasn't changed and the dataset URI isn't set. The
652 652
 		// other case, i.e. a new key is inserted, is handled at `update_key`.
653
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
654
-		$new_key = isset( $value['key'] ) ? trim( $value['key'] ) : '';
653
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
654
+		$new_key = isset($value['key']) ? trim($value['key']) : '';
655 655
 
656 656
 		$dataset_uri = $this->get_dataset_uri();
657 657
 
658
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
658
+		if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) {
659 659
 
660 660
 			// make the request to the remote server to try to get the dataset uri.
661
-			$this->get_remote_dataset_uri( $new_key );
661
+			$this->get_remote_dataset_uri($new_key);
662 662
 		}
663 663
 
664 664
 		return $value;
@@ -672,9 +672,9 @@  discard block
 block discarded – undo
672 672
 	 * @return string The API URI.
673 673
 	 * @since 3.11.0
674 674
 	 */
675
-	public function get_accounts_by_key_dataset_uri( $key ) {
675
+	public function get_accounts_by_key_dataset_uri($key) {
676 676
 
677
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
677
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri";
678 678
 	}
679 679
 
680 680
 	/**
@@ -685,7 +685,7 @@  discard block
 block discarded – undo
685 685
 	 */
686 686
 	public function get_accounts() {
687 687
 
688
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
688
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts';
689 689
 	}
690 690
 
691 691
 	/**
@@ -696,7 +696,7 @@  discard block
 block discarded – undo
696 696
 	 */
697 697
 	public function is_link_by_default() {
698 698
 
699
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
699
+		return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes');
700 700
 	}
701 701
 
702 702
 	/**
@@ -706,9 +706,9 @@  discard block
 block discarded – undo
706 706
 	 *
707 707
 	 * @since 3.13.0
708 708
 	 */
709
-	public function set_link_by_default( $value ) {
709
+	public function set_link_by_default($value) {
710 710
 
711
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
711
+		$this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no');
712 712
 	}
713 713
 
714 714
 	/**
@@ -718,7 +718,7 @@  discard block
 block discarded – undo
718 718
 	 * @since 3.21.0
719 719
 	 */
720 720
 	public function is_analytics_enable() {
721
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
721
+		return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no');
722 722
 	}
723 723
 
724 724
 	/**
@@ -728,9 +728,9 @@  discard block
 block discarded – undo
728 728
 	 *
729 729
 	 * @since 3.21.0
730 730
 	 */
731
-	public function set_is_analytics_enable( $value ) {
731
+	public function set_is_analytics_enable($value) {
732 732
 
733
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
733
+		$this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no');
734 734
 	}
735 735
 
736 736
 	/**
@@ -740,7 +740,7 @@  discard block
 block discarded – undo
740 740
 	 * @since 3.21.0
741 741
 	 */
742 742
 	public function get_analytics_entity_uri_dimension() {
743
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
743
+		return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1);
744 744
 	}
745 745
 
746 746
 	/**
@@ -750,7 +750,7 @@  discard block
 block discarded – undo
750 750
 	 * @since 3.21.0
751 751
 	 */
752 752
 	public function get_analytics_entity_type_dimension() {
753
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
753
+		return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2);
754 754
 	}
755 755
 
756 756
 	/**
@@ -761,7 +761,7 @@  discard block
 block discarded – undo
761 761
 	 */
762 762
 	public function get_autocomplete_url() {
763 763
 
764
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
764
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete';
765 765
 
766 766
 	}
767 767
 
@@ -773,7 +773,7 @@  discard block
 block discarded – undo
773 773
 	 */
774 774
 	public function get_deactivation_feedback_url() {
775 775
 
776
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
776
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks';
777 777
 
778 778
 	}
779 779
 
@@ -789,21 +789,21 @@  discard block
 block discarded – undo
789 789
 	}
790 790
 
791 791
 	public function get_network_dataset_ids() {
792
-		return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
792
+		return $this->get('wl_advanced_settings', self::NETWORK_DATASET_IDS, array());
793 793
 	}
794 794
 
795
-	public function set_network_dataset_ids( $network_dataset_ids ) {
796
-		$this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
795
+	public function set_network_dataset_ids($network_dataset_ids) {
796
+		$this->set('wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids);
797 797
 	}
798 798
 
799 799
 	public function get_skip_installation_notice() {
800 800
 
801
-		return $this->get( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false );
801
+		return $this->get('wl_general_settings', self::SKIP_INSTALLATION_NOTICE, false);
802 802
 	}
803 803
 
804
-	public function set_skip_installation_notice( $value ) {
804
+	public function set_skip_installation_notice($value) {
805 805
 
806
-		$this->set( 'wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value );
806
+		$this->set('wl_general_settings', self::SKIP_INSTALLATION_NOTICE, $value);
807 807
 
808 808
 	}
809 809
 
@@ -813,16 +813,16 @@  discard block
 block discarded – undo
813 813
 	 * @return false|string
814 814
 	 */
815 815
 	public function get_override_website_url() {
816
-		$value = $this->get( 'wl_general_settings', self::OVERRIDE_WEBSITE_URL, false );
817
-		if ( empty( $value ) ) {
816
+		$value = $this->get('wl_general_settings', self::OVERRIDE_WEBSITE_URL, false);
817
+		if (empty($value)) {
818 818
 			return false;
819 819
 		}
820 820
 
821
-		return untrailingslashit( $value );
821
+		return untrailingslashit($value);
822 822
 	}
823 823
 
824
-	public function set_override_website_url( $value ) {
825
-		$this->set( 'wl_general_settings', self::OVERRIDE_WEBSITE_URL, $value );
824
+	public function set_override_website_url($value) {
825
+		$this->set('wl_general_settings', self::OVERRIDE_WEBSITE_URL, $value);
826 826
 	}
827 827
 
828 828
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-uri-service.php 2 patches
Indentation   +249 added lines, -249 removed lines patch added patch discarded remove patch
@@ -20,100 +20,100 @@  discard block
 block discarded – undo
20 20
  */
21 21
 class Wordlift_Entity_Uri_Service {
22 22
 
23
-	/**
24
-	 * A {@link Wordlift_Log_Service} instance.
25
-	 *
26
-	 * @since  3.16.3
27
-	 * @access private
28
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
-	 */
30
-	private $log;
31
-
32
-	/**
33
-	 * An array of URIs to post ID valid for the current request.
34
-	 *
35
-	 * @since  3.16.3
36
-	 * @access private
37
-	 * @var array $uri_to_post An array of URIs to post ID valid for the current request.
38
-	 */
39
-	protected $uri_to_post;
40
-
41
-	/**
42
-	 * Create a {@link Wordlift_Entity_Uri_Service} instance.
43
-	 *
44
-	 * @since 3.16.3
45
-	 */
46
-	protected function __construct() {
47
-
48
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
49
-
50
-		// Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`.
51
-		add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) );
52
-		add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 );
53
-
54
-	}
55
-
56
-	/**
57
-	 * Holds the {@link Wordlift_Entity_Uri_Service} instance.
58
-	 *
59
-	 * @since 3.21.5
60
-	 * @access private
61
-	 * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton.
62
-	 */
63
-	private static $instance = null;
64
-
65
-	/**
66
-	 * Get the singleton.
67
-	 *
68
-	 * @return Wordlift_Entity_Uri_Service The singleton instance.
69
-	 * @since 3.21.5
70
-	 */
71
-	public static function get_instance() {
72
-
73
-		if ( ! isset( self::$instance ) ) {
74
-			$entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' );
75
-			self::$instance           = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service );
76
-
77
-		}
78
-
79
-		return self::$instance;
80
-	}
81
-
82
-	/**
83
-	 * Try to find a post when the content service doesn't find it.
84
-	 *
85
-	 * @param WP_Post|null $post
86
-	 * @param string       $uri
87
-	 *
88
-	 * @return false|int
89
-	 */
90
-	public function content_service__post__not_found( $post, $uri ) {
91
-		return $this->get_post_id_from_url( $uri );
92
-	}
93
-
94
-	/**
95
-	 * Preload the provided URIs in the local cache.
96
-	 *
97
-	 * This function will populate the local `$uri_to_post` array by running a
98
-	 * single query with all the URIs and returning the mappings in the array.
99
-	 *
100
-	 * @param array $uris An array of URIs.
101
-	 *
102
-	 * @since 3.16.3
103
-	 */
104
-	public function preload_uris( $uris ) {
105
-
106
-		// Bail out if there are no URIs.
107
-		if ( 0 === count( $uris ) ) {
108
-			return;
109
-		}
110
-
111
-		$this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' );
112
-
113
-		global $wpdb;
114
-		$in_post_types  = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) );
115
-		$in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) );
116
-		$sql            = "
23
+    /**
24
+     * A {@link Wordlift_Log_Service} instance.
25
+     *
26
+     * @since  3.16.3
27
+     * @access private
28
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
+     */
30
+    private $log;
31
+
32
+    /**
33
+     * An array of URIs to post ID valid for the current request.
34
+     *
35
+     * @since  3.16.3
36
+     * @access private
37
+     * @var array $uri_to_post An array of URIs to post ID valid for the current request.
38
+     */
39
+    protected $uri_to_post;
40
+
41
+    /**
42
+     * Create a {@link Wordlift_Entity_Uri_Service} instance.
43
+     *
44
+     * @since 3.16.3
45
+     */
46
+    protected function __construct() {
47
+
48
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
49
+
50
+        // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`.
51
+        add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) );
52
+        add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 );
53
+
54
+    }
55
+
56
+    /**
57
+     * Holds the {@link Wordlift_Entity_Uri_Service} instance.
58
+     *
59
+     * @since 3.21.5
60
+     * @access private
61
+     * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton.
62
+     */
63
+    private static $instance = null;
64
+
65
+    /**
66
+     * Get the singleton.
67
+     *
68
+     * @return Wordlift_Entity_Uri_Service The singleton instance.
69
+     * @since 3.21.5
70
+     */
71
+    public static function get_instance() {
72
+
73
+        if ( ! isset( self::$instance ) ) {
74
+            $entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' );
75
+            self::$instance           = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service );
76
+
77
+        }
78
+
79
+        return self::$instance;
80
+    }
81
+
82
+    /**
83
+     * Try to find a post when the content service doesn't find it.
84
+     *
85
+     * @param WP_Post|null $post
86
+     * @param string       $uri
87
+     *
88
+     * @return false|int
89
+     */
90
+    public function content_service__post__not_found( $post, $uri ) {
91
+        return $this->get_post_id_from_url( $uri );
92
+    }
93
+
94
+    /**
95
+     * Preload the provided URIs in the local cache.
96
+     *
97
+     * This function will populate the local `$uri_to_post` array by running a
98
+     * single query with all the URIs and returning the mappings in the array.
99
+     *
100
+     * @param array $uris An array of URIs.
101
+     *
102
+     * @since 3.16.3
103
+     */
104
+    public function preload_uris( $uris ) {
105
+
106
+        // Bail out if there are no URIs.
107
+        if ( 0 === count( $uris ) ) {
108
+            return;
109
+        }
110
+
111
+        $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' );
112
+
113
+        global $wpdb;
114
+        $in_post_types  = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) );
115
+        $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) );
116
+        $sql            = "
117 117
 			SELECT ID FROM $wpdb->posts p
118 118
 			INNER JOIN $wpdb->postmeta pm
119 119
 			 ON pm.post_id = p.ID
@@ -123,160 +123,160 @@  discard block
 block discarded – undo
123 123
 			  AND p.post_status IN ( 'publish', 'draft', 'private', 'future' )
124 124
   		";
125 125
 
126
-		// Get the posts.
127
-		$posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
128
-
129
-		// Populate the array. We reinitialize the array on purpose because
130
-		// we don't want these data to long live.
131
-		$this->uri_to_post = array_reduce(
132
-			$posts,
133
-			function ( $carry, $item ) {
134
-				$uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS );
135
-
136
-				$uri = Wordpress_Content_Service::get_instance()
137
-												->get_entity_id( Wordpress_Content_Id::create_post( $item ) );
138
-
139
-				if ( isset( $uri ) ) {
140
-					$uris[] = $uri;
141
-				}
142
-
143
-				return $carry
144
-					   // Get the URI related to the post and fill them with the item id.
145
-					   + array_fill_keys( $uris, $item );
146
-			},
147
-			array()
148
-		);
149
-
150
-		// Add the not found URIs.
151
-		$this->uri_to_post += array_fill_keys( $uris, null );
152
-
153
-		$this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' );
154
-
155
-	}
156
-
157
-	/**
158
-	 * Reset the URI to post local cache.
159
-	 *
160
-	 * @since 3.16.3
161
-	 */
162
-	public function reset_uris() {
163
-
164
-		$this->uri_to_post = array();
165
-
166
-	}
167
-
168
-	/**
169
-	 * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
170
-	 *
171
-	 * @param string $uri The entity URI.
172
-	 *
173
-	 * @return WP_Post|null A WP_Post instance or null if not found.
174
-	 * @since 3.2.0
175
-	 */
176
-	public function get_entity( $uri ) {
177
-		if ( ! isset( $uri ) ) {
178
-			return null;
179
-		}
180
-
181
-		$this->log->trace( "Getting an entity post for URI $uri..." );
182
-
183
-		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
184
-
185
-		// Return null if the content isn't found or isn't a post.
186
-		if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
187
-			return null;
188
-		}
189
-
190
-		return $content->get_bag();
191
-	}
192
-
193
-	/**
194
-	 * Determines whether a given uri is an internal uri or not.
195
-	 *
196
-	 * @param string $uri An uri.
197
-	 *
198
-	 * @return true if the uri internal to the current dataset otherwise false.
199
-	 * @since 3.16.3
200
-	 */
201
-	public function is_internal( $uri ) {
202
-
203
-		return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) );
204
-	}
205
-
206
-	/**
207
-	 * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`.
208
-	 *
209
-	 * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need
210
-	 * to provide full compatibility with WordPress 4.4+.
211
-	 *
212
-	 * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response.
213
-	 *
214
-	 * @return WP_HTTP_Response The result to send to the client.
215
-	 *
216
-	 * @since 3.23.0
217
-	 */
218
-	public function rest_post_dispatch( $result ) {
219
-
220
-		// Get a reference to the actual data.
221
-		$data = &$result->data;
222
-
223
-		// Bail out if we don't have the required parameters, or if the type is not a valid entity.
224
-		if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] )
225
-			 || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) {
226
-			return $result;
227
-		}
228
-
229
-		// Add the `wl:entity_url`.
230
-		$data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] );
231
-
232
-		return $result;
233
-	}
234
-
235
-	/**
236
-	 * Helper function to fetch post_id from a WordPress URL
237
-	 * Primarily used when dataset is not enabled
238
-	 *
239
-	 * @param $url
240
-	 *
241
-	 * @return int Post ID | bool false
242
-	 */
243
-	public function get_post_id_from_url( $url ) {
244
-		global $wp_rewrite;
245
-
246
-		// We need to check that rewrite is available because the `url_to_postid` uses it and can raise an exception
247
-		// otherwise.
248
-		if ( $wp_rewrite === null ) {
249
-			return false;
250
-		}
251
-
252
-		// Try url_to_postid
253
-		$post_id = url_to_postid( htmlspecialchars_decode( $url ) );
254
-		if ( 0 !== $post_id ) {
255
-			return $post_id;
256
-		}
257
-
258
-		$parsed_url = wp_parse_url( $url );
259
-
260
-		if ( ! isset( $parsed_url['query'] ) ) {
261
-			return false;
262
-		}
263
-
264
-		parse_str( $parsed_url['query'], $parsed_query );
265
-
266
-		// Try to parse WooCommerce non-pretty product URL
267
-		if ( $parsed_query['product'] ) {
268
-			$posts = get_posts(
269
-				array(
270
-					'name'      => $parsed_query['product'],
271
-					'post_type' => 'product',
272
-				)
273
-			);
274
-			if ( count( $posts ) > 0 ) {
275
-				return $posts[0]->ID;
276
-			}
277
-		}
278
-
279
-		return false;
280
-	}
126
+        // Get the posts.
127
+        $posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
128
+
129
+        // Populate the array. We reinitialize the array on purpose because
130
+        // we don't want these data to long live.
131
+        $this->uri_to_post = array_reduce(
132
+            $posts,
133
+            function ( $carry, $item ) {
134
+                $uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS );
135
+
136
+                $uri = Wordpress_Content_Service::get_instance()
137
+                                                ->get_entity_id( Wordpress_Content_Id::create_post( $item ) );
138
+
139
+                if ( isset( $uri ) ) {
140
+                    $uris[] = $uri;
141
+                }
142
+
143
+                return $carry
144
+                        // Get the URI related to the post and fill them with the item id.
145
+                       + array_fill_keys( $uris, $item );
146
+            },
147
+            array()
148
+        );
149
+
150
+        // Add the not found URIs.
151
+        $this->uri_to_post += array_fill_keys( $uris, null );
152
+
153
+        $this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' );
154
+
155
+    }
156
+
157
+    /**
158
+     * Reset the URI to post local cache.
159
+     *
160
+     * @since 3.16.3
161
+     */
162
+    public function reset_uris() {
163
+
164
+        $this->uri_to_post = array();
165
+
166
+    }
167
+
168
+    /**
169
+     * Find entity posts by the entity URI. Entity as searched by their entity URI or same as.
170
+     *
171
+     * @param string $uri The entity URI.
172
+     *
173
+     * @return WP_Post|null A WP_Post instance or null if not found.
174
+     * @since 3.2.0
175
+     */
176
+    public function get_entity( $uri ) {
177
+        if ( ! isset( $uri ) ) {
178
+            return null;
179
+        }
180
+
181
+        $this->log->trace( "Getting an entity post for URI $uri..." );
182
+
183
+        $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
184
+
185
+        // Return null if the content isn't found or isn't a post.
186
+        if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
187
+            return null;
188
+        }
189
+
190
+        return $content->get_bag();
191
+    }
192
+
193
+    /**
194
+     * Determines whether a given uri is an internal uri or not.
195
+     *
196
+     * @param string $uri An uri.
197
+     *
198
+     * @return true if the uri internal to the current dataset otherwise false.
199
+     * @since 3.16.3
200
+     */
201
+    public function is_internal( $uri ) {
202
+
203
+        return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) );
204
+    }
205
+
206
+    /**
207
+     * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`.
208
+     *
209
+     * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need
210
+     * to provide full compatibility with WordPress 4.4+.
211
+     *
212
+     * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response.
213
+     *
214
+     * @return WP_HTTP_Response The result to send to the client.
215
+     *
216
+     * @since 3.23.0
217
+     */
218
+    public function rest_post_dispatch( $result ) {
219
+
220
+        // Get a reference to the actual data.
221
+        $data = &$result->data;
222
+
223
+        // Bail out if we don't have the required parameters, or if the type is not a valid entity.
224
+        if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] )
225
+             || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) {
226
+            return $result;
227
+        }
228
+
229
+        // Add the `wl:entity_url`.
230
+        $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] );
231
+
232
+        return $result;
233
+    }
234
+
235
+    /**
236
+     * Helper function to fetch post_id from a WordPress URL
237
+     * Primarily used when dataset is not enabled
238
+     *
239
+     * @param $url
240
+     *
241
+     * @return int Post ID | bool false
242
+     */
243
+    public function get_post_id_from_url( $url ) {
244
+        global $wp_rewrite;
245
+
246
+        // We need to check that rewrite is available because the `url_to_postid` uses it and can raise an exception
247
+        // otherwise.
248
+        if ( $wp_rewrite === null ) {
249
+            return false;
250
+        }
251
+
252
+        // Try url_to_postid
253
+        $post_id = url_to_postid( htmlspecialchars_decode( $url ) );
254
+        if ( 0 !== $post_id ) {
255
+            return $post_id;
256
+        }
257
+
258
+        $parsed_url = wp_parse_url( $url );
259
+
260
+        if ( ! isset( $parsed_url['query'] ) ) {
261
+            return false;
262
+        }
263
+
264
+        parse_str( $parsed_url['query'], $parsed_query );
265
+
266
+        // Try to parse WooCommerce non-pretty product URL
267
+        if ( $parsed_query['product'] ) {
268
+            $posts = get_posts(
269
+                array(
270
+                    'name'      => $parsed_query['product'],
271
+                    'post_type' => 'product',
272
+                )
273
+            );
274
+            if ( count( $posts ) > 0 ) {
275
+                return $posts[0]->ID;
276
+            }
277
+        }
278
+
279
+        return false;
280
+    }
281 281
 
282 282
 }
Please login to merge, or discard this patch.
Spacing   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -45,11 +45,11 @@  discard block
 block discarded – undo
45 45
 	 */
46 46
 	protected function __construct() {
47 47
 
48
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
48
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
49 49
 
50 50
 		// Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`.
51
-		add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) );
52
-		add_filter( 'wl_content_service__post__not_found', array( $this, 'content_service__post__not_found' ), 10, 2 );
51
+		add_filter('rest_post_dispatch', array($this, 'rest_post_dispatch'));
52
+		add_filter('wl_content_service__post__not_found', array($this, 'content_service__post__not_found'), 10, 2);
53 53
 
54 54
 	}
55 55
 
@@ -70,9 +70,9 @@  discard block
 block discarded – undo
70 70
 	 */
71 71
 	public static function get_instance() {
72 72
 
73
-		if ( ! isset( self::$instance ) ) {
74
-			$entity_uri_cache_service = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'entity_uri/' );
75
-			self::$instance           = new Wordlift_Cached_Entity_Uri_Service( $entity_uri_cache_service );
73
+		if ( ! isset(self::$instance)) {
74
+			$entity_uri_cache_service = new Wordlift_File_Cache_Service(WL_TEMP_DIR.'entity_uri/');
75
+			self::$instance           = new Wordlift_Cached_Entity_Uri_Service($entity_uri_cache_service);
76 76
 
77 77
 		}
78 78
 
@@ -87,8 +87,8 @@  discard block
 block discarded – undo
87 87
 	 *
88 88
 	 * @return false|int
89 89
 	 */
90
-	public function content_service__post__not_found( $post, $uri ) {
91
-		return $this->get_post_id_from_url( $uri );
90
+	public function content_service__post__not_found($post, $uri) {
91
+		return $this->get_post_id_from_url($uri);
92 92
 	}
93 93
 
94 94
 	/**
@@ -101,18 +101,18 @@  discard block
 block discarded – undo
101 101
 	 *
102 102
 	 * @since 3.16.3
103 103
 	 */
104
-	public function preload_uris( $uris ) {
104
+	public function preload_uris($uris) {
105 105
 
106 106
 		// Bail out if there are no URIs.
107
-		if ( 0 === count( $uris ) ) {
107
+		if (0 === count($uris)) {
108 108
 			return;
109 109
 		}
110 110
 
111
-		$this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' );
111
+		$this->log->trace('Preloading '.count($uris).' URI(s)...');
112 112
 
113 113
 		global $wpdb;
114
-		$in_post_types  = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) );
115
-		$in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) );
114
+		$in_post_types  = implode("','", array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types()));
115
+		$in_entity_uris = implode("','", array_map('esc_sql', $uris));
116 116
 		$sql            = "
117 117
 			SELECT ID FROM $wpdb->posts p
118 118
 			INNER JOIN $wpdb->postmeta pm
@@ -124,33 +124,33 @@  discard block
 block discarded – undo
124 124
   		";
125 125
 
126 126
 		// Get the posts.
127
-		$posts = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
127
+		$posts = $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
128 128
 
129 129
 		// Populate the array. We reinitialize the array on purpose because
130 130
 		// we don't want these data to long live.
131 131
 		$this->uri_to_post = array_reduce(
132 132
 			$posts,
133
-			function ( $carry, $item ) {
134
-				$uris = get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS );
133
+			function($carry, $item) {
134
+				$uris = get_post_meta($item, Wordlift_Schema_Service::FIELD_SAME_AS);
135 135
 
136 136
 				$uri = Wordpress_Content_Service::get_instance()
137
-												->get_entity_id( Wordpress_Content_Id::create_post( $item ) );
137
+												->get_entity_id(Wordpress_Content_Id::create_post($item));
138 138
 
139
-				if ( isset( $uri ) ) {
139
+				if (isset($uri)) {
140 140
 					$uris[] = $uri;
141 141
 				}
142 142
 
143 143
 				return $carry
144 144
 					   // Get the URI related to the post and fill them with the item id.
145
-					   + array_fill_keys( $uris, $item );
145
+					   + array_fill_keys($uris, $item);
146 146
 			},
147 147
 			array()
148 148
 		);
149 149
 
150 150
 		// Add the not found URIs.
151
-		$this->uri_to_post += array_fill_keys( $uris, null );
151
+		$this->uri_to_post += array_fill_keys($uris, null);
152 152
 
153
-		$this->log->debug( count( $this->uri_to_post ) . ' URI(s) preloaded.' );
153
+		$this->log->debug(count($this->uri_to_post).' URI(s) preloaded.');
154 154
 
155 155
 	}
156 156
 
@@ -173,17 +173,17 @@  discard block
 block discarded – undo
173 173
 	 * @return WP_Post|null A WP_Post instance or null if not found.
174 174
 	 * @since 3.2.0
175 175
 	 */
176
-	public function get_entity( $uri ) {
177
-		if ( ! isset( $uri ) ) {
176
+	public function get_entity($uri) {
177
+		if ( ! isset($uri)) {
178 178
 			return null;
179 179
 		}
180 180
 
181
-		$this->log->trace( "Getting an entity post for URI $uri..." );
181
+		$this->log->trace("Getting an entity post for URI $uri...");
182 182
 
183
-		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri );
183
+		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri);
184 184
 
185 185
 		// Return null if the content isn't found or isn't a post.
186
-		if ( ! isset( $content ) || ! is_a( $content->get_bag(), '\WP_Post' ) ) {
186
+		if ( ! isset($content) || ! is_a($content->get_bag(), '\WP_Post')) {
187 187
 			return null;
188 188
 		}
189 189
 
@@ -198,9 +198,9 @@  discard block
 block discarded – undo
198 198
 	 * @return true if the uri internal to the current dataset otherwise false.
199 199
 	 * @since 3.16.3
200 200
 	 */
201
-	public function is_internal( $uri ) {
201
+	public function is_internal($uri) {
202 202
 
203
-		return ( 0 === strrpos( $uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) );
203
+		return (0 === strrpos($uri, (string) Wordlift_Configuration_Service::get_instance()->get_dataset_uri()));
204 204
 	}
205 205
 
206 206
 	/**
@@ -215,19 +215,19 @@  discard block
 block discarded – undo
215 215
 	 *
216 216
 	 * @since 3.23.0
217 217
 	 */
218
-	public function rest_post_dispatch( $result ) {
218
+	public function rest_post_dispatch($result) {
219 219
 
220 220
 		// Get a reference to the actual data.
221 221
 		$data = &$result->data;
222 222
 
223 223
 		// Bail out if we don't have the required parameters, or if the type is not a valid entity.
224
-		if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] )
225
-			 || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) {
224
+		if ( ! is_array($data) || ! isset($data['id']) || ! isset($data['type'])
225
+			 || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type($data['type'])) {
226 226
 			return $result;
227 227
 		}
228 228
 
229 229
 		// Add the `wl:entity_url`.
230
-		$data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] );
230
+		$data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri($data['id']);
231 231
 
232 232
 		return $result;
233 233
 	}
@@ -240,38 +240,38 @@  discard block
 block discarded – undo
240 240
 	 *
241 241
 	 * @return int Post ID | bool false
242 242
 	 */
243
-	public function get_post_id_from_url( $url ) {
243
+	public function get_post_id_from_url($url) {
244 244
 		global $wp_rewrite;
245 245
 
246 246
 		// We need to check that rewrite is available because the `url_to_postid` uses it and can raise an exception
247 247
 		// otherwise.
248
-		if ( $wp_rewrite === null ) {
248
+		if ($wp_rewrite === null) {
249 249
 			return false;
250 250
 		}
251 251
 
252 252
 		// Try url_to_postid
253
-		$post_id = url_to_postid( htmlspecialchars_decode( $url ) );
254
-		if ( 0 !== $post_id ) {
253
+		$post_id = url_to_postid(htmlspecialchars_decode($url));
254
+		if (0 !== $post_id) {
255 255
 			return $post_id;
256 256
 		}
257 257
 
258
-		$parsed_url = wp_parse_url( $url );
258
+		$parsed_url = wp_parse_url($url);
259 259
 
260
-		if ( ! isset( $parsed_url['query'] ) ) {
260
+		if ( ! isset($parsed_url['query'])) {
261 261
 			return false;
262 262
 		}
263 263
 
264
-		parse_str( $parsed_url['query'], $parsed_query );
264
+		parse_str($parsed_url['query'], $parsed_query);
265 265
 
266 266
 		// Try to parse WooCommerce non-pretty product URL
267
-		if ( $parsed_query['product'] ) {
267
+		if ($parsed_query['product']) {
268 268
 			$posts = get_posts(
269 269
 				array(
270 270
 					'name'      => $parsed_query['product'],
271 271
 					'post_type' => 'product',
272 272
 				)
273 273
 			);
274
-			if ( count( $posts ) > 0 ) {
274
+			if (count($posts) > 0) {
275 275
 				return $posts[0]->ID;
276 276
 			}
277 277
 		}
Please login to merge, or discard this patch.
src/admin/elements/class-wordlift-admin-input-element.php 2 patches
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -18,95 +18,95 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Admin_Input_Element implements Wordlift_Admin_Element {
20 20
 
21
-	/**
22
-	 * Output the HTML for an input box type settings_page
23
-	 *
24
-	 * @param array $args An array with the following keys:
25
-	 *                    Parameters controlling the result.
26
-	 *
27
-	 * @type string name The name attribute of the input element. Mandatory.
28
-	 *
29
-	 * @type string id    The id attribute of the input element. Optional.
30
-	 * @type string id    The id attribute of the input element.
31
-	 *                            Optional, randomly generated one is used if not supplied.
32
-	 * @type string value    The value of the input element.
33
-	 *                            Optional, defaults to empty string.
34
-	 * @type bool readonly    Indicates whether the input is read only.
35
-	 *                            Optional, defaults to read-write
36
-	 * @type string css_class    The class attribute for the input element.
37
-	 *                            If empty string no class attribute will be added.
38
-	 *                            Optional, defaults to empty string.
39
-	 * @type string description    The descriptio text to be displayed below the element.
40
-	 *                            Can include some HTML element.
41
-	 *                            If empty string no description will be displayed.
42
-	 *                            Optional, defaults to empty string.
43
-	 * @return $this|Wordlift_Admin_Element
44
-	 * @since 3.21.0 added the ability to use a $type arg.
45
-	 */
46
-	public function render( $args ) {
21
+    /**
22
+     * Output the HTML for an input box type settings_page
23
+     *
24
+     * @param array $args An array with the following keys:
25
+     *                    Parameters controlling the result.
26
+     *
27
+     * @type string name The name attribute of the input element. Mandatory.
28
+     *
29
+     * @type string id    The id attribute of the input element. Optional.
30
+     * @type string id    The id attribute of the input element.
31
+     *                            Optional, randomly generated one is used if not supplied.
32
+     * @type string value    The value of the input element.
33
+     *                            Optional, defaults to empty string.
34
+     * @type bool readonly    Indicates whether the input is read only.
35
+     *                            Optional, defaults to read-write
36
+     * @type string css_class    The class attribute for the input element.
37
+     *                            If empty string no class attribute will be added.
38
+     *                            Optional, defaults to empty string.
39
+     * @type string description    The descriptio text to be displayed below the element.
40
+     *                            Can include some HTML element.
41
+     *                            If empty string no description will be displayed.
42
+     *                            Optional, defaults to empty string.
43
+     * @return $this|Wordlift_Admin_Element
44
+     * @since 3.21.0 added the ability to use a $type arg.
45
+     */
46
+    public function render( $args ) {
47 47
 
48
-		/*
48
+        /*
49 49
 		 * Parse the arguments and merge with default values.
50 50
 		 * Name intentionally do not have a default as it has to be in SyncEvent
51 51
 		 * with form handling code
52 52
 		 */
53
-		$pre_params = wp_parse_args(
54
-			$args,
55
-			array(
56
-				'id'          => uniqid( 'wl-input-' ),
57
-				'value'       => '',
58
-				'readonly'    => false,
59
-				'css_class'   => '',
60
-				'description' => '',
61
-				'pattern'     => false,
62
-				'placeholder' => false,
63
-			)
64
-		);
65
-		$params     = apply_filters( 'wl_admin_input_element_params', $pre_params );
66
-		// allow different types of input - default to 'text'.
67
-		$input_type = ! empty( $params['type'] ) ? $params['type'] : 'text';
68
-		?>
53
+        $pre_params = wp_parse_args(
54
+            $args,
55
+            array(
56
+                'id'          => uniqid( 'wl-input-' ),
57
+                'value'       => '',
58
+                'readonly'    => false,
59
+                'css_class'   => '',
60
+                'description' => '',
61
+                'pattern'     => false,
62
+                'placeholder' => false,
63
+            )
64
+        );
65
+        $params     = apply_filters( 'wl_admin_input_element_params', $pre_params );
66
+        // allow different types of input - default to 'text'.
67
+        $input_type = ! empty( $params['type'] ) ? $params['type'] : 'text';
68
+        ?>
69 69
 
70 70
 		<input type="<?php echo esc_attr( $input_type ); ?>"
71 71
 			   id="<?php echo esc_attr( $params['id'] ); ?>"
72 72
 			   name="<?php echo esc_attr( $params['name'] ); ?>"
73 73
 			   value="<?php echo esc_attr( $params['value'] ); ?>"
74 74
 			<?php
75
-			if ( $params['pattern'] ) {
76
-				echo ' pattern="';
77
-				echo esc_attr( $params['pattern'] );
78
-				echo '"';
79
-			}
80
-			?>
75
+            if ( $params['pattern'] ) {
76
+                echo ' pattern="';
77
+                echo esc_attr( $params['pattern'] );
78
+                echo '"';
79
+            }
80
+            ?>
81 81
 			<?php
82
-			if ( $params['placeholder'] ) {
83
-				echo ' placeholder="';
84
-				echo esc_attr( $params['placeholder'] );
85
-				echo '"';
86
-			}
87
-			?>
82
+            if ( $params['placeholder'] ) {
83
+                echo ' placeholder="';
84
+                echo esc_attr( $params['placeholder'] );
85
+                echo '"';
86
+            }
87
+            ?>
88 88
 			<?php
89
-			if ( ! empty( $params['data'] ) && is_array( $params['data'] ) ) {
90
-				foreach ( $params['data'] as $key => $value ) {
91
-					echo 'data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
92
-				}
93
-			}
94
-			if ( ! empty( $params['readonly'] ) ) {
95
-				?>
89
+            if ( ! empty( $params['data'] ) && is_array( $params['data'] ) ) {
90
+                foreach ( $params['data'] as $key => $value ) {
91
+                    echo 'data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
92
+                }
93
+            }
94
+            if ( ! empty( $params['readonly'] ) ) {
95
+                ?>
96 96
 				readonly="readonly" <?php } ?>
97 97
 			<?php
98
-			if ( ! empty( $params['css_class'] ) ) {
99
-				?>
98
+            if ( ! empty( $params['css_class'] ) ) {
99
+                ?>
100 100
 				class="<?php echo esc_attr( $params['css_class'] ); ?>" <?php } ?>
101 101
 		/>
102 102
 		<?php
103
-		if ( ! empty( $params['description'] ) ) {
104
-			?>
103
+        if ( ! empty( $params['description'] ) ) {
104
+            ?>
105 105
 			<p><?php echo wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ); ?></p><?php } ?>
106 106
 
107 107
 		<?php
108 108
 
109
-		return $this;
110
-	}
109
+        return $this;
110
+    }
111 111
 
112 112
 }
Please login to merge, or discard this patch.
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
 	 * @return $this|Wordlift_Admin_Element
44 44
 	 * @since 3.21.0 added the ability to use a $type arg.
45 45
 	 */
46
-	public function render( $args ) {
46
+	public function render($args) {
47 47
 
48 48
 		/*
49 49
 		 * Parse the arguments and merge with default values.
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
 		$pre_params = wp_parse_args(
54 54
 			$args,
55 55
 			array(
56
-				'id'          => uniqid( 'wl-input-' ),
56
+				'id'          => uniqid('wl-input-'),
57 57
 				'value'       => '',
58 58
 				'readonly'    => false,
59 59
 				'css_class'   => '',
@@ -62,47 +62,47 @@  discard block
 block discarded – undo
62 62
 				'placeholder' => false,
63 63
 			)
64 64
 		);
65
-		$params     = apply_filters( 'wl_admin_input_element_params', $pre_params );
65
+		$params     = apply_filters('wl_admin_input_element_params', $pre_params);
66 66
 		// allow different types of input - default to 'text'.
67
-		$input_type = ! empty( $params['type'] ) ? $params['type'] : 'text';
67
+		$input_type = ! empty($params['type']) ? $params['type'] : 'text';
68 68
 		?>
69 69
 
70
-		<input type="<?php echo esc_attr( $input_type ); ?>"
71
-			   id="<?php echo esc_attr( $params['id'] ); ?>"
72
-			   name="<?php echo esc_attr( $params['name'] ); ?>"
73
-			   value="<?php echo esc_attr( $params['value'] ); ?>"
70
+		<input type="<?php echo esc_attr($input_type); ?>"
71
+			   id="<?php echo esc_attr($params['id']); ?>"
72
+			   name="<?php echo esc_attr($params['name']); ?>"
73
+			   value="<?php echo esc_attr($params['value']); ?>"
74 74
 			<?php
75
-			if ( $params['pattern'] ) {
75
+			if ($params['pattern']) {
76 76
 				echo ' pattern="';
77
-				echo esc_attr( $params['pattern'] );
77
+				echo esc_attr($params['pattern']);
78 78
 				echo '"';
79 79
 			}
80 80
 			?>
81 81
 			<?php
82
-			if ( $params['placeholder'] ) {
82
+			if ($params['placeholder']) {
83 83
 				echo ' placeholder="';
84
-				echo esc_attr( $params['placeholder'] );
84
+				echo esc_attr($params['placeholder']);
85 85
 				echo '"';
86 86
 			}
87 87
 			?>
88 88
 			<?php
89
-			if ( ! empty( $params['data'] ) && is_array( $params['data'] ) ) {
90
-				foreach ( $params['data'] as $key => $value ) {
91
-					echo 'data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
89
+			if ( ! empty($params['data']) && is_array($params['data'])) {
90
+				foreach ($params['data'] as $key => $value) {
91
+					echo 'data-'.esc_attr($key).'="'.esc_attr($value).'" ';
92 92
 				}
93 93
 			}
94
-			if ( ! empty( $params['readonly'] ) ) {
94
+			if ( ! empty($params['readonly'])) {
95 95
 				?>
96 96
 				readonly="readonly" <?php } ?>
97 97
 			<?php
98
-			if ( ! empty( $params['css_class'] ) ) {
98
+			if ( ! empty($params['css_class'])) {
99 99
 				?>
100
-				class="<?php echo esc_attr( $params['css_class'] ); ?>" <?php } ?>
100
+				class="<?php echo esc_attr($params['css_class']); ?>" <?php } ?>
101 101
 		/>
102 102
 		<?php
103
-		if ( ! empty( $params['description'] ) ) {
103
+		if ( ! empty($params['description'])) {
104 104
 			?>
105
-			<p><?php echo wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ); ?></p><?php } ?>
105
+			<p><?php echo wp_kses($params['description'], array('a' => array('href' => array()))); ?></p><?php } ?>
106 106
 
107 107
 		<?php
108 108
 
Please login to merge, or discard this patch.
src/admin/class-wordlift-admin-settings-page.php 2 patches
Indentation   +403 added lines, -403 removed lines patch added patch discarded remove patch
@@ -18,408 +18,408 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Admin_Settings_Page extends Wordlift_Admin_Page {
20 20
 
21
-	/**
22
-	 * A {@link Wordlift_Entity_Service} instance.
23
-	 *
24
-	 * @since  3.11.0
25
-	 * @access private
26
-	 * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
27
-	 */
28
-	private $entity_service;
29
-
30
-	/**
31
-	 * A {@link Wordlift_Admin_Input_Element} element renderer.
32
-	 *
33
-	 * @since  3.11.0
34
-	 * @access private
35
-	 * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer.
36
-	 */
37
-	private $input_element;
38
-
39
-	/**
40
-	 * A {@link Wordlift_Admin_Radio_Input_Element} element renderer.
41
-	 *
42
-	 * @since  3.13.0
43
-	 * @access protected
44
-	 * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer.
45
-	 */
46
-	private $radio_input_element;
47
-
48
-	/**
49
-	 * A {@link Wordlift_Admin_Language_Select_Element} element renderer.
50
-	 *
51
-	 * @since  3.11.0
52
-	 * @access private
53
-	 * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer.
54
-	 */
55
-	private $language_select_element;
56
-
57
-	/**
58
-	 * A {@link Wordlift_Admin_Country_Select_Element} element renderer.
59
-	 *
60
-	 * @since  3.18.0
61
-	 * @access private
62
-	 * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer.
63
-	 */
64
-	private $country_select_element;
65
-
66
-	/**
67
-	 * A {@link Wordlift_Admin_Publisher_Element} element renderer.
68
-	 *
69
-	 * @since  3.11.0
70
-	 * @access private
71
-	 * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer.
72
-	 */
73
-	private $publisher_element;
74
-
75
-	/**
76
-	 * Create a {@link Wordlift_Admin_Settings_Page} instance.
77
-	 *
78
-	 * @param \Wordlift_Entity_Service                $entity_service A {@link Wordlift_Entity_Service} instance.
79
-	 * @param \Wordlift_Admin_Input_Element           $input_element A {@link Wordlift_Admin_Input_Element} element renderer.
80
-	 * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer.
81
-	 * @param \Wordlift_Admin_Country_Select_Element  $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer.
82
-	 * @param \Wordlift_Admin_Publisher_Element       $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer.
83
-	 * @param \Wordlift_Admin_Radio_Input_Element     $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer.
84
-	 *
85
-	 * @since 3.11.0
86
-	 */
87
-	public function __construct( $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) {
88
-
89
-		$this->entity_service = $entity_service;
90
-
91
-		// Set a reference to the UI elements.
92
-		$this->input_element           = $input_element;
93
-		$this->radio_input_element     = $radio_input_element;
94
-		$this->language_select_element = $language_select_element;
95
-		$this->country_select_element  = $country_select_element;
96
-		$this->publisher_element       = $publisher_element;
97
-
98
-	}
99
-
100
-	private static $instance;
101
-
102
-	/**
103
-	 * Get the singleton instance of the Notice service.
104
-	 *
105
-	 * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service.
106
-	 * @since 3.14.0
107
-	 */
108
-	public static function get_instance() {
109
-
110
-		if ( ! isset( self::$instance ) ) {
111
-			$publisher_element = new Wordlift_Admin_Publisher_Element(
112
-				Wordlift_Publisher_Service::get_instance(),
113
-				new Wordlift_Admin_Tabs_Element(),
114
-				new Wordlift_Admin_Select2_Element()
115
-			);
116
-
117
-			self::$instance = new self(
118
-				Wordlift_Entity_Service::get_instance(),
119
-				new Wordlift_Admin_Input_Element(),
120
-				new Wordlift_Admin_Language_Select_Element(),
121
-				new Wordlift_Admin_Country_Select_Element(),
122
-				$publisher_element,
123
-				new Wordlift_Admin_Radio_Input_Element()
124
-			);
125
-		}
126
-
127
-		return self::$instance;
128
-	}
129
-
130
-	/**
131
-	 * @inheritdoc
132
-	 */
133
-	public function get_parent_slug() {
134
-
135
-		return 'wl_admin_menu';
136
-	}
137
-
138
-	/**
139
-	 * @inheritdoc
140
-	 */
141
-	public function get_capability() {
142
-
143
-		return 'manage_options';
144
-	}
145
-
146
-	/**
147
-	 * @inheritdoc
148
-	 */
149
-	public function get_page_title() {
150
-
151
-		return __( 'WordLift Settings', 'wordlift' );
152
-	}
153
-
154
-	/**
155
-	 * @inheritdoc
156
-	 */
157
-	public function get_menu_title() {
158
-
159
-		return __( 'Settings', 'wordlift' );
160
-	}
161
-
162
-	/**
163
-	 * @inheritdoc
164
-	 */
165
-	public function get_menu_slug() {
166
-
167
-		return 'wl_configuration_admin_menu';
168
-	}
169
-
170
-	/**
171
-	 * @inheritdoc
172
-	 */
173
-	public function get_partial_name() {
174
-
175
-		return 'wordlift-admin-settings-page.php';
176
-	}
177
-
178
-	/**
179
-	 * @inheritdoc
180
-	 */
181
-	public function enqueue_scripts() {
182
-
183
-		// Enqueue the media scripts to be used for the publisher's logo selection.
184
-		wp_enqueue_media();
185
-
186
-		// JavaScript required for the settings page.
187
-		// @todo: try to move to the `wordlift-admin.bundle.js`.
188
-		wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( __DIR__ ) . 'admin/js/1/settings.js', array( 'wp-util' ), WORDLIFT_VERSION, false );
189
-		wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( __DIR__ ) . 'admin/js/1/settings.css', array(), WORDLIFT_VERSION );
190
-
191
-	}
192
-
193
-	/**
194
-	 * Configure all the configuration parameters.
195
-	 *
196
-	 * Called by the *admin_init* hook.
197
-	 *
198
-	 * @since 3.11.0
199
-	 */
200
-	public function admin_init() {
201
-		// Register WordLift's general settings, providing our own sanitize callback
202
-		// which will also check whether the user filled the WL Publisher form.
203
-		register_setting(
204
-			'wl_general_settings',
205
-			'wl_general_settings',
206
-			array( $this, 'sanitize_callback' )
207
-		);
208
-
209
-		// Add the general settings section.
210
-		add_settings_section(
211
-			'wl_general_settings_section', // ID used to identify this section and with which to register options.
212
-			'',                            // Section header.
213
-			'',                            // Callback used to render the description of the section.
214
-			'wl_general_settings'          // Page on which to add this section of options.
215
-		);
216
-
217
-		$key_args = array(
218
-			'id'          => 'wl-key',
219
-			'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']',
220
-			'value'       => Wordlift_Configuration_Service::get_instance()->get_key(),
221
-			'description' => __( 'Insert the <a href="https://wordlift.io">WordLift Key</a> you received via email.', 'wordlift' )
222
-							 . ' [' . apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ) . ']',
223
-		);
224
-
225
-		// Before we were used to validate the key beforehand, but this means
226
-		// an http call whenever a page is opened in the admin area. Therefore
227
-		// we now leave the input `untouched`, leaving to the client to update
228
-		// the `css_class`.
229
-		//
230
-		// See https://github.com/insideout10/wordlift-plugin/issues/669.
231
-		$key_args['css_class'] = 'untouched';
232
-
233
-		// Add the `key` field.
234
-		add_settings_field(
235
-			'wl-key',                                       // Element id used to identify the field throughout the theme.
236
-			__( 'WordLift Key', 'wordlift' ),               // The label to the left of the option interface element.
237
-			// The name of the function responsible for rendering the option interface.
238
-			array( $this->input_element, 'render' ),
239
-			'wl_general_settings',                          // The page on which this option will be displayed.
240
-			'wl_general_settings_section',                  // The name of the section to which this field belongs.
241
-			$key_args                                       // The array of arguments to pass to the callback. In this case, just a description.
242
-		);
243
-
244
-		// Entity Base Path input.
245
-		$entity_base_path_args = array(
246
-			// The array of arguments to pass to the callback. In this case, just a description.
247
-			'id'          => 'wl-entity-base-path',
248
-			'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']',
249
-			'value'       => Wordlift_Configuration_Service::get_instance()->get_entity_base_path(),
250
-			/* translators: Placeholders: %s - a link to FAQ's page. */
251
-			'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ),
252
-		);
253
-
254
-		// The following call is very heavy on large web sites and is always run
255
-		// also when not needed:
256
-		// $entity_base_path_args['readonly'] = 0 < $this->entity_service->count();
257
-		//
258
-		// It is now replaced by a filter to add the `readonly` flag to the
259
-		// input element when this is actually rendered.
260
-		add_filter(
261
-			'wl_admin_input_element_params',
262
-			array(
263
-				$this,
264
-				'entity_path_input_element_params',
265
-			)
266
-		);
267
-
268
-		// Add the `wl_entity_base_path` field.
269
-		add_settings_field(
270
-			'wl-entity-base-path',                                // ID used to identify the field throughout the theme
271
-			__( 'Entity Base Path', 'wordlift' ),                 // The label to the left of the option interface element
272
-			// The name of the function responsible for rendering the option interface
273
-			array( $this->input_element, 'render' ),
274
-			'wl_general_settings',                                // The page on which this option will be displayed
275
-			'wl_general_settings_section',                        // The name of the section to which this field belongs
276
-			$entity_base_path_args
277
-		);
278
-
279
-		// Add the `country_code` field.
280
-		add_settings_field(
281
-			'wl-country-code',
282
-			__( 'Country', 'wordlift' ),
283
-			array( $this->country_select_element, 'render' ),
284
-			'wl_general_settings',
285
-			'wl_general_settings_section',
286
-			array(
287
-				'id'          => 'wl-country-code',
288
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']',
289
-				'value'       => Wordlift_Configuration_Service::get_instance()->get_country_code(),
290
-				'description' => __( 'Please select a country.', 'wordlift' ),
291
-				'notice'      => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ),
292
-			)
293
-		);
294
-
295
-		// Add the `alternateName` field.
296
-		add_settings_field(
297
-			'wl-alternate-name',
298
-			__( 'Website Alternate Name', 'wordlift' ),
299
-			array( $this->input_element, 'render' ),
300
-			'wl_general_settings',
301
-			'wl_general_settings_section',
302
-			array(
303
-				'id'    => 'wl-alternate-name',
304
-				'name'  => 'wl_general_settings[' . Wordlift_Configuration_Service::ALTERNATE_NAME . ']',
305
-				'value' => Wordlift_Configuration_Service::get_instance()->get_alternate_name(),
306
-			)
307
-		);
308
-
309
-		// Add the override URL.
310
-		add_settings_field(
311
-			'wl-override-website-url',
312
-			__( 'Override Website URL', 'wordlift' ),
313
-			array( $this->input_element, 'render' ),
314
-			'wl_general_settings',
315
-			'wl_general_settings_section',
316
-			array(
317
-				'id'          => 'wl-override-website-url',
318
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::OVERRIDE_WEBSITE_URL . ']',
319
-				'value'       => Wordlift_Configuration_Service::get_instance()->get_override_website_url(),
320
-				'pattern'     => '^https?://.+$',
321
-				'placeholder' => __( 'Optionally type a URL like https://...', 'wordlift' ),
322
-			)
323
-		);
324
-
325
-		// Add the `publisher` field.
326
-		add_settings_field(
327
-			'wl-publisher-id',
328
-			__( 'Publisher', 'wordlift' ),
329
-			array( $this->publisher_element, 'render' ),
330
-			'wl_general_settings',
331
-			'wl_general_settings_section',
332
-			array(
333
-				'id'   => 'wl-publisher-id',
334
-				'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']',
335
-			)
336
-		);
337
-
338
-		// Add the `link by default` field.
339
-		add_settings_field(
340
-			'wl-link-by-default',
341
-			__( 'Link by Default', 'wordlift' ),
342
-			array( $this->radio_input_element, 'render' ),
343
-			'wl_general_settings',
344
-			'wl_general_settings_section',
345
-			array(
346
-				'id'          => 'wl-link-by-default',
347
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']',
348
-				'value'       => Wordlift_Configuration_Service::get_instance()->is_link_by_default() ? 'yes' : 'no',
349
-				'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ),
350
-			)
351
-		);
352
-
353
-		// Add the `diagnostic data` field.
354
-		add_settings_field(
355
-			'wl-send-diagnostic',
356
-			__( 'Send Diagnostic Data', 'wordlift' ),
357
-			array( $this->radio_input_element, 'render' ),
358
-			'wl_general_settings',
359
-			'wl_general_settings_section',
360
-			array(
361
-				'id'          => 'wl-send-diagnostic',
362
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']',
363
-				'value'       => 'yes' === Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences() ? 'yes' : 'no',
364
-				'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ),
365
-			)
366
-		);
367
-
368
-	}
369
-
370
-	/**
371
-	 * Filter the {@link Wordlift_Admin_Input_Element} in order to add the
372
-	 * `readonly` flag to the `wl-entity-base-path` input.
373
-	 *
374
-	 * @param array $args An array of {@link Wordlift_Admin_Input_Element} parameters.
375
-	 *
376
-	 * @return array The updated array.
377
-	 * @since 3.17.0
378
-	 */
379
-	public function entity_path_input_element_params( $args ) {
380
-
381
-		// Bail out if it's not the `wl-entity-base-path`).
382
-		if ( 'wl-entity-base-path' !== $args['id'] ) {
383
-			return $args;
384
-		}
385
-
386
-		// Set the readonly flag according to the entities count.
387
-		$args['readonly'] = 0 < $this->entity_service->count();
388
-
389
-		// Return the updated args.
390
-		return $args;
391
-	}
392
-
393
-	/**
394
-	 * Sanitize the configuration settings to be stored.
395
-	 *
396
-	 * If a new entity is being created for the publisher, create it and set The
397
-	 * publisher setting.
398
-	 *
399
-	 * @param array $input The configuration settings array.
400
-	 *
401
-	 * @return array The sanitized input array.
402
-	 * @since 3.11.0
403
-	 */
404
-	public function sanitize_callback( $input ) {
405
-		// No nonce verification since this callback is handled by settings api.
406
-		// Check whether a publisher name has been set.
407
-		// phpcs:ignore Standard.Category.SniffName.ErrorCode
408
-		if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
409
-			$name = isset( $_POST['wl_publisher']['name'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['wl_publisher']['name'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
410
-			$type = isset( $_POST['wl_publisher']['type'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['wl_publisher']['type'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
411
-			// phpcs:ignore Standard.Category.SniffName.ErrorCode
412
-			$thumbnail_id = isset( $_POST['wl_publisher']['thumbnail_id'] ) ? sanitize_text_field( wp_unslash( $_POST['wl_publisher']['thumbnail_id'] ) ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Missing
413
-
414
-			// Set the type URI, either http://schema.org/Person or http://schema.org/Organization.
415
-			$type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' );
416
-
417
-			// Create an entity for the publisher and assign it to the input
418
-			// parameter which WordPress automatically saves into the settings.
419
-			$input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' );
420
-		}
421
-
422
-		return $input;
423
-	}
21
+    /**
22
+     * A {@link Wordlift_Entity_Service} instance.
23
+     *
24
+     * @since  3.11.0
25
+     * @access private
26
+     * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
27
+     */
28
+    private $entity_service;
29
+
30
+    /**
31
+     * A {@link Wordlift_Admin_Input_Element} element renderer.
32
+     *
33
+     * @since  3.11.0
34
+     * @access private
35
+     * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer.
36
+     */
37
+    private $input_element;
38
+
39
+    /**
40
+     * A {@link Wordlift_Admin_Radio_Input_Element} element renderer.
41
+     *
42
+     * @since  3.13.0
43
+     * @access protected
44
+     * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer.
45
+     */
46
+    private $radio_input_element;
47
+
48
+    /**
49
+     * A {@link Wordlift_Admin_Language_Select_Element} element renderer.
50
+     *
51
+     * @since  3.11.0
52
+     * @access private
53
+     * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer.
54
+     */
55
+    private $language_select_element;
56
+
57
+    /**
58
+     * A {@link Wordlift_Admin_Country_Select_Element} element renderer.
59
+     *
60
+     * @since  3.18.0
61
+     * @access private
62
+     * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer.
63
+     */
64
+    private $country_select_element;
65
+
66
+    /**
67
+     * A {@link Wordlift_Admin_Publisher_Element} element renderer.
68
+     *
69
+     * @since  3.11.0
70
+     * @access private
71
+     * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer.
72
+     */
73
+    private $publisher_element;
74
+
75
+    /**
76
+     * Create a {@link Wordlift_Admin_Settings_Page} instance.
77
+     *
78
+     * @param \Wordlift_Entity_Service                $entity_service A {@link Wordlift_Entity_Service} instance.
79
+     * @param \Wordlift_Admin_Input_Element           $input_element A {@link Wordlift_Admin_Input_Element} element renderer.
80
+     * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer.
81
+     * @param \Wordlift_Admin_Country_Select_Element  $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer.
82
+     * @param \Wordlift_Admin_Publisher_Element       $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer.
83
+     * @param \Wordlift_Admin_Radio_Input_Element     $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer.
84
+     *
85
+     * @since 3.11.0
86
+     */
87
+    public function __construct( $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) {
88
+
89
+        $this->entity_service = $entity_service;
90
+
91
+        // Set a reference to the UI elements.
92
+        $this->input_element           = $input_element;
93
+        $this->radio_input_element     = $radio_input_element;
94
+        $this->language_select_element = $language_select_element;
95
+        $this->country_select_element  = $country_select_element;
96
+        $this->publisher_element       = $publisher_element;
97
+
98
+    }
99
+
100
+    private static $instance;
101
+
102
+    /**
103
+     * Get the singleton instance of the Notice service.
104
+     *
105
+     * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service.
106
+     * @since 3.14.0
107
+     */
108
+    public static function get_instance() {
109
+
110
+        if ( ! isset( self::$instance ) ) {
111
+            $publisher_element = new Wordlift_Admin_Publisher_Element(
112
+                Wordlift_Publisher_Service::get_instance(),
113
+                new Wordlift_Admin_Tabs_Element(),
114
+                new Wordlift_Admin_Select2_Element()
115
+            );
116
+
117
+            self::$instance = new self(
118
+                Wordlift_Entity_Service::get_instance(),
119
+                new Wordlift_Admin_Input_Element(),
120
+                new Wordlift_Admin_Language_Select_Element(),
121
+                new Wordlift_Admin_Country_Select_Element(),
122
+                $publisher_element,
123
+                new Wordlift_Admin_Radio_Input_Element()
124
+            );
125
+        }
126
+
127
+        return self::$instance;
128
+    }
129
+
130
+    /**
131
+     * @inheritdoc
132
+     */
133
+    public function get_parent_slug() {
134
+
135
+        return 'wl_admin_menu';
136
+    }
137
+
138
+    /**
139
+     * @inheritdoc
140
+     */
141
+    public function get_capability() {
142
+
143
+        return 'manage_options';
144
+    }
145
+
146
+    /**
147
+     * @inheritdoc
148
+     */
149
+    public function get_page_title() {
150
+
151
+        return __( 'WordLift Settings', 'wordlift' );
152
+    }
153
+
154
+    /**
155
+     * @inheritdoc
156
+     */
157
+    public function get_menu_title() {
158
+
159
+        return __( 'Settings', 'wordlift' );
160
+    }
161
+
162
+    /**
163
+     * @inheritdoc
164
+     */
165
+    public function get_menu_slug() {
166
+
167
+        return 'wl_configuration_admin_menu';
168
+    }
169
+
170
+    /**
171
+     * @inheritdoc
172
+     */
173
+    public function get_partial_name() {
174
+
175
+        return 'wordlift-admin-settings-page.php';
176
+    }
177
+
178
+    /**
179
+     * @inheritdoc
180
+     */
181
+    public function enqueue_scripts() {
182
+
183
+        // Enqueue the media scripts to be used for the publisher's logo selection.
184
+        wp_enqueue_media();
185
+
186
+        // JavaScript required for the settings page.
187
+        // @todo: try to move to the `wordlift-admin.bundle.js`.
188
+        wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( __DIR__ ) . 'admin/js/1/settings.js', array( 'wp-util' ), WORDLIFT_VERSION, false );
189
+        wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( __DIR__ ) . 'admin/js/1/settings.css', array(), WORDLIFT_VERSION );
190
+
191
+    }
192
+
193
+    /**
194
+     * Configure all the configuration parameters.
195
+     *
196
+     * Called by the *admin_init* hook.
197
+     *
198
+     * @since 3.11.0
199
+     */
200
+    public function admin_init() {
201
+        // Register WordLift's general settings, providing our own sanitize callback
202
+        // which will also check whether the user filled the WL Publisher form.
203
+        register_setting(
204
+            'wl_general_settings',
205
+            'wl_general_settings',
206
+            array( $this, 'sanitize_callback' )
207
+        );
208
+
209
+        // Add the general settings section.
210
+        add_settings_section(
211
+            'wl_general_settings_section', // ID used to identify this section and with which to register options.
212
+            '',                            // Section header.
213
+            '',                            // Callback used to render the description of the section.
214
+            'wl_general_settings'          // Page on which to add this section of options.
215
+        );
216
+
217
+        $key_args = array(
218
+            'id'          => 'wl-key',
219
+            'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']',
220
+            'value'       => Wordlift_Configuration_Service::get_instance()->get_key(),
221
+            'description' => __( 'Insert the <a href="https://wordlift.io">WordLift Key</a> you received via email.', 'wordlift' )
222
+                                . ' [' . apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ) . ']',
223
+        );
224
+
225
+        // Before we were used to validate the key beforehand, but this means
226
+        // an http call whenever a page is opened in the admin area. Therefore
227
+        // we now leave the input `untouched`, leaving to the client to update
228
+        // the `css_class`.
229
+        //
230
+        // See https://github.com/insideout10/wordlift-plugin/issues/669.
231
+        $key_args['css_class'] = 'untouched';
232
+
233
+        // Add the `key` field.
234
+        add_settings_field(
235
+            'wl-key',                                       // Element id used to identify the field throughout the theme.
236
+            __( 'WordLift Key', 'wordlift' ),               // The label to the left of the option interface element.
237
+            // The name of the function responsible for rendering the option interface.
238
+            array( $this->input_element, 'render' ),
239
+            'wl_general_settings',                          // The page on which this option will be displayed.
240
+            'wl_general_settings_section',                  // The name of the section to which this field belongs.
241
+            $key_args                                       // The array of arguments to pass to the callback. In this case, just a description.
242
+        );
243
+
244
+        // Entity Base Path input.
245
+        $entity_base_path_args = array(
246
+            // The array of arguments to pass to the callback. In this case, just a description.
247
+            'id'          => 'wl-entity-base-path',
248
+            'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']',
249
+            'value'       => Wordlift_Configuration_Service::get_instance()->get_entity_base_path(),
250
+            /* translators: Placeholders: %s - a link to FAQ's page. */
251
+            'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ),
252
+        );
253
+
254
+        // The following call is very heavy on large web sites and is always run
255
+        // also when not needed:
256
+        // $entity_base_path_args['readonly'] = 0 < $this->entity_service->count();
257
+        //
258
+        // It is now replaced by a filter to add the `readonly` flag to the
259
+        // input element when this is actually rendered.
260
+        add_filter(
261
+            'wl_admin_input_element_params',
262
+            array(
263
+                $this,
264
+                'entity_path_input_element_params',
265
+            )
266
+        );
267
+
268
+        // Add the `wl_entity_base_path` field.
269
+        add_settings_field(
270
+            'wl-entity-base-path',                                // ID used to identify the field throughout the theme
271
+            __( 'Entity Base Path', 'wordlift' ),                 // The label to the left of the option interface element
272
+            // The name of the function responsible for rendering the option interface
273
+            array( $this->input_element, 'render' ),
274
+            'wl_general_settings',                                // The page on which this option will be displayed
275
+            'wl_general_settings_section',                        // The name of the section to which this field belongs
276
+            $entity_base_path_args
277
+        );
278
+
279
+        // Add the `country_code` field.
280
+        add_settings_field(
281
+            'wl-country-code',
282
+            __( 'Country', 'wordlift' ),
283
+            array( $this->country_select_element, 'render' ),
284
+            'wl_general_settings',
285
+            'wl_general_settings_section',
286
+            array(
287
+                'id'          => 'wl-country-code',
288
+                'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']',
289
+                'value'       => Wordlift_Configuration_Service::get_instance()->get_country_code(),
290
+                'description' => __( 'Please select a country.', 'wordlift' ),
291
+                'notice'      => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ),
292
+            )
293
+        );
294
+
295
+        // Add the `alternateName` field.
296
+        add_settings_field(
297
+            'wl-alternate-name',
298
+            __( 'Website Alternate Name', 'wordlift' ),
299
+            array( $this->input_element, 'render' ),
300
+            'wl_general_settings',
301
+            'wl_general_settings_section',
302
+            array(
303
+                'id'    => 'wl-alternate-name',
304
+                'name'  => 'wl_general_settings[' . Wordlift_Configuration_Service::ALTERNATE_NAME . ']',
305
+                'value' => Wordlift_Configuration_Service::get_instance()->get_alternate_name(),
306
+            )
307
+        );
308
+
309
+        // Add the override URL.
310
+        add_settings_field(
311
+            'wl-override-website-url',
312
+            __( 'Override Website URL', 'wordlift' ),
313
+            array( $this->input_element, 'render' ),
314
+            'wl_general_settings',
315
+            'wl_general_settings_section',
316
+            array(
317
+                'id'          => 'wl-override-website-url',
318
+                'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::OVERRIDE_WEBSITE_URL . ']',
319
+                'value'       => Wordlift_Configuration_Service::get_instance()->get_override_website_url(),
320
+                'pattern'     => '^https?://.+$',
321
+                'placeholder' => __( 'Optionally type a URL like https://...', 'wordlift' ),
322
+            )
323
+        );
324
+
325
+        // Add the `publisher` field.
326
+        add_settings_field(
327
+            'wl-publisher-id',
328
+            __( 'Publisher', 'wordlift' ),
329
+            array( $this->publisher_element, 'render' ),
330
+            'wl_general_settings',
331
+            'wl_general_settings_section',
332
+            array(
333
+                'id'   => 'wl-publisher-id',
334
+                'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']',
335
+            )
336
+        );
337
+
338
+        // Add the `link by default` field.
339
+        add_settings_field(
340
+            'wl-link-by-default',
341
+            __( 'Link by Default', 'wordlift' ),
342
+            array( $this->radio_input_element, 'render' ),
343
+            'wl_general_settings',
344
+            'wl_general_settings_section',
345
+            array(
346
+                'id'          => 'wl-link-by-default',
347
+                'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']',
348
+                'value'       => Wordlift_Configuration_Service::get_instance()->is_link_by_default() ? 'yes' : 'no',
349
+                'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ),
350
+            )
351
+        );
352
+
353
+        // Add the `diagnostic data` field.
354
+        add_settings_field(
355
+            'wl-send-diagnostic',
356
+            __( 'Send Diagnostic Data', 'wordlift' ),
357
+            array( $this->radio_input_element, 'render' ),
358
+            'wl_general_settings',
359
+            'wl_general_settings_section',
360
+            array(
361
+                'id'          => 'wl-send-diagnostic',
362
+                'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']',
363
+                'value'       => 'yes' === Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences() ? 'yes' : 'no',
364
+                'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ),
365
+            )
366
+        );
367
+
368
+    }
369
+
370
+    /**
371
+     * Filter the {@link Wordlift_Admin_Input_Element} in order to add the
372
+     * `readonly` flag to the `wl-entity-base-path` input.
373
+     *
374
+     * @param array $args An array of {@link Wordlift_Admin_Input_Element} parameters.
375
+     *
376
+     * @return array The updated array.
377
+     * @since 3.17.0
378
+     */
379
+    public function entity_path_input_element_params( $args ) {
380
+
381
+        // Bail out if it's not the `wl-entity-base-path`).
382
+        if ( 'wl-entity-base-path' !== $args['id'] ) {
383
+            return $args;
384
+        }
385
+
386
+        // Set the readonly flag according to the entities count.
387
+        $args['readonly'] = 0 < $this->entity_service->count();
388
+
389
+        // Return the updated args.
390
+        return $args;
391
+    }
392
+
393
+    /**
394
+     * Sanitize the configuration settings to be stored.
395
+     *
396
+     * If a new entity is being created for the publisher, create it and set The
397
+     * publisher setting.
398
+     *
399
+     * @param array $input The configuration settings array.
400
+     *
401
+     * @return array The sanitized input array.
402
+     * @since 3.11.0
403
+     */
404
+    public function sanitize_callback( $input ) {
405
+        // No nonce verification since this callback is handled by settings api.
406
+        // Check whether a publisher name has been set.
407
+        // phpcs:ignore Standard.Category.SniffName.ErrorCode
408
+        if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
409
+            $name = isset( $_POST['wl_publisher']['name'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['wl_publisher']['name'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
410
+            $type = isset( $_POST['wl_publisher']['type'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['wl_publisher']['type'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
411
+            // phpcs:ignore Standard.Category.SniffName.ErrorCode
412
+            $thumbnail_id = isset( $_POST['wl_publisher']['thumbnail_id'] ) ? sanitize_text_field( wp_unslash( $_POST['wl_publisher']['thumbnail_id'] ) ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Missing
413
+
414
+            // Set the type URI, either http://schema.org/Person or http://schema.org/Organization.
415
+            $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' );
416
+
417
+            // Create an entity for the publisher and assign it to the input
418
+            // parameter which WordPress automatically saves into the settings.
419
+            $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' );
420
+        }
421
+
422
+        return $input;
423
+    }
424 424
 
425 425
 }
Please login to merge, or discard this patch.
Spacing   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 	 *
85 85
 	 * @since 3.11.0
86 86
 	 */
87
-	public function __construct( $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) {
87
+	public function __construct($entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element) {
88 88
 
89 89
 		$this->entity_service = $entity_service;
90 90
 
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
 	 */
108 108
 	public static function get_instance() {
109 109
 
110
-		if ( ! isset( self::$instance ) ) {
110
+		if ( ! isset(self::$instance)) {
111 111
 			$publisher_element = new Wordlift_Admin_Publisher_Element(
112 112
 				Wordlift_Publisher_Service::get_instance(),
113 113
 				new Wordlift_Admin_Tabs_Element(),
@@ -148,7 +148,7 @@  discard block
 block discarded – undo
148 148
 	 */
149 149
 	public function get_page_title() {
150 150
 
151
-		return __( 'WordLift Settings', 'wordlift' );
151
+		return __('WordLift Settings', 'wordlift');
152 152
 	}
153 153
 
154 154
 	/**
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
 	 */
157 157
 	public function get_menu_title() {
158 158
 
159
-		return __( 'Settings', 'wordlift' );
159
+		return __('Settings', 'wordlift');
160 160
 	}
161 161
 
162 162
 	/**
@@ -185,8 +185,8 @@  discard block
 block discarded – undo
185 185
 
186 186
 		// JavaScript required for the settings page.
187 187
 		// @todo: try to move to the `wordlift-admin.bundle.js`.
188
-		wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( __DIR__ ) . 'admin/js/1/settings.js', array( 'wp-util' ), WORDLIFT_VERSION, false );
189
-		wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( __DIR__ ) . 'admin/js/1/settings.css', array(), WORDLIFT_VERSION );
188
+		wp_enqueue_script('wordlift-admin-settings-page', plugin_dir_url(__DIR__).'admin/js/1/settings.js', array('wp-util'), WORDLIFT_VERSION, false);
189
+		wp_enqueue_style('wordlift-admin-settings-page', plugin_dir_url(__DIR__).'admin/js/1/settings.css', array(), WORDLIFT_VERSION);
190 190
 
191 191
 	}
192 192
 
@@ -203,23 +203,23 @@  discard block
 block discarded – undo
203 203
 		register_setting(
204 204
 			'wl_general_settings',
205 205
 			'wl_general_settings',
206
-			array( $this, 'sanitize_callback' )
206
+			array($this, 'sanitize_callback')
207 207
 		);
208 208
 
209 209
 		// Add the general settings section.
210 210
 		add_settings_section(
211 211
 			'wl_general_settings_section', // ID used to identify this section and with which to register options.
212
-			'',                            // Section header.
213
-			'',                            // Callback used to render the description of the section.
212
+			'', // Section header.
213
+			'', // Callback used to render the description of the section.
214 214
 			'wl_general_settings'          // Page on which to add this section of options.
215 215
 		);
216 216
 
217 217
 		$key_args = array(
218 218
 			'id'          => 'wl-key',
219
-			'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']',
219
+			'name'        => 'wl_general_settings['.Wordlift_Configuration_Service::KEY.']',
220 220
 			'value'       => Wordlift_Configuration_Service::get_instance()->get_key(),
221
-			'description' => __( 'Insert the <a href="https://wordlift.io">WordLift Key</a> you received via email.', 'wordlift' )
222
-							 . ' [' . apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ) . ']',
221
+			'description' => __('Insert the <a href="https://wordlift.io">WordLift Key</a> you received via email.', 'wordlift')
222
+							 . ' ['.apply_filters('wl_production_site_url', untrailingslashit(get_option('home'))).']',
223 223
 		);
224 224
 
225 225
 		// Before we were used to validate the key beforehand, but this means
@@ -232,12 +232,12 @@  discard block
 block discarded – undo
232 232
 
233 233
 		// Add the `key` field.
234 234
 		add_settings_field(
235
-			'wl-key',                                       // Element id used to identify the field throughout the theme.
236
-			__( 'WordLift Key', 'wordlift' ),               // The label to the left of the option interface element.
235
+			'wl-key', // Element id used to identify the field throughout the theme.
236
+			__('WordLift Key', 'wordlift'), // The label to the left of the option interface element.
237 237
 			// The name of the function responsible for rendering the option interface.
238
-			array( $this->input_element, 'render' ),
239
-			'wl_general_settings',                          // The page on which this option will be displayed.
240
-			'wl_general_settings_section',                  // The name of the section to which this field belongs.
238
+			array($this->input_element, 'render'),
239
+			'wl_general_settings', // The page on which this option will be displayed.
240
+			'wl_general_settings_section', // The name of the section to which this field belongs.
241 241
 			$key_args                                       // The array of arguments to pass to the callback. In this case, just a description.
242 242
 		);
243 243
 
@@ -245,10 +245,10 @@  discard block
 block discarded – undo
245 245
 		$entity_base_path_args = array(
246 246
 			// The array of arguments to pass to the callback. In this case, just a description.
247 247
 			'id'          => 'wl-entity-base-path',
248
-			'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']',
248
+			'name'        => 'wl_general_settings['.Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY.']',
249 249
 			'value'       => Wordlift_Configuration_Service::get_instance()->get_entity_base_path(),
250 250
 			/* translators: Placeholders: %s - a link to FAQ's page. */
251
-			'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ),
251
+			'description' => sprintf(__('All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift'), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary'),
252 252
 		);
253 253
 
254 254
 		// The following call is very heavy on large web sites and is always run
@@ -267,41 +267,41 @@  discard block
 block discarded – undo
267 267
 
268 268
 		// Add the `wl_entity_base_path` field.
269 269
 		add_settings_field(
270
-			'wl-entity-base-path',                                // ID used to identify the field throughout the theme
271
-			__( 'Entity Base Path', 'wordlift' ),                 // The label to the left of the option interface element
270
+			'wl-entity-base-path', // ID used to identify the field throughout the theme
271
+			__('Entity Base Path', 'wordlift'), // The label to the left of the option interface element
272 272
 			// The name of the function responsible for rendering the option interface
273
-			array( $this->input_element, 'render' ),
274
-			'wl_general_settings',                                // The page on which this option will be displayed
275
-			'wl_general_settings_section',                        // The name of the section to which this field belongs
273
+			array($this->input_element, 'render'),
274
+			'wl_general_settings', // The page on which this option will be displayed
275
+			'wl_general_settings_section', // The name of the section to which this field belongs
276 276
 			$entity_base_path_args
277 277
 		);
278 278
 
279 279
 		// Add the `country_code` field.
280 280
 		add_settings_field(
281 281
 			'wl-country-code',
282
-			__( 'Country', 'wordlift' ),
283
-			array( $this->country_select_element, 'render' ),
282
+			__('Country', 'wordlift'),
283
+			array($this->country_select_element, 'render'),
284 284
 			'wl_general_settings',
285 285
 			'wl_general_settings_section',
286 286
 			array(
287 287
 				'id'          => 'wl-country-code',
288
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']',
288
+				'name'        => 'wl_general_settings['.Wordlift_Configuration_Service::COUNTRY_CODE.']',
289 289
 				'value'       => Wordlift_Configuration_Service::get_instance()->get_country_code(),
290
-				'description' => __( 'Please select a country.', 'wordlift' ),
291
-				'notice'      => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ),
290
+				'description' => __('Please select a country.', 'wordlift'),
291
+				'notice'      => __('The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift'),
292 292
 			)
293 293
 		);
294 294
 
295 295
 		// Add the `alternateName` field.
296 296
 		add_settings_field(
297 297
 			'wl-alternate-name',
298
-			__( 'Website Alternate Name', 'wordlift' ),
299
-			array( $this->input_element, 'render' ),
298
+			__('Website Alternate Name', 'wordlift'),
299
+			array($this->input_element, 'render'),
300 300
 			'wl_general_settings',
301 301
 			'wl_general_settings_section',
302 302
 			array(
303 303
 				'id'    => 'wl-alternate-name',
304
-				'name'  => 'wl_general_settings[' . Wordlift_Configuration_Service::ALTERNATE_NAME . ']',
304
+				'name'  => 'wl_general_settings['.Wordlift_Configuration_Service::ALTERNATE_NAME.']',
305 305
 				'value' => Wordlift_Configuration_Service::get_instance()->get_alternate_name(),
306 306
 			)
307 307
 		);
@@ -309,59 +309,59 @@  discard block
 block discarded – undo
309 309
 		// Add the override URL.
310 310
 		add_settings_field(
311 311
 			'wl-override-website-url',
312
-			__( 'Override Website URL', 'wordlift' ),
313
-			array( $this->input_element, 'render' ),
312
+			__('Override Website URL', 'wordlift'),
313
+			array($this->input_element, 'render'),
314 314
 			'wl_general_settings',
315 315
 			'wl_general_settings_section',
316 316
 			array(
317 317
 				'id'          => 'wl-override-website-url',
318
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::OVERRIDE_WEBSITE_URL . ']',
318
+				'name'        => 'wl_general_settings['.Wordlift_Configuration_Service::OVERRIDE_WEBSITE_URL.']',
319 319
 				'value'       => Wordlift_Configuration_Service::get_instance()->get_override_website_url(),
320 320
 				'pattern'     => '^https?://.+$',
321
-				'placeholder' => __( 'Optionally type a URL like https://...', 'wordlift' ),
321
+				'placeholder' => __('Optionally type a URL like https://...', 'wordlift'),
322 322
 			)
323 323
 		);
324 324
 
325 325
 		// Add the `publisher` field.
326 326
 		add_settings_field(
327 327
 			'wl-publisher-id',
328
-			__( 'Publisher', 'wordlift' ),
329
-			array( $this->publisher_element, 'render' ),
328
+			__('Publisher', 'wordlift'),
329
+			array($this->publisher_element, 'render'),
330 330
 			'wl_general_settings',
331 331
 			'wl_general_settings_section',
332 332
 			array(
333 333
 				'id'   => 'wl-publisher-id',
334
-				'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']',
334
+				'name' => 'wl_general_settings['.Wordlift_Configuration_Service::PUBLISHER_ID.']',
335 335
 			)
336 336
 		);
337 337
 
338 338
 		// Add the `link by default` field.
339 339
 		add_settings_field(
340 340
 			'wl-link-by-default',
341
-			__( 'Link by Default', 'wordlift' ),
342
-			array( $this->radio_input_element, 'render' ),
341
+			__('Link by Default', 'wordlift'),
342
+			array($this->radio_input_element, 'render'),
343 343
 			'wl_general_settings',
344 344
 			'wl_general_settings_section',
345 345
 			array(
346 346
 				'id'          => 'wl-link-by-default',
347
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']',
347
+				'name'        => 'wl_general_settings['.Wordlift_Configuration_Service::LINK_BY_DEFAULT.']',
348 348
 				'value'       => Wordlift_Configuration_Service::get_instance()->is_link_by_default() ? 'yes' : 'no',
349
-				'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ),
349
+				'description' => __('Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift'),
350 350
 			)
351 351
 		);
352 352
 
353 353
 		// Add the `diagnostic data` field.
354 354
 		add_settings_field(
355 355
 			'wl-send-diagnostic',
356
-			__( 'Send Diagnostic Data', 'wordlift' ),
357
-			array( $this->radio_input_element, 'render' ),
356
+			__('Send Diagnostic Data', 'wordlift'),
357
+			array($this->radio_input_element, 'render'),
358 358
 			'wl_general_settings',
359 359
 			'wl_general_settings_section',
360 360
 			array(
361 361
 				'id'          => 'wl-send-diagnostic',
362
-				'name'        => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']',
362
+				'name'        => 'wl_general_settings['.Wordlift_Configuration_Service::SEND_DIAGNOSTIC.']',
363 363
 				'value'       => 'yes' === Wordlift_Configuration_Service::get_instance()->get_diagnostic_preferences() ? 'yes' : 'no',
364
-				'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ),
364
+				'description' => __('Whether to send diagnostic data or not.', 'wordlift'),
365 365
 			)
366 366
 		);
367 367
 
@@ -376,10 +376,10 @@  discard block
 block discarded – undo
376 376
 	 * @return array The updated array.
377 377
 	 * @since 3.17.0
378 378
 	 */
379
-	public function entity_path_input_element_params( $args ) {
379
+	public function entity_path_input_element_params($args) {
380 380
 
381 381
 		// Bail out if it's not the `wl-entity-base-path`).
382
-		if ( 'wl-entity-base-path' !== $args['id'] ) {
382
+		if ('wl-entity-base-path' !== $args['id']) {
383 383
 			return $args;
384 384
 		}
385 385
 
@@ -401,22 +401,22 @@  discard block
 block discarded – undo
401 401
 	 * @return array The sanitized input array.
402 402
 	 * @since 3.11.0
403 403
 	 */
404
-	public function sanitize_callback( $input ) {
404
+	public function sanitize_callback($input) {
405 405
 		// No nonce verification since this callback is handled by settings api.
406 406
 		// Check whether a publisher name has been set.
407 407
 		// phpcs:ignore Standard.Category.SniffName.ErrorCode
408
-		if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
409
-			$name = isset( $_POST['wl_publisher']['name'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['wl_publisher']['name'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
410
-			$type = isset( $_POST['wl_publisher']['type'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['wl_publisher']['type'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
408
+		if (isset($_POST['wl_publisher']) && ! empty($_POST['wl_publisher']['name'])) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
409
+			$name = isset($_POST['wl_publisher']['name']) ? sanitize_text_field(wp_unslash((string) $_POST['wl_publisher']['name'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
410
+			$type = isset($_POST['wl_publisher']['type']) ? sanitize_text_field(wp_unslash((string) $_POST['wl_publisher']['type'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Missing
411 411
 			// phpcs:ignore Standard.Category.SniffName.ErrorCode
412
-			$thumbnail_id = isset( $_POST['wl_publisher']['thumbnail_id'] ) ? sanitize_text_field( wp_unslash( $_POST['wl_publisher']['thumbnail_id'] ) ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Missing
412
+			$thumbnail_id = isset($_POST['wl_publisher']['thumbnail_id']) ? sanitize_text_field(wp_unslash($_POST['wl_publisher']['thumbnail_id'])) : null; //phpcs:ignore WordPress.Security.NonceVerification.Missing
413 413
 
414 414
 			// Set the type URI, either http://schema.org/Person or http://schema.org/Organization.
415
-			$type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' );
415
+			$type_uri = sprintf('http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person');
416 416
 
417 417
 			// Create an entity for the publisher and assign it to the input
418 418
 			// parameter which WordPress automatically saves into the settings.
419
-			$input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' );
419
+			$input['publisher_id'] = $this->entity_service->create($name, $type_uri, $thumbnail_id, 'publish');
420 420
 		}
421 421
 
422 422
 		return $input;
Please login to merge, or discard this patch.
src/modules/override-url/load.php 2 patches
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -1,21 +1,21 @@
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 function _wl_override_url__production_site_url( $value ) {
4
-	$override_website_url = Wordlift_Configuration_Service::get_instance()->get_override_website_url();
4
+    $override_website_url = Wordlift_Configuration_Service::get_instance()->get_override_website_url();
5 5
 
6
-	return ! empty( $override_website_url ) ? $override_website_url : $value;
6
+    return ! empty( $override_website_url ) ? $override_website_url : $value;
7 7
 }
8 8
 
9 9
 function _wl_override_url__wl_production_permalink( $value ) {
10
-	$override_website_url = Wordlift_Configuration_Service::get_instance()->get_override_website_url();
10
+    $override_website_url = Wordlift_Configuration_Service::get_instance()->get_override_website_url();
11 11
 
12
-	if ( empty( $override_website_url ) ) {
13
-		return $value;
14
-	}
12
+    if ( empty( $override_website_url ) ) {
13
+        return $value;
14
+    }
15 15
 
16
-	$home_url = untrailingslashit( get_option( 'home' ) );
16
+    $home_url = untrailingslashit( get_option( 'home' ) );
17 17
 
18
-	return $override_website_url . substr( $value, strlen( $home_url ) );
18
+    return $override_website_url . substr( $value, strlen( $home_url ) );
19 19
 }
20 20
 
21 21
 add_filter( 'wl_production_site_url', '_wl_override_url__production_site_url' );
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -1,23 +1,23 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-function _wl_override_url__production_site_url( $value ) {
3
+function _wl_override_url__production_site_url($value) {
4 4
 	$override_website_url = Wordlift_Configuration_Service::get_instance()->get_override_website_url();
5 5
 
6
-	return ! empty( $override_website_url ) ? $override_website_url : $value;
6
+	return ! empty($override_website_url) ? $override_website_url : $value;
7 7
 }
8 8
 
9
-function _wl_override_url__wl_production_permalink( $value ) {
9
+function _wl_override_url__wl_production_permalink($value) {
10 10
 	$override_website_url = Wordlift_Configuration_Service::get_instance()->get_override_website_url();
11 11
 
12
-	if ( empty( $override_website_url ) ) {
12
+	if (empty($override_website_url)) {
13 13
 		return $value;
14 14
 	}
15 15
 
16
-	$home_url = untrailingslashit( get_option( 'home' ) );
16
+	$home_url = untrailingslashit(get_option('home'));
17 17
 
18
-	return $override_website_url . substr( $value, strlen( $home_url ) );
18
+	return $override_website_url.substr($value, strlen($home_url));
19 19
 }
20 20
 
21
-add_filter( 'wl_production_site_url', '_wl_override_url__production_site_url' );
21
+add_filter('wl_production_site_url', '_wl_override_url__production_site_url');
22 22
 
23
-add_filter( 'wl_production_permalink', '_wl_override_url__wl_production_permalink' );
23
+add_filter('wl_production_permalink', '_wl_override_url__wl_production_permalink');
Please login to merge, or discard this patch.
src/wordlift.php 2 patches
Indentation   +172 added lines, -172 removed lines patch added patch discarded remove patch
@@ -47,7 +47,7 @@  discard block
 block discarded – undo
47 47
  * @since 3.33.6
48 48
  */
49 49
 if ( ! apply_filters( 'wl_is_enabled', true ) ) {
50
-	return;
50
+    return;
51 51
 }
52 52
 
53 53
 require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
@@ -76,33 +76,33 @@  discard block
 block discarded – undo
76 76
  */
77 77
 function activate_wordlift() {
78 78
 
79
-	$log = Wordlift_Log_Service::get_logger( 'activate_wordlift' );
79
+    $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' );
80 80
 
81
-	$log->info( 'Activating WordLift...' );
81
+    $log->info( 'Activating WordLift...' );
82 82
 
83
-	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php';
84
-	Wordlift_Activator::activate();
83
+    require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php';
84
+    Wordlift_Activator::activate();
85 85
 
86
-	/**
87
-	 * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks.
88
-	 *
89
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue.
90
-	 * @since 3.19.2
91
-	 */
92
-	Wordlift_Http_Api::activate();
86
+    /**
87
+     * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks.
88
+     *
89
+     * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue.
90
+     * @since 3.19.2
91
+     */
92
+    Wordlift_Http_Api::activate();
93 93
 
94
-	// Ensure the post type is registered before flushing the rewrite rules.
95
-	Wordlift_Entity_Post_Type_Service::get_instance()->register();
96
-	flush_rewrite_rules();
97
-	/**
98
-	 * @since 3.27.7
99
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1214
100
-	 */
101
-	Top_Entities::activate();
94
+    // Ensure the post type is registered before flushing the rewrite rules.
95
+    Wordlift_Entity_Post_Type_Service::get_instance()->register();
96
+    flush_rewrite_rules();
97
+    /**
98
+     * @since 3.27.7
99
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1214
100
+     */
101
+    Top_Entities::activate();
102 102
 
103
-	if ( ! wp_next_scheduled( 'wl_daily_cron' ) ) {
104
-		wp_schedule_event( time(), 'daily', 'wl_daily_cron' );
105
-	}
103
+    if ( ! wp_next_scheduled( 'wl_daily_cron' ) ) {
104
+        wp_schedule_event( time(), 'daily', 'wl_daily_cron' );
105
+    }
106 106
 
107 107
 }
108 108
 
@@ -112,23 +112,23 @@  discard block
 block discarded – undo
112 112
  */
113 113
 function deactivate_wordlift() {
114 114
 
115
-	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php';
116
-	Wordlift_Deactivator::deactivate();
117
-	Wordlift_Http_Api::deactivate();
118
-	Ttl_Cache_Cleaner::deactivate();
119
-	/**
120
-	 * @since 3.27.7
121
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1214
122
-	 */
123
-	Top_Entities::deactivate();
124
-	/**
125
-	 * @since 3.27.8
126
-	 * Remove notification flag on deactivation.
127
-	 */
128
-	Key_Validation_Notice::remove_notification_flag();
129
-	flush_rewrite_rules();
130
-
131
-	wp_clear_scheduled_hook( 'wl_daily_cron' );
115
+    require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php';
116
+    Wordlift_Deactivator::deactivate();
117
+    Wordlift_Http_Api::deactivate();
118
+    Ttl_Cache_Cleaner::deactivate();
119
+    /**
120
+     * @since 3.27.7
121
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1214
122
+     */
123
+    Top_Entities::deactivate();
124
+    /**
125
+     * @since 3.27.8
126
+     * Remove notification flag on deactivation.
127
+     */
128
+    Key_Validation_Notice::remove_notification_flag();
129
+    flush_rewrite_rules();
130
+
131
+    wp_clear_scheduled_hook( 'wl_daily_cron' );
132 132
 
133 133
 }
134 134
 
@@ -151,84 +151,84 @@  discard block
 block discarded – undo
151 151
  * @since    1.0.0
152 152
  */
153 153
 function run_wordlift() {
154
-	/**
155
-	 * Filter: wl_feature__enable__widgets.
156
-	 *
157
-	 * @param bool whether the widgets needed to be registered, defaults to true.
158
-	 *
159
-	 * @return bool
160
-	 * @since 3.27.6
161
-	 */
162
-	if ( apply_filters( 'wl_feature__enable__widgets', true ) ) {
163
-		add_action( 'widgets_init', 'wl_register_chord_widget' );
164
-		add_action( 'widgets_init', 'wl_register_geo_widget' );
165
-		add_action( 'widgets_init', 'wl_register_timeline_widget' );
166
-	}
167
-	add_filter( 'widget_text', 'do_shortcode' );
168
-
169
-	/**
170
-	 * Filter: wl_feature__enable__analysis
171
-	 *
172
-	 * @param bool Whether to send api request to analysis or not
173
-	 *
174
-	 * @return bool
175
-	 * @since 3.27.6
176
-	 */
177
-	if ( apply_filters( 'wl_feature__enable__analysis', true ) ) {
178
-		add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' );
179
-	} else {
180
-		add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' );
181
-	}
182
-
183
-	$plugin = new Wordlift();
184
-	$plugin->run();
185
-
186
-	// Initialize the TTL Cache Cleaner.
187
-	new Ttl_Cache_Cleaner();
188
-
189
-	// Load the new Post Adapter.
190
-	new Post_Adapter();
191
-
192
-	// Load the API Data Hooks.
193
-	new Api_Data_Hooks();
194
-
195
-	add_action(
196
-		'plugins_loaded',
197
-		function () {
198
-			// All features from registry should be initialized here.
199
-			$features_registry = Features_Registry::get_instance();
200
-			$features_registry->initialize_all_features();
201
-		},
202
-		5
203
-	);
204
-
205
-	add_action(
206
-		'plugins_loaded',
207
-		// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
208
-		function () use ( $plugin ) {
209
-
210
-			new Wordlift_Products_Navigator_Shortcode_REST();
211
-
212
-			// Register the Dataset module, requires `$api_service`.
213
-			require_once plugin_dir_path( __FILE__ ) . 'classes/dataset/index.php';
214
-			require_once plugin_dir_path( __FILE__ ) . 'classes/shipping-data/index.php';
215
-
216
-			/*
154
+    /**
155
+     * Filter: wl_feature__enable__widgets.
156
+     *
157
+     * @param bool whether the widgets needed to be registered, defaults to true.
158
+     *
159
+     * @return bool
160
+     * @since 3.27.6
161
+     */
162
+    if ( apply_filters( 'wl_feature__enable__widgets', true ) ) {
163
+        add_action( 'widgets_init', 'wl_register_chord_widget' );
164
+        add_action( 'widgets_init', 'wl_register_geo_widget' );
165
+        add_action( 'widgets_init', 'wl_register_timeline_widget' );
166
+    }
167
+    add_filter( 'widget_text', 'do_shortcode' );
168
+
169
+    /**
170
+     * Filter: wl_feature__enable__analysis
171
+     *
172
+     * @param bool Whether to send api request to analysis or not
173
+     *
174
+     * @return bool
175
+     * @since 3.27.6
176
+     */
177
+    if ( apply_filters( 'wl_feature__enable__analysis', true ) ) {
178
+        add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' );
179
+    } else {
180
+        add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' );
181
+    }
182
+
183
+    $plugin = new Wordlift();
184
+    $plugin->run();
185
+
186
+    // Initialize the TTL Cache Cleaner.
187
+    new Ttl_Cache_Cleaner();
188
+
189
+    // Load the new Post Adapter.
190
+    new Post_Adapter();
191
+
192
+    // Load the API Data Hooks.
193
+    new Api_Data_Hooks();
194
+
195
+    add_action(
196
+        'plugins_loaded',
197
+        function () {
198
+            // All features from registry should be initialized here.
199
+            $features_registry = Features_Registry::get_instance();
200
+            $features_registry->initialize_all_features();
201
+        },
202
+        5
203
+    );
204
+
205
+    add_action(
206
+        'plugins_loaded',
207
+        // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
208
+        function () use ( $plugin ) {
209
+
210
+            new Wordlift_Products_Navigator_Shortcode_REST();
211
+
212
+            // Register the Dataset module, requires `$api_service`.
213
+            require_once plugin_dir_path( __FILE__ ) . 'classes/dataset/index.php';
214
+            require_once plugin_dir_path( __FILE__ ) . 'classes/shipping-data/index.php';
215
+
216
+            /*
217 217
 			* Require the Entity annotation cleanup module.
218 218
 			*
219 219
 			* @since 3.34.6
220 220
 			*/
221
-			require_once plugin_dir_path( __FILE__ ) . 'classes/cleanup/index.php';
221
+            require_once plugin_dir_path( __FILE__ ) . 'classes/cleanup/index.php';
222 222
 
223
-			/*
223
+            /*
224 224
 			* Import LOD entities.
225 225
 			*
226 226
 			* @since 3.35.0
227 227
 			*/
228
-			require_once plugin_dir_path( __FILE__ ) . 'classes/lod-import/index.php';
228
+            require_once plugin_dir_path( __FILE__ ) . 'classes/lod-import/index.php';
229 229
 
230
-		}
231
-	);
230
+        }
231
+    );
232 232
 
233 233
 }
234 234
 
@@ -242,50 +242,50 @@  discard block
 block discarded – undo
242 242
  */
243 243
 function wordlift_plugin_autoload_register() {
244 244
 
245
-	spl_autoload_register(
246
-		function ( $class_name ) {
245
+    spl_autoload_register(
246
+        function ( $class_name ) {
247 247
 
248
-			// Bail out if these are not our classes.
249
-			if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) {
250
-				return false;
251
-			}
248
+            // Bail out if these are not our classes.
249
+            if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) {
250
+                return false;
251
+            }
252 252
 
253
-			$class_name_lc = strtolower( str_replace( '_', '-', $class_name ) );
253
+            $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) );
254 254
 
255
-			preg_match( '|^wordlift\\\\(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches );
255
+            preg_match( '|^wordlift\\\\(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches );
256 256
 
257
-			$path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] );
258
-			$file = 'class-' . $matches[2] . '.php';
257
+            $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] );
258
+            $file = 'class-' . $matches[2] . '.php';
259 259
 
260
-			$full_path = plugin_dir_path( __FILE__ ) . 'classes' . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file;
260
+            $full_path = plugin_dir_path( __FILE__ ) . 'classes' . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file;
261 261
 
262
-			if ( ! file_exists( $full_path ) ) {
263
-				return false;
264
-			}
262
+            if ( ! file_exists( $full_path ) ) {
263
+                return false;
264
+            }
265 265
 
266
-			try {
267
-				require_once $full_path;
268
-			} catch ( Exception $e ) {
269
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
270
-				error_log( "[WordLift] $full_path not found, cannot include." );
271
-			}
266
+            try {
267
+                require_once $full_path;
268
+            } catch ( Exception $e ) {
269
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
270
+                error_log( "[WordLift] $full_path not found, cannot include." );
271
+            }
272 272
 
273
-			return true;
274
-		}
275
-	);
273
+            return true;
274
+        }
275
+    );
276 276
 
277 277
 }
278 278
 
279 279
 function wl_block_categories( $categories ) {
280
-	return array_merge(
281
-		$categories,
282
-		array(
283
-			array(
284
-				'slug'  => 'wordlift',
285
-				'title' => __( 'WordLift', 'wordlift' ),
286
-			),
287
-		)
288
-	);
280
+    return array_merge(
281
+        $categories,
282
+        array(
283
+            array(
284
+                'slug'  => 'wordlift',
285
+                'title' => __( 'WordLift', 'wordlift' ),
286
+            ),
287
+        )
288
+    );
289 289
 }
290 290
 
291 291
 /**
@@ -293,19 +293,19 @@  discard block
 block discarded – undo
293 293
  * this has to be removed when removing the legacy fields from the ui.
294 294
  */
295 295
 function wl_enqueue_leaflet( $in_footer = false ) {
296
-	// Leaflet.
297
-	wp_enqueue_style( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.css', array(), '1.6.0' );
298
-	wp_enqueue_script( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer );
296
+    // Leaflet.
297
+    wp_enqueue_style( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.css', array(), '1.6.0' );
298
+    wp_enqueue_script( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer );
299 299
 }
300 300
 
301 301
 add_filter( 'block_categories', 'wl_block_categories', 10 );
302 302
 
303 303
 // Temporary fix for a typo in WooCommerce Extension.
304 304
 add_filter(
305
-	'wl_feature__enable__dataset',
306
-	function ( $value ) {
307
-		return apply_filters( 'wl_features__enable__dataset', $value );
308
-	}
305
+    'wl_feature__enable__dataset',
306
+    function ( $value ) {
307
+        return apply_filters( 'wl_features__enable__dataset', $value );
308
+    }
309 309
 );
310 310
 
311 311
 require_once __DIR__ . '/modules/food-kg/load.php';
@@ -322,35 +322,35 @@  discard block
 block discarded – undo
322 322
 require_once __DIR__ . '/modules/override-url/load.php';
323 323
 
324 324
 function _wl_update_plugins_raptive_domain( $update, $plugin_data, $plugin_file ) {
325
-	// Bail out if it's not our plugin.
326
-	$update_uri = $plugin_data['UpdateURI'];
327
-	if ( 'wordlift/wordlift.php' !== $plugin_file || ! isset( $update_uri ) ) {
328
-		return $update;
329
-	}
330
-
331
-	$response = wp_remote_get( "$update_uri?nocache=" . time() );
332
-
333
-	if ( is_wp_error( $response ) ) {
334
-		return $update;
335
-	}
336
-
337
-	try {
338
-		return json_decode( wp_remote_retrieve_body( $response ) );
339
-	} catch ( Exception $e ) {
340
-		return $update;
341
-	}
325
+    // Bail out if it's not our plugin.
326
+    $update_uri = $plugin_data['UpdateURI'];
327
+    if ( 'wordlift/wordlift.php' !== $plugin_file || ! isset( $update_uri ) ) {
328
+        return $update;
329
+    }
330
+
331
+    $response = wp_remote_get( "$update_uri?nocache=" . time() );
332
+
333
+    if ( is_wp_error( $response ) ) {
334
+        return $update;
335
+    }
336
+
337
+    try {
338
+        return json_decode( wp_remote_retrieve_body( $response ) );
339
+    } catch ( Exception $e ) {
340
+        return $update;
341
+    }
342 342
 }
343 343
 
344 344
 add_action(
345
-	'update_plugins_adthrive.wordlift.io',
346
-	'_wl_update_plugins_raptive_domain',
347
-	10,
348
-	3
345
+    'update_plugins_adthrive.wordlift.io',
346
+    '_wl_update_plugins_raptive_domain',
347
+    10,
348
+    3
349 349
 );
350 350
 
351 351
 add_action(
352
-	'update_plugins_raptive.wordlift.io',
353
-	'_wl_update_plugins_raptive_domain',
354
-	10,
355
-	3
352
+    'update_plugins_raptive.wordlift.io',
353
+    '_wl_update_plugins_raptive_domain',
354
+    10,
355
+    3
356 356
 );
Please login to merge, or discard this patch.
Spacing   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -31,26 +31,26 @@  discard block
 block discarded – undo
31 31
 use Wordlift\Features\Features_Registry;
32 32
 use Wordlift\Post\Post_Adapter;
33 33
 
34
-define( 'WORDLIFT_PLUGIN_FILE', __FILE__ );
35
-define( 'WORDLIFT_VERSION', '3.52.0-0' );
34
+define('WORDLIFT_PLUGIN_FILE', __FILE__);
35
+define('WORDLIFT_VERSION', '3.52.0-0');
36 36
 
37 37
 // ## DO NOT REMOVE THIS LINE: WHITELABEL PLACEHOLDER ##
38 38
 
39
-require_once plugin_dir_path( __FILE__ ) . '/libraries/action-scheduler/action-scheduler.php';
40
-require_once __DIR__ . '/modules/common/load.php';
41
-require_once __DIR__ . '/modules/app/load.php';
42
-require_once __DIR__ . '/modules/include-exclude/load.php';
39
+require_once plugin_dir_path(__FILE__).'/libraries/action-scheduler/action-scheduler.php';
40
+require_once __DIR__.'/modules/common/load.php';
41
+require_once __DIR__.'/modules/app/load.php';
42
+require_once __DIR__.'/modules/include-exclude/load.php';
43 43
 
44 44
 /**
45 45
  * Filter to disable WLP on any request, defaults to true.
46 46
  *
47 47
  * @since 3.33.6
48 48
  */
49
-if ( ! apply_filters( 'wl_is_enabled', true ) ) {
49
+if ( ! apply_filters('wl_is_enabled', true)) {
50 50
 	return;
51 51
 }
52 52
 
53
-require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
53
+require_once plugin_dir_path(__FILE__).'vendor/autoload.php';
54 54
 
55 55
 /*
56 56
  * We introduce the WordLift autoloader, since we start using classes in namespaces, i.e. Wordlift\Http.
@@ -60,15 +60,15 @@  discard block
 block discarded – undo
60 60
 wordlift_plugin_autoload_register();
61 61
 
62 62
 // Include WordLift constants.
63
-require_once plugin_dir_path( __FILE__ ) . 'wordlift-constants.php';
63
+require_once plugin_dir_path(__FILE__).'wordlift-constants.php';
64 64
 
65 65
 // Load modules.
66
-require_once plugin_dir_path( __FILE__ ) . 'modules/core/wordlift-core.php';
66
+require_once plugin_dir_path(__FILE__).'modules/core/wordlift-core.php';
67 67
 
68
-require_once plugin_dir_path( __FILE__ ) . 'deprecations.php';
68
+require_once plugin_dir_path(__FILE__).'deprecations.php';
69 69
 
70 70
 // Load early to enable/disable features.
71
-require_once plugin_dir_path( __FILE__ ) . 'classes/features/index.php';
71
+require_once plugin_dir_path(__FILE__).'classes/features/index.php';
72 72
 
73 73
 /**
74 74
  * The code that runs during plugin activation.
@@ -76,11 +76,11 @@  discard block
 block discarded – undo
76 76
  */
77 77
 function activate_wordlift() {
78 78
 
79
-	$log = Wordlift_Log_Service::get_logger( 'activate_wordlift' );
79
+	$log = Wordlift_Log_Service::get_logger('activate_wordlift');
80 80
 
81
-	$log->info( 'Activating WordLift...' );
81
+	$log->info('Activating WordLift...');
82 82
 
83
-	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php';
83
+	require_once plugin_dir_path(__FILE__).'includes/class-wordlift-activator.php';
84 84
 	Wordlift_Activator::activate();
85 85
 
86 86
 	/**
@@ -100,8 +100,8 @@  discard block
 block discarded – undo
100 100
 	 */
101 101
 	Top_Entities::activate();
102 102
 
103
-	if ( ! wp_next_scheduled( 'wl_daily_cron' ) ) {
104
-		wp_schedule_event( time(), 'daily', 'wl_daily_cron' );
103
+	if ( ! wp_next_scheduled('wl_daily_cron')) {
104
+		wp_schedule_event(time(), 'daily', 'wl_daily_cron');
105 105
 	}
106 106
 
107 107
 }
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
  */
113 113
 function deactivate_wordlift() {
114 114
 
115
-	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php';
115
+	require_once plugin_dir_path(__FILE__).'includes/class-wordlift-deactivator.php';
116 116
 	Wordlift_Deactivator::deactivate();
117 117
 	Wordlift_Http_Api::deactivate();
118 118
 	Ttl_Cache_Cleaner::deactivate();
@@ -128,18 +128,18 @@  discard block
 block discarded – undo
128 128
 	Key_Validation_Notice::remove_notification_flag();
129 129
 	flush_rewrite_rules();
130 130
 
131
-	wp_clear_scheduled_hook( 'wl_daily_cron' );
131
+	wp_clear_scheduled_hook('wl_daily_cron');
132 132
 
133 133
 }
134 134
 
135
-register_activation_hook( __FILE__, 'activate_wordlift' );
136
-register_deactivation_hook( __FILE__, 'deactivate_wordlift' );
135
+register_activation_hook(__FILE__, 'activate_wordlift');
136
+register_deactivation_hook(__FILE__, 'deactivate_wordlift');
137 137
 
138 138
 /**
139 139
  * The core plugin class that is used to define internationalization,
140 140
  * admin-specific hooks, and public-facing site hooks.
141 141
  */
142
-require plugin_dir_path( __FILE__ ) . 'includes/class-wordlift.php';
142
+require plugin_dir_path(__FILE__).'includes/class-wordlift.php';
143 143
 
144 144
 /**
145 145
  * Begins execution of the plugin.
@@ -159,12 +159,12 @@  discard block
 block discarded – undo
159 159
 	 * @return bool
160 160
 	 * @since 3.27.6
161 161
 	 */
162
-	if ( apply_filters( 'wl_feature__enable__widgets', true ) ) {
163
-		add_action( 'widgets_init', 'wl_register_chord_widget' );
164
-		add_action( 'widgets_init', 'wl_register_geo_widget' );
165
-		add_action( 'widgets_init', 'wl_register_timeline_widget' );
162
+	if (apply_filters('wl_feature__enable__widgets', true)) {
163
+		add_action('widgets_init', 'wl_register_chord_widget');
164
+		add_action('widgets_init', 'wl_register_geo_widget');
165
+		add_action('widgets_init', 'wl_register_timeline_widget');
166 166
 	}
167
-	add_filter( 'widget_text', 'do_shortcode' );
167
+	add_filter('widget_text', 'do_shortcode');
168 168
 
169 169
 	/**
170 170
 	 * Filter: wl_feature__enable__analysis
@@ -174,10 +174,10 @@  discard block
 block discarded – undo
174 174
 	 * @return bool
175 175
 	 * @since 3.27.6
176 176
 	 */
177
-	if ( apply_filters( 'wl_feature__enable__analysis', true ) ) {
178
-		add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' );
177
+	if (apply_filters('wl_feature__enable__analysis', true)) {
178
+		add_action('wp_ajax_wl_analyze', 'wl_ajax_analyze_action');
179 179
 	} else {
180
-		add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' );
180
+		add_action('wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action');
181 181
 	}
182 182
 
183 183
 	$plugin = new Wordlift();
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
 
195 195
 	add_action(
196 196
 		'plugins_loaded',
197
-		function () {
197
+		function() {
198 198
 			// All features from registry should be initialized here.
199 199
 			$features_registry = Features_Registry::get_instance();
200 200
 			$features_registry->initialize_all_features();
@@ -205,27 +205,27 @@  discard block
 block discarded – undo
205 205
 	add_action(
206 206
 		'plugins_loaded',
207 207
 		// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
208
-		function () use ( $plugin ) {
208
+		function() use ($plugin) {
209 209
 
210 210
 			new Wordlift_Products_Navigator_Shortcode_REST();
211 211
 
212 212
 			// Register the Dataset module, requires `$api_service`.
213
-			require_once plugin_dir_path( __FILE__ ) . 'classes/dataset/index.php';
214
-			require_once plugin_dir_path( __FILE__ ) . 'classes/shipping-data/index.php';
213
+			require_once plugin_dir_path(__FILE__).'classes/dataset/index.php';
214
+			require_once plugin_dir_path(__FILE__).'classes/shipping-data/index.php';
215 215
 
216 216
 			/*
217 217
 			* Require the Entity annotation cleanup module.
218 218
 			*
219 219
 			* @since 3.34.6
220 220
 			*/
221
-			require_once plugin_dir_path( __FILE__ ) . 'classes/cleanup/index.php';
221
+			require_once plugin_dir_path(__FILE__).'classes/cleanup/index.php';
222 222
 
223 223
 			/*
224 224
 			* Import LOD entities.
225 225
 			*
226 226
 			* @since 3.35.0
227 227
 			*/
228
-			require_once plugin_dir_path( __FILE__ ) . 'classes/lod-import/index.php';
228
+			require_once plugin_dir_path(__FILE__).'classes/lod-import/index.php';
229 229
 
230 230
 		}
231 231
 	);
@@ -243,31 +243,31 @@  discard block
 block discarded – undo
243 243
 function wordlift_plugin_autoload_register() {
244 244
 
245 245
 	spl_autoload_register(
246
-		function ( $class_name ) {
246
+		function($class_name) {
247 247
 
248 248
 			// Bail out if these are not our classes.
249
-			if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) {
249
+			if (0 !== strpos($class_name, 'Wordlift\\')) {
250 250
 				return false;
251 251
 			}
252 252
 
253
-			$class_name_lc = strtolower( str_replace( '_', '-', $class_name ) );
253
+			$class_name_lc = strtolower(str_replace('_', '-', $class_name));
254 254
 
255
-			preg_match( '|^wordlift\\\\(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches );
255
+			preg_match('|^wordlift\\\\(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches);
256 256
 
257
-			$path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] );
258
-			$file = 'class-' . $matches[2] . '.php';
257
+			$path = str_replace('\\', DIRECTORY_SEPARATOR, $matches[1]);
258
+			$file = 'class-'.$matches[2].'.php';
259 259
 
260
-			$full_path = plugin_dir_path( __FILE__ ) . 'classes' . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file;
260
+			$full_path = plugin_dir_path(__FILE__).'classes'.DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$file;
261 261
 
262
-			if ( ! file_exists( $full_path ) ) {
262
+			if ( ! file_exists($full_path)) {
263 263
 				return false;
264 264
 			}
265 265
 
266 266
 			try {
267 267
 				require_once $full_path;
268
-			} catch ( Exception $e ) {
268
+			} catch (Exception $e) {
269 269
 				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
270
-				error_log( "[WordLift] $full_path not found, cannot include." );
270
+				error_log("[WordLift] $full_path not found, cannot include.");
271 271
 			}
272 272
 
273 273
 			return true;
@@ -276,13 +276,13 @@  discard block
 block discarded – undo
276 276
 
277 277
 }
278 278
 
279
-function wl_block_categories( $categories ) {
279
+function wl_block_categories($categories) {
280 280
 	return array_merge(
281 281
 		$categories,
282 282
 		array(
283 283
 			array(
284 284
 				'slug'  => 'wordlift',
285
-				'title' => __( 'WordLift', 'wordlift' ),
285
+				'title' => __('WordLift', 'wordlift'),
286 286
 			),
287 287
 		)
288 288
 	);
@@ -292,51 +292,51 @@  discard block
 block discarded – undo
292 292
  * This function is created temporarily to handle the legacy library,
293 293
  * this has to be removed when removing the legacy fields from the ui.
294 294
  */
295
-function wl_enqueue_leaflet( $in_footer = false ) {
295
+function wl_enqueue_leaflet($in_footer = false) {
296 296
 	// Leaflet.
297
-	wp_enqueue_style( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.css', array(), '1.6.0' );
298
-	wp_enqueue_script( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer );
297
+	wp_enqueue_style('wl-leaflet', plugin_dir_url(__FILE__).'js/leaflet/leaflet.css', array(), '1.6.0');
298
+	wp_enqueue_script('wl-leaflet', plugin_dir_url(__FILE__).'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer);
299 299
 }
300 300
 
301
-add_filter( 'block_categories', 'wl_block_categories', 10 );
301
+add_filter('block_categories', 'wl_block_categories', 10);
302 302
 
303 303
 // Temporary fix for a typo in WooCommerce Extension.
304 304
 add_filter(
305 305
 	'wl_feature__enable__dataset',
306
-	function ( $value ) {
307
-		return apply_filters( 'wl_features__enable__dataset', $value );
306
+	function($value) {
307
+		return apply_filters('wl_features__enable__dataset', $value);
308 308
 	}
309 309
 );
310 310
 
311
-require_once __DIR__ . '/modules/food-kg/load.php';
312
-require_once __DIR__ . '/modules/gardening-kg/load.php';
313
-require_once __DIR__ . '/modules/acf4so/load.php';
314
-require_once __DIR__ . '/modules/dashboard/load.php';
315
-require_once __DIR__ . '/modules/pods/load.php';
316
-require_once __DIR__ . '/modules/include-exclude-push-config/load.php';
317
-require_once __DIR__ . '/modules/super-resolution/load.php';
318
-require_once __DIR__ . '/modules/redeem-code/load.php';
319
-require_once __DIR__ . '/modules/raptive-setup/load.php';
320
-require_once __DIR__ . '/modules/events/load.php';
321
-require_once __DIR__ . '/modules/plugin-diagnostics/load.php';
322
-require_once __DIR__ . '/modules/override-url/load.php';
323
-
324
-function _wl_update_plugins_raptive_domain( $update, $plugin_data, $plugin_file ) {
311
+require_once __DIR__.'/modules/food-kg/load.php';
312
+require_once __DIR__.'/modules/gardening-kg/load.php';
313
+require_once __DIR__.'/modules/acf4so/load.php';
314
+require_once __DIR__.'/modules/dashboard/load.php';
315
+require_once __DIR__.'/modules/pods/load.php';
316
+require_once __DIR__.'/modules/include-exclude-push-config/load.php';
317
+require_once __DIR__.'/modules/super-resolution/load.php';
318
+require_once __DIR__.'/modules/redeem-code/load.php';
319
+require_once __DIR__.'/modules/raptive-setup/load.php';
320
+require_once __DIR__.'/modules/events/load.php';
321
+require_once __DIR__.'/modules/plugin-diagnostics/load.php';
322
+require_once __DIR__.'/modules/override-url/load.php';
323
+
324
+function _wl_update_plugins_raptive_domain($update, $plugin_data, $plugin_file) {
325 325
 	// Bail out if it's not our plugin.
326 326
 	$update_uri = $plugin_data['UpdateURI'];
327
-	if ( 'wordlift/wordlift.php' !== $plugin_file || ! isset( $update_uri ) ) {
327
+	if ('wordlift/wordlift.php' !== $plugin_file || ! isset($update_uri)) {
328 328
 		return $update;
329 329
 	}
330 330
 
331
-	$response = wp_remote_get( "$update_uri?nocache=" . time() );
331
+	$response = wp_remote_get("$update_uri?nocache=".time());
332 332
 
333
-	if ( is_wp_error( $response ) ) {
333
+	if (is_wp_error($response)) {
334 334
 		return $update;
335 335
 	}
336 336
 
337 337
 	try {
338
-		return json_decode( wp_remote_retrieve_body( $response ) );
339
-	} catch ( Exception $e ) {
338
+		return json_decode(wp_remote_retrieve_body($response));
339
+	} catch (Exception $e) {
340 340
 		return $update;
341 341
 	}
342 342
 }
Please login to merge, or discard this patch.