From e0d220d2493550fab0f2736b23b68655832dec1a Mon Sep 17 00:00:00 2001 From: Jan Willem Oostendorp Date: Wed, 5 Apr 2023 14:45:45 +0200 Subject: [PATCH 1/5] Added command: wp cache supports --- features/cache.feature | 6 ++++++ src/Cache_Command.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/features/cache.feature b/features/cache.feature index 472f28d19..c9425264b 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -132,3 +132,9 @@ Feature: Managed the WordPress object cache """ Error: Could not replace object 'bar' in group 'foo'. Does it not exist? """ + + When I try `wp cache supports non_existing` + Then the return code should be 1 + + When I try `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 1b5c8b172..c37541aa1 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -339,4 +339,34 @@ 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; + $supports = wp_cache_supports( $feature ); + + if ( $supports ) { + WP_CLI::halt( 0 ); + } + WP_CLI::halt( 1 ); + } + } From a9258f5922ffa5d024ab5c21867da76f256ff14c Mon Sep 17 00:00:00 2001 From: Jan Willem Oostendorp Date: Wed, 5 Apr 2023 17:09:37 +0200 Subject: [PATCH 2/5] Added failsafe for older WP versions. --- src/Cache_Command.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Cache_Command.php b/src/Cache_Command.php index c37541aa1..a63f04e68 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -361,7 +361,12 @@ public function type( $args, $assoc_args ) { */ public function supports( $args, $assoc_args ) { list ( $feature ) = $args; - $supports = wp_cache_supports( $feature ); + + 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 ); From d848236b7c568abc0fd7bbb63613d8d66809daa6 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Wed, 5 Apr 2023 17:32:59 -0700 Subject: [PATCH 3/5] Create a dedicated `Scenario` and only run for WP 6.1 --- features/cache.feature | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/features/cache.feature b/features/cache.feature index 6fc58101d..1c7c25b59 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -148,6 +148,10 @@ 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 From cd12d8d4f333c78dc7e794bcd258d2607c134cbf Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Wed, 5 Apr 2023 17:33:12 -0700 Subject: [PATCH 4/5] Use `When I run` for successful invocations --- features/cache.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/cache.feature b/features/cache.feature index 1c7c25b59..c6cb71f09 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -155,5 +155,5 @@ Feature: Managed the WordPress object cache When I try `wp cache supports non_existing` Then the return code should be 1 - When I try `wp cache supports set_multiple` + When I run `wp cache supports set_multiple` Then the return code should be 0 From 4da2223377a4d47e709c8b3e2e5257ce444a3108 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Wed, 5 Apr 2023 17:34:03 -0700 Subject: [PATCH 5/5] Register the command and regenerate the README --- README.md | 27 +++++++++++++++++++++++++++ composer.json | 1 + 2 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 57d5a7f72..10c91ecce 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 b453f5ef9..3fbceb9c8 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "cache incr", "cache replace", "cache set", + "cache supports", "cache type", "transient", "transient delete",