diff --git a/README.md b/README.md index 57d5a7f7..10c91ecc 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,33 @@ Errors if the value can't be set. +### wp cache supports + +Determines whether the object cache implementation supports a particular feature. + +~~~ +wp cache supports +~~~ + +**OPTIONS** + + + Name of the feature to check for. + +**EXAMPLES** + + # Check whether is add_multiple supported. + $ wp cache supports add_multiple + $ echo $? + 0 + + # Bash script for checking whether for support like this: + if ! wp cache supports non_existing; then + echo 'non_existing is not supported' + fi + + + ### wp cache type Attempts to determine which object cache is being used. diff --git a/composer.json b/composer.json index b453f5ef..3fbceb9c 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "cache incr", "cache replace", "cache set", + "cache supports", "cache type", "transient", "transient delete", diff --git a/features/cache.feature b/features/cache.feature index 941698de..c6cb71f0 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -147,3 +147,13 @@ Feature: Managed the WordPress object cache """ Warning: Ignoring the --url= argument because flushing the cache affects all sites on a multisite installation. """ + + @require-wp-6.1 + Scenario: Checking if the cache supports a feature + Given a WP install + + When I try `wp cache supports non_existing` + Then the return code should be 1 + + When I run `wp cache supports set_multiple` + Then the return code should be 0 diff --git a/src/Cache_Command.php b/src/Cache_Command.php index fd2d0c57..0c9a34ce 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -343,4 +343,39 @@ public function type( $args, $assoc_args ) { WP_CLI::line( $message ); } + /** + * Determines whether the object cache implementation supports a particular feature. + * + * ## OPTIONS + * + * + * : Name of the feature to check for. + * + * ## EXAMPLES + * + * # Check whether is add_multiple supported. + * $ wp cache supports add_multiple + * $ echo $? + * 0 + * + * # Bash script for checking whether for support like this: + * if ! wp cache supports non_existing; then + * echo 'non_existing is not supported' + * fi + */ + public function supports( $args, $assoc_args ) { + list ( $feature ) = $args; + + if ( ! function_exists( 'wp_cache_supports' ) ) { + WP_CLI::error( 'Checking cache features is only available in WordPress 6.1 and higher' ); + } + + $supports = wp_cache_supports( $feature ); + + if ( $supports ) { + WP_CLI::halt( 0 ); + } + WP_CLI::halt( 1 ); + } + }