class.jetpack-sync-module-network-options.php
| 1 | <?php |
| 2 | |
| 3 | class Jetpack_Sync_Module_Network_Options extends Jetpack_Sync_Module { |
| 4 | private $network_options_whitelist; |
| 5 | |
| 6 | public function name() { |
| 7 | return 'network_options'; |
| 8 | } |
| 9 | |
| 10 | public function init_listeners( $callable ) { |
| 11 | if ( ! is_multisite() ) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | // multi site network options |
| 16 | add_action( 'add_site_option', $callable, 10, 2 ); |
| 17 | add_action( 'update_site_option', $callable, 10, 3 ); |
| 18 | add_action( 'delete_site_option', $callable, 10, 1 ); |
| 19 | |
| 20 | $whitelist_network_option_handler = array( $this, 'whitelist_network_options' ); |
| 21 | add_filter( 'jetpack_sync_before_enqueue_delete_site_option', $whitelist_network_option_handler ); |
| 22 | add_filter( 'jetpack_sync_before_enqueue_add_site_option', $whitelist_network_option_handler ); |
| 23 | add_filter( 'jetpack_sync_before_enqueue_update_site_option', $whitelist_network_option_handler ); |
| 24 | } |
| 25 | |
| 26 | public function init_full_sync_listeners( $callable ) { |
| 27 | add_action( 'jetpack_full_sync_network_options', $callable ); |
| 28 | } |
| 29 | |
| 30 | public function init_before_send() { |
| 31 | if ( ! is_multisite() ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | // full sync |
| 36 | add_filter( |
| 37 | 'jetpack_sync_before_send_jetpack_full_sync_network_options', |
| 38 | array( |
| 39 | $this, |
| 40 | 'expand_network_options', |
| 41 | ) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | public function set_defaults() { |
| 46 | $this->network_options_whitelist = Jetpack_Sync_Defaults::$default_network_options_whitelist; |
| 47 | } |
| 48 | |
| 49 | function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
| 50 | if ( ! is_multisite() ) { |
| 51 | return array( 0, true ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tells the client to sync all options to the server |
| 56 | * |
| 57 | * @since 4.2.0 |
| 58 | * |
| 59 | * @param boolean Whether to expand options (should always be true) |
| 60 | */ |
| 61 | do_action( 'jetpack_full_sync_network_options', true ); |
| 62 | |
| 63 | // The number of actions enqueued, and next module state (true == done) |
| 64 | return array( 1, true ); |
| 65 | } |
| 66 | |
| 67 | function estimate_full_sync_actions( $config ) { |
| 68 | if ( ! is_multisite() ) { |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | function get_full_sync_actions() { |
| 76 | return array( 'jetpack_full_sync_network_options' ); |
| 77 | } |
| 78 | |
| 79 | function get_all_network_options() { |
| 80 | $options = array(); |
| 81 | foreach ( $this->network_options_whitelist as $option ) { |
| 82 | $options[ $option ] = get_site_option( $option ); |
| 83 | } |
| 84 | |
| 85 | return $options; |
| 86 | } |
| 87 | |
| 88 | function set_network_options_whitelist( $options ) { |
| 89 | $this->network_options_whitelist = $options; |
| 90 | } |
| 91 | |
| 92 | function get_network_options_whitelist() { |
| 93 | return $this->network_options_whitelist; |
| 94 | } |
| 95 | |
| 96 | // reject non-whitelisted network options |
| 97 | function whitelist_network_options( $args ) { |
| 98 | if ( ! $this->is_whitelisted_network_option( $args[0] ) ) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | return $args; |
| 103 | } |
| 104 | |
| 105 | function is_whitelisted_network_option( $option ) { |
| 106 | return is_multisite() && in_array( $option, $this->network_options_whitelist ); |
| 107 | } |
| 108 | |
| 109 | public function expand_network_options( $args ) { |
| 110 | if ( $args[0] ) { |
| 111 | return $this->get_all_network_options(); |
| 112 | } |
| 113 | |
| 114 | return $args; |
| 115 | } |
| 116 | } |
| 117 |