1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Constants 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 constants. |
14
|
|
|
*/ |
15
|
|
|
class Constants extends Module { |
16
|
|
|
/** |
17
|
|
|
* Name of the constants checksum option. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Name of the transient for locking constants. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sync module name. |
32
|
|
|
* |
33
|
|
|
* @access public |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function name() { |
38
|
|
|
return 'constants'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initialize constants action listeners. |
43
|
|
|
* |
44
|
|
|
* @access public |
45
|
|
|
* |
46
|
|
|
* @param callable $callable Action handler callable. |
47
|
|
|
*/ |
48
|
|
|
public function init_listeners( $callable ) { |
49
|
|
|
add_action( 'jetpack_sync_constant', $callable, 10, 2 ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize constants action listeners for full sync. |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* |
57
|
|
|
* @param callable $callable Action handler callable. |
58
|
|
|
*/ |
59
|
|
|
public function init_full_sync_listeners( $callable ) { |
60
|
|
|
add_action( 'jetpack_full_sync_constants', $callable ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Initialize the module in the sender. |
65
|
|
|
* |
66
|
|
|
* @access public |
67
|
|
|
*/ |
68
|
|
|
public function init_before_send() { |
69
|
|
|
add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_constants' ) ); |
70
|
|
|
|
71
|
|
|
// Full sync. |
72
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Perform module cleanup. |
77
|
|
|
* Deletes any transients and options that this module uses. |
78
|
|
|
* Usually triggered when uninstalling the plugin. |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
*/ |
82
|
|
|
public function reset_data() { |
83
|
|
|
delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); |
84
|
|
|
delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the constants whitelist. |
89
|
|
|
* |
90
|
|
|
* @access public |
91
|
|
|
* @todo We don't seem to use this one. Should we remove it? |
92
|
|
|
* |
93
|
|
|
* @param array $constants The new constants whitelist. |
94
|
|
|
*/ |
95
|
|
|
public function set_constants_whitelist( $constants ) { |
96
|
|
|
$this->constants_whitelist = $constants; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the constants whitelist. |
101
|
|
|
* |
102
|
|
|
* @access public |
103
|
|
|
* |
104
|
|
|
* @return array The constants whitelist. |
105
|
|
|
*/ |
106
|
|
|
public function get_constants_whitelist() { |
107
|
|
|
return Defaults::get_constants_whitelist(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Enqueue the constants actions for full sync. |
112
|
|
|
* |
113
|
|
|
* @access public |
114
|
|
|
* |
115
|
|
|
* @param array $config Full sync configuration for this sync module. |
116
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
117
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
118
|
|
|
* |
119
|
|
|
* @return array Number of actions enqueued, and next module state. |
120
|
|
|
*/ |
121
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
122
|
|
|
/** |
123
|
|
|
* Tells the client to sync all constants to the server |
124
|
|
|
* |
125
|
|
|
* @param boolean Whether to expand constants (should always be true) |
126
|
|
|
* |
127
|
|
|
* @since 4.2.0 |
128
|
|
|
*/ |
129
|
|
|
do_action( 'jetpack_full_sync_constants', true ); |
130
|
|
|
|
131
|
|
|
// The number of actions enqueued, and next module state (true == done). |
132
|
|
|
return array( 1, true ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Send the constants actions for full sync. |
137
|
|
|
* |
138
|
|
|
* @access public |
139
|
|
|
* |
140
|
|
|
* @param array $config Full sync configuration for this sync module. |
141
|
|
|
* @param int $send_until The timestamp until the current request can send. |
142
|
|
|
* @param array $state This module Full Sync status. |
143
|
|
|
* |
144
|
|
|
* @return array This module Full Sync status. |
145
|
|
|
*/ |
146
|
|
|
public function send_full_sync_actions( $config, $send_until, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
147
|
|
|
// we call this instead of do_action when sending immediately. |
148
|
|
|
$this->send_action( 'jetpack_full_sync_constants', array( true ) ); |
149
|
|
|
|
150
|
|
|
// The number of actions enqueued, and next module state (true == done). |
151
|
|
|
return array( 'finished' => true ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
156
|
|
|
* |
157
|
|
|
* @access public |
158
|
|
|
* |
159
|
|
|
* @param array $config Full sync configuration for this sync module. |
160
|
|
|
* |
161
|
|
|
* @return array Number of items yet to be enqueued. |
162
|
|
|
*/ |
163
|
|
|
public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
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_constants' ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Sync the constants if we're supposed to. |
180
|
|
|
* |
181
|
|
|
* @access public |
182
|
|
|
*/ |
183
|
|
|
public function maybe_sync_constants() { |
184
|
|
|
if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) { |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Defaults::$default_sync_constants_wait_time ); |
189
|
|
|
|
190
|
|
|
$constants = $this->get_all_constants(); |
191
|
|
|
if ( empty( $constants ) ) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() ); |
196
|
|
|
|
197
|
|
|
foreach ( $constants as $name => $value ) { |
198
|
|
|
$checksum = $this->get_check_sum( $value ); |
199
|
|
|
// Explicitly not using Identical comparison as get_option returns a string. |
200
|
|
|
if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) { |
201
|
|
|
/** |
202
|
|
|
* Tells the client to sync a constant to the server |
203
|
|
|
* |
204
|
|
|
* @param string The name of the constant |
205
|
|
|
* @param mixed The value of the constant |
206
|
|
|
* |
207
|
|
|
* @since 4.2.0 |
208
|
|
|
*/ |
209
|
|
|
do_action( 'jetpack_sync_constant', $name, $value ); |
210
|
|
|
$constants_checksums[ $name ] = $checksum; |
211
|
|
|
} else { |
212
|
|
|
$constants_checksums[ $name ] = $checksum; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Retrieve all constants as per the current constants whitelist. |
220
|
|
|
* Public so that we don't have to store an option for each constant. |
221
|
|
|
* |
222
|
|
|
* @access public |
223
|
|
|
* |
224
|
|
|
* @return array All constants. |
225
|
|
|
*/ |
226
|
|
|
public function get_all_constants() { |
227
|
|
|
$constants_whitelist = $this->get_constants_whitelist(); |
228
|
|
|
|
229
|
|
|
return array_combine( |
230
|
|
|
$constants_whitelist, |
231
|
|
|
array_map( array( $this, 'get_constant' ), $constants_whitelist ) |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Retrieve the value of a constant. |
237
|
|
|
* Used as a wrapper to standartize access to constants. |
238
|
|
|
* |
239
|
|
|
* @access private |
240
|
|
|
* |
241
|
|
|
* @param string $constant Constant name. |
242
|
|
|
* |
243
|
|
|
* @return mixed Return value of the constant. |
244
|
|
|
*/ |
245
|
|
|
private function get_constant( $constant ) { |
246
|
|
|
return ( defined( $constant ) ) ? |
247
|
|
|
constant( $constant ) |
248
|
|
|
: null; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Expand the constants within a hook before they are serialized and sent to the server. |
253
|
|
|
* |
254
|
|
|
* @access public |
255
|
|
|
* |
256
|
|
|
* @param array $args The hook parameters. |
257
|
|
|
* |
258
|
|
|
* @return array $args The hook parameters. |
259
|
|
|
*/ |
260
|
|
|
public function expand_constants( $args ) { |
261
|
|
|
if ( $args[0] ) { |
262
|
|
|
$constants = $this->get_all_constants(); |
263
|
|
|
$constants_checksums = array(); |
264
|
|
|
foreach ( $constants as $name => $value ) { |
265
|
|
|
$constants_checksums[ $name ] = $this->get_check_sum( $value ); |
266
|
|
|
} |
267
|
|
|
update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); |
268
|
|
|
|
269
|
|
|
return $constants; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $args; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Return Total number of objects. |
277
|
|
|
* |
278
|
|
|
* @param array $config Full Sync config. |
279
|
|
|
* |
280
|
|
|
* @return int total |
281
|
|
|
*/ |
282
|
|
|
public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
283
|
|
|
return count( $this->get_constants_whitelist() ); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Retrieve a set of constants by their IDs. |
288
|
|
|
* |
289
|
|
|
* @access public |
290
|
|
|
* |
291
|
|
|
* @param string $object_type Object type. |
292
|
|
|
* @param array $ids Object IDs. |
293
|
|
|
* @return array Array of objects. |
294
|
|
|
*/ |
295
|
|
View Code Duplication |
public function get_objects_by_id( $object_type, $ids ) { |
296
|
|
|
if ( empty( $ids ) || empty( $object_type ) || 'constant' !== $object_type ) { |
297
|
|
|
return array(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$objects = array(); |
301
|
|
|
foreach ( (array) $ids as $id ) { |
302
|
|
|
$object = $this->get_object_by_id( $object_type, $id ); |
303
|
|
|
|
304
|
|
|
if ( 'all' === $id ) { |
305
|
|
|
// If all was requested it contains all options and can simply be returned. |
306
|
|
|
return $object; |
307
|
|
|
} |
308
|
|
|
$objects[ $id ] = $object; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return $objects; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Retrieve a constant by its name. |
316
|
|
|
* |
317
|
|
|
* @access public |
318
|
|
|
* |
319
|
|
|
* @param string $object_type Type of the sync object. |
320
|
|
|
* @param string $id ID of the sync object. |
321
|
|
|
* @return mixed Value of Constant. |
322
|
|
|
*/ |
323
|
|
|
public function get_object_by_id( $object_type, $id ) { |
324
|
|
|
if ( 'constant' === $object_type ) { |
325
|
|
|
|
326
|
|
|
// Only whitelisted constants can be returned. |
327
|
|
|
if ( in_array( $id, $this->get_constants_whitelist(), true ) ) { |
328
|
|
|
return $this->get_constant( $id ); |
329
|
|
|
} elseif ( 'all' === $id ) { |
330
|
|
|
return $this->get_all_constants(); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return false; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: