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
|
|
|
); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Whether WordPress is currently running an import. |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
* @static |
60
|
|
|
* |
61
|
|
|
* @var null|boolean |
62
|
|
|
*/ |
63
|
|
|
public static $is_importing; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Whether WordPress is currently running a WP cron request. |
67
|
|
|
* |
68
|
|
|
* @access public |
69
|
|
|
* @static |
70
|
|
|
* |
71
|
|
|
* @var null|boolean |
72
|
|
|
*/ |
73
|
|
|
public static $is_doing_cron; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Whether we're currently syncing. |
77
|
|
|
* |
78
|
|
|
* @access public |
79
|
|
|
* @static |
80
|
|
|
* |
81
|
|
|
* @var null|boolean |
82
|
|
|
*/ |
83
|
|
|
public static $is_syncing; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Whether we're currently sending sync items. |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @static |
90
|
|
|
* |
91
|
|
|
* @var null|boolean |
92
|
|
|
*/ |
93
|
|
|
public static $is_sending; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Some settings can be expensive to compute - let's cache them. |
97
|
|
|
* |
98
|
|
|
* @access public |
99
|
|
|
* @static |
100
|
|
|
* |
101
|
|
|
* @var array |
102
|
|
|
*/ |
103
|
|
|
public static $settings_cache = array(); |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieve all settings with their current values. |
107
|
|
|
* |
108
|
|
|
* @access public |
109
|
|
|
* @static |
110
|
|
|
* |
111
|
|
|
* @return array All current settings. |
112
|
|
|
*/ |
113
|
|
|
public static function get_settings() { |
114
|
|
|
$settings = array(); |
115
|
|
|
foreach ( array_keys( self::$valid_settings ) as $setting ) { |
116
|
|
|
$settings[ $setting ] = self::get_setting( $setting ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $settings; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
124
|
|
|
* autoloaded on page load rather than re-queried every time. |
125
|
|
|
* |
126
|
|
|
* @access public |
127
|
|
|
* @static |
128
|
|
|
* |
129
|
|
|
* @param string $setting The setting name. |
130
|
|
|
* @return mixed The setting value. |
131
|
|
|
*/ |
132
|
|
|
public static function get_setting( $setting ) { |
133
|
|
|
if ( ! isset( self::$valid_settings[ $setting ] ) ) { |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ( isset( self::$settings_cache[ $setting ] ) ) { |
138
|
|
|
return self::$settings_cache[ $setting ]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ( self::is_network_setting( $setting ) ) { |
142
|
|
|
if ( is_multisite() ) { |
143
|
|
|
$value = get_site_option( self::SETTINGS_OPTION_PREFIX . $setting ); |
144
|
|
|
} else { |
145
|
|
|
// On single sites just return the default setting. |
146
|
|
|
$value = Defaults::get_default_setting( $setting ); |
147
|
|
|
self::$settings_cache[ $setting ] = $value; |
148
|
|
|
return $value; |
149
|
|
|
} |
150
|
|
|
} else { |
151
|
|
|
$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ( false === $value ) { // No default value is set. |
155
|
|
|
$value = Defaults::get_default_setting( $setting ); |
156
|
|
|
if ( self::is_network_setting( $setting ) ) { |
157
|
|
|
update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value ); |
158
|
|
|
} else { |
159
|
|
|
// We set one so that it gets autoloaded. |
160
|
|
|
update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ( is_numeric( $value ) ) { |
165
|
|
|
$value = intval( $value ); |
166
|
|
|
} |
167
|
|
|
$default_array_value = null; |
168
|
|
|
switch ( $setting ) { |
169
|
|
|
case 'post_types_blacklist': |
170
|
|
|
$default_array_value = Defaults::$blacklisted_post_types; |
|
|
|
|
171
|
|
|
break; |
172
|
|
|
case 'taxonomies_blacklist': |
173
|
|
|
$default_array_value = Defaults::$blacklisted_taxonomies; |
174
|
|
|
break; |
175
|
|
|
case 'post_meta_whitelist': |
176
|
|
|
$default_array_value = Defaults::get_post_meta_whitelist(); |
177
|
|
|
break; |
178
|
|
|
case 'comment_meta_whitelist': |
179
|
|
|
$default_array_value = Defaults::get_comment_meta_whitelist(); |
180
|
|
|
break; |
181
|
|
|
case 'known_importers': |
182
|
|
|
$default_array_value = Defaults::get_known_importers(); |
183
|
|
|
break; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ( $default_array_value ) { |
187
|
|
|
if ( is_array( $value ) ) { |
188
|
|
|
$value = array_unique( array_merge( $value, $default_array_value ) ); |
189
|
|
|
} else { |
190
|
|
|
$value = $default_array_value; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
self::$settings_cache[ $setting ] = $value; |
195
|
|
|
|
196
|
|
|
return $value; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Change multiple settings in the same time. |
201
|
|
|
* |
202
|
|
|
* @access public |
203
|
|
|
* @static |
204
|
|
|
* |
205
|
|
|
* @param array $new_settings The new settings. |
206
|
|
|
*/ |
207
|
|
|
public static function update_settings( $new_settings ) { |
208
|
|
|
$validated_settings = array_intersect_key( $new_settings, self::$valid_settings ); |
209
|
|
|
foreach ( $validated_settings as $setting => $value ) { |
210
|
|
|
|
211
|
|
|
if ( self::is_network_setting( $setting ) ) { |
212
|
|
|
if ( is_multisite() && is_main_site() ) { |
213
|
|
|
update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value ); |
214
|
|
|
} |
215
|
|
|
} else { |
216
|
|
|
update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
unset( self::$settings_cache[ $setting ] ); |
220
|
|
|
|
221
|
|
|
// If we set the disabled option to true, clear the queues. |
222
|
|
|
if ( ( 'disable' === $setting || 'network_disable' === $setting ) && ! ! $value ) { |
223
|
|
|
$listener = Listener::get_instance(); |
224
|
|
|
$listener->get_sync_queue()->reset(); |
225
|
|
|
$listener->get_full_sync_queue()->reset(); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Whether the specified setting is a network setting. |
232
|
|
|
* |
233
|
|
|
* @access public |
234
|
|
|
* @static |
235
|
|
|
* |
236
|
|
|
* @param string $setting Setting name. |
237
|
|
|
* @return boolean Whether the setting is a network setting. |
238
|
|
|
*/ |
239
|
|
|
public static function is_network_setting( $setting ) { |
240
|
|
|
return strpos( $setting, 'network_' ) === 0; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns escaped SQL for blacklisted post types. |
245
|
|
|
* Can be injected directly into a WHERE clause. |
246
|
|
|
* |
247
|
|
|
* @access public |
248
|
|
|
* @static |
249
|
|
|
* |
250
|
|
|
* @return string SQL WHERE clause. |
251
|
|
|
*/ |
252
|
|
|
public static function get_blacklisted_post_types_sql() { |
253
|
|
|
return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Returns escaped SQL for blacklisted taxonomies. |
258
|
|
|
* Can be injected directly into a WHERE clause. |
259
|
|
|
* |
260
|
|
|
* @access public |
261
|
|
|
* @static |
262
|
|
|
* |
263
|
|
|
* @return string SQL WHERE clause. |
264
|
|
|
*/ |
265
|
|
|
public static function get_blacklisted_taxonomies_sql() { |
266
|
|
|
return 'taxonomy NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'taxonomies_blacklist' ) ) ) . '\')'; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns escaped SQL for blacklisted post meta. |
271
|
|
|
* Can be injected directly into a WHERE clause. |
272
|
|
|
* |
273
|
|
|
* @access public |
274
|
|
|
* @static |
275
|
|
|
* |
276
|
|
|
* @return string SQL WHERE clause. |
277
|
|
|
*/ |
278
|
|
|
public static function get_whitelisted_post_meta_sql() { |
279
|
|
|
return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')'; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns escaped SQL for blacklisted comment meta. |
284
|
|
|
* Can be injected directly into a WHERE clause. |
285
|
|
|
* |
286
|
|
|
* @access public |
287
|
|
|
* @static |
288
|
|
|
* |
289
|
|
|
* @return string SQL WHERE clause. |
290
|
|
|
*/ |
291
|
|
|
public static function get_whitelisted_comment_meta_sql() { |
292
|
|
|
return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')'; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Returns escaped SQL for comments, excluding any spam comments. |
297
|
|
|
* Can be injected directly into a WHERE clause. |
298
|
|
|
* |
299
|
|
|
* @access public |
300
|
|
|
* @static |
301
|
|
|
* |
302
|
|
|
* @return string SQL WHERE clause. |
303
|
|
|
*/ |
304
|
|
|
public static function get_comments_filter_sql() { |
305
|
|
|
return "comment_approved <> 'spam'"; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Delete any settings options and clean up the current settings state. |
310
|
|
|
* |
311
|
|
|
* @access public |
312
|
|
|
* @static |
313
|
|
|
*/ |
314
|
|
|
public static function reset_data() { |
315
|
|
|
$valid_settings = self::$valid_settings; |
316
|
|
|
self::$settings_cache = array(); |
317
|
|
|
foreach ( $valid_settings as $option => $value ) { |
318
|
|
|
delete_option( self::SETTINGS_OPTION_PREFIX . $option ); |
319
|
|
|
} |
320
|
|
|
self::set_importing( null ); |
|
|
|
|
321
|
|
|
self::set_doing_cron( null ); |
|
|
|
|
322
|
|
|
self::set_is_syncing( null ); |
|
|
|
|
323
|
|
|
self::set_is_sending( null ); |
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Set the importing state. |
328
|
|
|
* |
329
|
|
|
* @access public |
330
|
|
|
* @static |
331
|
|
|
* |
332
|
|
|
* @param boolean $is_importing Whether WordPress is currently importing. |
333
|
|
|
*/ |
334
|
|
|
public static function set_importing( $is_importing ) { |
335
|
|
|
// Set to NULL to revert to WP_IMPORTING, the standard behavior. |
336
|
|
|
self::$is_importing = $is_importing; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Whether WordPress is currently importing. |
341
|
|
|
* |
342
|
|
|
* @access public |
343
|
|
|
* @static |
344
|
|
|
* |
345
|
|
|
* @return boolean Whether WordPress is currently importing. |
346
|
|
|
*/ |
347
|
|
|
public static function is_importing() { |
348
|
|
|
if ( ! is_null( self::$is_importing ) ) { |
349
|
|
|
return self::$is_importing; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return defined( 'WP_IMPORTING' ) && WP_IMPORTING; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Whether sync is enabled. |
357
|
|
|
* |
358
|
|
|
* @access public |
359
|
|
|
* @static |
360
|
|
|
* |
361
|
|
|
* @return boolean Whether sync is enabled. |
362
|
|
|
*/ |
363
|
|
|
public static function is_sync_enabled() { |
364
|
|
|
return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) ); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set the WP cron state. |
369
|
|
|
* |
370
|
|
|
* @access public |
371
|
|
|
* @static |
372
|
|
|
* |
373
|
|
|
* @param boolean $is_doing_cron Whether WordPress is currently doing WP cron. |
374
|
|
|
*/ |
375
|
|
|
public static function set_doing_cron( $is_doing_cron ) { |
376
|
|
|
// Set to NULL to revert to WP_IMPORTING, the standard behavior. |
377
|
|
|
self::$is_doing_cron = $is_doing_cron; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Whether WordPress is currently doing WP cron. |
382
|
|
|
* |
383
|
|
|
* @access public |
384
|
|
|
* @static |
385
|
|
|
* |
386
|
|
|
* @return boolean Whether WordPress is currently doing WP cron. |
387
|
|
|
*/ |
388
|
|
|
public static function is_doing_cron() { |
389
|
|
|
if ( ! is_null( self::$is_doing_cron ) ) { |
390
|
|
|
return self::$is_doing_cron; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
return defined( 'DOING_CRON' ) && DOING_CRON; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Whether we are currently syncing. |
398
|
|
|
* |
399
|
|
|
* @access public |
400
|
|
|
* @static |
401
|
|
|
* |
402
|
|
|
* @return boolean Whether we are currently syncing. |
403
|
|
|
*/ |
404
|
|
|
public static function is_syncing() { |
405
|
|
|
return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Set the syncing state. |
410
|
|
|
* |
411
|
|
|
* @access public |
412
|
|
|
* @static |
413
|
|
|
* |
414
|
|
|
* @param boolean $is_syncing Whether we are currently syncing. |
415
|
|
|
*/ |
416
|
|
|
public static function set_is_syncing( $is_syncing ) { |
417
|
|
|
self::$is_syncing = $is_syncing; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Whether we are currently sending sync items. |
422
|
|
|
* |
423
|
|
|
* @access public |
424
|
|
|
* @static |
425
|
|
|
* |
426
|
|
|
* @return boolean Whether we are currently sending sync items. |
427
|
|
|
*/ |
428
|
|
|
public static function is_sending() { |
429
|
|
|
return (bool) self::$is_sending; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Set the sending state. |
434
|
|
|
* |
435
|
|
|
* @access public |
436
|
|
|
* @static |
437
|
|
|
* |
438
|
|
|
* @param boolean $is_sending Whether we are currently sending sync items. |
439
|
|
|
*/ |
440
|
|
|
public static function set_is_sending( $is_sending ) { |
441
|
|
|
self::$is_sending = $is_sending; |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|
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.