Completed
Push — add/gdpr-ads-compliance ( c52a1e...cda5c5 )
by
unknown
26:32 queued 14:46
created

Jetpack_Sync_Module_Options::set_late_default()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Module_Options extends Jetpack_Sync_Module {
4
	private $options_whitelist, $options_contentless;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

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