Completed
Push — add/80-changelog ( 5b0ee5...e45d37 )
by Jeremy
08:51 queued 02:09
created

Settings::get_setting()   D

Complexity

Conditions 15
Paths 219

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
nc 219
nop 1
dl 0
loc 66
rs 4.8458
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	);
57
58
	/**
59
	 * Whether WordPress is currently running an import.
60
	 *
61
	 * @access public
62
	 * @static
63
	 *
64
	 * @var null|boolean
65
	 */
66
	public static $is_importing;
67
68
	/**
69
	 * Whether WordPress is currently running a WP cron request.
70
	 *
71
	 * @access public
72
	 * @static
73
	 *
74
	 * @var null|boolean
75
	 */
76
	public static $is_doing_cron;
77
78
	/**
79
	 * Whether we're currently syncing.
80
	 *
81
	 * @access public
82
	 * @static
83
	 *
84
	 * @var null|boolean
85
	 */
86
	public static $is_syncing;
87
88
	/**
89
	 * Whether we're currently sending sync items.
90
	 *
91
	 * @access public
92
	 * @static
93
	 *
94
	 * @var null|boolean
95
	 */
96
	public static $is_sending;
97
98
	/**
99
	 * Some settings can be expensive to compute - let's cache them.
100
	 *
101
	 * @access public
102
	 * @static
103
	 *
104
	 * @var array
105
	 */
106
	public static $settings_cache = array();
107
108
	/**
109
	 * Retrieve all settings with their current values.
110
	 *
111
	 * @access public
112
	 * @static
113
	 *
114
	 * @return array All current settings.
115
	 */
116
	public static function get_settings() {
117
		$settings = array();
118
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
119
			$settings[ $setting ] = self::get_setting( $setting );
120
		}
121
122
		return $settings;
123
	}
124
125
	/**
126
	 * Fetches the setting. It saves it if the setting doesn't exist, so that it gets
127
	 * autoloaded on page load rather than re-queried every time.
128
	 *
129
	 * @access public
130
	 * @static
131
	 *
132
	 * @param string $setting The setting name.
133
	 * @return mixed The setting value.
134
	 */
135
	public static function get_setting( $setting ) {
136
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
137
			return false;
138
		}
139
140
		if ( isset( self::$settings_cache[ $setting ] ) ) {
141
			return self::$settings_cache[ $setting ];
142
		}
143
144
		if ( self::is_network_setting( $setting ) ) {
145
			if ( is_multisite() ) {
146
				$value = get_site_option( self::SETTINGS_OPTION_PREFIX . $setting );
147
			} else {
148
				// On single sites just return the default setting.
149
				$value                            = Defaults::get_default_setting( $setting );
150
				self::$settings_cache[ $setting ] = $value;
151
				return $value;
152
			}
153
		} else {
154
			$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
155
		}
156
157
		if ( false === $value ) { // No default value is set.
158
			$value = Defaults::get_default_setting( $setting );
159
			if ( self::is_network_setting( $setting ) ) {
160
				update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
161
			} else {
162
				// We set one so that it gets autoloaded.
163
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
164
			}
165
		}
166
167
		if ( is_numeric( $value ) ) {
168
			$value = intval( $value );
169
		}
170
		$default_array_value = null;
171
		switch ( $setting ) {
172
			case 'post_types_blacklist':
173
				$default_array_value = Defaults::$blacklisted_post_types;
174
				break;
175
			case 'taxonomies_blacklist':
176
				$default_array_value = Defaults::$blacklisted_taxonomies;
177
				break;
178
			case 'post_meta_whitelist':
179
				$default_array_value = Defaults::get_post_meta_whitelist();
180
				break;
181
			case 'comment_meta_whitelist':
182
				$default_array_value = Defaults::get_comment_meta_whitelist();
183
				break;
184
			case 'known_importers':
185
				$default_array_value = Defaults::get_known_importers();
186
				break;
187
		}
188
189
		if ( $default_array_value ) {
190
			if ( is_array( $value ) ) {
191
				$value = array_unique( array_merge( $value, $default_array_value ) );
192
			} else {
193
				$value = $default_array_value;
194
			}
195
		}
196
197
		self::$settings_cache[ $setting ] = $value;
198
199
		return $value;
200
	}
201
202
	/**
203
	 * Change multiple settings in the same time.
204
	 *
205
	 * @access public
206
	 * @static
207
	 *
208
	 * @param array $new_settings The new settings.
209
	 */
210
	public static function update_settings( $new_settings ) {
211
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
212
		foreach ( $validated_settings as $setting => $value ) {
213
214
			if ( self::is_network_setting( $setting ) ) {
215
				if ( is_multisite() && is_main_site() ) {
216
					update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
217
				}
218
			} else {
219
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
220
			}
221
222
			unset( self::$settings_cache[ $setting ] );
223
224
			// If we set the disabled option to true, clear the queues.
225
			if ( ( 'disable' === $setting || 'network_disable' === $setting ) && ! ! $value ) {
226
				$listener = Listener::get_instance();
227
				$listener->get_sync_queue()->reset();
228
				$listener->get_full_sync_queue()->reset();
229
			}
230
		}
231
	}
232
233
	/**
234
	 * Whether the specified setting is a network setting.
235
	 *
236
	 * @access public
237
	 * @static
238
	 *
239
	 * @param string $setting Setting name.
240
	 * @return boolean Whether the setting is a network setting.
241
	 */
242
	public static function is_network_setting( $setting ) {
243
		return strpos( $setting, 'network_' ) === 0;
244
	}
245
246
	/**
247
	 * Returns escaped SQL for blacklisted post types.
248
	 * Can be injected directly into a WHERE clause.
249
	 *
250
	 * @access public
251
	 * @static
252
	 *
253
	 * @return string SQL WHERE clause.
254
	 */
255
	public static function get_blacklisted_post_types_sql() {
256
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
257
	}
258
259
	/**
260
	 * Returns escaped SQL for blacklisted taxonomies.
261
	 * Can be injected directly into a WHERE clause.
262
	 *
263
	 * @access public
264
	 * @static
265
	 *
266
	 * @return string SQL WHERE clause.
267
	 */
268
	public static function get_blacklisted_taxonomies_sql() {
269
		return 'taxonomy NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'taxonomies_blacklist' ) ) ) . '\')';
270
	}
271
272
	/**
273
	 * Returns escaped SQL for blacklisted post meta.
274
	 * Can be injected directly into a WHERE clause.
275
	 *
276
	 * @access public
277
	 * @static
278
	 *
279
	 * @return string SQL WHERE clause.
280
	 */
281
	public static function get_whitelisted_post_meta_sql() {
282
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')';
283
	}
284
285
	/**
286
	 * Returns escaped SQL for blacklisted comment meta.
287
	 * Can be injected directly into a WHERE clause.
288
	 *
289
	 * @access public
290
	 * @static
291
	 *
292
	 * @return string SQL WHERE clause.
293
	 */
294
	public static function get_whitelisted_comment_meta_sql() {
295
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')';
296
	}
297
298
	/**
299
	 * Returns escaped SQL for comments, excluding any spam comments.
300
	 * Can be injected directly into a WHERE clause.
301
	 *
302
	 * @access public
303
	 * @static
304
	 *
305
	 * @return string SQL WHERE clause.
306
	 */
307
	public static function get_comments_filter_sql() {
308
		return "comment_approved <> 'spam'";
309
	}
310
311
	/**
312
	 * Delete any settings options and clean up the current settings state.
313
	 *
314
	 * @access public
315
	 * @static
316
	 */
317
	public static function reset_data() {
318
		$valid_settings       = self::$valid_settings;
319
		self::$settings_cache = array();
320
		foreach ( $valid_settings as $option => $value ) {
321
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
322
		}
323
		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...
324
		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...
325
		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...
326
		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...
327
	}
328
329
	/**
330
	 * Set the importing state.
331
	 *
332
	 * @access public
333
	 * @static
334
	 *
335
	 * @param boolean $is_importing Whether WordPress is currently importing.
336
	 */
337
	public static function set_importing( $is_importing ) {
338
		// Set to NULL to revert to WP_IMPORTING, the standard behavior.
339
		self::$is_importing = $is_importing;
340
	}
341
342
	/**
343
	 * Whether WordPress is currently importing.
344
	 *
345
	 * @access public
346
	 * @static
347
	 *
348
	 * @return boolean Whether WordPress is currently importing.
349
	 */
350
	public static function is_importing() {
351
		if ( ! is_null( self::$is_importing ) ) {
352
			return self::$is_importing;
353
		}
354
355
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
356
	}
357
358
	/**
359
	 * Whether sync is enabled.
360
	 *
361
	 * @access public
362
	 * @static
363
	 *
364
	 * @return boolean Whether sync is enabled.
365
	 */
366
	public static function is_sync_enabled() {
367
		return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) );
368
	}
369
370
	/**
371
	 * Set the WP cron state.
372
	 *
373
	 * @access public
374
	 * @static
375
	 *
376
	 * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron.
377
	 */
378
	public static function set_doing_cron( $is_doing_cron ) {
379
		// Set to NULL to revert to WP_IMPORTING, the standard behavior.
380
		self::$is_doing_cron = $is_doing_cron;
381
	}
382
383
	/**
384
	 * Whether WordPress is currently doing WP cron.
385
	 *
386
	 * @access public
387
	 * @static
388
	 *
389
	 * @return boolean Whether WordPress is currently doing WP cron.
390
	 */
391
	public static function is_doing_cron() {
392
		if ( ! is_null( self::$is_doing_cron ) ) {
393
			return self::$is_doing_cron;
394
		}
395
396
		return defined( 'DOING_CRON' ) && DOING_CRON;
397
	}
398
399
	/**
400
	 * Whether we are currently syncing.
401
	 *
402
	 * @access public
403
	 * @static
404
	 *
405
	 * @return boolean Whether we are currently syncing.
406
	 */
407
	public static function is_syncing() {
408
		return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST );
409
	}
410
411
	/**
412
	 * Set the syncing state.
413
	 *
414
	 * @access public
415
	 * @static
416
	 *
417
	 * @param boolean $is_syncing Whether we are currently syncing.
418
	 */
419
	public static function set_is_syncing( $is_syncing ) {
420
		self::$is_syncing = $is_syncing;
421
	}
422
423
	/**
424
	 * Whether we are currently sending sync items.
425
	 *
426
	 * @access public
427
	 * @static
428
	 *
429
	 * @return boolean Whether we are currently sending sync items.
430
	 */
431
	public static function is_sending() {
432
		return (bool) self::$is_sending;
433
	}
434
435
	/**
436
	 * Set the sending state.
437
	 *
438
	 * @access public
439
	 * @static
440
	 *
441
	 * @param boolean $is_sending Whether we are currently sending sync items.
442
	 */
443
	public static function set_is_sending( $is_sending ) {
444
		self::$is_sending = $is_sending;
445
	}
446
447
	/**
448
	 * Whether should send from the queue
449
	 *
450
	 * @access public
451
	 * @static
452
	 *
453
	 * @param string $queue_id The queue identifier.
454
	 *
455
	 * @return boolean Whether sync is enabled.
456
	 */
457
	public static function is_sender_enabled( $queue_id ) {
458
		return (bool) self::get_setting( $queue_id . '_sender_enabled' );
459
	}
460
461
}
462