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
|
|
|
'render_filtered_content' => true, |
21
|
|
|
'post_meta_whitelist' => true, |
22
|
|
|
'comment_meta_whitelist' => true, |
23
|
|
|
'max_enqueue_full_sync' => true, |
24
|
|
|
'max_queue_size_full_sync' => true, |
25
|
|
|
'sync_via_cron' => true, |
26
|
|
|
'cron_sync_time_limit' => true, |
27
|
|
|
'known_importers' => 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
|
|
|
$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting ); |
58
|
|
|
|
59
|
|
|
if ( false === $value ) { |
60
|
|
|
$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes |
61
|
|
|
$value = Jetpack_Sync_Defaults::$$default_name; |
62
|
|
|
update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ( is_numeric( $value ) ) { |
66
|
|
|
$value = intval( $value ); |
67
|
|
|
} |
68
|
|
|
$default_array_value = null; |
69
|
|
|
switch ( $setting ) { |
70
|
|
|
case 'post_types_blacklist': |
71
|
|
|
$default_array_value = Jetpack_Sync_Defaults::$blacklisted_post_types; |
|
|
|
|
72
|
|
|
break; |
73
|
|
|
case 'post_meta_whitelist': |
74
|
|
|
$default_array_value = Jetpack_Sync_Defaults::get_post_meta_whitelist(); |
75
|
|
|
break; |
76
|
|
|
case 'comment_meta_whitelist': |
77
|
|
|
$default_array_value = Jetpack_Sync_Defaults::get_comment_meta_whitelist(); |
78
|
|
|
break; |
79
|
|
|
case 'known_importers': |
80
|
|
|
$default_array_value = Jetpack_Sync_Defaults::get_known_importers(); |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ( $default_array_value ) { |
85
|
|
|
if ( is_array( $value ) ) { |
86
|
|
|
$value = array_unique( array_merge( $value, $default_array_value ) ); |
87
|
|
|
} else { |
88
|
|
|
$value = $default_array_value; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
self::$settings_cache[ $setting ] = $value; |
93
|
|
|
|
94
|
|
|
return $value; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
static function update_settings( $new_settings ) { |
98
|
|
|
$validated_settings = array_intersect_key( $new_settings, self::$valid_settings ); |
99
|
|
|
foreach ( $validated_settings as $setting => $value ) { |
100
|
|
|
update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
101
|
|
|
unset( self::$settings_cache[ $setting ] ); |
102
|
|
|
|
103
|
|
|
// if we set the disabled option to true, clear the queues |
104
|
|
|
if ( 'disable' === $setting && ! ! $value ) { |
105
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
106
|
|
|
$listener = Jetpack_Sync_Listener::get_instance(); |
107
|
|
|
$listener->get_sync_queue()->reset(); |
108
|
|
|
$listener->get_full_sync_queue()->reset(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// returns escapted SQL that can be injected into a WHERE clause |
114
|
|
|
static function get_blacklisted_post_types_sql() { |
115
|
|
|
return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
static function get_whitelisted_post_meta_sql() { |
119
|
|
|
return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
static function get_whitelisted_comment_meta_sql() { |
123
|
|
|
return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
static function get_comments_filter_sql() { |
127
|
|
|
return "comment_approved <> 'spam'"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
static function reset_data() { |
131
|
|
|
$valid_settings = self::$valid_settings; |
132
|
|
|
self::$settings_cache = array(); |
133
|
|
|
foreach ( $valid_settings as $option => $value ) { |
134
|
|
|
delete_option( self::SETTINGS_OPTION_PREFIX . $option ); |
135
|
|
|
} |
136
|
|
|
self::set_importing( null ); |
137
|
|
|
self::set_doing_cron( null ); |
138
|
|
|
self::set_is_syncing( null ); |
139
|
|
|
self::set_is_sending( null ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
static function set_importing( $is_importing ) { |
143
|
|
|
// set to NULL to revert to WP_IMPORTING, the standard behavior |
144
|
|
|
self::$is_importing = $is_importing; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
static function is_importing() { |
148
|
|
|
if ( ! is_null( self::$is_importing ) ) { |
149
|
|
|
return self::$is_importing; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return defined( 'WP_IMPORTING' ) && WP_IMPORTING; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
static function set_doing_cron( $is_doing_cron ) { |
156
|
|
|
// set to NULL to revert to WP_IMPORTING, the standard behavior |
157
|
|
|
self::$is_doing_cron = $is_doing_cron; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
static function is_doing_cron() { |
161
|
|
|
if ( ! is_null( self::$is_doing_cron ) ) { |
162
|
|
|
return self::$is_doing_cron; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return defined( 'DOING_CRON' ) && DOING_CRON; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
static function is_syncing() { |
169
|
|
|
return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
static function set_is_syncing( $is_syncing ) { |
173
|
|
|
self::$is_syncing = $is_syncing; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
static function is_sending() { |
177
|
|
|
return (bool) self::$is_sending; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
static function set_is_sending( $is_sending ) { |
181
|
|
|
self::$is_sending = $is_sending; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
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.