File tree Expand file tree Collapse file tree 2 files changed +90
-0
lines changed
Expand file tree Collapse file tree 2 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -210,3 +210,35 @@ Feature: Managed the WordPress object cache
210210
211211 When I run `wp cache supports set_multiple`
212212 Then the return code should be 0
213+
214+ Scenario : Nested values can be retrieved at any depth.
215+ Given a WP install
216+ And a wp-content/mu-plugins/test-harness.php file:
217+ """
218+ <?php
219+ $set_foo = function(){
220+ wp_cache_set( 'my_key', ['foo' => 'bar'] );
221+ wp_cache_set( 'my_key_2', ['foo' => ['bar' => 'baz']] );
222+ wp_cache_set( 'my_key_custom', ['foo_custom' => ['bar_custom' => 'baz_custom']], 'my_custom_group' );
223+ };
224+
225+ WP_CLI::add_hook( 'before_invoke:cache pluck', $set_foo );
226+ """
227+
228+ When I try `wp cache pluck my_key foo`
229+ Then STDOUT should be:
230+ """
231+ bar
232+ """
233+
234+ When I try `wp cache pluck my_key_2 foo bar`
235+ Then STDOUT should be:
236+ """
237+ baz
238+ """
239+
240+ When I try `wp cache pluck my_key_custom foo_custom bar_custom --group=my_custom_group`
241+ Then STDOUT should be:
242+ """
243+ baz_custom
244+ """
Original file line number Diff line number Diff line change 11<?php
22
3+ use WP_CLI \Cache \RecursiveDataStructureTraverser ;
4+ use WP_CLI \Utils ;
5+
36/**
47 * Adds, removes, fetches, and flushes the WP Object Cache object.
58 *
@@ -406,4 +409,59 @@ public function flush_group( $args, $assoc_args ) {
406409 }
407410 WP_CLI ::success ( "Cache group ' $ group' was flushed. " );
408411 }
412+
413+ /**
414+ * Get a nested value from the cache.
415+ *
416+ * ## OPTIONS
417+ *
418+ * <key>
419+ * : Cache key.
420+ *
421+ * <key-path>...
422+ * : The name(s) of the keys within the value to locate the value to pluck.
423+ *
424+ * [--group=<group>]
425+ * : Method for grouping data within the cache which allows the same key to be used across groups.
426+ * ---
427+ * default: default
428+ * ---
429+ *
430+ * [--format=<format>]
431+ * : The output format of the value.
432+ * ---
433+ * default: plaintext
434+ * options:
435+ * - plaintext
436+ * - json
437+ * - yaml
438+ * ---
439+ */
440+ public function pluck ( $ args , $ assoc_args ) {
441+ list ($ key ) = $ args ;
442+ $ group = Utils \get_flag_value ($ assoc_args , 'group ' );
443+
444+ $ value = wp_cache_get ( $ key , $ group );
445+
446+ if ( false === $ value ) {
447+ WP_CLI ::halt ( 1 );
448+ }
449+
450+ $ key_path = array_map ( function ( $ key ) {
451+ if ( is_numeric ( $ key ) && ( $ key === (string ) intval ( $ key ) ) ) {
452+ return (int ) $ key ;
453+ }
454+ return $ key ;
455+ }, array_slice ( $ args , 1 ) );
456+
457+ $ traverser = new RecursiveDataStructureTraverser ( $ value );
458+
459+ try {
460+ $ value = $ traverser ->get ( $ key_path );
461+ } catch ( \Exception $ e ) {
462+ die ( 1 );
463+ }
464+
465+ WP_CLI ::print_value ( $ value , $ assoc_args );
466+ }
409467}
You can’t perform that action at this time.
0 commit comments