Skip to content

Add wp cache supports <feature> command #84

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 6 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <feature>
~~~

**OPTIONS**

<feature>
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.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"cache incr",
"cache replace",
"cache set",
"cache supports",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbachhuber I'm guessing that you add this line, run some command and it updates the README.
Can you show me how to do this?
Might come in handy next time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding that line README.md regeneration can be even skipped and Github Actions will create a separate PR after the main PR is merged - see https://github.com/wp-cli/.github/blob/main/.github/workflows/reusable-regenerate-readme.yml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding that line README.md regeneration can be even skipped and Github Actions will create a separate PR after the main PR is merged - see https://github.com/wp-cli/.github/blob/main/.github/workflows/reusable-regenerate-readme.yml

Correct. However, to avoid the separate PR (which was unnecessary because I was already in the code), I simply ran wp scaffold package-readme ./ in the repository directory.

"cache type",
"transient",
"transient delete",
Expand Down
10 changes: 10 additions & 0 deletions features/cache.feature
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,13 @@ Feature: Managed the WordPress object cache
"""
Warning: Ignoring the --url=<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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbachhuber I don't understand what use this has.
non_existing doesn't exist by default.
Wouldn't it be better to run a test on 6.1 for a feature that is defined (in 6.2)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what use this has.

@janw-me @require-wp-6.1 means the test will only run on WordPress 6.1 and higher. WordPress 3.7 is a part of the testing matrix, so an earlier test on this branch failed: https://github.com/wp-cli/cache-command/actions/runs/4620028330/jobs/8169572449

non_existing doesn't exist by default.
Wouldn't it be better to run a test on 6.1 for a feature that is defined (in 6.2)

I think the test is fine as it is. It doesn't need to be overly complex.

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
35 changes: 35 additions & 0 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* <feature>
* : 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 );
}

}