Completed
Push — branch-7.5 ( 9378b5...5f3718 )
by Jeremy
211:39 queued 203:47
created

Settings::get_setting()   C

Complexity

Conditions 14
Paths 183

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 183
nop 1
dl 0
loc 63
rs 5.575
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
		'disable'                  => true,
20
		'network_disable'          => true,
21
		'render_filtered_content'  => true,
22
		'post_meta_whitelist'      => true,
23
		'comment_meta_whitelist'   => true,
24
		'max_enqueue_full_sync'    => true,
25
		'max_queue_size_full_sync' => true,
26
		'sync_via_cron'            => true,
27
		'cron_sync_time_limit'     => true,
28
		'known_importers'          => true,
29
	);
30
31
	static $is_importing;
32
	static $is_doing_cron;
33
	static $is_syncing;
34
	static $is_sending;
35
36
	static $settings_cache = array(); // some settings can be expensive to compute - let's cache them
37
38
	static function get_settings() {
39
		$settings = array();
40
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
41
			$settings[ $setting ] = self::get_setting( $setting );
42
		}
43
44
		return $settings;
45
	}
46
47
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
48
	// autoloaded on page load rather than re-queried every time.
49
	static function get_setting( $setting ) {
50
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
51
			return false;
52
		}
53
54
		if ( isset( self::$settings_cache[ $setting ] ) ) {
55
			return self::$settings_cache[ $setting ];
56
		}
57
58
		if ( self::is_network_setting( $setting ) ) {
59
			if ( is_multisite() ) {
60
				$value = get_site_option( self::SETTINGS_OPTION_PREFIX . $setting );
61
			} else {
62
				// On single sites just return the default setting
63
				$value                            = Defaults::get_default_setting( $setting );
64
				self::$settings_cache[ $setting ] = $value;
65
				return $value;
66
			}
67
		} else {
68
			$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
69
		}
70
71
		if ( false === $value ) { // no default value is set.
72
			$value = Defaults::get_default_setting( $setting );
73
			if ( self::is_network_setting( $setting ) ) {
74
				update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
75
			} else {
76
				// We set one so that it gets autoloaded
77
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
78
			}
79
		}
80
81
		if ( is_numeric( $value ) ) {
82
			$value = intval( $value );
83
		}
84
		$default_array_value = null;
85
		switch ( $setting ) {
86
			case 'post_types_blacklist':
87
				$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...
88
				break;
89
			case 'post_meta_whitelist':
90
				$default_array_value = Defaults::get_post_meta_whitelist();
91
				break;
92
			case 'comment_meta_whitelist':
93
				$default_array_value = Defaults::get_comment_meta_whitelist();
94
				break;
95
			case 'known_importers':
96
				$default_array_value = Defaults::get_known_importers();
97
				break;
98
		}
99
100
		if ( $default_array_value ) {
101
			if ( is_array( $value ) ) {
102
				$value = array_unique( array_merge( $value, $default_array_value ) );
103
			} else {
104
				$value = $default_array_value;
105
			}
106
		}
107
108
		self::$settings_cache[ $setting ] = $value;
109
110
		return $value;
111
	}
112
113
	static function update_settings( $new_settings ) {
114
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
115
		foreach ( $validated_settings as $setting => $value ) {
116
117
			if ( self::is_network_setting( $setting ) ) {
118
				if ( is_multisite() && is_main_site() ) {
119
					update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
120
				}
121
			} else {
122
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
123
			}
124
125
			unset( self::$settings_cache[ $setting ] );
126
127
			// if we set the disabled option to true, clear the queues
128
			if ( ( 'disable' === $setting || 'network_disable' === $setting ) && ! ! $value ) {
129
				$listener = Listener::get_instance();
130
				$listener->get_sync_queue()->reset();
131
				$listener->get_full_sync_queue()->reset();
132
			}
133
		}
134
	}
135
136
	static function is_network_setting( $setting ) {
137
		return strpos( $setting, 'network_' ) === 0;
138
	}
139
140
	// returns escapted SQL that can be injected into a WHERE clause
141
	static function get_blacklisted_post_types_sql() {
142
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
143
	}
144
145
	static function get_whitelisted_post_meta_sql() {
146
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')';
147
	}
148
149
	static function get_whitelisted_comment_meta_sql() {
150
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')';
151
	}
152
153
	static function get_comments_filter_sql() {
154
		return "comment_approved <> 'spam'";
155
	}
156
157
	static function reset_data() {
158
		$valid_settings       = self::$valid_settings;
159
		self::$settings_cache = array();
160
		foreach ( $valid_settings as $option => $value ) {
161
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
162
		}
163
		self::set_importing( null );
164
		self::set_doing_cron( null );
165
		self::set_is_syncing( null );
166
		self::set_is_sending( null );
167
	}
168
169
	static function set_importing( $is_importing ) {
170
		// set to NULL to revert to WP_IMPORTING, the standard behavior
171
		self::$is_importing = $is_importing;
172
	}
173
174
	static function is_importing() {
175
		if ( ! is_null( self::$is_importing ) ) {
176
			return self::$is_importing;
177
		}
178
179
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
180
	}
181
182
	static function is_sync_enabled() {
183
		return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) );
184
	}
185
186
	static function set_doing_cron( $is_doing_cron ) {
187
		// set to NULL to revert to WP_IMPORTING, the standard behavior
188
		self::$is_doing_cron = $is_doing_cron;
189
	}
190
191
	static function is_doing_cron() {
192
		if ( ! is_null( self::$is_doing_cron ) ) {
193
			return self::$is_doing_cron;
194
		}
195
196
		return defined( 'DOING_CRON' ) && DOING_CRON;
197
	}
198
199
	static function is_syncing() {
200
		return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST );
201
	}
202
203
	static function set_is_syncing( $is_syncing ) {
204
		self::$is_syncing = $is_syncing;
205
	}
206
207
	static function is_sending() {
208
		return (bool) self::$is_sending;
209
	}
210
211
	static function set_is_sending( $is_sending ) {
212
		self::$is_sending = $is_sending;
213
	}
214
}
215