1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
4
|
|
|
|
5
|
|
|
use Automattic\Jetpack\Sync\Listener; |
6
|
|
|
use Automattic\Jetpack\Sync\Modules; |
7
|
|
|
use Automattic\Jetpack\Sync\Queue; |
8
|
|
|
use Automattic\Jetpack\Sync\Settings; |
9
|
|
|
/** |
10
|
|
|
* This class does a full resync of the database by |
11
|
|
|
* enqueuing an outbound action for every single object |
12
|
|
|
* that we care about. |
13
|
|
|
* |
14
|
|
|
* This class, and its related class Jetpack_Sync_Module, contain a few non-obvious optimisations that should be explained: |
15
|
|
|
* - we fire an action called jetpack_full_sync_start so that WPCOM can erase the contents of the cached database |
16
|
|
|
* - for each object type, we page through the object IDs and enqueue them by firing some monitored actions |
17
|
|
|
* - we load the full objects for those IDs in chunks of Jetpack_Sync_Module::ARRAY_CHUNK_SIZE (to reduce the number of MySQL calls) |
18
|
|
|
* - we fire a trigger for the entire array which the Automattic\Jetpack\Sync\Listener then serializes and queues. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class Full_Sync extends Module { |
22
|
|
|
const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
23
|
|
|
const FULL_SYNC_TIMEOUT = 3600; |
24
|
|
|
|
25
|
|
|
public function name() { |
26
|
|
|
return 'full-sync'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function init_full_sync_listeners( $callable ) { |
30
|
|
|
// synthetic actions for full sync |
31
|
|
|
add_action( 'jetpack_full_sync_start', $callable, 10, 3 ); |
32
|
|
|
add_action( 'jetpack_full_sync_end', $callable, 10, 2 ); |
33
|
|
|
add_action( 'jetpack_full_sync_cancelled', $callable ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function init_before_send() { |
37
|
|
|
// this is triggered after actions have been processed on the server |
38
|
|
|
add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
function start( $module_configs = null ) { |
42
|
|
|
$was_already_running = $this->is_started() && ! $this->is_finished(); |
43
|
|
|
|
44
|
|
|
// remove all evidence of previous full sync items and status |
45
|
|
|
$this->reset_data(); |
46
|
|
|
|
47
|
|
|
if ( $was_already_running ) { |
48
|
|
|
/** |
49
|
|
|
* Fires when a full sync is cancelled. |
50
|
|
|
* |
51
|
|
|
* @since 4.2.0 |
52
|
|
|
*/ |
53
|
|
|
do_action( 'jetpack_full_sync_cancelled' ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->update_status_option( 'started', time() ); |
57
|
|
|
$this->update_status_option( 'params', $module_configs ); |
58
|
|
|
|
59
|
|
|
$enqueue_status = array(); |
60
|
|
|
$full_sync_config = array(); |
61
|
|
|
$include_empty = false; |
62
|
|
|
$empty = array(); |
63
|
|
|
// default value is full sync |
64
|
|
|
if ( ! is_array( $module_configs ) ) { |
65
|
|
|
$module_configs = array(); |
66
|
|
|
$include_empty = true; |
67
|
|
|
foreach ( Modules::get_modules() as $module ) { |
68
|
|
|
$module_configs[ $module->name() ] = true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// set default configuration, calculate totals, and save configuration if totals > 0 |
73
|
|
|
foreach ( Modules::get_modules() as $module ) { |
74
|
|
|
$module_name = $module->name(); |
75
|
|
|
$module_config = isset( $module_configs[ $module_name ] ) ? $module_configs[ $module_name ] : false; |
76
|
|
|
|
77
|
|
|
if ( ! $module_config ) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( 'users' === $module_name && 'initial' === $module_config ) { |
82
|
|
|
$module_config = $module->get_initial_sync_user_config(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$enqueue_status[ $module_name ] = false; |
86
|
|
|
|
87
|
|
|
$total_items = $module->estimate_full_sync_actions( $module_config ); |
88
|
|
|
|
89
|
|
|
// if there's information to process, configure this module |
90
|
|
|
if ( ! is_null( $total_items ) && $total_items > 0 ) { |
91
|
|
|
$full_sync_config[ $module_name ] = $module_config; |
92
|
|
|
$enqueue_status[ $module_name ] = array( |
93
|
|
|
$total_items, // total |
94
|
|
|
0, // queued |
95
|
|
|
false, // current state |
96
|
|
|
); |
97
|
|
|
} elseif ( $include_empty && $total_items === 0 ) { |
98
|
|
|
$empty[ $module_name ] = true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->set_config( $full_sync_config ); |
103
|
|
|
$this->set_enqueue_status( $enqueue_status ); |
104
|
|
|
|
105
|
|
|
$range = $this->get_content_range( $full_sync_config ); |
106
|
|
|
/** |
107
|
|
|
* Fires when a full sync begins. This action is serialized |
108
|
|
|
* and sent to the server so that it knows a full sync is coming. |
109
|
|
|
* |
110
|
|
|
* @since 4.2.0 |
111
|
|
|
* @since 7.3.0 Added $range arg. |
112
|
|
|
* @since 7.4.0 Added $empty arg. |
113
|
|
|
* |
114
|
|
|
* @param array $full_sync_config Sync configuration for all sync modules. |
115
|
|
|
* @param array $range Range of the sync items, containing min and max IDs for some item types. |
116
|
|
|
* @param array $empty The modules with no items to sync during a full sync. |
117
|
|
|
*/ |
118
|
|
|
do_action( 'jetpack_full_sync_start', $full_sync_config, $range, $empty ); |
119
|
|
|
|
120
|
|
|
$this->continue_enqueuing( $full_sync_config, $enqueue_status ); |
121
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
function continue_enqueuing( $configs = null, $enqueue_status = null ) { |
126
|
|
|
if ( ! $this->is_started() || $this->get_status_option( 'queue_finished' ) ) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// if full sync queue is full, don't enqueue more items |
131
|
|
|
$max_queue_size_full_sync = Settings::get_setting( 'max_queue_size_full_sync' ); |
132
|
|
|
$full_sync_queue = new Queue( 'full_sync' ); |
133
|
|
|
|
134
|
|
|
$available_queue_slots = $max_queue_size_full_sync - $full_sync_queue->size(); |
135
|
|
|
|
136
|
|
|
if ( $available_queue_slots <= 0 ) { |
137
|
|
|
return; |
138
|
|
|
} else { |
139
|
|
|
$remaining_items_to_enqueue = min( Settings::get_setting( 'max_enqueue_full_sync' ), $available_queue_slots ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ( ! $configs ) { |
143
|
|
|
$configs = $this->get_config(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ( ! $enqueue_status ) { |
147
|
|
|
$enqueue_status = $this->get_enqueue_status(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
foreach ( Modules::get_modules() as $module ) { |
151
|
|
|
$module_name = $module->name(); |
152
|
|
|
|
153
|
|
|
// skip module if not configured for this sync or module is done |
154
|
|
|
if ( ! isset( $configs[ $module_name ] ) |
155
|
|
|
|| // no module config |
156
|
|
|
! $configs[ $module_name ] |
157
|
|
|
|| // no enqueue status |
158
|
|
|
! $enqueue_status[ $module_name ] |
159
|
|
|
|| // finished enqueuing this module |
160
|
|
|
true === $enqueue_status[ $module_name ][2] ) { |
161
|
|
|
continue; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $configs[ $module_name ], $remaining_items_to_enqueue, $enqueue_status[ $module_name ][2] ); |
165
|
|
|
|
166
|
|
|
$enqueue_status[ $module_name ][2] = $next_enqueue_state; |
167
|
|
|
|
168
|
|
|
// if items were processed, subtract them from the limit |
169
|
|
|
if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) { |
170
|
|
|
$enqueue_status[ $module_name ][1] += $items_enqueued; |
171
|
|
|
$remaining_items_to_enqueue -= $items_enqueued; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// stop processing if we've reached our limit of items to enqueue |
175
|
|
|
if ( 0 >= $remaining_items_to_enqueue ) { |
176
|
|
|
$this->set_enqueue_status( $enqueue_status ); |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->set_enqueue_status( $enqueue_status ); |
182
|
|
|
|
183
|
|
|
// setting autoload to true means that it's faster to check whether we should continue enqueuing |
184
|
|
|
$this->update_status_option( 'queue_finished', time(), true ); |
185
|
|
|
|
186
|
|
|
$range = $this->get_content_range( $configs ); |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Fires when a full sync ends. This action is serialized |
190
|
|
|
* and sent to the server. |
191
|
|
|
* |
192
|
|
|
* @since 4.2.0 |
193
|
|
|
* @since 7.3.0 Added $range arg. |
194
|
|
|
* |
195
|
|
|
* @param string $checksum Deprecated since 7.3.0 - @see https://github.com/Automattic/jetpack/pull/11945/ |
196
|
|
|
* @param array $range Range of the sync items, containing min and max IDs for some item types. |
197
|
|
|
*/ |
198
|
|
|
do_action( 'jetpack_full_sync_end', '', $range ); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
function get_range( $type ) { |
202
|
|
|
global $wpdb; |
203
|
|
|
if ( ! in_array( $type, array( 'comments', 'posts' ) ) ) { |
204
|
|
|
return array(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
switch ( $type ) { |
208
|
|
|
case 'posts': |
209
|
|
|
$table = $wpdb->posts; |
210
|
|
|
$id = 'ID'; |
211
|
|
|
$where_sql = Settings::get_blacklisted_post_types_sql(); |
212
|
|
|
|
213
|
|
|
break; |
214
|
|
|
case 'comments': |
215
|
|
|
$table = $wpdb->comments; |
216
|
|
|
$id = 'comment_ID'; |
217
|
|
|
$where_sql = Settings::get_comments_filter_sql(); |
218
|
|
|
break; |
219
|
|
|
} |
220
|
|
|
$results = $wpdb->get_results( "SELECT MAX({$id}) as max, MIN({$id}) as min, COUNT({$id}) as count FROM {$table} WHERE {$where_sql}" ); |
|
|
|
|
221
|
|
|
if ( isset( $results[0] ) ) { |
222
|
|
|
return $results[0]; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return array(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
private function get_content_range( $config ) { |
229
|
|
|
$range = array(); |
230
|
|
|
// Only when we are sending the whole range do we want to send also the range |
231
|
|
View Code Duplication |
if ( isset( $config['posts'] ) && $config['posts'] === true ) { |
232
|
|
|
$range['posts'] = $this->get_range( 'posts' ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
View Code Duplication |
if ( isset( $config['comments'] ) && $config['comments'] === true ) { |
236
|
|
|
$range['comments'] = $this->get_range( 'comments' ); |
237
|
|
|
} |
238
|
|
|
return $range; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
function update_sent_progress_action( $actions ) { |
242
|
|
|
// quick way to map to first items with an array of arrays |
243
|
|
|
$actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) ); |
244
|
|
|
|
245
|
|
|
// Total item counts for each action. |
246
|
|
|
$actions_with_total_counts = $this->get_actions_totals( $actions ); |
247
|
|
|
|
248
|
|
|
if ( ! $this->is_started() || $this->is_finished() ) { |
249
|
|
|
return; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) { |
253
|
|
|
$this->update_status_option( 'send_started', time() ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
foreach ( Modules::get_modules() as $module ) { |
257
|
|
|
$module_actions = $module->get_full_sync_actions(); |
258
|
|
|
$status_option_name = "{$module->name()}_sent"; |
259
|
|
|
$total_option_name = "{$status_option_name}_total"; |
260
|
|
|
$items_sent = $this->get_status_option( $status_option_name, 0 ); |
261
|
|
|
$items_sent_total = $this->get_status_option( $total_option_name, 0 ); |
262
|
|
|
|
263
|
|
|
foreach ( $module_actions as $module_action ) { |
264
|
|
|
if ( isset( $actions_with_counts[ $module_action ] ) ) { |
265
|
|
|
$items_sent += $actions_with_counts[ $module_action ]; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if ( ! empty( $actions_with_total_counts[ $module_action ] ) ) { |
269
|
|
|
$items_sent_total += $actions_with_total_counts[ $module_action ]; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if ( $items_sent > 0 ) { |
274
|
|
|
$this->update_status_option( $status_option_name, $items_sent ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if ( 0 !== $items_sent_total ) { |
278
|
|
|
$this->update_status_option( $total_option_name, $items_sent_total ); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) { |
283
|
|
|
$this->update_status_option( 'finished', time() ); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function get_action_name( $queue_item ) { |
288
|
|
|
if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) { |
289
|
|
|
return $queue_item[0]; |
290
|
|
|
} |
291
|
|
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Retrieve the total number of items we're syncing in a particular queue item (action). |
296
|
|
|
* `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
297
|
|
|
* represents the first (and only) chunk of items to sync in that action. |
298
|
|
|
* |
299
|
|
|
* @param array $queue_item Item of the sync queue that corresponds to a particular action. |
300
|
|
|
* @return int Total number of items in the action. |
301
|
|
|
*/ |
302
|
|
|
public function get_action_totals( $queue_item ) { |
303
|
|
|
if ( is_array( $queue_item ) && isset( $queue_item[1][0] ) ) { |
304
|
|
|
if ( is_array( $queue_item[1][0] ) ) { |
305
|
|
|
// Let's count the items we sync in this action. |
306
|
|
|
return count( $queue_item[1][0] ); |
307
|
|
|
} |
308
|
|
|
// -1 indicates that this action syncs all items by design. |
309
|
|
|
return -1; |
310
|
|
|
} |
311
|
|
|
return 0; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Retrieve the total number of items for a set of actions, grouped by action name. |
316
|
|
|
* |
317
|
|
|
* @param array $actions An array of actions. |
318
|
|
|
* @return array An array, representing the total number of items, grouped per action. |
319
|
|
|
*/ |
320
|
|
|
public function get_actions_totals( $actions ) { |
321
|
|
|
$totals = array(); |
322
|
|
|
|
323
|
|
|
foreach ( $actions as $action ) { |
324
|
|
|
$name = $this->get_action_name( $action ); |
325
|
|
|
$action_totals = $this->get_action_totals( $action ); |
326
|
|
|
if ( ! isset( $totals[ $name ] ) ) { |
327
|
|
|
$totals[ $name ] = 0; |
328
|
|
|
} |
329
|
|
|
$totals[ $name ] += $action_totals; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return $totals; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function is_started() { |
336
|
|
|
return ! ! $this->get_status_option( 'started' ); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public function is_finished() { |
340
|
|
|
return ! ! $this->get_status_option( 'finished' ); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function get_status() { |
344
|
|
|
$status = array( |
345
|
|
|
'started' => $this->get_status_option( 'started' ), |
346
|
|
|
'queue_finished' => $this->get_status_option( 'queue_finished' ), |
347
|
|
|
'send_started' => $this->get_status_option( 'send_started' ), |
348
|
|
|
'finished' => $this->get_status_option( 'finished' ), |
349
|
|
|
'sent' => array(), |
350
|
|
|
'sent_total' => array(), |
351
|
|
|
'queue' => array(), |
352
|
|
|
'config' => $this->get_status_option( 'params' ), |
353
|
|
|
'total' => array(), |
354
|
|
|
); |
355
|
|
|
|
356
|
|
|
$enqueue_status = $this->get_enqueue_status(); |
357
|
|
|
|
358
|
|
|
foreach ( Modules::get_modules() as $module ) { |
359
|
|
|
$name = $module->name(); |
360
|
|
|
|
361
|
|
|
if ( ! isset( $enqueue_status[ $name ] ) ) { |
362
|
|
|
continue; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
list( $total, $queued, $state ) = $enqueue_status[ $name ]; |
|
|
|
|
366
|
|
|
|
367
|
|
|
if ( $total ) { |
368
|
|
|
$status['total'][ $name ] = $total; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if ( $queued ) { |
372
|
|
|
$status['queue'][ $name ] = $queued; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
if ( $sent = $this->get_status_option( "{$name}_sent" ) ) { |
376
|
|
|
$status['sent'][ $name ] = $sent; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$sent_total = $this->get_status_option( "{$name}_sent_total" ); |
380
|
|
|
if ( $sent_total ) { |
381
|
|
|
$status['sent_total'][ $name ] = $sent_total; |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
return $status; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function clear_status() { |
389
|
|
|
$prefix = self::STATUS_OPTION_PREFIX; |
390
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_started" ); |
391
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_params" ); |
392
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_queue_finished" ); |
393
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_send_started" ); |
394
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_finished" ); |
395
|
|
|
|
396
|
|
|
$this->delete_enqueue_status(); |
397
|
|
|
|
398
|
|
|
foreach ( Modules::get_modules() as $module ) { |
399
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent" ); |
400
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent_total" ); |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
public function reset_data() { |
405
|
|
|
$this->clear_status(); |
406
|
|
|
$this->delete_config(); |
407
|
|
|
|
408
|
|
|
$listener = Listener::get_instance(); |
409
|
|
|
$listener->get_full_sync_queue()->reset(); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
private function get_status_option( $name, $default = null ) { |
413
|
|
|
$value = \Jetpack_Options::get_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $default ); |
414
|
|
|
|
415
|
|
|
return is_numeric( $value ) ? intval( $value ) : $value; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
private function update_status_option( $name, $value, $autoload = false ) { |
419
|
|
|
\Jetpack_Options::update_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $value, $autoload ); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
private function set_enqueue_status( $new_status ) { |
423
|
|
|
\Jetpack_Options::update_raw_option( 'jetpack_sync_full_enqueue_status', $new_status ); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
private function delete_enqueue_status() { |
427
|
|
|
return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_enqueue_status' ); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
private function get_enqueue_status() { |
431
|
|
|
return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_enqueue_status' ); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
private function set_config( $config ) { |
435
|
|
|
\Jetpack_Options::update_raw_option( 'jetpack_sync_full_config', $config ); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
private function delete_config() { |
439
|
|
|
return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_config' ); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
private function get_config() { |
443
|
|
|
return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_config' ); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
private function write_option( $name, $value ) { |
447
|
|
|
// we write our own option updating code to bypass filters/caching/etc on set_option/get_option |
448
|
|
|
global $wpdb; |
449
|
|
|
$serialized_value = maybe_serialize( $value ); |
450
|
|
|
// try updating, if no update then insert |
451
|
|
|
// TODO: try to deal with the fact that unchanged values can return updated_num = 0 |
452
|
|
|
// below we used "insert ignore" to at least suppress the resulting error |
453
|
|
|
$updated_num = $wpdb->query( |
454
|
|
|
$wpdb->prepare( |
455
|
|
|
"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", |
456
|
|
|
$serialized_value, |
457
|
|
|
$name |
458
|
|
|
) |
459
|
|
|
); |
460
|
|
|
|
461
|
|
|
if ( ! $updated_num ) { |
462
|
|
|
$updated_num = $wpdb->query( |
463
|
|
|
$wpdb->prepare( |
464
|
|
|
"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )", |
465
|
|
|
$name, |
466
|
|
|
$serialized_value |
467
|
|
|
) |
468
|
|
|
); |
469
|
|
|
} |
470
|
|
|
return $updated_num; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
private function read_option( $name, $default = null ) { |
474
|
|
|
global $wpdb; |
475
|
|
|
$value = $wpdb->get_var( |
476
|
|
|
$wpdb->prepare( |
477
|
|
|
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
478
|
|
|
$name |
479
|
|
|
) |
480
|
|
|
); |
481
|
|
|
$value = maybe_unserialize( $value ); |
482
|
|
|
|
483
|
|
|
if ( $value === null && $default !== null ) { |
484
|
|
|
return $default; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
return $value; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: