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