1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sync replicastore. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Sync\Replicastore\Table_Checksum; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* An implementation of Replicastore Interface which returns data stored in a WordPress.org DB. |
14
|
|
|
* This is useful to compare values in the local WP DB to values in the synced replica store |
15
|
|
|
*/ |
16
|
|
|
class Replicastore implements Replicastore_Interface { |
17
|
|
|
/** |
18
|
|
|
* Empty and reset the replicastore. |
19
|
|
|
* |
20
|
|
|
* @access public |
21
|
|
|
*/ |
22
|
|
|
public function reset() { |
23
|
|
|
global $wpdb; |
24
|
|
|
|
25
|
|
|
$wpdb->query( "DELETE FROM $wpdb->posts" ); |
26
|
|
|
|
27
|
|
|
// Delete comments from cache. |
28
|
|
|
$comment_ids = $wpdb->get_col( "SELECT comment_ID FROM $wpdb->comments" ); |
29
|
|
|
if ( ! empty( $comment_ids ) ) { |
30
|
|
|
clean_comment_cache( $comment_ids ); |
31
|
|
|
} |
32
|
|
|
$wpdb->query( "DELETE FROM $wpdb->comments" ); |
33
|
|
|
|
34
|
|
|
// Also need to delete terms from cache. |
35
|
|
|
$term_ids = $wpdb->get_col( "SELECT term_id FROM $wpdb->terms" ); |
36
|
|
|
foreach ( $term_ids as $term_id ) { |
37
|
|
|
wp_cache_delete( $term_id, 'terms' ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$wpdb->query( "DELETE FROM $wpdb->terms" ); |
41
|
|
|
|
42
|
|
|
$wpdb->query( "DELETE FROM $wpdb->term_taxonomy" ); |
43
|
|
|
$wpdb->query( "DELETE FROM $wpdb->term_relationships" ); |
44
|
|
|
|
45
|
|
|
// Callables and constants. |
46
|
|
|
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'jetpack_%'" ); |
47
|
|
|
$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key NOT LIKE '\_%'" ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Ran when full sync has just started. |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
* |
55
|
|
|
* @param array $config Full sync configuration for this sync module. |
56
|
|
|
*/ |
57
|
|
|
public function full_sync_start( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
58
|
|
|
$this->reset(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Ran when full sync has just finished. |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
* |
66
|
|
|
* @param string $checksum Deprecated since 7.3.0. |
67
|
|
|
*/ |
68
|
|
|
public function full_sync_end( $checksum ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
69
|
|
|
// Noop right now. |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve the number of terms. |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* |
77
|
|
|
* @return int Number of terms. |
78
|
|
|
*/ |
79
|
|
|
public function term_count() { |
80
|
|
|
global $wpdb; |
81
|
|
|
return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->terms" ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrieve the number of rows in the `term_taxonomy` table. |
86
|
|
|
* |
87
|
|
|
* @access public |
88
|
|
|
* |
89
|
|
|
* @return int Number of terms. |
90
|
|
|
*/ |
91
|
|
|
public function term_taxonomy_count() { |
92
|
|
|
global $wpdb; |
93
|
|
|
return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->term_taxonomy" ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Retrieve the number of term relationships. |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
* |
101
|
|
|
* @return int Number of rows in the term relationships table. |
102
|
|
|
*/ |
103
|
|
|
public function term_relationship_count() { |
104
|
|
|
global $wpdb; |
105
|
|
|
return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->term_relationships" ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Retrieve the number of posts with a particular post status within a certain range. |
110
|
|
|
* |
111
|
|
|
* @access public |
112
|
|
|
* |
113
|
|
|
* @todo Prepare the SQL query before executing it. |
114
|
|
|
* |
115
|
|
|
* @param string $status Post status. |
|
|
|
|
116
|
|
|
* @param int $min_id Minimum post ID. |
|
|
|
|
117
|
|
|
* @param int $max_id Maximum post ID. |
|
|
|
|
118
|
|
|
* @return int Number of posts. |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function post_count( $status = null, $min_id = null, $max_id = null ) { |
121
|
|
|
global $wpdb; |
122
|
|
|
|
123
|
|
|
$where = ''; |
|
|
|
|
124
|
|
|
|
125
|
|
|
if ( $status ) { |
|
|
|
|
126
|
|
|
$where = "post_status = '" . esc_sql( $status ) . "'"; |
127
|
|
|
} else { |
128
|
|
|
$where = '1=1'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ( ! empty( $min_id ) ) { |
132
|
|
|
$where .= ' AND ID >= ' . (int) $min_id; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ( ! empty( $max_id ) ) { |
136
|
|
|
$where .= ' AND ID <= ' . (int) $max_id; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
140
|
|
|
return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE $where" ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Retrieve the posts with a particular post status. |
145
|
|
|
* |
146
|
|
|
* @access public |
147
|
|
|
* |
148
|
|
|
* @todo Implement range and actually use max_id/min_id arguments. |
149
|
|
|
* |
150
|
|
|
* @param string $status Post status. |
|
|
|
|
151
|
|
|
* @param int $min_id Minimum post ID. |
|
|
|
|
152
|
|
|
* @param int $max_id Maximum post ID. |
|
|
|
|
153
|
|
|
* @return array Array of posts. |
154
|
|
|
*/ |
155
|
|
|
public function get_posts( $status = null, $min_id = null, $max_id = null ) { |
156
|
|
|
$args = array( |
157
|
|
|
'orderby' => 'ID', |
158
|
|
|
'posts_per_page' => -1, |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
if ( $status ) { |
|
|
|
|
162
|
|
|
$args['post_status'] = $status; |
163
|
|
|
} else { |
164
|
|
|
$args['post_status'] = 'any'; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return get_posts( $args ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Retrieve a post object by the post ID. |
172
|
|
|
* |
173
|
|
|
* @access public |
174
|
|
|
* |
175
|
|
|
* @param int $id Post ID. |
176
|
|
|
* @return \WP_Post Post object. |
177
|
|
|
*/ |
178
|
|
|
public function get_post( $id ) { |
179
|
|
|
return get_post( $id ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Update or insert a post. |
184
|
|
|
* |
185
|
|
|
* @access public |
186
|
|
|
* |
187
|
|
|
* @param \WP_Post $post Post object. |
188
|
|
|
* @param bool $silent Whether to perform a silent action. Not used in this implementation. |
189
|
|
|
*/ |
190
|
|
|
public function upsert_post( $post, $silent = false ) { |
191
|
|
|
global $wpdb; |
192
|
|
|
|
193
|
|
|
// Reject the post if it's not a \WP_Post. |
194
|
|
|
if ( ! $post instanceof \WP_Post ) { |
|
|
|
|
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$post = $post->to_array(); |
199
|
|
|
|
200
|
|
|
// Reject posts without an ID. |
201
|
|
|
if ( ! isset( $post['ID'] ) ) { |
202
|
|
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$now = current_time( 'mysql' ); |
206
|
|
|
$now_gmt = get_gmt_from_date( $now ); |
207
|
|
|
|
208
|
|
|
$defaults = array( |
209
|
|
|
'ID' => 0, |
210
|
|
|
'post_author' => '0', |
211
|
|
|
'post_content' => '', |
212
|
|
|
'post_content_filtered' => '', |
213
|
|
|
'post_title' => '', |
214
|
|
|
'post_name' => '', |
215
|
|
|
'post_excerpt' => '', |
216
|
|
|
'post_status' => 'draft', |
217
|
|
|
'post_type' => 'post', |
218
|
|
|
'comment_status' => 'closed', |
219
|
|
|
'comment_count' => '0', |
220
|
|
|
'ping_status' => '', |
221
|
|
|
'post_password' => '', |
222
|
|
|
'to_ping' => '', |
223
|
|
|
'pinged' => '', |
224
|
|
|
'post_parent' => 0, |
225
|
|
|
'menu_order' => 0, |
226
|
|
|
'guid' => '', |
227
|
|
|
'post_date' => $now, |
228
|
|
|
'post_date_gmt' => $now_gmt, |
229
|
|
|
'post_modified' => $now, |
230
|
|
|
'post_modified_gmt' => $now_gmt, |
231
|
|
|
); |
232
|
|
|
|
233
|
|
|
$post = array_intersect_key( $post, $defaults ); |
234
|
|
|
|
235
|
|
|
$post = sanitize_post( $post, 'db' ); |
236
|
|
|
|
237
|
|
|
unset( $post['filter'] ); |
238
|
|
|
|
239
|
|
|
$exists = $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS( SELECT 1 FROM $wpdb->posts WHERE ID = %d )", $post['ID'] ) ); |
240
|
|
|
|
241
|
|
|
if ( $exists ) { |
242
|
|
|
$wpdb->update( $wpdb->posts, $post, array( 'ID' => $post['ID'] ) ); |
243
|
|
|
} else { |
244
|
|
|
$wpdb->insert( $wpdb->posts, $post ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
clean_post_cache( $post['ID'] ); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Delete a post by the post ID. |
252
|
|
|
* |
253
|
|
|
* @access public |
254
|
|
|
* |
255
|
|
|
* @param int $post_id Post ID. |
256
|
|
|
*/ |
257
|
|
|
public function delete_post( $post_id ) { |
258
|
|
|
wp_delete_post( $post_id, true ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Retrieve the checksum for posts within a range. |
263
|
|
|
* |
264
|
|
|
* @access public |
265
|
|
|
* |
266
|
|
|
* @param int $min_id Minimum post ID. |
|
|
|
|
267
|
|
|
* @param int $max_id Maximum post ID. |
|
|
|
|
268
|
|
|
* @return int The checksum. |
269
|
|
|
*/ |
270
|
|
|
public function posts_checksum( $min_id = null, $max_id = null ) { |
271
|
|
|
return $this->summarize_checksum_histogram( $this->checksum_histogram( 'posts', $this->calculate_buckets( 'posts', $min_id, $max_id ), $min_id, $max_id ) ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Retrieve the checksum for post meta within a range. |
276
|
|
|
* |
277
|
|
|
* @access public |
278
|
|
|
* |
279
|
|
|
* @param int $min_id Minimum post meta ID. |
|
|
|
|
280
|
|
|
* @param int $max_id Maximum post meta ID. |
|
|
|
|
281
|
|
|
* @return int The checksum. |
282
|
|
|
*/ |
283
|
|
|
public function post_meta_checksum( $min_id = null, $max_id = null ) { |
284
|
|
|
return $this->summarize_checksum_histogram( $this->checksum_histogram( 'postmeta', $this->calculate_buckets( 'postmeta', $min_id, $max_id ), $min_id, $max_id ) ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Retrieve the number of comments with a particular comment status within a certain range. |
289
|
|
|
* |
290
|
|
|
* @access public |
291
|
|
|
* |
292
|
|
|
* @todo Prepare the SQL query before executing it. |
293
|
|
|
* |
294
|
|
|
* @param string $status Comment status. |
|
|
|
|
295
|
|
|
* @param int $min_id Minimum comment ID. |
|
|
|
|
296
|
|
|
* @param int $max_id Maximum comment ID. |
|
|
|
|
297
|
|
|
* @return int Number of comments. |
298
|
|
|
*/ |
299
|
|
View Code Duplication |
public function comment_count( $status = null, $min_id = null, $max_id = null ) { |
300
|
|
|
global $wpdb; |
301
|
|
|
|
302
|
|
|
$comment_approved = $this->comment_status_to_approval_value( $status ); |
303
|
|
|
|
304
|
|
|
if ( false !== $comment_approved ) { |
305
|
|
|
$where = "comment_approved = '" . esc_sql( $comment_approved ) . "'"; |
306
|
|
|
} else { |
307
|
|
|
$where = '1=1'; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
if ( ! empty( $min_id ) ) { |
311
|
|
|
$where .= ' AND comment_ID >= ' . (int) $min_id; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
if ( ! empty( $max_id ) ) { |
315
|
|
|
$where .= ' AND comment_ID <= ' . (int) $max_id; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
319
|
|
|
return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE $where" ); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Translate a comment status to a value of the comment_approved field. |
324
|
|
|
* |
325
|
|
|
* @access private |
326
|
|
|
* |
327
|
|
|
* @param string $status Comment status. |
328
|
|
|
* @return string|bool New comment_approved value, false if the status doesn't affect it. |
329
|
|
|
*/ |
330
|
|
|
private function comment_status_to_approval_value( $status ) { |
331
|
|
|
switch ( $status ) { |
332
|
|
|
case 'approve': |
333
|
|
|
return '1'; |
334
|
|
|
case 'hold': |
335
|
|
|
return '0'; |
336
|
|
|
case 'spam': |
337
|
|
|
return 'spam'; |
338
|
|
|
case 'trash': |
339
|
|
|
return 'trash'; |
340
|
|
|
case 'any': |
341
|
|
|
return false; |
342
|
|
|
case 'all': |
343
|
|
|
return false; |
344
|
|
|
default: |
345
|
|
|
return false; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Retrieve the comments with a particular comment status. |
351
|
|
|
* |
352
|
|
|
* @access public |
353
|
|
|
* |
354
|
|
|
* @todo Implement range and actually use max_id/min_id arguments. |
355
|
|
|
* |
356
|
|
|
* @param string $status Comment status. |
|
|
|
|
357
|
|
|
* @param int $min_id Minimum comment ID. |
|
|
|
|
358
|
|
|
* @param int $max_id Maximum comment ID. |
|
|
|
|
359
|
|
|
* @return array Array of comments. |
360
|
|
|
*/ |
361
|
|
|
public function get_comments( $status = null, $min_id = null, $max_id = null ) { |
362
|
|
|
$args = array( |
363
|
|
|
'orderby' => 'ID', |
364
|
|
|
'status' => 'all', |
365
|
|
|
); |
366
|
|
|
|
367
|
|
|
if ( $status ) { |
|
|
|
|
368
|
|
|
$args['status'] = $status; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return get_comments( $args ); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Retrieve a comment object by the comment ID. |
376
|
|
|
* |
377
|
|
|
* @access public |
378
|
|
|
* |
379
|
|
|
* @param int $id Comment ID. |
380
|
|
|
* @return \WP_Comment Comment object. |
381
|
|
|
*/ |
382
|
|
|
public function get_comment( $id ) { |
383
|
|
|
return \WP_Comment::get_instance( $id ); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Update or insert a comment. |
388
|
|
|
* |
389
|
|
|
* @access public |
390
|
|
|
* |
391
|
|
|
* @param \WP_Comment $comment Comment object. |
392
|
|
|
*/ |
393
|
|
|
public function upsert_comment( $comment ) { |
394
|
|
|
global $wpdb; |
395
|
|
|
|
396
|
|
|
$comment = $comment->to_array(); |
397
|
|
|
|
398
|
|
|
// Filter by fields on comment table. |
399
|
|
|
$comment_fields_whitelist = array( |
400
|
|
|
'comment_ID', |
401
|
|
|
'comment_post_ID', |
402
|
|
|
'comment_author', |
403
|
|
|
'comment_author_email', |
404
|
|
|
'comment_author_url', |
405
|
|
|
'comment_author_IP', |
406
|
|
|
'comment_date', |
407
|
|
|
'comment_date_gmt', |
408
|
|
|
'comment_content', |
409
|
|
|
'comment_karma', |
410
|
|
|
'comment_approved', |
411
|
|
|
'comment_agent', |
412
|
|
|
'comment_type', |
413
|
|
|
'comment_parent', |
414
|
|
|
'user_id', |
415
|
|
|
); |
416
|
|
|
|
417
|
|
|
foreach ( $comment as $key => $value ) { |
418
|
|
|
if ( ! in_array( $key, $comment_fields_whitelist, true ) ) { |
419
|
|
|
unset( $comment[ $key ] ); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
$exists = $wpdb->get_var( |
424
|
|
|
$wpdb->prepare( |
425
|
|
|
"SELECT EXISTS( SELECT 1 FROM $wpdb->comments WHERE comment_ID = %d )", |
426
|
|
|
$comment['comment_ID'] |
427
|
|
|
) |
428
|
|
|
); |
429
|
|
|
|
430
|
|
|
if ( $exists ) { |
431
|
|
|
$wpdb->update( $wpdb->comments, $comment, array( 'comment_ID' => $comment['comment_ID'] ) ); |
432
|
|
|
} else { |
433
|
|
|
$wpdb->insert( $wpdb->comments, $comment ); |
434
|
|
|
} |
435
|
|
|
// Remove comment from cache. |
436
|
|
|
clean_comment_cache( $comment['comment_ID'] ); |
437
|
|
|
|
438
|
|
|
wp_update_comment_count( $comment['comment_post_ID'] ); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Trash a comment by the comment ID. |
443
|
|
|
* |
444
|
|
|
* @access public |
445
|
|
|
* |
446
|
|
|
* @param int $comment_id Comment ID. |
447
|
|
|
*/ |
448
|
|
|
public function trash_comment( $comment_id ) { |
449
|
|
|
wp_delete_comment( $comment_id ); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Delete a comment by the comment ID. |
454
|
|
|
* |
455
|
|
|
* @access public |
456
|
|
|
* |
457
|
|
|
* @param int $comment_id Comment ID. |
458
|
|
|
*/ |
459
|
|
|
public function delete_comment( $comment_id ) { |
460
|
|
|
wp_delete_comment( $comment_id, true ); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Mark a comment by the comment ID as spam. |
465
|
|
|
* |
466
|
|
|
* @access public |
467
|
|
|
* |
468
|
|
|
* @param int $comment_id Comment ID. |
469
|
|
|
*/ |
470
|
|
|
public function spam_comment( $comment_id ) { |
471
|
|
|
wp_spam_comment( $comment_id ); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Trash the comments of a post. |
476
|
|
|
* |
477
|
|
|
* @access public |
478
|
|
|
* |
479
|
|
|
* @param int $post_id Post ID. |
480
|
|
|
* @param array $statuses Post statuses. Not used in this implementation. |
481
|
|
|
*/ |
482
|
|
|
public function trashed_post_comments( $post_id, $statuses ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
483
|
|
|
wp_trash_post_comments( $post_id ); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Untrash the comments of a post. |
488
|
|
|
* |
489
|
|
|
* @access public |
490
|
|
|
* |
491
|
|
|
* @param int $post_id Post ID. |
492
|
|
|
*/ |
493
|
|
|
public function untrashed_post_comments( $post_id ) { |
494
|
|
|
wp_untrash_post_comments( $post_id ); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Retrieve the checksum for comments within a range. |
499
|
|
|
* |
500
|
|
|
* @access public |
501
|
|
|
* |
502
|
|
|
* @param int $min_id Minimum comment ID. |
|
|
|
|
503
|
|
|
* @param int $max_id Maximum comment ID. |
|
|
|
|
504
|
|
|
* @return int The checksum. |
505
|
|
|
*/ |
506
|
|
|
public function comments_checksum( $min_id = null, $max_id = null ) { |
507
|
|
|
return $this->summarize_checksum_histogram( $this->checksum_histogram( 'comments', $this->calculate_buckets( 'comments', $min_id, $max_id ), $min_id, $max_id ) ); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Retrieve the checksum for comment meta within a range. |
512
|
|
|
* |
513
|
|
|
* @access public |
514
|
|
|
* |
515
|
|
|
* @param int $min_id Minimum comment meta ID. |
|
|
|
|
516
|
|
|
* @param int $max_id Maximum comment meta ID. |
|
|
|
|
517
|
|
|
* @return int The checksum. |
518
|
|
|
*/ |
519
|
|
|
public function comment_meta_checksum( $min_id = null, $max_id = null ) { |
520
|
|
|
return $this->summarize_checksum_histogram( $this->checksum_histogram( 'commentmeta', $this->calculate_buckets( 'commentmeta', $min_id, $max_id ), $min_id, $max_id ) ); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Update the value of an option. |
525
|
|
|
* |
526
|
|
|
* @access public |
527
|
|
|
* |
528
|
|
|
* @param string $option Option name. |
529
|
|
|
* @param mixed $value Option value. |
530
|
|
|
* @return bool False if value was not updated and true if value was updated. |
531
|
|
|
*/ |
532
|
|
|
public function update_option( $option, $value ) { |
533
|
|
|
return update_option( $option, $value ); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* Retrieve an option value based on an option name. |
538
|
|
|
* |
539
|
|
|
* @access public |
540
|
|
|
* |
541
|
|
|
* @param string $option Name of option to retrieve. |
542
|
|
|
* @param mixed $default Optional. Default value to return if the option does not exist. |
543
|
|
|
* @return mixed Value set for the option. |
544
|
|
|
*/ |
545
|
|
|
public function get_option( $option, $default = false ) { |
546
|
|
|
return get_option( $option, $default ); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Remove an option by name. |
551
|
|
|
* |
552
|
|
|
* @access public |
553
|
|
|
* |
554
|
|
|
* @param string $option Name of option to remove. |
555
|
|
|
* @return bool True, if option is successfully deleted. False on failure. |
556
|
|
|
*/ |
557
|
|
|
public function delete_option( $option ) { |
558
|
|
|
return delete_option( $option ); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Change the features that the current theme supports. |
563
|
|
|
* Intentionally not implemented in this replicastore. |
564
|
|
|
* |
565
|
|
|
* @access public |
566
|
|
|
* |
567
|
|
|
* @param array $theme_support Features that the theme supports. |
568
|
|
|
*/ |
569
|
|
|
public function set_theme_support( $theme_support ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
570
|
|
|
// Noop. |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Whether the current theme supports a certain feature. |
575
|
|
|
* |
576
|
|
|
* @access public |
577
|
|
|
* |
578
|
|
|
* @param string $feature Name of the feature. |
579
|
|
|
*/ |
580
|
|
|
public function current_theme_supports( $feature ) { |
581
|
|
|
return current_theme_supports( $feature ); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* Retrieve metadata for the specified object. |
586
|
|
|
* |
587
|
|
|
* @access public |
588
|
|
|
* |
589
|
|
|
* @param string $type Meta type. |
590
|
|
|
* @param int $object_id ID of the object. |
591
|
|
|
* @param string $meta_key Meta key. |
592
|
|
|
* @param bool $single If true, return only the first value of the specified meta_key. |
593
|
|
|
* |
594
|
|
|
* @return mixed Single metadata value, or array of values. |
595
|
|
|
*/ |
596
|
|
|
public function get_metadata( $type, $object_id, $meta_key = '', $single = false ) { |
597
|
|
|
return get_metadata( $type, $object_id, $meta_key, $single ); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Stores remote meta key/values alongside an ID mapping key. |
602
|
|
|
* |
603
|
|
|
* @access public |
604
|
|
|
* |
605
|
|
|
* @todo Refactor to not use interpolated values when preparing the SQL query. |
606
|
|
|
* |
607
|
|
|
* @param string $type Meta type. |
608
|
|
|
* @param int $object_id ID of the object. |
609
|
|
|
* @param string $meta_key Meta key. |
610
|
|
|
* @param mixed $meta_value Meta value. |
611
|
|
|
* @param int $meta_id ID of the meta. |
612
|
|
|
* |
613
|
|
|
* @return bool False if meta table does not exist, true otherwise. |
614
|
|
|
*/ |
615
|
|
|
public function upsert_metadata( $type, $object_id, $meta_key, $meta_value, $meta_id ) { |
616
|
|
|
$table = _get_meta_table( $type ); |
617
|
|
|
if ( ! $table ) { |
618
|
|
|
return false; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
global $wpdb; |
622
|
|
|
|
623
|
|
|
$exists = $wpdb->get_var( |
624
|
|
|
$wpdb->prepare( |
625
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
626
|
|
|
"SELECT EXISTS( SELECT 1 FROM $table WHERE meta_id = %d )", |
627
|
|
|
$meta_id |
628
|
|
|
) |
629
|
|
|
); |
630
|
|
|
|
631
|
|
|
if ( $exists ) { |
632
|
|
|
$wpdb->update( |
633
|
|
|
$table, |
634
|
|
|
array( |
635
|
|
|
'meta_key' => $meta_key, |
636
|
|
|
'meta_value' => maybe_serialize( $meta_value ), |
637
|
|
|
), |
638
|
|
|
array( 'meta_id' => $meta_id ) |
639
|
|
|
); |
640
|
|
|
} else { |
641
|
|
|
$object_id_field = $type . '_id'; |
642
|
|
|
$wpdb->insert( |
643
|
|
|
$table, |
644
|
|
|
array( |
645
|
|
|
'meta_id' => $meta_id, |
646
|
|
|
$object_id_field => $object_id, |
647
|
|
|
'meta_key' => $meta_key, |
648
|
|
|
'meta_value' => maybe_serialize( $meta_value ), |
649
|
|
|
) |
650
|
|
|
); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
wp_cache_delete( $object_id, $type . '_meta' ); |
654
|
|
|
|
655
|
|
|
return true; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Delete metadata for the specified object. |
660
|
|
|
* |
661
|
|
|
* @access public |
662
|
|
|
* |
663
|
|
|
* @todo Refactor to not use interpolated values when preparing the SQL query. |
664
|
|
|
* |
665
|
|
|
* @param string $type Meta type. |
666
|
|
|
* @param int $object_id ID of the object. |
667
|
|
|
* @param array $meta_ids IDs of the meta objects to delete. |
668
|
|
|
*/ |
669
|
|
|
public function delete_metadata( $type, $object_id, $meta_ids ) { |
670
|
|
|
global $wpdb; |
671
|
|
|
|
672
|
|
|
$table = _get_meta_table( $type ); |
673
|
|
|
if ( ! $table ) { |
674
|
|
|
return false; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
foreach ( $meta_ids as $meta_id ) { |
678
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
679
|
|
|
$wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE meta_id = %d", $meta_id ) ); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
// If we don't have an object ID what do we do - invalidate ALL meta? |
683
|
|
|
if ( $object_id ) { |
684
|
|
|
wp_cache_delete( $object_id, $type . '_meta' ); |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Delete metadata with a certain key for the specified objects. |
690
|
|
|
* |
691
|
|
|
* @access public |
692
|
|
|
* |
693
|
|
|
* @todo Test this out to make sure it works as expected. |
694
|
|
|
* @todo Refactor to not use interpolated values when preparing the SQL query. |
695
|
|
|
* |
696
|
|
|
* @param string $type Meta type. |
697
|
|
|
* @param array $object_ids IDs of the objects. |
698
|
|
|
* @param string $meta_key Meta key. |
699
|
|
|
*/ |
700
|
|
|
public function delete_batch_metadata( $type, $object_ids, $meta_key ) { |
701
|
|
|
global $wpdb; |
702
|
|
|
|
703
|
|
|
$table = _get_meta_table( $type ); |
704
|
|
|
if ( ! $table ) { |
705
|
|
|
return false; |
706
|
|
|
} |
707
|
|
|
$column = sanitize_key( $type . '_id' ); |
708
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
709
|
|
|
$wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $column IN (%s) && meta_key = %s", implode( ',', $object_ids ), $meta_key ) ); |
710
|
|
|
|
711
|
|
|
// If we don't have an object ID what do we do - invalidate ALL meta? |
712
|
|
|
foreach ( $object_ids as $object_id ) { |
713
|
|
|
wp_cache_delete( $object_id, $type . '_meta' ); |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Retrieve value of a constant based on the constant name. |
719
|
|
|
* |
720
|
|
|
* @access public |
721
|
|
|
* |
722
|
|
|
* @param string $constant Name of constant to retrieve. |
723
|
|
|
* @return mixed Value set for the constant. |
724
|
|
|
*/ |
725
|
|
|
public function get_constant( $constant ) { |
726
|
|
|
$value = get_option( 'jetpack_constant_' . $constant ); |
727
|
|
|
|
728
|
|
|
if ( $value ) { |
729
|
|
|
return $value; |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
return null; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* Set the value of a constant. |
737
|
|
|
* |
738
|
|
|
* @access public |
739
|
|
|
* |
740
|
|
|
* @param string $constant Name of constant to retrieve. |
741
|
|
|
* @param mixed $value Value set for the constant. |
742
|
|
|
*/ |
743
|
|
|
public function set_constant( $constant, $value ) { |
744
|
|
|
update_option( 'jetpack_constant_' . $constant, $value ); |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
/** |
748
|
|
|
* Retrieve the number of the available updates of a certain type. |
749
|
|
|
* Type is one of: `plugins`, `themes`, `wordpress`, `translations`, `total`, `wp_update_version`. |
750
|
|
|
* |
751
|
|
|
* @access public |
752
|
|
|
* |
753
|
|
|
* @param string $type Type of updates to retrieve. |
754
|
|
|
* @return int|null Number of updates available, `null` if type is invalid or missing. |
755
|
|
|
*/ |
756
|
|
|
public function get_updates( $type ) { |
757
|
|
|
$all_updates = get_option( 'jetpack_updates', array() ); |
758
|
|
|
|
759
|
|
|
if ( isset( $all_updates[ $type ] ) ) { |
760
|
|
|
return $all_updates[ $type ]; |
761
|
|
|
} else { |
762
|
|
|
return null; |
763
|
|
|
} |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* Set the available updates of a certain type. |
768
|
|
|
* Type is one of: `plugins`, `themes`, `wordpress`, `translations`, `total`, `wp_update_version`. |
769
|
|
|
* |
770
|
|
|
* @access public |
771
|
|
|
* |
772
|
|
|
* @param string $type Type of updates to set. |
773
|
|
|
* @param int $updates Total number of updates. |
774
|
|
|
*/ |
775
|
|
|
public function set_updates( $type, $updates ) { |
776
|
|
|
$all_updates = get_option( 'jetpack_updates', array() ); |
777
|
|
|
$all_updates[ $type ] = $updates; |
778
|
|
|
update_option( 'jetpack_updates', $all_updates ); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* Retrieve a callable value based on its name. |
783
|
|
|
* |
784
|
|
|
* @access public |
785
|
|
|
* |
786
|
|
|
* @param string $name Name of the callable to retrieve. |
787
|
|
|
* @return mixed Value of the callable. |
788
|
|
|
*/ |
789
|
|
|
public function get_callable( $name ) { |
790
|
|
|
$value = get_option( 'jetpack_' . $name ); |
791
|
|
|
|
792
|
|
|
if ( $value ) { |
793
|
|
|
return $value; |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
return null; |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* Update the value of a callable. |
801
|
|
|
* |
802
|
|
|
* @access public |
803
|
|
|
* |
804
|
|
|
* @param string $name Callable name. |
805
|
|
|
* @param mixed $value Callable value. |
806
|
|
|
*/ |
807
|
|
|
public function set_callable( $name, $value ) { |
808
|
|
|
update_option( 'jetpack_' . $name, $value ); |
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
/** |
812
|
|
|
* Retrieve a network option value based on a network option name. |
813
|
|
|
* |
814
|
|
|
* @access public |
815
|
|
|
* |
816
|
|
|
* @param string $option Name of network option to retrieve. |
817
|
|
|
* @return mixed Value set for the network option. |
818
|
|
|
*/ |
819
|
|
|
public function get_site_option( $option ) { |
820
|
|
|
return get_option( 'jetpack_network_' . $option ); |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
/** |
824
|
|
|
* Update the value of a network option. |
825
|
|
|
* |
826
|
|
|
* @access public |
827
|
|
|
* |
828
|
|
|
* @param string $option Network option name. |
829
|
|
|
* @param mixed $value Network option value. |
830
|
|
|
* @return bool False if value was not updated and true if value was updated. |
831
|
|
|
*/ |
832
|
|
|
public function update_site_option( $option, $value ) { |
833
|
|
|
return update_option( 'jetpack_network_' . $option, $value ); |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* Remove a network option by name. |
838
|
|
|
* |
839
|
|
|
* @access public |
840
|
|
|
* |
841
|
|
|
* @param string $option Name of option to remove. |
842
|
|
|
* @return bool True, if option is successfully deleted. False on failure. |
843
|
|
|
*/ |
844
|
|
|
public function delete_site_option( $option ) { |
845
|
|
|
return delete_option( 'jetpack_network_' . $option ); |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* Retrieve the terms from a particular taxonomy. |
850
|
|
|
* |
851
|
|
|
* @access public |
852
|
|
|
* |
853
|
|
|
* @param string $taxonomy Taxonomy slug. |
854
|
|
|
* @return array Array of terms. |
855
|
|
|
*/ |
856
|
|
|
public function get_terms( $taxonomy ) { |
857
|
|
|
return get_terms( $taxonomy ); |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
/** |
861
|
|
|
* Retrieve a particular term. |
862
|
|
|
* |
863
|
|
|
* @access public |
864
|
|
|
* |
865
|
|
|
* @param string $taxonomy Taxonomy slug. |
866
|
|
|
* @param int $term_id ID of the term. |
867
|
|
|
* @param bool $is_term_id Whether this is a `term_id` or a `term_taxonomy_id`. |
868
|
|
|
* @return \WP_Term|\WP_Error Term object on success, \WP_Error object on failure. |
869
|
|
|
*/ |
870
|
|
|
public function get_term( $taxonomy, $term_id, $is_term_id = true ) { |
871
|
|
|
$t = $this->ensure_taxonomy( $taxonomy ); |
872
|
|
|
if ( ! $t || is_wp_error( $t ) ) { |
873
|
|
|
return $t; |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
return get_term( $term_id, $taxonomy ); |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
/** |
880
|
|
|
* Verify a taxonomy is legitimate and register it if necessary. |
881
|
|
|
* |
882
|
|
|
* @access private |
883
|
|
|
* |
884
|
|
|
* @param string $taxonomy Taxonomy slug. |
885
|
|
|
* @return bool|void|\WP_Error True if already exists; void if it was registered; \WP_Error on error. |
886
|
|
|
*/ |
887
|
|
|
private function ensure_taxonomy( $taxonomy ) { |
888
|
|
|
if ( ! taxonomy_exists( $taxonomy ) ) { |
889
|
|
|
// Try re-registering synced taxonomies. |
890
|
|
|
$taxonomies = $this->get_callable( 'taxonomies' ); |
891
|
|
|
if ( ! isset( $taxonomies[ $taxonomy ] ) ) { |
892
|
|
|
// Doesn't exist, or somehow hasn't been synced. |
893
|
|
|
return new \WP_Error( 'invalid_taxonomy', "The taxonomy '$taxonomy' doesn't exist" ); |
|
|
|
|
894
|
|
|
} |
895
|
|
|
$t = $taxonomies[ $taxonomy ]; |
896
|
|
|
|
897
|
|
|
return register_taxonomy( |
898
|
|
|
$taxonomy, |
899
|
|
|
$t->object_type, |
900
|
|
|
(array) $t |
901
|
|
|
); |
902
|
|
|
} |
903
|
|
|
|
904
|
|
|
return true; |
905
|
|
|
} |
906
|
|
|
|
907
|
|
|
/** |
908
|
|
|
* Retrieve all terms from a taxonomy that are related to an object with a particular ID. |
909
|
|
|
* |
910
|
|
|
* @access public |
911
|
|
|
* |
912
|
|
|
* @param int $object_id Object ID. |
913
|
|
|
* @param string $taxonomy Taxonomy slug. |
914
|
|
|
* @return array|bool|\WP_Error Array of terms on success, `false` if no terms or post doesn't exist, \WP_Error on failure. |
915
|
|
|
*/ |
916
|
|
|
public function get_the_terms( $object_id, $taxonomy ) { |
917
|
|
|
return get_the_terms( $object_id, $taxonomy ); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
/** |
921
|
|
|
* Insert or update a term. |
922
|
|
|
* |
923
|
|
|
* @access public |
924
|
|
|
* |
925
|
|
|
* @param \WP_Term $term_object Term object. |
926
|
|
|
* @return array|bool|\WP_Error Array of term_id and term_taxonomy_id if updated, true if inserted, \WP_Error on failure. |
927
|
|
|
*/ |
928
|
|
|
public function update_term( $term_object ) { |
929
|
|
|
$taxonomy = $term_object->taxonomy; |
930
|
|
|
global $wpdb; |
931
|
|
|
$exists = $wpdb->get_var( |
932
|
|
|
$wpdb->prepare( |
933
|
|
|
"SELECT EXISTS( SELECT 1 FROM $wpdb->terms WHERE term_id = %d )", |
934
|
|
|
$term_object->term_id |
935
|
|
|
) |
936
|
|
|
); |
937
|
|
|
if ( ! $exists ) { |
938
|
|
|
$term_object = sanitize_term( clone( $term_object ), $taxonomy, 'db' ); |
939
|
|
|
$term = array( |
940
|
|
|
'term_id' => $term_object->term_id, |
941
|
|
|
'name' => $term_object->name, |
942
|
|
|
'slug' => $term_object->slug, |
943
|
|
|
'term_group' => $term_object->term_group, |
944
|
|
|
); |
945
|
|
|
$term_taxonomy = array( |
946
|
|
|
'term_taxonomy_id' => $term_object->term_taxonomy_id, |
947
|
|
|
'term_id' => $term_object->term_id, |
948
|
|
|
'taxonomy' => $term_object->taxonomy, |
949
|
|
|
'description' => $term_object->description, |
950
|
|
|
'parent' => (int) $term_object->parent, |
951
|
|
|
'count' => (int) $term_object->count, |
952
|
|
|
); |
953
|
|
|
$wpdb->insert( $wpdb->terms, $term ); |
954
|
|
|
$wpdb->insert( $wpdb->term_taxonomy, $term_taxonomy ); |
955
|
|
|
|
956
|
|
|
return true; |
957
|
|
|
} |
958
|
|
|
|
959
|
|
|
return wp_update_term( $term_object->term_id, $taxonomy, (array) $term_object ); |
960
|
|
|
} |
961
|
|
|
|
962
|
|
|
/** |
963
|
|
|
* Delete a term by the term ID and its corresponding taxonomy. |
964
|
|
|
* |
965
|
|
|
* @access public |
966
|
|
|
* |
967
|
|
|
* @param int $term_id Term ID. |
968
|
|
|
* @param string $taxonomy Taxonomy slug. |
969
|
|
|
* @return bool|int|\WP_Error True on success, false if term doesn't exist. Zero if trying with default category. \WP_Error on invalid taxonomy. |
970
|
|
|
*/ |
971
|
|
|
public function delete_term( $term_id, $taxonomy ) { |
972
|
|
|
return wp_delete_term( $term_id, $taxonomy ); |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
/** |
976
|
|
|
* Add/update terms of a particular taxonomy of an object with the specified ID. |
977
|
|
|
* |
978
|
|
|
* @access public |
979
|
|
|
* |
980
|
|
|
* @param int $object_id The object to relate to. |
981
|
|
|
* @param string $taxonomy The context in which to relate the term to the object. |
982
|
|
|
* @param string|int|array $terms A single term slug, single term id, or array of either term slugs or ids. |
983
|
|
|
* @param bool $append Optional. If false will delete difference of terms. Default false. |
984
|
|
|
*/ |
985
|
|
|
public function update_object_terms( $object_id, $taxonomy, $terms, $append ) { |
986
|
|
|
wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); |
987
|
|
|
} |
988
|
|
|
|
989
|
|
|
/** |
990
|
|
|
* Remove certain term relationships from the specified object. |
991
|
|
|
* |
992
|
|
|
* @access public |
993
|
|
|
* |
994
|
|
|
* @todo Refactor to not use interpolated values when preparing the SQL query. |
995
|
|
|
* |
996
|
|
|
* @param int $object_id ID of the object. |
997
|
|
|
* @param array $tt_ids Term taxonomy IDs. |
998
|
|
|
* @return bool True on success, false on failure. |
999
|
|
|
*/ |
1000
|
|
|
public function delete_object_terms( $object_id, $tt_ids ) { |
1001
|
|
|
global $wpdb; |
1002
|
|
|
|
1003
|
|
|
if ( is_array( $tt_ids ) && ! empty( $tt_ids ) ) { |
1004
|
|
|
// Escape. |
1005
|
|
|
$tt_ids_sanitized = array_map( 'intval', $tt_ids ); |
1006
|
|
|
|
1007
|
|
|
$taxonomies = array(); |
1008
|
|
|
foreach ( $tt_ids_sanitized as $tt_id ) { |
1009
|
|
|
$term = get_term_by( 'term_taxonomy_id', $tt_id ); |
1010
|
|
|
$taxonomies[ $term->taxonomy ][] = $tt_id; |
1011
|
|
|
} |
1012
|
|
|
$in_tt_ids = implode( ', ', $tt_ids_sanitized ); |
1013
|
|
|
|
1014
|
|
|
/** |
1015
|
|
|
* Fires immediately before an object-term relationship is deleted. |
1016
|
|
|
* |
1017
|
|
|
* @since 2.9.0 |
1018
|
|
|
* |
1019
|
|
|
* @param int $object_id Object ID. |
1020
|
|
|
* @param array $tt_ids An array of term taxonomy IDs. |
1021
|
|
|
*/ |
1022
|
|
|
do_action( 'delete_term_relationships', $object_id, $tt_ids_sanitized ); |
1023
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
1024
|
|
|
$deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); |
1025
|
|
|
foreach ( $taxonomies as $taxonomy => $taxonomy_tt_ids ) { |
1026
|
|
|
$this->ensure_taxonomy( $taxonomy ); |
1027
|
|
|
wp_cache_delete( $object_id, $taxonomy . '_relationships' ); |
1028
|
|
|
/** |
1029
|
|
|
* Fires immediately after an object-term relationship is deleted. |
1030
|
|
|
* |
1031
|
|
|
* @since 2.9.0 |
1032
|
|
|
* |
1033
|
|
|
* @param int $object_id Object ID. |
1034
|
|
|
* @param array $tt_ids An array of term taxonomy IDs. |
1035
|
|
|
*/ |
1036
|
|
|
do_action( 'deleted_term_relationships', $object_id, $taxonomy_tt_ids ); |
1037
|
|
|
wp_update_term_count( $taxonomy_tt_ids, $taxonomy ); |
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
return (bool) $deleted; |
1041
|
|
|
} |
1042
|
|
|
|
1043
|
|
|
return false; |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
|
|
/** |
1047
|
|
|
* Retrieve the number of users. |
1048
|
|
|
* Not supported in this replicastore. |
1049
|
|
|
* |
1050
|
|
|
* @access public |
1051
|
|
|
*/ |
1052
|
|
|
public function user_count() { |
1053
|
|
|
// Noop. |
1054
|
|
|
} |
1055
|
|
|
|
1056
|
|
|
/** |
1057
|
|
|
* Retrieve a user object by the user ID. |
1058
|
|
|
* |
1059
|
|
|
* @access public |
1060
|
|
|
* |
1061
|
|
|
* @param int $user_id User ID. |
1062
|
|
|
* @return \WP_User User object. |
1063
|
|
|
*/ |
1064
|
|
|
public function get_user( $user_id ) { |
1065
|
|
|
return \WP_User::get_instance( $user_id ); |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
/** |
1069
|
|
|
* Insert or update a user. |
1070
|
|
|
* Not supported in this replicastore. |
1071
|
|
|
* |
1072
|
|
|
* @access public |
1073
|
|
|
* @throws \Exception If this method is invoked. |
1074
|
|
|
* |
1075
|
|
|
* @param \WP_User $user User object. |
1076
|
|
|
*/ |
1077
|
|
|
public function upsert_user( $user ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
1078
|
|
|
$this->invalid_call(); |
1079
|
|
|
} |
1080
|
|
|
|
1081
|
|
|
/** |
1082
|
|
|
* Delete a user. |
1083
|
|
|
* Not supported in this replicastore. |
1084
|
|
|
* |
1085
|
|
|
* @access public |
1086
|
|
|
* @throws \Exception If this method is invoked. |
1087
|
|
|
* |
1088
|
|
|
* @param int $user_id User ID. |
1089
|
|
|
*/ |
1090
|
|
|
public function delete_user( $user_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
1091
|
|
|
$this->invalid_call(); |
1092
|
|
|
} |
1093
|
|
|
|
1094
|
|
|
/** |
1095
|
|
|
* Update/insert user locale. |
1096
|
|
|
* Not supported in this replicastore. |
1097
|
|
|
* |
1098
|
|
|
* @access public |
1099
|
|
|
* @throws \Exception If this method is invoked. |
1100
|
|
|
* |
1101
|
|
|
* @param int $user_id User ID. |
1102
|
|
|
* @param string $local The user locale. |
1103
|
|
|
*/ |
1104
|
|
|
public function upsert_user_locale( $user_id, $local ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
1105
|
|
|
$this->invalid_call(); |
1106
|
|
|
} |
1107
|
|
|
|
1108
|
|
|
/** |
1109
|
|
|
* Delete user locale. |
1110
|
|
|
* Not supported in this replicastore. |
1111
|
|
|
* |
1112
|
|
|
* @access public |
1113
|
|
|
* @throws \Exception If this method is invoked. |
1114
|
|
|
* |
1115
|
|
|
* @param int $user_id User ID. |
1116
|
|
|
*/ |
1117
|
|
|
public function delete_user_locale( $user_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
1118
|
|
|
$this->invalid_call(); |
1119
|
|
|
} |
1120
|
|
|
|
1121
|
|
|
/** |
1122
|
|
|
* Retrieve the user locale. |
1123
|
|
|
* |
1124
|
|
|
* @access public |
1125
|
|
|
* |
1126
|
|
|
* @param int $user_id User ID. |
1127
|
|
|
* @return string The user locale. |
1128
|
|
|
*/ |
1129
|
|
|
public function get_user_locale( $user_id ) { |
1130
|
|
|
return get_user_locale( $user_id ); |
1131
|
|
|
} |
1132
|
|
|
|
1133
|
|
|
/** |
1134
|
|
|
* Retrieve the allowed mime types for the user. |
1135
|
|
|
* Not supported in this replicastore. |
1136
|
|
|
* |
1137
|
|
|
* @access public |
1138
|
|
|
* |
1139
|
|
|
* @param int $user_id User ID. |
1140
|
|
|
*/ |
1141
|
|
|
public function get_allowed_mime_types( $user_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
1142
|
|
|
// Noop. |
1143
|
|
|
} |
1144
|
|
|
|
1145
|
|
|
/** |
1146
|
|
|
* Retrieve all the checksums we are interested in. |
1147
|
|
|
* Currently that is posts, comments, post meta and comment meta. |
1148
|
|
|
* |
1149
|
|
|
* @access public |
1150
|
|
|
* |
1151
|
|
|
* @return array Checksums. |
1152
|
|
|
*/ |
1153
|
|
|
public function checksum_all() { |
1154
|
|
|
$post_checksum = $this->checksum_histogram( 'posts', $this->calculate_buckets( 'posts' ) ); |
1155
|
|
|
$comments_checksum = $this->checksum_histogram( 'comments', $this->calculate_buckets( 'comments' ) ); |
1156
|
|
|
$post_meta_checksum = $this->checksum_histogram( 'postmeta', $this->calculate_buckets( 'postmeta' ) ); |
1157
|
|
|
$comment_meta_checksum = $this->checksum_histogram( 'commentmeta', $this->calculate_buckets( 'commentmeta' ) ); |
1158
|
|
|
$terms_checksum = $this->checksum_histogram( 'terms', $this->calculate_buckets( 'terms' ) ); |
1159
|
|
|
$term_relationships_checksum = $this->checksum_histogram( 'term_relationships', $this->calculate_buckets( 'term_relationships' ) ); |
1160
|
|
|
$term_taxonomy_checksum = $this->checksum_histogram( 'term_taxonomy', $this->calculate_buckets( 'term_taxonomy' ) ); |
1161
|
|
|
|
1162
|
|
|
return array( |
1163
|
|
|
'posts' => $this->summarize_checksum_histogram( $post_checksum ), |
1164
|
|
|
'comments' => $this->summarize_checksum_histogram( $comments_checksum ), |
1165
|
|
|
'post_meta' => $this->summarize_checksum_histogram( $post_meta_checksum ), |
1166
|
|
|
'comment_meta' => $this->summarize_checksum_histogram( $comment_meta_checksum ), |
1167
|
|
|
'terms' => $this->summarize_checksum_histogram( $terms_checksum ), |
1168
|
|
|
'term_relationships' => $this->summarize_checksum_histogram( $term_relationships_checksum ), |
1169
|
|
|
'term_taxonomy' => $this->summarize_checksum_histogram( $term_taxonomy_checksum ), |
1170
|
|
|
); |
1171
|
|
|
} |
1172
|
|
|
|
1173
|
|
|
/** |
1174
|
|
|
* Return the summarized checksum from buckets or the WP_Error. |
1175
|
|
|
* |
1176
|
|
|
* @param array $histogram checksum_histogram result. |
1177
|
|
|
* |
1178
|
|
|
* @return int|WP_Error checksum or Error. |
1179
|
|
|
*/ |
1180
|
|
|
protected function summarize_checksum_histogram( $histogram ) { |
1181
|
|
|
if ( is_wp_error( $histogram ) ) { |
1182
|
|
|
return $histogram; |
1183
|
|
|
} else { |
1184
|
|
|
return array_sum( $histogram ); |
1185
|
|
|
} |
1186
|
|
|
} |
1187
|
|
|
|
1188
|
|
|
/** |
1189
|
|
|
* Grabs the minimum and maximum object ids for the given parameters. |
1190
|
|
|
* |
1191
|
|
|
* @access public |
1192
|
|
|
* |
1193
|
|
|
* @param string $id_field The id column in the table to query. |
1194
|
|
|
* @param string $object_table The table to query. |
1195
|
|
|
* @param string $where A sql where clause without 'WHERE'. |
1196
|
|
|
* @param int $bucket_size The maximum amount of objects to include in the query. |
1197
|
|
|
* For `term_relationships` table, the bucket size will refer to the amount |
1198
|
|
|
* of distinct object ids. This will likely include more database rows than |
1199
|
|
|
* the bucket size implies. |
1200
|
|
|
* |
1201
|
|
|
* @return object An object with min_id and max_id properties. |
1202
|
|
|
*/ |
1203
|
|
|
public function get_min_max_object_id( $id_field, $object_table, $where, $bucket_size ) { |
1204
|
|
|
global $wpdb; |
1205
|
|
|
|
1206
|
|
|
// The term relationship table's unique key is a combination of 2 columns. `DISTINCT` helps us get a more acurate query. |
1207
|
|
|
$distinct_sql = ( $wpdb->term_relationships === $object_table ) ? 'DISTINCT' : ''; |
1208
|
|
|
$where_sql = $where ? "WHERE $where" : ''; |
1209
|
|
|
|
1210
|
|
|
// Since MIN() and MAX() do not work with LIMIT, we'll need to adjust the dataset we query if a limit is present. |
1211
|
|
|
// With a limit present, we'll look at a dataset consisting of object_ids that meet the constructs of the $where clause. |
1212
|
|
|
// Without a limit, we can use the actual table as a dataset. |
1213
|
|
|
$from = $bucket_size ? |
1214
|
|
|
"( SELECT $distinct_sql $id_field FROM $object_table $where_sql ORDER BY $id_field ASC LIMIT $bucket_size ) as ids" : |
1215
|
|
|
"$object_table $where_sql ORDER BY $id_field ASC"; |
1216
|
|
|
|
1217
|
|
|
return $wpdb->get_row( |
1218
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
1219
|
|
|
"SELECT MIN($id_field) as min, MAX($id_field) as max FROM $from" |
1220
|
|
|
); |
1221
|
|
|
} |
1222
|
|
|
|
1223
|
|
|
/** |
1224
|
|
|
* Retrieve the checksum histogram for a specific object type. |
1225
|
|
|
* |
1226
|
|
|
* @access public |
1227
|
|
|
* |
1228
|
|
|
* @param string $table Object type. |
1229
|
|
|
* @param int $buckets Number of buckets to split the objects to. |
1230
|
|
|
* @param int $start_id Minimum object ID. |
|
|
|
|
1231
|
|
|
* @param int $end_id Maximum object ID. |
|
|
|
|
1232
|
|
|
* @param array $columns Table columns to calculate the checksum from. |
|
|
|
|
1233
|
|
|
* @param bool $strip_non_ascii Whether to strip non-ASCII characters. |
1234
|
|
|
* @param string $salt Salt, used for $wpdb->prepare()'s args. |
1235
|
|
|
* |
1236
|
|
|
* @return array The checksum histogram. |
1237
|
|
|
*/ |
1238
|
|
|
public function checksum_histogram( $table, $buckets, $start_id = null, $end_id = null, $columns = null, $strip_non_ascii = true, $salt = '' ) { |
1239
|
|
|
global $wpdb; |
1240
|
|
|
|
1241
|
|
|
$wpdb->queries = array(); |
1242
|
|
|
|
1243
|
|
|
$checksum_table = new Table_Checksum( $table, $salt ); |
1244
|
|
|
$range_edges = $checksum_table->get_range_edges( $start_id, $end_id ); |
1245
|
|
|
|
1246
|
|
|
$object_count = $range_edges['item_count']; |
1247
|
|
|
|
1248
|
|
|
$bucket_size = (int) ceil( $object_count / $buckets ); |
1249
|
|
|
$previous_max_id = max( 0, $range_edges['min_range'] ); |
1250
|
|
|
$histogram = array(); |
1251
|
|
|
|
1252
|
|
|
do { |
1253
|
|
|
$ids_range = $checksum_table->get_range_edges( $previous_max_id, null, $bucket_size ); |
1254
|
|
|
|
1255
|
|
|
if ( empty( $ids_range['min_range'] ) || empty( $ids_range['max_range'] ) ) { |
1256
|
|
|
// Nothing to checksum here... |
1257
|
|
|
break; |
1258
|
|
|
} |
1259
|
|
|
|
1260
|
|
|
// Get the checksum value. |
1261
|
|
|
$batch_checksum = $checksum_table->calculate_checksum( $ids_range['min_range'], $ids_range['max_range'] ); |
1262
|
|
|
|
1263
|
|
|
if ( is_wp_error( $batch_checksum ) ) { |
1264
|
|
|
return $batch_checksum; |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
if ( $ids_range['min_range'] === $ids_range['max_range'] ) { |
1268
|
|
|
$histogram[ $ids_range['min_range'] ] = $batch_checksum; |
1269
|
|
|
} else { |
1270
|
|
|
$histogram[ "{$ids_range[ 'min_range' ]}-{$ids_range[ 'max_range' ]}" ] = $batch_checksum; |
1271
|
|
|
} |
1272
|
|
|
|
1273
|
|
|
$previous_max_id = $ids_range['max_range'] + 1; |
1274
|
|
|
// If we've reached the max_range lets bail out. |
1275
|
|
|
if ( $previous_max_id >= $range_edges['max_range'] ) { |
1276
|
|
|
break; |
1277
|
|
|
} |
1278
|
|
|
} while ( true ); |
1279
|
|
|
|
1280
|
|
|
return $histogram; |
1281
|
|
|
} |
1282
|
|
|
|
1283
|
|
|
/** |
1284
|
|
|
* Retrieve the type of the checksum. |
1285
|
|
|
* |
1286
|
|
|
* @access public |
1287
|
|
|
* |
1288
|
|
|
* @return string Type of the checksum. |
1289
|
|
|
*/ |
1290
|
|
|
public function get_checksum_type() { |
1291
|
|
|
return 'sum'; |
1292
|
|
|
} |
1293
|
|
|
|
1294
|
|
|
/** |
1295
|
|
|
* Used in methods that are not implemented and shouldn't be invoked. |
1296
|
|
|
* |
1297
|
|
|
* @access private |
1298
|
|
|
* @throws \Exception If this method is invoked. |
1299
|
|
|
*/ |
1300
|
|
|
private function invalid_call() { |
1301
|
|
|
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
1302
|
|
|
$backtrace = debug_backtrace(); |
1303
|
|
|
$caller = $backtrace[1]['function']; |
1304
|
|
|
throw new \Exception( "This function $caller is not supported on the WP Replicastore" ); |
1305
|
|
|
} |
1306
|
|
|
|
1307
|
|
|
/** |
1308
|
|
|
* Determine number of buckets to use in full table checksum. |
1309
|
|
|
* |
1310
|
|
|
* @param string $table Object Type. |
1311
|
|
|
* @param int $start_id Min Object ID. |
|
|
|
|
1312
|
|
|
* @param int $end_id Max Object ID. |
|
|
|
|
1313
|
|
|
* @return int Number of Buckets to use. |
1314
|
|
|
*/ |
1315
|
|
|
private function calculate_buckets( $table, $start_id = null, $end_id = null ) { |
1316
|
|
|
// Get # of objects. |
1317
|
|
|
$checksum_table = new Table_Checksum( $table ); |
1318
|
|
|
$range_edges = $checksum_table->get_range_edges( $start_id, $end_id ); |
1319
|
|
|
$object_count = $range_edges['item_count']; |
1320
|
|
|
|
1321
|
|
|
// Ensure no division by 0. |
1322
|
|
|
if ( 0 === (int) $object_count ) { |
1323
|
|
|
return 1; |
1324
|
|
|
} |
1325
|
|
|
|
1326
|
|
|
// Default Bucket sizes. |
1327
|
|
|
$bucket_size = 10000; // Default bucket size is 10,000 items. |
1328
|
|
|
switch ( $table ) { |
1329
|
|
|
case 'postmeta': |
1330
|
|
|
case 'commentmeta': |
1331
|
|
|
$bucket_size = 5000; // Meta bucket size is restricted to 5,000 items. |
1332
|
|
|
} |
1333
|
|
|
|
1334
|
|
|
return (int) ceil( $object_count / $bucket_size ); |
1335
|
|
|
} |
1336
|
|
|
} |
1337
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.