Skip to content

Commit eef8e56

Browse files
committed
add pluck command to transient
1 parent c1ba13e commit eef8e56

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
@@ -572,3 +572,53 @@ Feature: Manage WordPress transient cache
572572
name
573573
foo4
574574
"""
575+
576+
Scenario: Nested values from transient can be retrieved at any depth.
577+
Given a WP install
578+
And a wp-content/mu-plugins/test-harness.php file:
579+
"""
580+
<?php
581+
$set_foo = function(){
582+
set_transient( 'my_key', ['foo' => 'bar'] );
583+
set_transient( 'my_key_2', ['foo' => ['bar' => 'baz']] );
584+
};
585+
586+
WP_CLI::add_hook( 'before_invoke:transient pluck', $set_foo );
587+
"""
588+
589+
When I try `wp transient pluck my_key foo`
590+
Then STDOUT should be:
591+
"""
592+
bar
593+
"""
594+
595+
When I try `wp transient pluck my_key_2 foo bar`
596+
Then STDOUT should be:
597+
"""
598+
baz
599+
"""
600+
601+
Scenario: Nested values from site transient can be retrieved at any depth.
602+
Given a WP multisite install
603+
And I run `wp site create --slug=foo`
604+
And a wp-content/mu-plugins/test-harness.php file:
605+
"""
606+
<?php
607+
$set_foo = function(){
608+
set_site_transient( 'my_key', ['foo' => 'bar'] );
609+
};
610+
611+
WP_CLI::add_hook( 'before_invoke:transient pluck', $set_foo );
612+
"""
613+
614+
When I try `wp transient pluck my_key foo --network`
615+
Then STDOUT should be:
616+
"""
617+
bar
618+
"""
619+
620+
When I try `wp transient pluck my_key foo`
621+
Then STDERR should be:
622+
"""
623+
Warning: Transient with key "my_key" is not set.
624+
"""

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)