Skip to content

Commit 8e1b63d

Browse files
petitphpschlessera
authored andcommitted
add pluck command to transient
1 parent 7292638 commit 8e1b63d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

features/transient.feature

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,53 @@ Feature: Manage WordPress transient cache
604604
name
605605
foo4
606606
"""
607+
608+
Scenario: Nested values from transient can be retrieved at any depth.
609+
Given a WP install
610+
And a wp-content/mu-plugins/test-harness.php file:
611+
"""
612+
<?php
613+
$set_foo = function(){
614+
set_transient( 'my_key', ['foo' => 'bar'] );
615+
set_transient( 'my_key_2', ['foo' => ['bar' => 'baz']] );
616+
};
617+
618+
WP_CLI::add_hook( 'before_invoke:transient pluck', $set_foo );
619+
"""
620+
621+
When I try `wp transient pluck my_key foo`
622+
Then STDOUT should be:
623+
"""
624+
bar
625+
"""
626+
627+
When I try `wp transient pluck my_key_2 foo bar`
628+
Then STDOUT should be:
629+
"""
630+
baz
631+
"""
632+
633+
Scenario: Nested values from site transient can be retrieved at any depth.
634+
Given a WP multisite install
635+
And I run `wp site create --slug=foo`
636+
And a wp-content/mu-plugins/test-harness.php file:
637+
"""
638+
<?php
639+
$set_foo = function(){
640+
set_site_transient( 'my_key', ['foo' => 'bar'] );
641+
};
642+
643+
WP_CLI::add_hook( 'before_invoke:transient pluck', $set_foo );
644+
"""
645+
646+
When I try `wp transient pluck my_key foo --network`
647+
Then STDOUT should be:
648+
"""
649+
bar
650+
"""
651+
652+
When I try `wp transient pluck my_key foo`
653+
Then STDERR should be:
654+
"""
655+
Warning: Transient with key "my_key" is not set.
656+
"""

src/Transient_Command.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use WP_CLI\Cache\RecursiveDataStructureTraverser;
34
use WP_CLI\Utils;
45

56
/**
@@ -404,6 +405,61 @@ public function list_( $args, $assoc_args ) {
404405
$formatter->display_items( $results );
405406
}
406407

408+
/**
409+
* Get a nested value from a transient.
410+
*
411+
* ## OPTIONS
412+
*
413+
* <key>
414+
* : Key for the transient.
415+
*
416+
* <key-path>...
417+
* : The name(s) of the keys within the value to locate the value to pluck.
418+
*
419+
* [--format=<format>]
420+
* : The output format of the value.
421+
* ---
422+
* default: plaintext
423+
* options:
424+
* - plaintext
425+
* - json
426+
* - yaml
427+
* ---
428+
*
429+
* [--network]
430+
* : Get the value of a network|site transient. On single site, this is
431+
* a specially-named cache key. On multisite, this is a global cache
432+
* (instead of local to the site).
433+
*/
434+
public function pluck( $args, $assoc_args ) {
435+
list( $key ) = $args;
436+
437+
$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
438+
$value = $func( $key );
439+
440+
if ( false === $value ) {
441+
WP_CLI::warning( 'Transient with key "' . $key . '" is not set.' );
442+
exit;
443+
}
444+
445+
$key_path = array_map( function( $key ) {
446+
if ( is_numeric( $key ) && ( $key === (string) intval( $key ) ) ) {
447+
return (int) $key;
448+
}
449+
return $key;
450+
}, array_slice( $args, 1 ) );
451+
452+
$traverser = new RecursiveDataStructureTraverser( $value );
453+
454+
try {
455+
$value = $traverser->get( $key_path );
456+
} catch ( \Exception $e ) {
457+
die( 1 );
458+
}
459+
460+
WP_CLI::print_value( $value, $assoc_args );
461+
}
462+
407463
/**
408464
* Retrieves the expiration time.
409465
*

0 commit comments

Comments
 (0)