Completed
Push — renovate/jest-monorepo ( bbafd9...36469d )
by
unknown
33:09 queued 24:56
created

Jetpack_Sync_Settings::get_setting()   C

Complexity

Conditions 13
Paths 147

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

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