Jetpack_Sync_Settings

The WordPress Core Jetpack Sync Settings class.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-settings.php  
  1. class Jetpack_Sync_Settings { 
  2. const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; 
  3.  
  4. static $valid_settings = array( 
  5. 'dequeue_max_bytes' => true,  
  6. 'upload_max_bytes' => true,  
  7. 'upload_max_rows' => true,  
  8. 'sync_wait_time' => true,  
  9. 'sync_wait_threshold' => true,  
  10. 'enqueue_wait_time' => true,  
  11. 'max_queue_size' => true,  
  12. 'max_queue_lag' => true,  
  13. 'queue_max_writes_sec' => true,  
  14. 'post_types_blacklist' => true,  
  15. 'disable' => true,  
  16. 'render_filtered_content' => true,  
  17. 'post_meta_whitelist' => true,  
  18. 'comment_meta_whitelist' => true,  
  19. 'max_enqueue_full_sync' => true,  
  20. 'max_queue_size_full_sync'=> true,  
  21. 'sync_via_cron' => true,  
  22. 'cron_sync_time_limit' => true,  
  23. ); 
  24.  
  25. static $is_importing; 
  26. static $is_doing_cron; 
  27. static $is_syncing; 
  28. static $is_sending; 
  29.  
  30. static $settings_cache = array(); // some settings can be expensive to compute - let's cache them 
  31.  
  32. static function get_settings() { 
  33. $settings = array(); 
  34. foreach ( array_keys( self::$valid_settings ) as $setting ) { 
  35. $settings[ $setting ] = self::get_setting( $setting ); 
  36.  
  37. return $settings; 
  38.  
  39. // Fetches the setting. It saves it if the setting doesn't exist, so that it gets 
  40. // autoloaded on page load rather than re-queried every time. 
  41. static function get_setting( $setting ) { 
  42. if ( ! isset( self::$valid_settings[ $setting ] ) ) { 
  43. return false; 
  44.  
  45. if ( isset( self::$settings_cache[ $setting ] ) ) { 
  46. return self::$settings_cache[ $setting ]; 
  47.  
  48. $value = get_option( self::SETTINGS_OPTION_PREFIX . $setting ); 
  49.  
  50. if ( false === $value ) { 
  51. $default_name = "default_$setting"; // e.g. default_dequeue_max_bytes 
  52. $value = Jetpack_Sync_Defaults::$$default_name; 
  53. update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); 
  54.  
  55. if ( is_numeric( $value ) ) { 
  56. $value = intval( $value ); 
  57. $default_array_value = null; 
  58. switch( $setting ) { 
  59. case 'post_types_blacklist': 
  60. $default_array_value = Jetpack_Sync_Defaults::$blacklisted_post_types; 
  61. break; 
  62. case 'post_meta_whitelist': 
  63. $default_array_value = Jetpack_Sync_Defaults::get_post_meta_whitelist(); 
  64. break; 
  65. case 'comment_meta_whitelist': 
  66. $default_array_value = Jetpack_Sync_Defaults::$comment_meta_whitelist; 
  67. break; 
  68.  
  69. if ( $default_array_value ) { 
  70. if ( is_array( $value ) ) { 
  71. $value = array_unique( array_merge( $value, $default_array_value ) ); 
  72. } else { 
  73. $value = $default_array_value; 
  74.  
  75. self::$settings_cache[ $setting ] = $value; 
  76.  
  77. return $value; 
  78.  
  79. static function update_settings( $new_settings ) { 
  80. $validated_settings = array_intersect_key( $new_settings, self::$valid_settings ); 
  81. foreach ( $validated_settings as $setting => $value ) { 
  82. update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); 
  83. unset( self::$settings_cache[ $setting ] ); 
  84.  
  85. // if we set the disabled option to true, clear the queues 
  86. if ( 'disable' === $setting && !! $value ) { 
  87. require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; 
  88. $listener = Jetpack_Sync_Listener::get_instance(); 
  89. $listener->get_sync_queue()->reset(); 
  90. $listener->get_full_sync_queue()->reset(); 
  91.  
  92. // returns escapted SQL that can be injected into a WHERE clause 
  93. static function get_blacklisted_post_types_sql() { 
  94. return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')'; 
  95.  
  96. static function get_whitelisted_post_meta_sql() { 
  97. return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')'; 
  98.  
  99. static function get_whitelisted_comment_meta_sql() { 
  100. return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')'; 
  101.  
  102. static function get_comments_filter_sql() { 
  103. return "comment_approved <> 'spam'"; 
  104.  
  105. static function reset_data() { 
  106. $valid_settings = self::$valid_settings; 
  107. self::$settings_cache = array(); 
  108. foreach ( $valid_settings as $option => $value ) { 
  109. delete_option( self::SETTINGS_OPTION_PREFIX . $option ); 
  110. self::set_importing( null ); 
  111. self::set_doing_cron( null ); 
  112. self::set_is_syncing( null ); 
  113. self::set_is_sending( null ); 
  114.  
  115. static function set_importing( $is_importing ) { 
  116. // set to NULL to revert to WP_IMPORTING, the standard behaviour 
  117. self::$is_importing = $is_importing; 
  118.  
  119. static function is_importing() { 
  120. if ( ! is_null( self::$is_importing ) ) { 
  121. return self::$is_importing; 
  122.  
  123. return defined( 'WP_IMPORTING' ) && WP_IMPORTING; 
  124.  
  125. static function set_doing_cron( $is_doing_cron ) { 
  126. // set to NULL to revert to WP_IMPORTING, the standard behaviour 
  127. self::$is_doing_cron = $is_doing_cron; 
  128.  
  129. static function is_doing_cron() { 
  130. if ( ! is_null( self::$is_doing_cron ) ) { 
  131. return self::$is_doing_cron; 
  132.  
  133. return defined( 'DOING_CRON' ) && DOING_CRON; 
  134.  
  135. static function is_syncing() { 
  136. return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ); 
  137.  
  138. static function set_is_syncing( $is_syncing ) { 
  139. self::$is_syncing = $is_syncing; 
  140.  
  141. static function is_sending() { 
  142. return (bool) self::$is_sending; 
  143.  
  144. static function set_is_sending( $is_sending ) { 
  145. self::$is_sending = $is_sending;