Skip to content

Commit cdc371f

Browse files
Add wp cache supports <feature> command (#84)
* Added command: wp cache supports <feature> * Added failsafe for older WP versions. * Create a dedicated `Scenario` and only run for WP 6.1 * Use `When I run` for successful invocations * Register the command and regenerate the README --------- Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent a2d8e53 commit cdc371f

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,33 @@ Errors if the value can't be set.
304304

305305

306306

307+
### wp cache supports
308+
309+
Determines whether the object cache implementation supports a particular feature.
310+
311+
~~~
312+
wp cache supports <feature>
313+
~~~
314+
315+
**OPTIONS**
316+
317+
<feature>
318+
Name of the feature to check for.
319+
320+
**EXAMPLES**
321+
322+
# Check whether is add_multiple supported.
323+
$ wp cache supports add_multiple
324+
$ echo $?
325+
0
326+
327+
# Bash script for checking whether for support like this:
328+
if ! wp cache supports non_existing; then
329+
echo 'non_existing is not supported'
330+
fi
331+
332+
333+
307334
### wp cache type
308335

309336
Attempts to determine which object cache is being used.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"cache incr",
4242
"cache replace",
4343
"cache set",
44+
"cache supports",
4445
"cache type",
4546
"transient",
4647
"transient delete",

features/cache.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,13 @@ Feature: Managed the WordPress object cache
147147
"""
148148
Warning: Ignoring the --url=<url> argument because flushing the cache affects all sites on a multisite installation.
149149
"""
150+
151+
@require-wp-6.1
152+
Scenario: Checking if the cache supports a feature
153+
Given a WP install
154+
155+
When I try `wp cache supports non_existing`
156+
Then the return code should be 1
157+
158+
When I run `wp cache supports set_multiple`
159+
Then the return code should be 0

src/Cache_Command.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,39 @@ public function type( $args, $assoc_args ) {
343343
WP_CLI::line( $message );
344344
}
345345

346+
/**
347+
* Determines whether the object cache implementation supports a particular feature.
348+
*
349+
* ## OPTIONS
350+
*
351+
* <feature>
352+
* : Name of the feature to check for.
353+
*
354+
* ## EXAMPLES
355+
*
356+
* # Check whether is add_multiple supported.
357+
* $ wp cache supports add_multiple
358+
* $ echo $?
359+
* 0
360+
*
361+
* # Bash script for checking whether for support like this:
362+
* if ! wp cache supports non_existing; then
363+
* echo 'non_existing is not supported'
364+
* fi
365+
*/
366+
public function supports( $args, $assoc_args ) {
367+
list ( $feature ) = $args;
368+
369+
if ( ! function_exists( 'wp_cache_supports' ) ) {
370+
WP_CLI::error( 'Checking cache features is only available in WordPress 6.1 and higher' );
371+
}
372+
373+
$supports = wp_cache_supports( $feature );
374+
375+
if ( $supports ) {
376+
WP_CLI::halt( 0 );
377+
}
378+
WP_CLI::halt( 1 );
379+
}
380+
346381
}

0 commit comments

Comments
 (0)