Completed
Push — branch-8.sync ( b15093 )
by Jeremy
07:09
created

Network_Options::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Network Options sync module.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync\Modules;
9
10
use Automattic\Jetpack\Sync\Defaults;
11
12
/**
13
 * Class to handle sync for network options.
14
 */
15
class Network_Options extends Module {
16
	/**
17
	 * Whitelist for network options we want to sync.
18
	 *
19
	 * @access private
20
	 *
21
	 * @var array
22
	 */
23
	private $network_options_whitelist;
24
25
	/**
26
	 * Sync module name.
27
	 *
28
	 * @access public
29
	 *
30
	 * @return string
31
	 */
32
	public function name() {
33
		return 'network_options';
34
	}
35
36
	/**
37
	 * Initialize network options action listeners when on multisite.
38
	 *
39
	 * @access public
40
	 *
41
	 * @param callable $callable Action handler callable.
42
	 */
43
	public function init_listeners( $callable ) {
44
		if ( ! is_multisite() ) {
45
			return;
46
		}
47
48
		// Multi site network options.
49
		add_action( 'add_site_option', $callable, 10, 2 );
50
		add_action( 'update_site_option', $callable, 10, 3 );
51
		add_action( 'delete_site_option', $callable, 10, 1 );
52
53
		$whitelist_network_option_handler = array( $this, 'whitelist_network_options' );
54
		add_filter( 'jetpack_sync_before_enqueue_delete_site_option', $whitelist_network_option_handler );
55
		add_filter( 'jetpack_sync_before_enqueue_add_site_option', $whitelist_network_option_handler );
56
		add_filter( 'jetpack_sync_before_enqueue_update_site_option', $whitelist_network_option_handler );
57
	}
58
59
	/**
60
	 * Initialize network options action listeners for full sync.
61
	 *
62
	 * @access public
63
	 *
64
	 * @param callable $callable Action handler callable.
65
	 */
66
	public function init_full_sync_listeners( $callable ) {
67
		add_action( 'jetpack_full_sync_network_options', $callable );
68
	}
69
70
	/**
71
	 * Initialize the module in the sender.
72
	 *
73
	 * @access public
74
	 */
75
	public function init_before_send() {
76
		if ( ! is_multisite() ) {
77
			return;
78
		}
79
80
		// Full sync.
81
		add_filter(
82
			'jetpack_sync_before_send_jetpack_full_sync_network_options',
83
			array(
84
				$this,
85
				'expand_network_options',
86
			)
87
		);
88
	}
89
90
	/**
91
	 * Set module defaults.
92
	 * Define the network options whitelist based on the default one.
93
	 *
94
	 * @access public
95
	 */
96
	public function set_defaults() {
97
		$this->network_options_whitelist = Defaults::$default_network_options_whitelist;
98
	}
99
100
	/**
101
	 * Enqueue the network options actions for full sync.
102
	 *
103
	 * @access public
104
	 *
105
	 * @param array   $config               Full sync configuration for this sync module.
106
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
107
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
108
	 * @return array Number of actions enqueued, and next module state.
109
	 */
110
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
111
		if ( ! is_multisite() ) {
112
			return array( null, true );
113
		}
114
115
		/**
116
		 * Tells the client to sync all options to the server
117
		 *
118
		 * @since 4.2.0
119
		 *
120
		 * @param boolean Whether to expand options (should always be true)
121
		 */
122
		do_action( 'jetpack_full_sync_network_options', true );
123
124
		// The number of actions enqueued, and next module state (true == done).
125
		return array( 1, true );
126
	}
127
128
	/**
129
	 * Send the network options actions for full sync.
130
	 *
131
	 * @access public
132
	 *
133
	 * @param array $config Full sync configuration for this sync module.
134
	 * @param int   $send_until The timestamp until the current request can send.
135
	 * @param array $state This module Full Sync status.
136
	 *
137
	 * @return array This module Full Sync status.
138
	 */
139
	public function send_full_sync_actions( $config, $send_until, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
140
		if ( ! is_multisite() ) {
141
			return array( null, true );
142
		}
143
144
		// we call this instead of do_action when sending immediately.
145
		$this->send_action( 'jetpack_full_sync_network_options', array( true ) );
146
147
		// The number of actions enqueued, and next module state (true == done).
148
		return array( 'finished' => true );
149
	}
150
151
	/**
152
	 * Retrieve an estimated number of actions that will be enqueued.
153
	 *
154
	 * @access public
155
	 *
156
	 * @param array $config Full sync configuration for this sync module.
157
	 * @return array Number of items yet to be enqueued.
158
	 */
159
	public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
160
		if ( ! is_multisite() ) {
161
			return null;
162
		}
163
164
		return 1;
165
	}
166
167
	/**
168
	 * Retrieve the actions that will be sent for this module during a full sync.
169
	 *
170
	 * @access public
171
	 *
172
	 * @return array Full sync actions of this module.
173
	 */
174
	public function get_full_sync_actions() {
175
		return array( 'jetpack_full_sync_network_options' );
176
	}
177
178
	/**
179
	 * Retrieve all network options as per the current network options whitelist.
180
	 *
181
	 * @access public
182
	 *
183
	 * @return array All network options.
184
	 */
185
	public function get_all_network_options() {
186
		$options = array();
187
		foreach ( $this->network_options_whitelist as $option ) {
188
			$options[ $option ] = get_site_option( $option );
189
		}
190
191
		return $options;
192
	}
193
194
	/**
195
	 * Set the network options whitelist.
196
	 *
197
	 * @access public
198
	 *
199
	 * @param array $options The new network options whitelist.
200
	 */
201
	public function set_network_options_whitelist( $options ) {
202
		$this->network_options_whitelist = $options;
203
	}
204
205
	/**
206
	 * Get the network options whitelist.
207
	 *
208
	 * @access public
209
	 *
210
	 * @return array The network options whitelist.
211
	 */
212
	public function get_network_options_whitelist() {
213
		return $this->network_options_whitelist;
214
	}
215
216
	/**
217
	 * Reject non-whitelisted network options.
218
	 *
219
	 * @access public
220
	 *
221
	 * @param array $args The hook parameters.
222
	 * @return array|false $args The hook parameters, false if not a whitelisted network option.
223
	 */
224
	public function whitelist_network_options( $args ) {
225
		if ( ! $this->is_whitelisted_network_option( $args[0] ) ) {
226
			return false;
227
		}
228
229
		return $args;
230
	}
231
232
	/**
233
	 * Whether the option is a whitelisted network option in a multisite system.
234
	 *
235
	 * @access public
236
	 *
237
	 * @param string $option Option name.
238
	 * @return boolean True if this is a whitelisted network option.
239
	 */
240
	public function is_whitelisted_network_option( $option ) {
241
		return is_multisite() && in_array( $option, $this->network_options_whitelist, true );
242
	}
243
244
	/**
245
	 * Expand the network options within a hook before they are serialized and sent to the server.
246
	 *
247
	 * @access public
248
	 *
249
	 * @param array $args The hook parameters.
250
	 * @return array $args The hook parameters.
251
	 */
252
	public function expand_network_options( $args ) {
253
		if ( $args[0] ) {
254
			return $this->get_all_network_options();
255
		}
256
257
		return $args;
258
	}
259
260
	/**
261
	 * Return Total number of objects.
262
	 *
263
	 * @param array $config Full Sync config.
264
	 *
265
	 * @return int total
266
	 */
267
	public function total( $config ) {
268
		return count( $this->network_options_whitelist );
269
	}
270
271
}
272