Completed
Push — memberships/widget ( 983427...00b130 )
by
unknown
155:51 queued 142:38
created

Options::jetpack_sync_core_icon()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
5
class Options extends \Jetpack_Sync_Module {
6
	private $options_whitelist, $options_contentless;
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
18
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
19
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
20
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
21
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
22
23
		$whitelist_option_handler = array( $this, 'whitelist_options' );
24
		add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
25
		add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
26
		add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler );
27
	}
28
29
	public function init_full_sync_listeners( $callable ) {
30
		add_action( 'jetpack_full_sync_options', $callable );
31
	}
32
33
	public function init_before_send() {
34
		// full sync
35
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) );
36
	}
37
38
	public function set_defaults() {
39
		$this->update_options_whitelist();
40
		$this->update_options_contentless();
41
	}
42
43
	public function set_late_default() {
44
45
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
46
		$late_options = apply_filters( 'jetpack_options_whitelist', array() );
47
		if ( ! empty( $late_options ) && is_array( $late_options ) ) {
48
			$this->options_whitelist = array_merge( $this->options_whitelist, $late_options );
49
		}
50
	}
51
52
	function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
53
		/**
54
		 * Tells the client to sync all options to the server
55
		 *
56
		 * @since 4.2.0
57
		 *
58
		 * @param boolean Whether to expand options (should always be true)
59
		 */
60
		do_action( 'jetpack_full_sync_options', true );
61
62
		// The number of actions enqueued, and next module state (true == done)
63
		return array( 1, true );
64
	}
65
66
	public function estimate_full_sync_actions( $config ) {
67
		return 1;
68
	}
69
70
	function get_full_sync_actions() {
71
		return array( 'jetpack_full_sync_options' );
72
	}
73
74
	// Is public so that we don't have to store so much data all the options twice.
75
	function get_all_options() {
76
		$options       = array();
77
		$random_string = wp_generate_password();
78
		foreach ( $this->options_whitelist as $option ) {
79
			$option_value = get_option( $option, $random_string );
80
			if ( $option_value !== $random_string ) {
81
				$options[ $option ] = $option_value;
82
			}
83
		}
84
85
		// add theme mods
86
		$theme_mods_option = 'theme_mods_' . get_option( 'stylesheet' );
87
		$theme_mods_value  = get_option( $theme_mods_option, $random_string );
88
		if ( $theme_mods_value === $random_string ) {
89
			return $options;
90
		}
91
		$this->filter_theme_mods( $theme_mods_value );
92
		$options[ $theme_mods_option ] = $theme_mods_value;
93
		return $options;
94
	}
95
96
	function update_options_whitelist() {
97
		$this->options_whitelist = \Jetpack_Sync_Defaults::get_options_whitelist();
98
	}
99
100
	function set_options_whitelist( $options ) {
101
		$this->options_whitelist = $options;
102
	}
103
104
	function get_options_whitelist() {
105
		return $this->options_whitelist;
106
	}
107
108
	function update_options_contentless() {
109
		$this->options_contentless = \Jetpack_Sync_Defaults::get_options_contentless();
110
	}
111
112
	function get_options_contentless() {
113
		return $this->options_contentless;
114
	}
115
116
	function whitelist_options( $args ) {
117
		// Reject non-whitelisted options
118
		if ( ! $this->is_whitelisted_option( $args[0] ) ) {
119
			return false;
120
		}
121
122
		// filter our weird array( false ) value for theme_mods_*
123
		if ( 'theme_mods_' === substr( $args[0], 0, 11 ) ) {
124
			$this->filter_theme_mods( $args[1] );
125
			if ( isset( $args[2] ) ) {
126
				$this->filter_theme_mods( $args[2] );
127
			}
128
		}
129
130
		// Set value(s) of contentless option to empty string(s)
131
		if ( $this->is_contentless_option( $args[0] ) ) {
132
			// Create a new array matching length of $args, containing empty strings
133
			$empty    = array_fill( 0, count( $args ), '' );
134
			$empty[0] = $args[0];
135
			return $empty;
136
		}
137
138
		return $args;
139
	}
140
141
	function is_whitelisted_option( $option ) {
142
		return in_array( $option, $this->options_whitelist ) || 'theme_mods_' === substr( $option, 0, 11 );
143
	}
144
145
	private function is_contentless_option( $option ) {
146
		return in_array( $option, $this->options_contentless );
147
	}
148
149
	private function filter_theme_mods( &$value ) {
150
		if ( is_array( $value ) && isset( $value[0] ) ) {
151
			unset( $value[0] );
152
		}
153
	}
154
155
	function jetpack_sync_core_icon() {
156
		$url = get_site_icon_url();
157
158
		require_once JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php';
159
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
160
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
161
			// This is the option that is synced with dotcom
162
			\Jetpack_Options::update_option( 'site_icon_url', $url );
163
		} elseif ( empty( $url ) ) {
164
			\Jetpack_Options::delete_option( 'site_icon_url' );
165
		}
166
	}
167
168
	public function expand_options( $args ) {
169
		if ( $args[0] ) {
170
			return $this->get_all_options();
171
		}
172
173
		return $args;
174
	}
175
}
176