Completed
Push — fix/refactor-full-sync-status-... ( baeb97...466398 )
by
unknown
63:36 queued 54:01
created

Jetpack_Sync_Module_Options::set_defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
class Jetpack_Sync_Module_Options extends Jetpack_Sync_Module {
4
	private $options_whitelist;
5
	private $network_options_whitelist;
6
7
	public function name() {
8
		return 'options';
9
	}
10
11
	public function init_listeners( $callable ) {
12
		// options
13
		add_action( 'added_option', $callable, 10, 2 );
14
		add_action( 'updated_option', $callable, 10, 3 );
15
		add_action( 'deleted_option', $callable, 10, 1 );
16
17
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
18
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
19
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
20
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
21
22
		// full sync
23
		add_action( 'jetpack_full_sync_options', $callable );
24
25
		// multi site network options
26
		if ( is_multisite() ) {
27
			add_action( 'add_site_option', $callable, 10, 2 );
28
			add_action( 'update_site_option', $callable, 10, 3 );
29
			add_action( 'delete_site_option', $callable, 10, 1 );
30
31
			// full sync
32
			add_action( 'jetpack_full_sync_network_options', $callable );
33
		}
34
35
		$whitelist_option_handler = array( $this, 'whitelist_options' );
36
		add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
37
		add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
38
		add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler );
39
40
		$whitelist_network_option_handler = array( $this, 'whitelist_network_options' );
41
		add_filter( 'jetpack_sync_before_enqueue_delete_site_option', $whitelist_network_option_handler );
42
		add_filter( 'jetpack_sync_before_enqueue_add_site_option', $whitelist_network_option_handler );
43
		add_filter( 'jetpack_sync_before_enqueue_update_site_option', $whitelist_network_option_handler );
44
	}
45
46
	public function init_before_send() {
47
		// full sync
48
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) );
49
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_network_options', array(
50
			$this,
51
			'expand_network_options'
52
		) );
53
	}
54
55
	public function set_defaults() {
56
		$this->update_options_whitelist();
57
		$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...
58
		// theme mod varies from theme to theme.
59
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
60
	}
61
62
	function enqueue_full_sync_actions() {
63
		/**
64
		 * Tells the client to sync all options to the server
65
		 *
66
		 * @since 4.2.0
67
		 *
68
		 * @param boolean Whether to expand options (should always be true)
69
		 */
70
		do_action( 'jetpack_full_sync_options', true );
71
		return 1; // The number of actions enqueued
72
	}
73
74
	function get_full_sync_actions() {
75
		return array( 'jetpack_full_sync_options' );
76
	}
77
78
	// TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
79
	function full_sync_network() {
80
		/**
81
		 * Tells the client to sync all network options to the server
82
		 *
83
		 * @since 4.2.0
84
		 *
85
		 * @param boolean Whether to expand options (should always be true)
86
		 */
87
		do_action( 'jetpack_full_sync_network_options', true );
88
		return 1; // The number of actions enqueued
89
	}
90
91
	// Is public so that we don't have to store so much data all the options twice.
92
	function get_all_options() {
93
		$options = array();
94
		foreach ( $this->options_whitelist as $option ) {
95
			$options[ $option ] = get_option( $option );
96
		}
97
98
		return $options;
99
	}
100
101
	function get_all_network_options() {
102
		$options = array();
103
		foreach ( $this->network_options_whitelist as $option ) {
104
			$options[ $option ] = get_site_option( $option );
105
		}
106
107
		return $options;
108
	}
109
110
	function set_network_options_whitelist( $options ) {
111
		$this->network_options_whitelist = $options;
112
	}
113
114
	function get_network_options_whitelist() {
115
		return $this->network_options_whitelist;
116
	}
117
118
	function update_options_whitelist() {
119
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
120
		$this->options_whitelist = apply_filters( 'jetpack_options_whitelist', Jetpack_Sync_Defaults::$default_options_whitelist );
0 ignored issues
show
Bug introduced by
The property default_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...
121
	}
122
123
	function set_options_whitelist( $options ) {
124
		$this->options_whitelist = $options;
125
	}
126
127
	function get_options_whitelist() {
128
		return $this->options_whitelist;
129
	}
130
131
	// reject non-whitelisted options
132
	function whitelist_options( $args ) {
133
		if ( ! $this->is_whitelisted_option( $args[0] ) ) {
134
			return false;
135
		}
136
137
		return $args;
138
	}
139
140
	function is_whitelisted_option( $option ) {
141
		return in_array( $option, $this->options_whitelist );
142
	}
143
144
	// reject non-whitelisted network options
145
	function whitelist_network_options( $args ) {
146
		if ( ! $this->is_whitelisted_network_option( $args[0] ) ) {
147
			return false;
148
		}
149
150
		return $args;
151
	}
152
153
	function is_whitelisted_network_option( $option ) {
154
		return is_multisite() && in_array( $option, $this->network_options_whitelist );
155
	}
156
157
	function jetpack_sync_core_icon() {
158
		if ( function_exists( 'get_site_icon_url' ) ) {
159
			$url = get_site_icon_url();
160
		} else {
161
			return;
162
		}
163
164
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
165
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
166
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
167
			// This is the option that is synced with dotcom
168
			Jetpack_Options::update_option( 'site_icon_url', $url );
169
		} else if ( empty( $url ) ) {
170
			Jetpack_Options::delete_option( 'site_icon_url' );
171
		}
172
	}
173
174
	public function expand_options( $args ) {
175
		if ( $args[0] ) {
176
			return $this->get_all_options();
177
		}
178
179
		return $args;
180
	}
181
182
	public function expand_network_options( $args ) {
183
		if ( $args[0] ) {
184
			return $this->get_all_network_options();
185
		}
186
187
		return $args;
188
	}
189
}
190