Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d212c2e
Add `RecursiveDataStructureTraverser` helper to manipulate nested data
petitphp Jun 26, 2023
7292638
add pluck command to cache
petitphp Jun 26, 2023
8e1b63d
add pluck command to transient
petitphp Jun 26, 2023
6e286af
add patch command to cache
petitphp Jul 1, 2023
bd13459
add patch command to transient
petitphp Aug 23, 2023
05424ec
Add missing `expiration` option for patch cache command
petitphp Aug 23, 2023
ca02d28
Fix cs
petitphp Aug 28, 2023
15ad011
Update Behat features tests
petitphp Aug 31, 2023
ba1f610
Add temporary log to debug failing tests in github action
petitphp Aug 31, 2023
6b8d91c
Small CS fixes for WPCS v3
petitphp Sep 7, 2023
a70e2d7
Fix failing functional tests
petitphp Sep 15, 2023
5d0e6df
Add tests for RecursiveDataStructureTraverser.php
petitphp Sep 18, 2023
dd9eec3
Fix wrong command in docblock
petitphp Sep 19, 2023
d174717
Fix CS issue
petitphp Sep 19, 2023
ae6d0a6
Reuse has_stdin() from framework
schlessera Nov 14, 2023
b74bc47
Fix PHPCS issues
schlessera Nov 14, 2023
d28445f
Use `wp eval` to set the transient
petitphp Dec 4, 2023
b1afe2e
split `patch` feature tests and add additionnal checks with `transien…
petitphp Dec 8, 2023
8d9ba42
split `patch` feature tests for site transient and add additionnal ch…
petitphp Dec 11, 2023
7d3a483
Add test cases for invalid keys
petitphp Dec 11, 2023
d061301
Refactor `pluck` feature tests to use `wp eval` to set the transient
petitphp Dec 11, 2023
cf568fb
new feature test for calling cache pluck command with invalid key
petitphp Dec 12, 2023
3fa7fe1
add the new commands to the `composer.json`
petitphp Apr 26, 2024
119c991
use string interpolation for building message
petitphp Apr 29, 2024
2d17185
explicitly call update methods for clearer code
petitphp Apr 29, 2024
9eca4d9
fix tests
petitphp Apr 29, 2024
de71744
Merge branch 'main' into feature/pluck-patch-cache-transient
schlessera May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add pluck command to transient
  • Loading branch information
petitphp authored and schlessera committed Aug 4, 2024
commit 8e1b63dcd0166e74085b2376255c43c8445613a5
50 changes: 50 additions & 0 deletions features/transient.feature
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,53 @@ Feature: Manage WordPress transient cache
name
foo4
"""

Scenario: Nested values from transient can be retrieved at any depth.
Comment thread
petitphp marked this conversation as resolved.
Outdated
Given a WP install
And a wp-content/mu-plugins/test-harness.php file:
"""
<?php
$set_foo = function(){
set_transient( 'my_key', ['foo' => 'bar'] );
set_transient( 'my_key_2', ['foo' => ['bar' => 'baz']] );
};

WP_CLI::add_hook( 'before_invoke:transient pluck', $set_foo );
"""

When I try `wp transient pluck my_key foo`
Then STDOUT should be:
"""
bar
"""

When I try `wp transient pluck my_key_2 foo bar`
Then STDOUT should be:
"""
baz
"""

Scenario: Nested values from site transient can be retrieved at any depth.
Comment thread
petitphp marked this conversation as resolved.
Outdated
Given a WP multisite install
And I run `wp site create --slug=foo`
And a wp-content/mu-plugins/test-harness.php file:
"""
<?php
$set_foo = function(){
set_site_transient( 'my_key', ['foo' => 'bar'] );
};

WP_CLI::add_hook( 'before_invoke:transient pluck', $set_foo );
"""

When I try `wp transient pluck my_key foo --network`
Then STDOUT should be:
"""
bar
"""

When I try `wp transient pluck my_key foo`
Then STDERR should be:
"""
Warning: Transient with key "my_key" is not set.
"""
56 changes: 56 additions & 0 deletions src/Transient_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use WP_CLI\Cache\RecursiveDataStructureTraverser;
use WP_CLI\Utils;

/**
Expand Down Expand Up @@ -404,6 +405,61 @@ public function list_( $args, $assoc_args ) {
$formatter->display_items( $results );
}

/**
* Get a nested value from a transient.
*
* ## OPTIONS
*
* <key>
* : Key for the transient.
*
* <key-path>...
* : The name(s) of the keys within the value to locate the value to pluck.
*
* [--format=<format>]
* : The output format of the value.
* ---
* default: plaintext
* options:
* - plaintext
* - json
* - yaml
* ---
*
* [--network]
* : Get the value of a network|site transient. On single site, this is
* a specially-named cache key. On multisite, this is a global cache
* (instead of local to the site).
*/
public function pluck( $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;
}

$key_path = array_map( function( $key ) {
if ( is_numeric( $key ) && ( $key === (string) intval( $key ) ) ) {
return (int) $key;
}
return $key;
}, array_slice( $args, 1 ) );

$traverser = new RecursiveDataStructureTraverser( $value );

try {
$value = $traverser->get( $key_path );
} catch ( \Exception $e ) {
die( 1 );
}

WP_CLI::print_value( $value, $assoc_args );
}

/**
* Retrieves the expiration time.
*
Expand Down