Completed
Push — add/sync-partial-sync-checksum... ( e921de...304861 )
by
unknown
76:02 queued 67:35
created

Settings::get_allowed_comment_meta_structured()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 8
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Sync settings.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync;
9
10
/**
11
 * Class to manage the sync settings.
12
 */
13
class Settings {
14
	/**
15
	 * Prefix, used for the sync settings option names.
16
	 *
17
	 * @access public
18
	 *
19
	 * @var string
20
	 */
21
	const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_';
22
23
	/**
24
	 * A whitelist of valid settings.
25
	 *
26
	 * @access public
27
	 * @static
28
	 *
29
	 * @var array
30
	 */
31
	public static $valid_settings = array(
32
		'dequeue_max_bytes'                      => true,
33
		'upload_max_bytes'                       => true,
34
		'upload_max_rows'                        => true,
35
		'sync_wait_time'                         => true,
36
		'sync_wait_threshold'                    => true,
37
		'enqueue_wait_time'                      => true,
38
		'max_queue_size'                         => true,
39
		'max_queue_lag'                          => true,
40
		'queue_max_writes_sec'                   => true,
41
		'post_types_blacklist'                   => true,
42
		'taxonomies_blacklist'                   => true,
43
		'disable'                                => true,
44
		'network_disable'                        => true,
45
		'render_filtered_content'                => true,
46
		'post_meta_whitelist'                    => true,
47
		'comment_meta_whitelist'                 => true,
48
		'max_enqueue_full_sync'                  => true,
49
		'max_queue_size_full_sync'               => true,
50
		'sync_via_cron'                          => true,
51
		'cron_sync_time_limit'                   => true,
52
		'known_importers'                        => true,
53
		'term_relationships_full_sync_item_size' => true,
54
		'sync_sender_enabled'                    => true,
55
		'full_sync_sender_enabled'               => true,
56
		'full_sync_send_duration'                => true,
57
		'full_sync_limits'                       => true,
58
	);
59
60
	/**
61
	 * Whether WordPress is currently running an import.
62
	 *
63
	 * @access public
64
	 * @static
65
	 *
66
	 * @var null|boolean
67
	 */
68
	public static $is_importing;
69
70
	/**
71
	 * Whether WordPress is currently running a WP cron request.
72
	 *
73
	 * @access public
74
	 * @static
75
	 *
76
	 * @var null|boolean
77
	 */
78
	public static $is_doing_cron;
79
80
	/**
81
	 * Whether we're currently syncing.
82
	 *
83
	 * @access public
84
	 * @static
85
	 *
86
	 * @var null|boolean
87
	 */
88
	public static $is_syncing;
89
90
	/**
91
	 * Whether we're currently sending sync items.
92
	 *
93
	 * @access public
94
	 * @static
95
	 *
96
	 * @var null|boolean
97
	 */
98
	public static $is_sending;
99
100
	/**
101
	 * Retrieve all settings with their current values.
102
	 *
103
	 * @access public
104
	 * @static
105
	 *
106
	 * @return array All current settings.
107
	 */
108
	public static function get_settings() {
109
		$settings = array();
110
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
111
			$settings[ $setting ] = self::get_setting( $setting );
112
		}
113
114
		return $settings;
115
	}
116
117
	/**
118
	 * Fetches the setting. It saves it if the setting doesn't exist, so that it gets
119
	 * autoloaded on page load rather than re-queried every time.
120
	 *
121
	 * @access public
122
	 * @static
123
	 *
124
	 * @param string $setting The setting name.
125
	 * @return mixed The setting value.
126
	 */
127
	public static function get_setting( $setting ) {
128
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
129
			return false;
130
		}
131
132
		if ( self::is_network_setting( $setting ) ) {
133
			if ( is_multisite() ) {
134
				$value = get_site_option( self::SETTINGS_OPTION_PREFIX . $setting );
135
			} else {
136
				// On single sites just return the default setting.
137
				return Defaults::get_default_setting( $setting );
138
			}
139
		} else {
140
			$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
141
		}
142
143
		if ( false === $value ) { // No default value is set.
144
			$value = Defaults::get_default_setting( $setting );
145
			if ( self::is_network_setting( $setting ) ) {
146
				update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
147
			} else {
148
				// We set one so that it gets autoloaded.
149
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
150
			}
151
		}
152
153
		if ( is_numeric( $value ) ) {
154
			$value = (int) $value;
155
		}
156
		$default_array_value = null;
157
		switch ( $setting ) {
158
			case 'post_types_blacklist':
159
				$default_array_value = Defaults::$blacklisted_post_types;
160
				break;
161
			case 'taxonomies_blacklist':
162
				$default_array_value = Defaults::$blacklisted_taxonomies;
163
				break;
164
			case 'post_meta_whitelist':
165
				$default_array_value = Defaults::get_post_meta_whitelist();
166
				break;
167
			case 'comment_meta_whitelist':
168
				$default_array_value = Defaults::get_comment_meta_whitelist();
169
				break;
170
			case 'known_importers':
171
				$default_array_value = Defaults::get_known_importers();
172
				break;
173
		}
174
175
		if ( $default_array_value ) {
176
			if ( is_array( $value ) ) {
177
				$value = array_unique( array_merge( $value, $default_array_value ) );
178
			} else {
179
				$value = $default_array_value;
180
			}
181
		}
182
183
		return $value;
184
	}
185
186
	/**
187
	 * Change multiple settings in the same time.
188
	 *
189
	 * @access public
190
	 * @static
191
	 *
192
	 * @param array $new_settings The new settings.
193
	 */
194
	public static function update_settings( $new_settings ) {
195
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
196
		foreach ( $validated_settings as $setting => $value ) {
197
198
			if ( self::is_network_setting( $setting ) ) {
199
				if ( is_multisite() && is_main_site() ) {
200
					update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
201
				}
202
			} else {
203
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
204
			}
205
206
			// If we set the disabled option to true, clear the queues.
207
			if ( ( 'disable' === $setting || 'network_disable' === $setting ) && ! ! $value ) {
208
				$listener = Listener::get_instance();
209
				$listener->get_sync_queue()->reset();
210
				$listener->get_full_sync_queue()->reset();
211
			}
212
		}
213
	}
214
215
	/**
216
	 * Whether the specified setting is a network setting.
217
	 *
218
	 * @access public
219
	 * @static
220
	 *
221
	 * @param string $setting Setting name.
222
	 * @return boolean Whether the setting is a network setting.
223
	 */
224
	public static function is_network_setting( $setting ) {
225
		return strpos( $setting, 'network_' ) === 0;
226
	}
227
228
	/**
229
	 * Returns escaped SQL for blacklisted post types.
230
	 * Can be injected directly into a WHERE clause.
231
	 *
232
	 * @access public
233
	 * @static
234
	 *
235
	 * @return string SQL WHERE clause.
236
	 */
237
	public static function get_blacklisted_post_types_sql() {
238
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
239
	}
240
241
	/**
242
	 * Returns escaped values for disallowed post types.
243
	 *
244
	 * @access public
245
	 * @static
246
	 *
247
	 * @return array Post type filter values
248
	 */
249
	public static function get_disallowed_post_types_structured() {
250
		return array(
251
			'post_type' => array(
252
				'operator' => 'NOT IN',
253
				'values'   => array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ),
254
			),
255
		);
256
	}
257
258
	/**
259
	 * Returns escaped SQL for blacklisted taxonomies.
260
	 * Can be injected directly into a WHERE clause.
261
	 *
262
	 * @access public
263
	 * @static
264
	 *
265
	 * @return string SQL WHERE clause.
266
	 */
267
	public static function get_blacklisted_taxonomies_sql() {
268
		return "taxonomy NOT IN ('" . join( "', '", array_map( 'esc_sql', self::get_setting( 'taxonomies_blacklist' ) ) ) . "')";
269
	}
270
271
	/**
272
	 * Returns escaped SQL for blacklisted post meta.
273
	 * Can be injected directly into a WHERE clause.
274
	 *
275
	 * @access public
276
	 * @static
277
	 *
278
	 * @return string SQL WHERE clause.
279
	 */
280
	public static function get_whitelisted_post_meta_sql() {
281
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')';
282
	}
283
284
	/**
285
	 * Returns escaped SQL for allowed post meta keys.
286
	 *
287
	 * @access public
288
	 * @static
289
	 *
290
	 * @return array Meta keys filter values
291
	 */
292 View Code Duplication
	public static function get_allowed_post_meta_structured() {
293
		return array(
294
			'meta_key' => array(
295
				'operator' => 'IN',
296
				'values'   => array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ),
297
			),
298
		);
299
	}
300
301
	/**
302
	 * Returns escaped SQL for blacklisted comment meta.
303
	 * Can be injected directly into a WHERE clause.
304
	 *
305
	 * @access public
306
	 * @static
307
	 *
308
	 * @return string SQL WHERE clause.
309
	 */
310
	public static function get_whitelisted_comment_meta_sql() {
311
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')';
312
	}
313
314
	/**
315
	 * Returns SQL-escaped values for allowed post meta keys.
316
	 *
317
	 * @access public
318
	 * @static
319
	 *
320
	 * @return array Meta keys filter values
321
	 */
322 View Code Duplication
	public static function get_allowed_comment_meta_structured() {
323
		return array(
324
			'meta_key' => array(
325
				'operator' => 'IN',
326
				'values'   => array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ),
327
			),
328
		);
329
	}
330
331
	/**
332
	 * Returns escaped SQL for comments, excluding any spam comments.
333
	 * Can be injected directly into a WHERE clause.
334
	 *
335
	 * @access public
336
	 * @static
337
	 *
338
	 * @return string SQL WHERE clause.
339
	 */
340
	public static function get_comments_filter_sql() {
341
		return "comment_approved <> 'spam'";
342
	}
343
344
	/**
345
	 * Delete any settings options and clean up the current settings state.
346
	 *
347
	 * @access public
348
	 * @static
349
	 */
350
	public static function reset_data() {
351
		$valid_settings = self::$valid_settings;
352
		foreach ( $valid_settings as $option => $value ) {
353
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
354
		}
355
		self::set_importing( null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
356
		self::set_doing_cron( null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
357
		self::set_is_syncing( null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
358
		self::set_is_sending( null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
359
	}
360
361
	/**
362
	 * Set the importing state.
363
	 *
364
	 * @access public
365
	 * @static
366
	 *
367
	 * @param boolean $is_importing Whether WordPress is currently importing.
368
	 */
369
	public static function set_importing( $is_importing ) {
370
		// Set to NULL to revert to WP_IMPORTING, the standard behavior.
371
		self::$is_importing = $is_importing;
372
	}
373
374
	/**
375
	 * Whether WordPress is currently importing.
376
	 *
377
	 * @access public
378
	 * @static
379
	 *
380
	 * @return boolean Whether WordPress is currently importing.
381
	 */
382
	public static function is_importing() {
383
		if ( ! is_null( self::$is_importing ) ) {
384
			return self::$is_importing;
385
		}
386
387
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
388
	}
389
390
	/**
391
	 * Whether sync is enabled.
392
	 *
393
	 * @access public
394
	 * @static
395
	 *
396
	 * @return boolean Whether sync is enabled.
397
	 */
398
	public static function is_sync_enabled() {
399
		return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) );
400
	}
401
402
	/**
403
	 * Set the WP cron state.
404
	 *
405
	 * @access public
406
	 * @static
407
	 *
408
	 * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron.
409
	 */
410
	public static function set_doing_cron( $is_doing_cron ) {
411
		// Set to NULL to revert to WP_IMPORTING, the standard behavior.
412
		self::$is_doing_cron = $is_doing_cron;
413
	}
414
415
	/**
416
	 * Whether WordPress is currently doing WP cron.
417
	 *
418
	 * @access public
419
	 * @static
420
	 *
421
	 * @return boolean Whether WordPress is currently doing WP cron.
422
	 */
423
	public static function is_doing_cron() {
424
		if ( ! is_null( self::$is_doing_cron ) ) {
425
			return self::$is_doing_cron;
426
		}
427
428
		return defined( 'DOING_CRON' ) && DOING_CRON;
429
	}
430
431
	/**
432
	 * Whether we are currently syncing.
433
	 *
434
	 * @access public
435
	 * @static
436
	 *
437
	 * @return boolean Whether we are currently syncing.
438
	 */
439
	public static function is_syncing() {
440
		return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST );
441
	}
442
443
	/**
444
	 * Set the syncing state.
445
	 *
446
	 * @access public
447
	 * @static
448
	 *
449
	 * @param boolean $is_syncing Whether we are currently syncing.
450
	 */
451
	public static function set_is_syncing( $is_syncing ) {
452
		self::$is_syncing = $is_syncing;
453
	}
454
455
	/**
456
	 * Whether we are currently sending sync items.
457
	 *
458
	 * @access public
459
	 * @static
460
	 *
461
	 * @return boolean Whether we are currently sending sync items.
462
	 */
463
	public static function is_sending() {
464
		return (bool) self::$is_sending;
465
	}
466
467
	/**
468
	 * Set the sending state.
469
	 *
470
	 * @access public
471
	 * @static
472
	 *
473
	 * @param boolean $is_sending Whether we are currently sending sync items.
474
	 */
475
	public static function set_is_sending( $is_sending ) {
476
		self::$is_sending = $is_sending;
477
	}
478
479
	/**
480
	 * Whether should send from the queue
481
	 *
482
	 * @access public
483
	 * @static
484
	 *
485
	 * @param string $queue_id The queue identifier.
486
	 *
487
	 * @return boolean Whether sync is enabled.
488
	 */
489
	public static function is_sender_enabled( $queue_id ) {
490
		return (bool) self::get_setting( $queue_id . '_sender_enabled' );
491
	}
492
493
}
494