Completed
Push — psr4-jetpack-sync-module ( 910f64...b1c668 )
by
unknown
284:58 queued 277:10
created

Options   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 3
dl 0
loc 171
rs 9.44
c 0
b 0
f 0

21 Methods

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