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