summaryrefslogtreecommitdiff
blob: 2c323a2bf1bd9eb7d52dc5a0c5e9c8b0bd56d596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
 * Options sync module.
 *
 * @package automattic/jetpack-sync
 */

namespace Automattic\Jetpack\Sync\Modules;

use Automattic\Jetpack\Sync\Defaults;

/**
 * Class to handle sync for options.
 */
class Options extends Module {
	/**
	 * Whitelist for options we want to sync.
	 *
	 * @access private
	 *
	 * @var array
	 */
	private $options_whitelist;

	/**
	 * Contentless options we want to sync.
	 *
	 * @access private
	 *
	 * @var array
	 */
	private $options_contentless;

	/**
	 * Sync module name.
	 *
	 * @access public
	 *
	 * @return string
	 */
	public function name() {
		return 'options';
	}

	/**
	 * Initialize options action listeners.
	 *
	 * @access public
	 *
	 * @param callable $callable Action handler callable.
	 */
	public function init_listeners( $callable ) {
		// Options.
		add_action( 'added_option', $callable, 10, 2 );
		add_action( 'updated_option', $callable, 10, 3 );
		add_action( 'deleted_option', $callable, 10, 1 );

		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );

		$whitelist_option_handler = array( $this, 'whitelist_options' );
		add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
		add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
		add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler );
	}

	/**
	 * Initialize options action listeners for full sync.
	 *
	 * @access public
	 *
	 * @param callable $callable Action handler callable.
	 */
	public function init_full_sync_listeners( $callable ) {
		add_action( 'jetpack_full_sync_options', $callable );
	}

	/**
	 * Initialize the module in the sender.
	 *
	 * @access public
	 */
	public function init_before_send() {
		// Full sync.
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) );
	}

	/**
	 * Set module defaults.
	 * Define the options whitelist and contentless options.
	 *
	 * @access public
	 */
	public function set_defaults() {
		$this->update_options_whitelist();
		$this->update_options_contentless();
	}

	/**
	 * Set module defaults at a later time.
	 *
	 * @access public
	 */
	public function set_late_default() {
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
		$late_options = apply_filters( 'jetpack_options_whitelist', array() );
		if ( ! empty( $late_options ) && is_array( $late_options ) ) {
			$this->options_whitelist = array_merge( $this->options_whitelist, $late_options );
		}
	}

	/**
	 * Enqueue the options actions for full sync.
	 *
	 * @access public
	 *
	 * @param array   $config               Full sync configuration for this sync module.
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
	 * @return array Number of actions enqueued, and next module state.
	 */
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
		/**
		 * Tells the client to sync all options to the server
		 *
		 * @since 4.2.0
		 *
		 * @param boolean Whether to expand options (should always be true)
		 */
		do_action( 'jetpack_full_sync_options', true );

		// The number of actions enqueued, and next module state (true == done).
		return array( 1, true );
	}

	/**
	 * Retrieve an estimated number of actions that will be enqueued.
	 *
	 * @access public
	 *
	 * @param array $config Full sync configuration for this sync module.
	 * @return int Number of items yet to be enqueued.
	 */
	public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
		return 1;
	}

	/**
	 * Retrieve the actions that will be sent for this module during a full sync.
	 *
	 * @access public
	 *
	 * @return array Full sync actions of this module.
	 */
	public function get_full_sync_actions() {
		return array( 'jetpack_full_sync_options' );
	}

	/**
	 * Retrieve all options as per the current options whitelist.
	 * Public so that we don't have to store so much data all the options twice.
	 *
	 * @access public
	 *
	 * @return array All options.
	 */
	public function get_all_options() {
		$options       = array();
		$random_string = wp_generate_password();
		foreach ( $this->options_whitelist as $option ) {
			$option_value = get_option( $option, $random_string );
			if ( $option_value !== $random_string ) {
				$options[ $option ] = $option_value;
			}
		}

		// Add theme mods.
		$theme_mods_option = 'theme_mods_' . get_option( 'stylesheet' );
		$theme_mods_value  = get_option( $theme_mods_option, $random_string );
		if ( $theme_mods_value === $random_string ) {
			return $options;
		}
		$this->filter_theme_mods( $theme_mods_value );
		$options[ $theme_mods_option ] = $theme_mods_value;
		return $options;
	}

	/**
	 * Update the options whitelist to the default one.
	 *
	 * @access public
	 */
	public function update_options_whitelist() {
		$this->options_whitelist = Defaults::get_options_whitelist();
	}

	/**
	 * Set the options whitelist.
	 *
	 * @access public
	 *
	 * @param array $options The new options whitelist.
	 */
	public function set_options_whitelist( $options ) {
		$this->options_whitelist = $options;
	}

	/**
	 * Get the options whitelist.
	 *
	 * @access public
	 *
	 * @return array The options whitelist.
	 */
	public function get_options_whitelist() {
		return $this->options_whitelist;
	}

	/**
	 * Update the contentless options to the defaults.
	 *
	 * @access public
	 */
	public function update_options_contentless() {
		$this->options_contentless = Defaults::get_options_contentless();
	}

	/**
	 * Get the contentless options.
	 *
	 * @access public
	 *
	 * @return array Array of the contentless options.
	 */
	public function get_options_contentless() {
		return $this->options_contentless;
	}

	/**
	 * Reject any options that aren't whitelisted or contentless.
	 *
	 * @access public
	 *
	 * @param array $args The hook parameters.
	 * @return array $args The hook parameters.
	 */
	public function whitelist_options( $args ) {
		// Reject non-whitelisted options.
		if ( ! $this->is_whitelisted_option( $args[0] ) ) {
			return false;
		}

		// Filter our weird array( false ) value for theme_mods_*.
		if ( 'theme_mods_' === substr( $args[0], 0, 11 ) ) {
			$this->filter_theme_mods( $args[1] );
			if ( isset( $args[2] ) ) {
				$this->filter_theme_mods( $args[2] );
			}
		}

		// Set value(s) of contentless option to empty string(s).
		if ( $this->is_contentless_option( $args[0] ) ) {
			// Create a new array matching length of $args, containing empty strings.
			$empty    = array_fill( 0, count( $args ), '' );
			$empty[0] = $args[0];
			return $empty;
		}

		return $args;
	}

	/**
	 * Whether a certain option is whitelisted for sync.
	 *
	 * @access public
	 *
	 * @param string $option Option name.
	 * @return boolean Whether the option is whitelisted.
	 */
	public function is_whitelisted_option( $option ) {
		return in_array( $option, $this->options_whitelist, true ) || 'theme_mods_' === substr( $option, 0, 11 );
	}

	/**
	 * Whether a certain option is a contentless one.
	 *
	 * @access private
	 *
	 * @param string $option Option name.
	 * @return boolean Whether the option is contentless.
	 */
	private function is_contentless_option( $option ) {
		return in_array( $option, $this->options_contentless, true );
	}

	/**
	 * Filters out falsy values from theme mod options.
	 *
	 * @access private
	 *
	 * @param array $value Option value.
	 */
	private function filter_theme_mods( &$value ) {
		if ( is_array( $value ) && isset( $value[0] ) ) {
			unset( $value[0] );
		}
	}

	/**
	 * Handle changes in the core site icon and sync them.
	 *
	 * @access public
	 */
	public function jetpack_sync_core_icon() {
		$url = get_site_icon_url();

		require_once JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php';
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
		if ( ! empty( $url ) && jetpack_site_icon_url() !== $url ) {
			// This is the option that is synced with dotcom.
			\Jetpack_Options::update_option( 'site_icon_url', $url );
		} elseif ( empty( $url ) ) {
			\Jetpack_Options::delete_option( 'site_icon_url' );
		}
	}

	/**
	 * Expand all options within a hook before they are serialized and sent to the server.
	 *
	 * @access public
	 *
	 * @param array $args The hook parameters.
	 * @return array $args The hook parameters.
	 */
	public function expand_options( $args ) {
		if ( $args[0] ) {
			return $this->get_all_options();
		}

		return $args;
	}
}