forked from wp-cli/cache-command
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransient_Command.php
More file actions
614 lines (561 loc) · 17.4 KB
/
Transient_Command.php
File metadata and controls
614 lines (561 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
use WP_CLI\Utils;
/**
* Adds, gets, and deletes entries in the WordPress Transient Cache.
*
* By default, the transient cache uses the WordPress database to persist values
* between requests. On a single site installation, values are stored in the
* `wp_options` table. On a multisite installation, values are stored in the
* `wp_options` or the `wp_sitemeta` table, depending on use of the `--network`
* flag.
*
* When a persistent object cache drop-in is installed (e.g. Redis or Memcached),
* the transient cache skips the database and simply wraps the WP Object Cache.
*
* ## EXAMPLES
*
* # Set transient.
* $ wp transient set sample_key "test data" 3600
* Success: Transient added.
*
* # Get transient.
* $ wp transient get sample_key
* test data
*
* # Delete transient.
* $ wp transient delete sample_key
* Success: Transient deleted.
*
* # Delete expired transients.
* $ wp transient delete --expired
* Success: 12 expired transients deleted from the database.
*
* # Delete all transients.
* $ wp transient delete --all
* Success: 14 transients deleted from the database.
*/
class Transient_Command extends WP_CLI_Command {
/**
* Gets a transient value.
*
* For a more complete explanation of the transient cache, including the
* network|site cache, please see docs for `wp transient`.
*
* ## OPTIONS
*
* <key>
* : Key for the transient.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* [--network]
* : Get the value of a network|site transient. On single site, this is
* is a specially-named cache key. On multisite, this is a global cache
* (instead of local to the site).
*
* ## EXAMPLES
*
* $ wp transient get sample_key
* test data
*
* $ wp transient get random_key
* Warning: Transient with key "random_key" is not set.
*/
public function get( $args, $assoc_args ) {
list( $key ) = $args;
$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
$value = $func( $key );
if ( false === $value ) {
WP_CLI::warning( 'Transient with key "' . $key . '" is not set.' );
exit;
}
WP_CLI::print_value( $value, $assoc_args );
}
/**
* Sets a transient value.
*
* `<expiration>` is the time until expiration, in seconds.
*
* For a more complete explanation of the transient cache, including the
* network|site cache, please see docs for `wp transient`.
*
* ## OPTIONS
*
* <key>
* : Key for the transient.
*
* <value>
* : Value to be set for the transient.
*
* [<expiration>]
* : Time until expiration, in seconds.
*
* [--network]
* : Set the value of a network|site transient. On single site, this is
* is a specially-named cache key. On multisite, this is a global cache
* (instead of local to the site).
*
* ## EXAMPLES
*
* $ wp transient set sample_key "test data" 3600
* Success: Transient added.
*/
public function set( $args, $assoc_args ) {
list( $key, $value ) = $args;
$expiration = Utils\get_flag_value( $args, 2, 0 );
$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
if ( $func( $key, $value, $expiration ) ) {
WP_CLI::success( 'Transient added.' );
} else {
WP_CLI::error( 'Transient could not be set.' );
}
}
/**
* Deletes a transient value.
*
* For a more complete explanation of the transient cache, including the
* network|site cache, please see docs for `wp transient`.
*
* ## OPTIONS
*
* [<key>]
* : Key for the transient.
*
* [--network]
* : Delete the value of a network|site transient. On single site, this is
* is a specially-named cache key. On multisite, this is a global cache
* (instead of local to the site).
*
* [--all]
* : Delete all transients.
*
* [--expired]
* : Delete all expired transients.
*
* ## EXAMPLES
*
* # Delete transient.
* $ wp transient delete sample_key
* Success: Transient deleted.
*
* # Delete expired transients.
* $ wp transient delete --expired
* Success: 12 expired transients deleted from the database.
*
* # Delete expired site transients.
* $ wp transient delete --expired --network
* Success: 1 expired transient deleted from the database.
*
* # Delete all transients.
* $ wp transient delete --all
* Success: 14 transients deleted from the database.
*
* # Delete all site transients.
* $ wp transient delete --all --network
* Success: 2 transients deleted from the database.
*
* # Delete all transients in a multisite.
* $ wp transient delete --all --network && wp site list --field=url | xargs -n1 -I % wp --url=% transient delete --all
*/
public function delete( $args, $assoc_args ) {
$key = ( ! empty( $args ) ) ? $args[0] : null;
$all = Utils\get_flag_value( $assoc_args, 'all' );
$expired = Utils\get_flag_value( $assoc_args, 'expired' );
$network = Utils\get_flag_value( $assoc_args, 'network' );
if ( true === $all ) {
$this->delete_all( $network );
return;
} elseif ( true === $expired ) {
$this->delete_expired( $network );
return;
}
if ( ! $key ) {
WP_CLI::error( 'Please specify transient key, or use --all or --expired.' );
}
$func = $network ? 'delete_site_transient' : 'delete_transient';
if ( $func( $key ) ) {
WP_CLI::success( 'Transient deleted.' );
} else {
$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
if ( $func( $key ) ) {
WP_CLI::error( 'Transient was not deleted even though the transient appears to exist.' );
} else {
WP_CLI::warning( 'Transient was not deleted; however, the transient does not appear to exist.' );
}
}
}
/**
* Determines the type of transients implementation.
*
* Indicates whether the transients API is using an object cache or the
* database.
*
* For a more complete explanation of the transient cache, including the
* network|site cache, please see docs for `wp transient`.
*
* ## EXAMPLES
*
* $ wp transient type
* Transients are saved to the database.
*/
public function type() {
if ( wp_using_ext_object_cache() ) {
$message = 'Transients are saved to the object cache.';
} else {
$message = 'Transients are saved to the database.';
}
WP_CLI::line( $message );
}
/**
* Lists transients and their values.
*
* ## OPTIONS
*
* [--search=<pattern>]
* : Use wildcards ( * and ? ) to match transient name.
*
* [--exclude=<pattern>]
* : Pattern to exclude. Use wildcards ( * and ? ) to match transient name.
*
* [--network]
* : Get the values of network|site transients. On single site, this is
* a specially-named cache key. On multisite, this is a global cache
* (instead of local to the site).
*
* [--unserialize]
* : Unserialize transient values in output.
*
* [--human-readable]
* : Human-readable output for expirations.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : The serialization format for the value.
* ---
* default: table
* options:
* - table
* - json
* - csv
* - count
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* This field will be displayed by default for each matching option:
*
* * name
* * value
* * expiration
*
* ## EXAMPLES
*
* # List all transients
* $ wp transient list
* +------+-------+---------------+
* | name | value | expiration |
* +------+-------+---------------+
* | foo | bar | 39 mins |
* | foo2 | bar2 | no expiration |
* | foo3 | bar2 | expired |
* | foo4 | bar4 | 4 hours |
* +------+-------+---------------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
global $wpdb;
if ( wp_using_ext_object_cache() ) {
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only shows those stored in the database.' );
}
$network = Utils\get_flag_value( $assoc_args, 'network', false );
$unserialize = Utils\get_flag_value( $assoc_args, 'unserialize', false );
$human_readable = Utils\get_flag_value( $assoc_args, 'human-readable', false );
$fields = array( 'name', 'value', 'expiration' );
if ( isset( $assoc_args['fields'] ) ) {
$fields = explode( ',', $assoc_args['fields'] );
}
$pattern = '%';
$exclude = '';
if ( isset( $assoc_args['search'] ) ) {
$pattern = Utils\esc_like( $assoc_args['search'] );
// Substitute wildcards.
$pattern = str_replace(
array( '*', '?' ),
array( '%', '_' ),
$pattern
);
}
if ( isset( $assoc_args['exclude'] ) ) {
$exclude = Utils\esc_like( $assoc_args['exclude'] );
// Substitute wildcards.
$exclude = str_replace(
array( '*', '?' ),
array( '%', '_' ),
$exclude
);
}
if ( $network ) {
if ( is_multisite() ) {
$where = $wpdb->prepare(
'WHERE `meta_key` LIKE %s',
Utils\esc_like( '_site_transient_' ) . $pattern
);
$where .= $wpdb->prepare(
' AND meta_key NOT LIKE %s',
Utils\esc_like( '_site_transient_timeout_' ) . '%'
);
if ( $exclude ) {
$where .= $wpdb->prepare(
' AND meta_key NOT LIKE %s',
Utils\esc_like( '_site_transient_' ) . $exclude
);
}
$query = "SELECT `meta_key` as `name`, `meta_value` as `value` FROM {$wpdb->sitemeta} {$where}";
} else {
$where = $wpdb->prepare(
'WHERE `option_name` LIKE %s',
Utils\esc_like( '_site_transient_' ) . $pattern
);
$where .= $wpdb->prepare(
' AND option_name NOT LIKE %s',
Utils\esc_like( '_site_transient_timeout_' ) . '%'
);
if ( $exclude ) {
$where .= $wpdb->prepare(
' AND option_name NOT LIKE %s',
Utils\esc_like( '_site_transient_' ) . $exclude
);
}
$query = "SELECT `option_name` as `name`, `option_value` as `value` FROM {$wpdb->options} {$where}";
}
} else {
$where = $wpdb->prepare(
'WHERE `option_name` LIKE %s',
Utils\esc_like( '_transient_' ) . $pattern
);
$where .= $wpdb->prepare(
' AND option_name NOT LIKE %s',
Utils\esc_like( '_transient_timeout_' ) . '%'
);
if ( $exclude ) {
$where .= $wpdb->prepare(
' AND option_name NOT LIKE %s',
Utils\esc_like( '_transient_' ) . $exclude
);
}
$query = "SELECT `option_name` as `name`, `option_value` as `value` FROM {$wpdb->options} {$where}";
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared properly above.
$results = $wpdb->get_results( $query );
foreach ( $results as $result ) {
$result->name = str_replace( array( '_site_transient_', '_transient_' ), '', $result->name );
$result->expiration = $this->get_transient_expiration( $result->name, $network, $human_readable );
if ( $unserialize ) {
$result->value = maybe_unserialize( $result->value );
}
}
$formatter = new \WP_CLI\Formatter(
$assoc_args,
$fields
);
$formatter->display_items( $results );
}
/**
* Retrieves the expiration time.
*
* @param string $name Transient name.
* @param bool $is_site_transient Optional. Whether this is a site transient. Default false.
* @param bool $human_readable Optional. Whether to return the difference between now and the
* expiration time in a human-readable format. Default false.
* @return string Expiration time string.
*/
private function get_transient_expiration( $name, $is_site_transient = false, $human_readable = false ) {
if ( $is_site_transient ) {
if ( is_multisite() ) {
$expiration = (int) get_site_option( '_site_transient_timeout_' . $name );
} else {
$expiration = (int) get_option( '_site_transient_timeout_' . $name );
}
} else {
$expiration = (int) get_option( '_transient_timeout_' . $name );
}
if ( 0 === $expiration ) {
return $human_readable ? 'never expires' : 'false';
}
if ( ! $human_readable ) {
return $expiration;
}
$now = time();
if ( $now > $expiration ) {
return 'expired';
}
return human_time_diff( $now, $expiration );
}
/**
* Deletes all expired transients.
*
* Only deletes the expired transients from the database.
*
* @param bool $network Whether to delete transients or network|site transients.
*/
private function delete_expired( $network ) {
global $wpdb;
$count = 0;
if ( ! $network ) {
$count += $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
WHERE a.option_name LIKE %s
AND a.option_name NOT LIKE %s
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
AND b.option_value < %d",
Utils\esc_like( '_transient_' ) . '%',
Utils\esc_like( '_transient_timeout_' ) . '%',
time()
)
);
} elseif ( ! is_multisite() ) {
// Non-Multisite stores site transients in the options table.
$count += $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
WHERE a.option_name LIKE %s
AND a.option_name NOT LIKE %s
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
AND b.option_value < %d",
Utils\esc_like( '_site_transient_' ) . '%',
Utils\esc_like( '_site_transient_timeout_' ) . '%',
time()
)
);
} else {
// Multisite stores site transients in the sitemeta table.
$count += $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
WHERE a.meta_key LIKE %s
AND a.meta_key NOT LIKE %s
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
AND b.meta_value < %d",
Utils\esc_like( '_site_transient_' ) . '%',
Utils\esc_like( '_site_transient_timeout_' ) . '%',
time()
)
);
}
// The above queries delete the transient and the transient timeout
// thus each transient is counted twice.
$count = $count / 2;
if ( $count > 0 ) {
WP_CLI::success(
sprintf(
'%d expired %s deleted from the database.',
$count,
Utils\pluralize( 'transient', $count )
)
);
} else {
WP_CLI::success( 'No expired transients found.' );
}
if ( wp_using_ext_object_cache() ) {
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only deletes those stored in the database. You must flush the cache to delete all transients.' );
}
}
/**
* Deletes all transients.
*
* Only deletes the transients from the database.
*
* @param bool $network Whether to delete transients or network|site transients.
*/
private function delete_all( $network ) {
global $wpdb;
// To ensure proper count values we first delete all transients with a timeout
// and then the remaining transients without a timeout.
$count = 0;
if ( ! $network ) {
$deleted = $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
WHERE a.option_name LIKE %s
AND a.option_name NOT LIKE %s
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )",
Utils\esc_like( '_transient_' ) . '%',
Utils\esc_like( '_transient_timeout_' ) . '%'
)
);
$count += $deleted / 2; // Ignore affected rows for timeouts.
$count += $wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
Utils\esc_like( '_transient_' ) . '%'
)
);
} elseif ( ! is_multisite() ) {
// Non-Multisite stores site transients in the options table.
$deleted = $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
WHERE a.option_name LIKE %s
AND a.option_name NOT LIKE %s
AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )",
Utils\esc_like( '_site_transient_' ) . '%',
Utils\esc_like( '_site_transient_timeout_' ) . '%'
)
);
$count += $deleted / 2; // Ignore affected rows for timeouts.
$count += $wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
Utils\esc_like( '_site_transient_' ) . '%'
)
);
} else {
// Multisite stores site transients in the sitemeta table.
$deleted = $wpdb->query(
$wpdb->prepare(
"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
WHERE a.meta_key LIKE %s
AND a.meta_key NOT LIKE %s
AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )",
Utils\esc_like( '_site_transient_' ) . '%',
Utils\esc_like( '_site_transient_timeout_' ) . '%'
)
);
$count += $deleted / 2; // Ignore affected rows for timeouts.
$count += $wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE %s",
Utils\esc_like( '_site_transient_' ) . '%'
)
);
}
if ( $count > 0 ) {
WP_CLI::success(
sprintf(
'%d %s deleted from the database.',
$count,
Utils\pluralize( 'transient', $count )
)
);
} else {
WP_CLI::success( 'No transients found.' );
}
if ( wp_using_ext_object_cache() ) {
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only deletes those stored in the database. You must flush the cache to delete all transients.' );
}
}
}