class.jetpack-sync-settings.php 5.3 KB

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