Completed
Push — update/non-admin-view ( b1ee9d...762240 )
by
unknown
10:19
created

Jetpack_Sync_Module_Network_Options::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
		// full sync
21
		add_action( 'jetpack_full_sync_network_options', $callable );
22
23
		$whitelist_network_option_handler = array( $this, 'whitelist_network_options' );
24
		add_filter( 'jetpack_sync_before_enqueue_delete_site_option', $whitelist_network_option_handler );
25
		add_filter( 'jetpack_sync_before_enqueue_add_site_option', $whitelist_network_option_handler );
26
		add_filter( 'jetpack_sync_before_enqueue_update_site_option', $whitelist_network_option_handler );
27
	}
28
29
	public function init_before_send() {
30
		if ( ! is_multisite() ) {
31
			return;
32
		}
33
34
		// full sync
35
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_network_options', array(
36
			$this,
37
			'expand_network_options'
38
		) );
39
	}
40
41
	public function set_defaults() {
42
		$this->network_options_whitelist = Jetpack_Sync_Defaults::$default_network_options_whitelist;
0 ignored issues
show
Bug introduced by
The property default_network_options_whitelist cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
43
	}
44
45
	function enqueue_full_sync_actions() {
46
		if ( ! is_multisite() ) {
47
			return 0;
48
		}
49
50
		/**
51
		 * Tells the client to sync all options to the server
52
		 *
53
		 * @since 4.2.0
54
		 *
55
		 * @param boolean Whether to expand options (should always be true)
56
		 */
57
		do_action( 'jetpack_full_sync_network_options', true );
58
		return 1; // The number of actions enqueued
59
	}
60
61
	function get_full_sync_actions() {
62
		return array( 'jetpack_full_sync_network_options' );
63
	}
64
65
	function get_all_network_options() {
66
		$options = array();
67
		foreach ( $this->network_options_whitelist as $option ) {
68
			$options[ $option ] = get_site_option( $option );
69
		}
70
71
		return $options;
72
	}
73
74
	function set_network_options_whitelist( $options ) {
75
		$this->network_options_whitelist = $options;
76
	}
77
78
	function get_network_options_whitelist() {
79
		return $this->network_options_whitelist;
80
	}
81
82
	// reject non-whitelisted network options
83
	function whitelist_network_options( $args ) {
84
		if ( ! $this->is_whitelisted_network_option( $args[0] ) ) {
85
			return false;
86
		}
87
88
		return $args;
89
	}
90
91
	function is_whitelisted_network_option( $option ) {
92
		return is_multisite() && in_array( $option, $this->network_options_whitelist );
93
	}
94
95
	public function expand_network_options( $args ) {
96
		if ( $args[0] ) {
97
			return $this->get_all_network_options();
98
		}
99
100
		return $args;
101
	}
102
}
103