Skip to content

Add pluck/patch commands for caches and transients #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
May 5, 2025
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
Fix cs
  • Loading branch information
petitphp authored and schlessera committed Aug 4, 2024
commit ca02d28fea13114664b93d5cce1a72bf29358b6d
40 changes: 23 additions & 17 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,20 +439,23 @@ public function flush_group( $args, $assoc_args ) {
*/
public function pluck( $args, $assoc_args ) {
list($key) = $args;
$group = Utils\get_flag_value($assoc_args, 'group');
$group = Utils\get_flag_value( $assoc_args, 'group' );

$value = wp_cache_get( $key, $group );

if ( false === $value ) {
WP_CLI::halt( 1 );
}

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

$traverser = new RecursiveDataStructureTraverser( $value );

Expand Down Expand Up @@ -511,16 +514,19 @@ public function pluck( $args, $assoc_args ) {
*/
public function patch( $args, $assoc_args ) {
list( $action, $key ) = $args;
$group = Utils\get_flag_value( $assoc_args, 'group' );
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );
$group = Utils\get_flag_value( $assoc_args, 'group' );
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );

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

return $key;
}, array_slice( $args, 2 ) );
return $key;
},
array_slice( $args, 2 )
);

if ( 'delete' === $action ) {
$patch_value = null;
Expand All @@ -533,9 +539,9 @@ public function patch( $args, $assoc_args ) {
}

/* Need to make a copy of $current_value here as it is modified by reference */
$old_value = wp_cache_get($key, $group);
$old_value = wp_cache_get( $key, $group );
$current_value = $old_value;
if (is_object($old_value)) {
if ( is_object( $old_value ) ) {
$current_value = clone $old_value;
}

Expand Down
38 changes: 22 additions & 16 deletions src/Transient_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,15 @@ public function pluck( $args, $assoc_args ) {
exit;
}

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

$traverser = new RecursiveDataStructureTraverser( $value );

Expand Down Expand Up @@ -502,18 +505,21 @@ public function pluck( $args, $assoc_args ) {
*/
public function patch( $args, $assoc_args ) {
list( $action, $key ) = $args;
$expiration = (int) Utils\get_flag_value($assoc_args, 'expiration', 0);
$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );

$read_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
$write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
$write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';

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

return $key;
}, array_slice( $args, 2 ) );
return $key;
},
array_slice( $args, 2 )
);

if ( 'delete' === $action ) {
$patch_value = null;
Expand All @@ -526,9 +532,9 @@ public function patch( $args, $assoc_args ) {
}

/* Need to make a copy of $current_value here as it is modified by reference */
$old_value = $read_func( $key );
$old_value = $read_func( $key );
$current_value = $old_value;
if ( is_object($old_value) ) {
if ( is_object( $old_value ) ) {
$current_value = clone $old_value;
}

Expand Down