Skip to content

Commit 59ca193

Browse files
committed
Add --human-readable flag for expiration times
1 parent 392e680 commit 59ca193

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

features/transient.feature

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -379,44 +379,88 @@ Feature: Manage WordPress transient cache
379379
Given a WP install
380380
And I run `wp transient set foo bar`
381381
And I run `wp transient set foo2 bar2 610`
382-
And I run `wp transient set foo3 bar3 --network`
383-
And I run `wp transient set foo4 bar4 610 --network`
382+
And I run `wp option update _transient_timeout_foo2 95649119999`
383+
And I run `wp transient set foo3 bar3 300`
384+
And I run `wp option update _transient_timeout_foo3 1321009871`
385+
And I run `wp transient set foo4 bar4 --network`
386+
And I run `wp transient set foo5 bar5 610 --network`
387+
And I run `wp option update _site_transient_timeout_foo5 95649119999`
388+
And I run `wp transient set foo6 bar6 300 --network`
389+
And I run `wp option update _site_transient_timeout_foo6 1321009871`
384390

385391
When I run `wp transient list --format=csv`
386392
Then STDOUT should be:
387393
"""
388394
name,value,expiration
389-
foo,bar,"no expiration"
390-
foo2,bar2,"10 mins"
395+
foo,bar,false
396+
foo2,bar2,95649119999
397+
foo3,bar3,1321009871
398+
"""
399+
400+
When I run `wp transient list --format=csv --human-readable`
401+
Then STDOUT should contain:
402+
"""
403+
foo,bar,"never expires"
404+
"""
405+
And STDOUT should contain:
406+
"""
407+
foo3,bar3,expired
408+
"""
409+
And STDOUT should not contain:
410+
"""
411+
foo2,bar2,95649119999
391412
"""
392413

393414
When I run `wp transient list --network --format=csv`
394415
Then STDOUT should be:
395416
"""
396417
name,value,expiration
397-
foo3,bar3,"no expiration"
398-
foo4,bar4,"10 mins"
418+
foo4,bar4,false
419+
foo5,bar5,95649119999
420+
foo6,bar6,1321009871
399421
"""
400422

401423
Scenario: List transients on multisite
402424
Given a WP multisite install
403425
And I run `wp transient set foo bar`
404426
And I run `wp transient set foo2 bar2 610`
405-
And I run `wp transient set foo3 bar3 --network`
406-
And I run `wp transient set foo4 bar4 610 --network`
427+
And I run `wp option update _transient_timeout_foo2 95649119999`
428+
And I run `wp transient set foo3 bar3 300`
429+
And I run `wp option update _transient_timeout_foo3 1321009871`
430+
And I run `wp transient set foo4 bar4 --network`
431+
And I run `wp transient set foo5 bar5 610 --network`
432+
And I run `wp site option update _site_transient_timeout_foo5 95649119999`
433+
And I run `wp transient set foo6 bar6 300 --network`
434+
And I run `wp site option update _site_transient_timeout_foo6 1321009871`
407435

408436
When I run `wp transient list --format=csv`
409437
Then STDOUT should be:
410438
"""
411439
name,value,expiration
412-
foo,bar,"no expiration"
413-
foo2,bar2,"10 mins"
440+
foo,bar,false
441+
foo2,bar2,95649119999
442+
foo3,bar3,1321009871
443+
"""
444+
445+
When I run `wp transient list --format=csv --human-readable`
446+
Then STDOUT should contain:
447+
"""
448+
foo,bar,"never expires"
449+
"""
450+
And STDOUT should contain:
451+
"""
452+
foo3,bar3,expired
453+
"""
454+
And STDOUT should not contain:
455+
"""
456+
foo2,bar2,95649119999
414457
"""
415458

416459
When I run `wp transient list --network --format=csv`
417460
Then STDOUT should be:
418461
"""
419462
name,value,expiration
420-
foo3,bar3,"no expiration"
421-
foo4,bar4,"10 mins"
463+
foo4,bar4,false
464+
foo5,bar5,95649119999
465+
foo6,bar6,1321009871
422466
"""

src/Transient_Command.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ public function type() {
248248
* [--fields=<fields>]
249249
* : Limit the output to specific object fields.
250250
*
251+
* [--human-readable]
252+
* : Human-readable output for expirations.
253+
*
251254
* [--format=<format>]
252255
* : The serialization format for the value.
253256
* ---
@@ -286,10 +289,11 @@ public function type() {
286289
public function _list( $args, $assoc_args ) {
287290
global $wpdb;
288291

289-
$fields = array( 'name', 'value', 'expiration' );
290-
$network = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network', false );
291-
$unserialize = \WP_CLI\Utils\get_flag_value( $assoc_args, 'unserialize', false );
292+
$network = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network', false );
293+
$unserialize = \WP_CLI\Utils\get_flag_value( $assoc_args, 'unserialize', false );
294+
$human_readable = \WP_CLI\Utils\get_flag_value( $assoc_args, 'human-readable', false );
292295

296+
$fields = array( 'name', 'value', 'expiration' );
293297
if ( isset( $assoc_args['fields'] ) ) {
294298
$fields = explode( ',', $assoc_args['fields'] );
295299
}
@@ -320,7 +324,7 @@ public function _list( $args, $assoc_args ) {
320324

321325
foreach ( $results as $result ) {
322326
$result->name = str_replace( array( '_site_transient_', '_transient_' ), '', $result->name );
323-
$result->expiration = $this->get_transient_expiration( $result->name, $network );
327+
$result->expiration = $this->get_transient_expiration( $result->name, $network, $human_readable );
324328

325329
if ( $unserialize ) {
326330
$result->value = maybe_unserialize( $result->value );
@@ -335,13 +339,15 @@ public function _list( $args, $assoc_args ) {
335339
}
336340

337341
/**
338-
* Retrieves the human-friendly expiration time.
342+
* Retrieves the expiration time.
339343
*
340344
* @param string $name Transient name.
341345
* @param bool $is_site_transient Optional. Whether this is a site transient. Default false.
346+
* @param bool $human_readable Optional. Whether to return the difference between now and the
347+
* expiration time in a human-readable format. Default false.
342348
* @return string Expiration time string.
343349
*/
344-
private function get_transient_expiration( $name, $is_site_transient = false ) {
350+
private function get_transient_expiration( $name, $is_site_transient = false, $human_readable = false ) {
345351
if ( $is_site_transient ) {
346352
if ( is_multisite() ) {
347353
$expiration = (int) get_site_option( '_site_transient_timeout_' . $name );
@@ -353,7 +359,11 @@ private function get_transient_expiration( $name, $is_site_transient = false ) {
353359
}
354360

355361
if ( 0 === $expiration ) {
356-
return 'no expiration';
362+
return $human_readable ? 'never expires' : 'false';
363+
}
364+
365+
if ( ! $human_readable ) {
366+
return $expiration;
357367
}
358368

359369
$now = time();

0 commit comments

Comments
 (0)