Completed
Push — psr4-jetpack-sync-module ( 862b11...910f64 )
by
unknown
277:16 queued 269:36
created

Network_Options   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 114
c 0
b 0
f 0
rs 10
wmc 22
lcom 1
cbo 1
1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
5
use Automattic\Jetpack\Sync\Defaults;
6
^
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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