class.jetpack-sync-module-network-options.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. class Jetpack_Sync_Module_Network_Options extends Jetpack_Sync_Module {
  3. private $network_options_whitelist;
  4. public function name() {
  5. return 'network_options';
  6. }
  7. public function init_listeners( $callable ) {
  8. if ( ! is_multisite() ) {
  9. return;
  10. }
  11. // multi site network options
  12. add_action( 'add_site_option', $callable, 10, 2 );
  13. add_action( 'update_site_option', $callable, 10, 3 );
  14. add_action( 'delete_site_option', $callable, 10, 1 );
  15. $whitelist_network_option_handler = array( $this, 'whitelist_network_options' );
  16. add_filter( 'jetpack_sync_before_enqueue_delete_site_option', $whitelist_network_option_handler );
  17. add_filter( 'jetpack_sync_before_enqueue_add_site_option', $whitelist_network_option_handler );
  18. add_filter( 'jetpack_sync_before_enqueue_update_site_option', $whitelist_network_option_handler );
  19. }
  20. public function init_full_sync_listeners( $callable ) {
  21. add_action( 'jetpack_full_sync_network_options', $callable );
  22. }
  23. public function init_before_send() {
  24. if ( ! is_multisite() ) {
  25. return;
  26. }
  27. // full sync
  28. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_network_options', array(
  29. $this,
  30. 'expand_network_options',
  31. ) );
  32. }
  33. public function set_defaults() {
  34. $this->network_options_whitelist = Jetpack_Sync_Defaults::$default_network_options_whitelist;
  35. }
  36. function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
  37. if ( ! is_multisite() ) {
  38. return array( 0, true );
  39. }
  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_network_options', true );
  48. // The number of actions enqueued, and next module state (true == done)
  49. return array( 1, true );
  50. }
  51. function estimate_full_sync_actions( $config ) {
  52. if ( ! is_multisite() ) {
  53. return 0;
  54. }
  55. return 1;
  56. }
  57. function get_full_sync_actions() {
  58. return array( 'jetpack_full_sync_network_options' );
  59. }
  60. function get_all_network_options() {
  61. $options = array();
  62. foreach ( $this->network_options_whitelist as $option ) {
  63. $options[ $option ] = get_site_option( $option );
  64. }
  65. return $options;
  66. }
  67. function set_network_options_whitelist( $options ) {
  68. $this->network_options_whitelist = $options;
  69. }
  70. function get_network_options_whitelist() {
  71. return $this->network_options_whitelist;
  72. }
  73. // reject non-whitelisted network options
  74. function whitelist_network_options( $args ) {
  75. if ( ! $this->is_whitelisted_network_option( $args[0] ) ) {
  76. return false;
  77. }
  78. return $args;
  79. }
  80. function is_whitelisted_network_option( $option ) {
  81. return is_multisite() && in_array( $option, $this->network_options_whitelist );
  82. }
  83. public function expand_network_options( $args ) {
  84. if ( $args[0] ) {
  85. return $this->get_all_network_options();
  86. }
  87. return $args;
  88. }
  89. }