File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -210,3 +210,35 @@ Feature: Managed the WordPress object cache
210
210
211
211
When I run `wp cache supports set_multiple`
212
212
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 1
1
<?php
2
2
3
+ use WP_CLI \Cache \RecursiveDataStructureTraverser ;
4
+ use WP_CLI \Utils ;
5
+
3
6
/**
4
7
* Adds, removes, fetches, and flushes the WP Object Cache object.
5
8
*
@@ -407,4 +410,58 @@ public function flush_group( $args, $assoc_args ) {
407
410
WP_CLI ::success ( "Cache group ' $ group' was flushed. " );
408
411
}
409
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
+ }
410
467
}
You can’t perform that action at this time.
0 commit comments