Completed
Push — add/google-maps/api-key ( 5599f7...90239a )
by
unknown
26:57 queued 16:47
created

Jetpack_Sync_Module_Network_Options   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 101
rs 10
wmc 19
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 18 2
A init_before_send() 0 11 2
A set_defaults() 0 3 1
A get_full_sync_actions() 0 3 1
A get_all_network_options() 0 8 2
A set_network_options_whitelist() 0 3 1
A get_network_options_whitelist() 0 3 1
A whitelist_network_options() 0 7 2
A is_whitelisted_network_option() 0 3 2
A expand_network_options() 0 7 2
A enqueue_full_sync_actions() 0 16 2
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
59
		return 1; // The number of actions enqueued
60
	}
61
62
	function get_full_sync_actions() {
63
		return array( 'jetpack_full_sync_network_options' );
64
	}
65
66
	function get_all_network_options() {
67
		$options = array();
68
		foreach ( $this->network_options_whitelist as $option ) {
69
			$options[ $option ] = get_site_option( $option );
70
		}
71
72
		return $options;
73
	}
74
75
	function set_network_options_whitelist( $options ) {
76
		$this->network_options_whitelist = $options;
77
	}
78
79
	function get_network_options_whitelist() {
80
		return $this->network_options_whitelist;
81
	}
82
83
	// reject non-whitelisted network options
84
	function whitelist_network_options( $args ) {
85
		if ( ! $this->is_whitelisted_network_option( $args[0] ) ) {
86
			return false;
87
		}
88
89
		return $args;
90
	}
91
92
	function is_whitelisted_network_option( $option ) {
93
		return is_multisite() && in_array( $option, $this->network_options_whitelist );
94
	}
95
96
	public function expand_network_options( $args ) {
97
		if ( $args[0] ) {
98
			return $this->get_all_network_options();
99
		}
100
101
		return $args;
102
	}
103
}
104