Completed
Push — update/sync-package ( 660c46 )
by
unknown
08:05
created

sync/legacy/class.jetpack-sync-settings.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class Jetpack_Sync_Settings {
4
	const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_';
5
6
	static $valid_settings = array(
7
		'dequeue_max_bytes'        => true,
8
		'upload_max_bytes'         => true,
9
		'upload_max_rows'          => true,
10
		'sync_wait_time'           => true,
11
		'sync_wait_threshold'      => true,
12
		'enqueue_wait_time'        => true,
13
		'max_queue_size'           => true,
14
		'max_queue_lag'            => true,
15
		'queue_max_writes_sec'     => true,
16
		'post_types_blacklist'     => true,
17
		'disable'                  => true,
18
		'network_disable'          => true,
19
		'render_filtered_content'  => true,
20
		'post_meta_whitelist'      => true,
21
		'comment_meta_whitelist'   => true,
22
		'max_enqueue_full_sync'    => true,
23
		'max_queue_size_full_sync' => true,
24
		'sync_via_cron'            => true,
25
		'cron_sync_time_limit'     => true,
26
		'known_importers'          => true,
27
	);
28
29
	static $is_importing;
30
	static $is_doing_cron;
31
	static $is_syncing;
32
	static $is_sending;
33
34
	static $settings_cache = array(); // some settings can be expensive to compute - let's cache them
35
36
	static function get_settings() {
37
		$settings = array();
38
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
39
			$settings[ $setting ] = self::get_setting( $setting );
40
		}
41
42
		return $settings;
43
	}
44
45
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
46
	// autoloaded on page load rather than re-queried every time.
47
	static function get_setting( $setting ) {
48
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
49
			return false;
50
		}
51
52
		if ( isset( self::$settings_cache[ $setting ] ) ) {
53
			return self::$settings_cache[ $setting ];
54
		}
55
56
		if ( self::is_network_setting( $setting ) ) {
57
			if ( is_multisite() ) {
58
				$value = get_site_option( self::SETTINGS_OPTION_PREFIX . $setting );
59
			} else {
60
				// On single sites just return the default setting
61
				$value                            = Jetpack_Sync_Defaults::get_default_setting( $setting );
62
				self::$settings_cache[ $setting ] = $value;
63
				return $value;
64
			}
65
		} else {
66
			$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
67
		}
68
69
		if ( false === $value ) { // no default value is set.
70
			$value = Jetpack_Sync_Defaults::get_default_setting( $setting );
71
			if ( self::is_network_setting( $setting ) ) {
72
				update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
73
			} else {
74
				// We set one so that it gets autoloaded
75
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
76
			}
77
		}
78
79
		if ( is_numeric( $value ) ) {
80
			$value = intval( $value );
81
		}
82
		$default_array_value = null;
83
		switch ( $setting ) {
84
			case 'post_types_blacklist':
85
				$default_array_value = Jetpack_Sync_Defaults::$blacklisted_post_types;
86
				break;
87
			case 'post_meta_whitelist':
88
				$default_array_value = Jetpack_Sync_Defaults::get_post_meta_whitelist();
89
				break;
90
			case 'comment_meta_whitelist':
91
				$default_array_value = Jetpack_Sync_Defaults::get_comment_meta_whitelist();
92
				break;
93
			case 'known_importers':
94
				$default_array_value = Jetpack_Sync_Defaults::get_known_importers();
95
				break;
96
		}
97
98
		if ( $default_array_value ) {
99
			if ( is_array( $value ) ) {
100
				$value = array_unique( array_merge( $value, $default_array_value ) );
101
			} else {
102
				$value = $default_array_value;
103
			}
104
		}
105
106
		self::$settings_cache[ $setting ] = $value;
107
108
		return $value;
109
	}
110
111
	static function update_settings( $new_settings ) {
112
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
113
		foreach ( $validated_settings as $setting => $value ) {
114
115
			if ( self::is_network_setting( $setting ) ) {
116
				if ( is_multisite() && is_main_site() ) {
117
					update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value );
118
				}
119
			} else {
120
				update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
121
			}
122
123
			unset( self::$settings_cache[ $setting ] );
124
125
			// if we set the disabled option to true, clear the queues
126
			if ( ( 'disable' === $setting || 'network_disable' === $setting ) && ! ! $value ) {
127
				$listener = Jetpack_Sync_Listener::get_instance();
0 ignored issues
show
The method get_instance() does not seem to exist on object<Jetpack_Sync_Listener>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
				$listener->get_sync_queue()->reset();
129
				$listener->get_full_sync_queue()->reset();
130
			}
131
		}
132
	}
133
134
	static function is_network_setting( $setting ) {
135
		return strpos( $setting, 'network_' ) === 0;
136
	}
137
138
	// returns escapted SQL that can be injected into a WHERE clause
139
	static function get_blacklisted_post_types_sql() {
140
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
141
	}
142
143
	static function get_whitelisted_post_meta_sql() {
144
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')';
145
	}
146
147
	static function get_whitelisted_comment_meta_sql() {
148
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')';
149
	}
150
151
	static function get_comments_filter_sql() {
152
		return "comment_approved <> 'spam'";
153
	}
154
155
	static function reset_data() {
156
		$valid_settings       = self::$valid_settings;
157
		self::$settings_cache = array();
158
		foreach ( $valid_settings as $option => $value ) {
159
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
160
		}
161
		self::set_importing( null );
162
		self::set_doing_cron( null );
163
		self::set_is_syncing( null );
164
		self::set_is_sending( null );
165
	}
166
167
	static function set_importing( $is_importing ) {
168
		// set to NULL to revert to WP_IMPORTING, the standard behavior
169
		self::$is_importing = $is_importing;
170
	}
171
172
	static function is_importing() {
173
		if ( ! is_null( self::$is_importing ) ) {
174
			return self::$is_importing;
175
		}
176
177
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
178
	}
179
180
	static function is_sync_enabled() {
181
		return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) );
182
	}
183
184
	static function set_doing_cron( $is_doing_cron ) {
185
		// set to NULL to revert to WP_IMPORTING, the standard behavior
186
		self::$is_doing_cron = $is_doing_cron;
187
	}
188
189
	static function is_doing_cron() {
190
		if ( ! is_null( self::$is_doing_cron ) ) {
191
			return self::$is_doing_cron;
192
		}
193
194
		return defined( 'DOING_CRON' ) && DOING_CRON;
195
	}
196
197
	static function is_syncing() {
198
		return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST );
199
	}
200
201
	static function set_is_syncing( $is_syncing ) {
202
		self::$is_syncing = $is_syncing;
203
	}
204
205
	static function is_sending() {
206
		return (bool) self::$is_sending;
207
	}
208
209
	static function set_is_sending( $is_sending ) {
210
		self::$is_sending = $is_sending;
211
	}
212
}
213