Completed
Push — update/add-remaining-states-to... ( a1cc22...7d04ff )
by
unknown
07:01
created

Settings::get_setting()   C

Complexity

Conditions 14
Paths 218

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

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