Completed
Push — update/do-full-sync-on-site-re... ( 7e9c9b )
by
unknown
40:13 queued 31:08
created

Jetpack_Sync_Module_Options::expand_options()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 9.4285
1
<?php
2
3
class Jetpack_Sync_Module_Options extends Jetpack_Sync_Module {
4
	const OPTIONS_CHECKSUM_OPTION_NAME = 'jetpack_options_sync_checksum';
5
	const OPTIONS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_options_await';
6
	private $options_whitelist;
7
8
	public function name() {
9
		return 'options';
10
	}
11
12
	public function init_listeners( $callable ) {
13
		// options
14
		add_action( 'added_option', $callable, 10, 2 );
15
		add_action( 'updated_option', $callable, 10, 3 );
16
		add_action( 'deleted_option', $callable, 10, 1 );
17
		add_action( 'jetpack_sync_option', $callable, 10, 2 );
18
19
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
20
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
21
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
22
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
23
24
		$whitelist_option_handler = array( $this, 'whitelist_options' );
25
		add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
26
		add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
27
		add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler );
28
29
	}
30
31 View Code Duplication
	function maybe_sync_options() {
32
		if ( get_transient( self::OPTIONS_AWAIT_TRANSIENT_NAME ) ) {
33
			return;
34
		}
35
36
		set_transient( self::OPTIONS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_options_wait_time );
0 ignored issues
show
Bug introduced by
The property default_sync_options_wait_time 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...
37
38
		$options = $this->get_all_options();
39
		if ( empty( $options ) ) {
40
			return;
41
		}
42
43
		$options_checksums = (array) get_option( self::OPTIONS_CHECKSUM_OPTION_NAME, array() );
44
45
		foreach ( $options as $name => $value ) {
46
			$checksum = $this->get_check_sum( $value );
47
			// explicitly not using Identical comparison as get_option returns a string
48
			if ( ! $this->still_valid_checksum( $options_checksums, $name, $checksum ) && ! is_null( $value ) ) {
49
				/**
50
				 * Tells the client to sync an option to the server
51
				 *
52
				 * @since 5.3.0
53
				 *
54
				 * @param string The name of the constant
55
				 * @param mixed The value of the constant
56
				 */
57
				do_action( 'jetpack_sync_option', $name, $value );
58
				$options_checksums[ $name ] = $checksum;
59
			} else {
60
				$options_checksums[ $name ] = $checksum;
61
			}
62
		}
63
		update_option( self::OPTIONS_CHECKSUM_OPTION_NAME, $options_checksums );
64
	}
65
66
	public function init_full_sync_listeners( $callable ) {
67
		add_action( 'jetpack_full_sync_options', $callable );
68
	}
69
70
	public function init_before_send() {
71
		// full sync
72
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) );
73
74
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_options' ) );
75
	}
76
77
	public function set_defaults() {
78
		$this->update_options_whitelist();
79
	}
80
81
	public function set_late_default() {
82
83
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
84
		$late_options = apply_filters( 'jetpack_options_whitelist', array() );
85
		if ( ! empty( $late_options ) && is_array( $late_options ) ) {
86
			$this->options_whitelist = array_merge( $this->options_whitelist, $late_options );
87
		}
88
	}
89
90
	function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
91
		/**
92
		 * Tells the client to sync all options to the server
93
		 *
94
		 * @since 4.2.0
95
		 *
96
		 * @param boolean Whether to expand options (should always be true)
97
		 */
98
		do_action( 'jetpack_full_sync_options', true );
99
100
		// The number of actions enqueued, and next module state (true == done)
101
		return array( 1, true );
102
	}
103
104
	public function estimate_full_sync_actions( $config ) {
105
		return 1;
106
	}
107
108
	function get_full_sync_actions() {
109
		return array( 'jetpack_full_sync_options' );
110
	}
111
112
	// Is public so that we don't have to store so much data all the options twice.
113
	function get_all_options() {
114
		$options = array();
115
		$random_string = wp_generate_password();
116
		foreach ( $this->options_whitelist as $option ) {
117
			$option_value = get_option( $option, $random_string );
118
			if ( $option_value !== $random_string ) {
119
				$options[ $option ] = $option_value;
120
			}
121
		}
122
123
		// add theme mods
124
		$theme_mods_option = 'theme_mods_'.get_option( 'stylesheet' );
125
		$theme_mods_value  = get_option( $theme_mods_option, $random_string );
126
		if ( $theme_mods_value === $random_string ) {
127
			return $options;
128
		}
129
		$this->filter_theme_mods( $theme_mods_value );
130
		$options[ $theme_mods_option ] = $theme_mods_value;
131
		return $options;
132
	}
133
134
	function update_options_whitelist() {
135
		$this->options_whitelist = Jetpack_Sync_Defaults::get_options_whitelist();
136
	}
137
138
	function set_options_whitelist( $options ) {
139
		$this->options_whitelist = $options;
140
	}
141
142
	function get_options_whitelist() {
143
		return $this->options_whitelist;
144
	}
145
146
	// reject non-whitelisted options
147
	function whitelist_options( $args ) {
148
		if ( ! $this->is_whitelisted_option( $args[0] ) ) {
149
			return false;
150
		}
151
152
		// filter our weird array( false ) value for theme_mods_*
153
		if ( 'theme_mods_' === substr( $args[0], 0, 11 ) ) {
154
			$this->filter_theme_mods( $args[1] );
155
			if ( isset( $args[2] ) ) { 
156
				$this->filter_theme_mods( $args[2] );
157
			}
158
		}
159
		return $args;
160
	}
161
162
	function is_whitelisted_option( $option ) {
163
		return in_array( $option, $this->options_whitelist ) || 'theme_mods_' === substr( $option, 0, 11 );
164
	}
165
166
	private function filter_theme_mods( &$value ) {
167
		if ( is_array( $value ) && isset( $value[0] ) ) {
168
			unset( $value[0] ); 
169
		}
170
	}
171
172
	function jetpack_sync_core_icon() {
173
		if ( function_exists( 'get_site_icon_url' ) ) {
174
			$url = get_site_icon_url();
175
		} else {
176
			return;
177
		}
178
179
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
180
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
181
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
182
			// This is the option that is synced with dotcom
183
			Jetpack_Options::update_option( 'site_icon_url', $url );
184
		} else if ( empty( $url ) ) {
185
			Jetpack_Options::delete_option( 'site_icon_url' );
186
		}
187
	}
188
189
	public function expand_options( $args ) {
190
		if ( $args[0] ) {
191
			return $this->get_all_options();
192
		}
193
194
		return $args;
195
	}
196
}
197