class.jetpack-sync-module-options.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. class Jetpack_Sync_Module_Options extends Jetpack_Sync_Module {
  3. private $options_whitelist, $options_contentless;
  4. public function name() {
  5. return 'options';
  6. }
  7. public function init_listeners( $callable ) {
  8. // options
  9. add_action( 'added_option', $callable, 10, 2 );
  10. add_action( 'updated_option', $callable, 10, 3 );
  11. add_action( 'deleted_option', $callable, 10, 1 );
  12. // Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
  13. add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
  14. add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
  15. add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
  16. $whitelist_option_handler = array( $this, 'whitelist_options' );
  17. add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
  18. add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
  19. add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler );
  20. }
  21. public function init_full_sync_listeners( $callable ) {
  22. add_action( 'jetpack_full_sync_options', $callable );
  23. }
  24. public function init_before_send() {
  25. // full sync
  26. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) );
  27. }
  28. public function set_defaults() {
  29. $this->update_options_whitelist();
  30. $this->update_options_contentless();
  31. }
  32. public function set_late_default() {
  33. /** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
  34. $late_options = apply_filters( 'jetpack_options_whitelist', array() );
  35. if ( ! empty( $late_options ) && is_array( $late_options ) ) {
  36. $this->options_whitelist = array_merge( $this->options_whitelist, $late_options );
  37. }
  38. }
  39. function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
  40. /**
  41. * Tells the client to sync all options to the server
  42. *
  43. * @since 4.2.0
  44. *
  45. * @param boolean Whether to expand options (should always be true)
  46. */
  47. do_action( 'jetpack_full_sync_options', true );
  48. // The number of actions enqueued, and next module state (true == done)
  49. return array( 1, true );
  50. }
  51. public function estimate_full_sync_actions( $config ) {
  52. return 1;
  53. }
  54. function get_full_sync_actions() {
  55. return array( 'jetpack_full_sync_options' );
  56. }
  57. // Is public so that we don't have to store so much data all the options twice.
  58. function get_all_options() {
  59. $options = array();
  60. $random_string = wp_generate_password();
  61. foreach ( $this->options_whitelist as $option ) {
  62. $option_value = get_option( $option, $random_string );
  63. if ( $option_value !== $random_string ) {
  64. $options[ $option ] = $option_value;
  65. }
  66. }
  67. // add theme mods
  68. $theme_mods_option = 'theme_mods_'.get_option( 'stylesheet' );
  69. $theme_mods_value = get_option( $theme_mods_option, $random_string );
  70. if ( $theme_mods_value === $random_string ) {
  71. return $options;
  72. }
  73. $this->filter_theme_mods( $theme_mods_value );
  74. $options[ $theme_mods_option ] = $theme_mods_value;
  75. return $options;
  76. }
  77. function update_options_whitelist() {
  78. $this->options_whitelist = Jetpack_Sync_Defaults::get_options_whitelist();
  79. }
  80. function set_options_whitelist( $options ) {
  81. $this->options_whitelist = $options;
  82. }
  83. function get_options_whitelist() {
  84. return $this->options_whitelist;
  85. }
  86. function update_options_contentless() {
  87. $this->options_contentless = Jetpack_Sync_Defaults::get_options_contentless();
  88. }
  89. function get_options_contentless() {
  90. return $this->options_contentless;
  91. }
  92. function whitelist_options( $args ) {
  93. // Reject non-whitelisted options
  94. if ( ! $this->is_whitelisted_option( $args[0] ) ) {
  95. return false;
  96. }
  97. // filter our weird array( false ) value for theme_mods_*
  98. if ( 'theme_mods_' === substr( $args[0], 0, 11 ) ) {
  99. $this->filter_theme_mods( $args[1] );
  100. if ( isset( $args[2] ) ) {
  101. $this->filter_theme_mods( $args[2] );
  102. }
  103. }
  104. // Set value(s) of contentless option to empty string(s)
  105. if ( $this->is_contentless_option( $args[0] ) ) {
  106. // Create a new array matching length of $args, containing empty strings
  107. $empty = array_fill( 0, count( $args ), '' );
  108. $empty[0] = $args[0];
  109. return $empty;
  110. }
  111. return $args;
  112. }
  113. function is_whitelisted_option( $option ) {
  114. return in_array( $option, $this->options_whitelist ) || 'theme_mods_' === substr( $option, 0, 11 );
  115. }
  116. private function is_contentless_option( $option ) {
  117. return in_array( $option, $this->options_contentless );
  118. }
  119. private function filter_theme_mods( &$value ) {
  120. if ( is_array( $value ) && isset( $value[0] ) ) {
  121. unset( $value[0] );
  122. }
  123. }
  124. function jetpack_sync_core_icon() {
  125. if ( function_exists( 'get_site_icon_url' ) ) {
  126. $url = get_site_icon_url();
  127. } else {
  128. return;
  129. }
  130. require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
  131. // If there's a core icon, maybe update the option. If not, fall back to Jetpack's.
  132. if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
  133. // This is the option that is synced with dotcom
  134. Jetpack_Options::update_option( 'site_icon_url', $url );
  135. } else if ( empty( $url ) ) {
  136. Jetpack_Options::delete_option( 'site_icon_url' );
  137. }
  138. }
  139. public function expand_options( $args ) {
  140. if ( $args[0] ) {
  141. return $this->get_all_options();
  142. }
  143. return $args;
  144. }
  145. }