Completed
Push — rm/minileven-ui ( 326a89...9f59a8 )
by Jeremy
09:25 queued 02:29
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
		'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 = intval( $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 SQL for blacklisted taxonomies.
243
	 * Can be injected directly into a WHERE clause.
244
	 *
245
	 * @access public
246
	 * @static
247
	 *
248
	 * @return string SQL WHERE clause.
249
	 */
250
	public static function get_blacklisted_taxonomies_sql() {
251
		return "taxonomy NOT IN ('" . join( "', '", array_map( 'esc_sql', self::get_setting( 'taxonomies_blacklist' ) ) ) . "')";
252
	}
253
254
	/**
255
	 * Returns escaped SQL for blacklisted post meta.
256
	 * Can be injected directly into a WHERE clause.
257
	 *
258
	 * @access public
259
	 * @static
260
	 *
261
	 * @return string SQL WHERE clause.
262
	 */
263
	public static function get_whitelisted_post_meta_sql() {
264
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')';
265
	}
266
267
	/**
268
	 * Returns escaped SQL for blacklisted comment meta.
269
	 * Can be injected directly into a WHERE clause.
270
	 *
271
	 * @access public
272
	 * @static
273
	 *
274
	 * @return string SQL WHERE clause.
275
	 */
276
	public static function get_whitelisted_comment_meta_sql() {
277
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')';
278
	}
279
280
	/**
281
	 * Returns escaped SQL for comments, excluding any spam comments.
282
	 * Can be injected directly into a WHERE clause.
283
	 *
284
	 * @access public
285
	 * @static
286
	 *
287
	 * @return string SQL WHERE clause.
288
	 */
289
	public static function get_comments_filter_sql() {
290
		return "comment_approved <> 'spam'";
291
	}
292
293
	/**
294
	 * Delete any settings options and clean up the current settings state.
295
	 *
296
	 * @access public
297
	 * @static
298
	 */
299
	public static function reset_data() {
300
		$valid_settings = self::$valid_settings;
301
		foreach ( $valid_settings as $option => $value ) {
302
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
303
		}
304
		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...
305
		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...
306
		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...
307
		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...
308
	}
309
310
	/**
311
	 * Set the importing state.
312
	 *
313
	 * @access public
314
	 * @static
315
	 *
316
	 * @param boolean $is_importing Whether WordPress is currently importing.
317
	 */
318
	public static function set_importing( $is_importing ) {
319
		// Set to NULL to revert to WP_IMPORTING, the standard behavior.
320
		self::$is_importing = $is_importing;
321
	}
322
323
	/**
324
	 * Whether WordPress is currently importing.
325
	 *
326
	 * @access public
327
	 * @static
328
	 *
329
	 * @return boolean Whether WordPress is currently importing.
330
	 */
331
	public static function is_importing() {
332
		if ( ! is_null( self::$is_importing ) ) {
333
			return self::$is_importing;
334
		}
335
336
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
337
	}
338
339
	/**
340
	 * Whether sync is enabled.
341
	 *
342
	 * @access public
343
	 * @static
344
	 *
345
	 * @return boolean Whether sync is enabled.
346
	 */
347
	public static function is_sync_enabled() {
348
		return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) );
349
	}
350
351
	/**
352
	 * Set the WP cron state.
353
	 *
354
	 * @access public
355
	 * @static
356
	 *
357
	 * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron.
358
	 */
359
	public static function set_doing_cron( $is_doing_cron ) {
360
		// Set to NULL to revert to WP_IMPORTING, the standard behavior.
361
		self::$is_doing_cron = $is_doing_cron;
362
	}
363
364
	/**
365
	 * Whether WordPress is currently doing WP cron.
366
	 *
367
	 * @access public
368
	 * @static
369
	 *
370
	 * @return boolean Whether WordPress is currently doing WP cron.
371
	 */
372
	public static function is_doing_cron() {
373
		if ( ! is_null( self::$is_doing_cron ) ) {
374
			return self::$is_doing_cron;
375
		}
376
377
		return defined( 'DOING_CRON' ) && DOING_CRON;
378
	}
379
380
	/**
381
	 * Whether we are currently syncing.
382
	 *
383
	 * @access public
384
	 * @static
385
	 *
386
	 * @return boolean Whether we are currently syncing.
387
	 */
388
	public static function is_syncing() {
389
		return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST );
390
	}
391
392
	/**
393
	 * Set the syncing state.
394
	 *
395
	 * @access public
396
	 * @static
397
	 *
398
	 * @param boolean $is_syncing Whether we are currently syncing.
399
	 */
400
	public static function set_is_syncing( $is_syncing ) {
401
		self::$is_syncing = $is_syncing;
402
	}
403
404
	/**
405
	 * Whether we are currently sending sync items.
406
	 *
407
	 * @access public
408
	 * @static
409
	 *
410
	 * @return boolean Whether we are currently sending sync items.
411
	 */
412
	public static function is_sending() {
413
		return (bool) self::$is_sending;
414
	}
415
416
	/**
417
	 * Set the sending state.
418
	 *
419
	 * @access public
420
	 * @static
421
	 *
422
	 * @param boolean $is_sending Whether we are currently sending sync items.
423
	 */
424
	public static function set_is_sending( $is_sending ) {
425
		self::$is_sending = $is_sending;
426
	}
427
428
	/**
429
	 * Whether should send from the queue
430
	 *
431
	 * @access public
432
	 * @static
433
	 *
434
	 * @param string $queue_id The queue identifier.
435
	 *
436
	 * @return boolean Whether sync is enabled.
437
	 */
438
	public static function is_sender_enabled( $queue_id ) {
439
		return (bool) self::get_setting( $queue_id . '_sender_enabled' );
440
	}
441
442
}
443