Completed
Push — renovate/glob-7.x ( 697d78...f7fc07 )
by
unknown
18:21 queued 12:01
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
namespace Automattic\Jetpack\Sync;
4
5
class Settings {
6
	const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_';
7
8
	static $valid_settings = array(
9
		'dequeue_max_bytes'        => true,
10
		'upload_max_bytes'         => true,
11
		'upload_max_rows'          => true,
12
		'sync_wait_time'           => true,
13
		'sync_wait_threshold'      => true,
14
		'enqueue_wait_time'        => true,
15
		'max_queue_size'           => true,
16
		'max_queue_lag'            => true,
17
		'queue_max_writes_sec'     => true,
18
		'post_types_blacklist'     => true,
19
		'taxonomies_blacklist'     => true,
20
		'disable'                  => true,
21
		'network_disable'          => true,
22
		'render_filtered_content'  => true,
23
		'post_meta_whitelist'      => true,
24
		'comment_meta_whitelist'   => true,
25
		'max_enqueue_full_sync'    => true,
26
		'max_queue_size_full_sync' => true,
27
		'sync_via_cron'            => true,
28
		'cron_sync_time_limit'     => true,
29
		'known_importers'          => true,
30
	);
31
32
	static $is_importing;
33
	static $is_doing_cron;
34
	static $is_syncing;
35
	static $is_sending;
36
37
	static $settings_cache = array(); // some settings can be expensive to compute - let's cache them
38
39
	static function get_settings() {
40
		$settings = array();
41
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
42
			$settings[ $setting ] = self::get_setting( $setting );
43
		}
44
45
		return $settings;
46
	}
47
48
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
49
	// autoloaded on page load rather than re-queried every time.
50
	static function get_setting( $setting ) {
51
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
52
			return false;
53
		}
54
55
		if ( isset( self::$settings_cache[ $setting ] ) ) {
56
			return self::$settings_cache[ $setting ];
57
		}
58
59
		if ( self::is_network_setting( $setting ) ) {
60
			if ( is_multisite() ) {
61
				$value = get_site_option( self::SETTINGS_OPTION_PREFIX . $setting );
62
			} else {
63
				// On single sites just return the default setting
64
				$value                            = Defaults::get_default_setting( $setting );
65
				self::$settings_cache[ $setting ] = $value;
66
				return $value;
67
			}
68
		} else {
69
			$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
70
		}
71
72
		if ( false === $value ) { // no default value is set.
73
			$value = Defaults::get_default_setting( $setting );
74
			if ( self::is_network_setting( $setting ) ) {
75
				update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
76
			} else {
77
				// We set one so that it gets autoloaded
78
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
79
			}
80
		}
81
82
		if ( is_numeric( $value ) ) {
83
			$value = intval( $value );
84
		}
85
		$default_array_value = null;
86
		switch ( $setting ) {
87
			case 'post_types_blacklist':
88
				$default_array_value = Defaults::$blacklisted_post_types;
0 ignored issues
show
Bug introduced by
The property blacklisted_post_types cannot be accessed from this context as it is declared private in class Automattic\Jetpack\Sync\Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
89
				break;
90
			case 'taxonomies_blacklist':
91
				$default_array_value = Defaults::$blacklisted_taxonomies;
92
				break;
93
			case 'post_meta_whitelist':
94
				$default_array_value = Defaults::get_post_meta_whitelist();
95
				break;
96
			case 'comment_meta_whitelist':
97
				$default_array_value = Defaults::get_comment_meta_whitelist();
98
				break;
99
			case 'known_importers':
100
				$default_array_value = Defaults::get_known_importers();
101
				break;
102
		}
103
104
		if ( $default_array_value ) {
105
			if ( is_array( $value ) ) {
106
				$value = array_unique( array_merge( $value, $default_array_value ) );
107
			} else {
108
				$value = $default_array_value;
109
			}
110
		}
111
112
		self::$settings_cache[ $setting ] = $value;
113
114
		return $value;
115
	}
116
117
	static function update_settings( $new_settings ) {
118
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
119
		foreach ( $validated_settings as $setting => $value ) {
120
121
			if ( self::is_network_setting( $setting ) ) {
122
				if ( is_multisite() && is_main_site() ) {
123
					update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
124
				}
125
			} else {
126
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
127
			}
128
129
			unset( self::$settings_cache[ $setting ] );
130
131
			// if we set the disabled option to true, clear the queues
132
			if ( ( 'disable' === $setting || 'network_disable' === $setting ) && ! ! $value ) {
133
				$listener = Listener::get_instance();
134
				$listener->get_sync_queue()->reset();
135
				$listener->get_full_sync_queue()->reset();
136
			}
137
		}
138
	}
139
140
	static function is_network_setting( $setting ) {
141
		return strpos( $setting, 'network_' ) === 0;
142
	}
143
144
	// returns escapted SQL that can be injected into a WHERE clause
145
	static function get_blacklisted_post_types_sql() {
146
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
147
	}
148
149
	static function get_blacklisted_taxonomies_sql() {
150
		return 'taxonomy NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'taxonomies_blacklist' ) ) ) . '\')';
151
	}
152
153
	static function get_whitelisted_post_meta_sql() {
154
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')';
155
	}
156
157
	static function get_whitelisted_comment_meta_sql() {
158
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')';
159
	}
160
161
	static function get_comments_filter_sql() {
162
		return "comment_approved <> 'spam'";
163
	}
164
165
	static function reset_data() {
166
		$valid_settings       = self::$valid_settings;
167
		self::$settings_cache = array();
168
		foreach ( $valid_settings as $option => $value ) {
169
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
170
		}
171
		self::set_importing( null );
172
		self::set_doing_cron( null );
173
		self::set_is_syncing( null );
174
		self::set_is_sending( null );
175
	}
176
177
	static function set_importing( $is_importing ) {
178
		// set to NULL to revert to WP_IMPORTING, the standard behavior
179
		self::$is_importing = $is_importing;
180
	}
181
182
	static function is_importing() {
183
		if ( ! is_null( self::$is_importing ) ) {
184
			return self::$is_importing;
185
		}
186
187
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
188
	}
189
190
	static function is_sync_enabled() {
191
		return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) );
192
	}
193
194
	static function set_doing_cron( $is_doing_cron ) {
195
		// set to NULL to revert to WP_IMPORTING, the standard behavior
196
		self::$is_doing_cron = $is_doing_cron;
197
	}
198
199
	static function is_doing_cron() {
200
		if ( ! is_null( self::$is_doing_cron ) ) {
201
			return self::$is_doing_cron;
202
		}
203
204
		return defined( 'DOING_CRON' ) && DOING_CRON;
205
	}
206
207
	static function is_syncing() {
208
		return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST );
209
	}
210
211
	static function set_is_syncing( $is_syncing ) {
212
		self::$is_syncing = $is_syncing;
213
	}
214
215
	static function is_sending() {
216
		return (bool) self::$is_sending;
217
	}
218
219
	static function set_is_sending( $is_sending ) {
220
		self::$is_sending = $is_sending;
221
	}
222
}
223