-
Notifications
You must be signed in to change notification settings - Fork 85
Add wp theme cache command #494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/add-wp-theme-cache-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
054741f
Initial plan
Copilot 3d762f7
Add Theme_Cache_Command with clear and flush methods
Copilot 27a6dae
Apply suggestions from code review
swissspidy 63c6211
fixes
swissspidy 02bd910
Update src/Theme_Cache_Command.php
swissspidy 63b7772
Update src/Theme_Cache_Command.php
swissspidy 37ac8df
Apply suggestions from code review
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| Feature: Manage theme cache | ||
|
|
||
| Background: | ||
| Given a WP installation | ||
|
|
||
| Scenario: Clear cache for a single theme | ||
| When I run `wp theme install twentytwenty --force --activate` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: | ||
| """ | ||
|
|
||
| When I run `wp theme cache clear twentytwenty` | ||
| Then STDOUT should be: | ||
| """ | ||
| Success: Cleared cache for 'twentytwenty' theme. | ||
| """ | ||
|
|
||
| Scenario: Clear cache for multiple themes | ||
| When I run `wp theme install twentytwentythree --force` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: | ||
| """ | ||
|
|
||
| When I run `wp theme install twentytwenty --force ` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: | ||
| """ | ||
|
|
||
| When I run `wp theme cache clear twentytwentythree twentytwenty` | ||
| Then STDOUT should be: | ||
| """ | ||
| Success: Cleared cache for 2 themes. | ||
| """ | ||
|
|
||
| Scenario: Clear cache for all themes | ||
| When I run `wp theme install twentytwentythree --force` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: | ||
| """ | ||
|
|
||
| When I run `wp theme cache clear --all` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Cleared cache for | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| themes. | ||
| """ | ||
|
|
||
| Scenario: Clear cache for non-existent theme | ||
| When I try `wp theme cache clear nonexistent` | ||
| Then STDERR should contain: | ||
| """ | ||
| Warning: Theme 'nonexistent' not found. | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Clear cache with no arguments | ||
| When I try `wp theme cache clear` | ||
| Then STDERR should be: | ||
| """ | ||
| Error: Please specify one or more themes, or use --all. | ||
| """ | ||
| And the return code should be 1 | ||
|
|
||
| Scenario: Flush the entire theme cache group | ||
| When I run `wp theme cache flush` | ||
| Then STDOUT should be: | ||
| """ | ||
| Success: The theme cache was flushed. | ||
| """ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Manages theme cache. | ||
| * | ||
| * ## EXAMPLES | ||
| * | ||
| * # Clear cache for a specific theme | ||
| * $ wp theme cache clear twentytwentyfour | ||
| * Success: Cleared cache for 'twentytwentyfour' theme. | ||
| * | ||
| * # Flush the entire theme cache group | ||
| * $ wp theme cache flush | ||
| * Success: The theme cache was flushed. | ||
| */ | ||
| class Theme_Cache_Command extends WP_CLI_Command { | ||
|
|
||
| /** | ||
| * Clears the cache for one or more themes. | ||
| * | ||
| * ## OPTIONS | ||
| * | ||
| * [<theme>...] | ||
| * : One or more themes to clear the cache for. | ||
| * | ||
| * [--all] | ||
| * : If set, clear cache for all installed themes. | ||
| * | ||
| * ## EXAMPLES | ||
| * | ||
| * # Clear cache for a single theme | ||
| * $ wp theme cache clear twentytwentyfour | ||
| * Success: Cleared cache for 'twentytwentyfour' theme. | ||
| * | ||
| * # Clear cache for multiple themes | ||
| * $ wp theme cache clear twentytwentythree twentytwentyfour | ||
| * Success: Cleared cache for 2 themes. | ||
| * | ||
| * # Clear cache for all themes | ||
| * $ wp theme cache clear --all | ||
| * Success: Cleared cache for all themes. | ||
| * | ||
| * @param string[] $args Positional arguments. | ||
| * @param array{all?: bool} $assoc_args Associative arguments. | ||
| */ | ||
| public function clear( $args, $assoc_args ) { | ||
| if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) { | ||
| WP_CLI::error( 'Please specify one or more themes, or use --all.' ); | ||
| } | ||
|
|
||
| $themes = []; | ||
|
|
||
| if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { | ||
| $all_themes = wp_get_themes(); | ||
| foreach ( $all_themes as $theme ) { | ||
| $themes[] = $theme; | ||
| } | ||
| } else { | ||
| foreach ( $args as $theme_slug ) { | ||
| $theme = wp_get_theme( $theme_slug ); | ||
| if ( ! $theme->exists() ) { | ||
| WP_CLI::warning( "Theme '{$theme_slug}' not found." ); | ||
| continue; | ||
| } | ||
| $themes[] = $theme; | ||
| } | ||
| } | ||
|
|
||
| if ( empty( $themes ) ) { | ||
| WP_CLI::error( 'No valid themes to clear cache for.' ); | ||
| } | ||
|
|
||
| $cleared = 0; | ||
| foreach ( $themes as $theme ) { | ||
| $this->clear_theme_cache( $theme ); | ||
| ++$cleared; | ||
| } | ||
|
|
||
| if ( 1 === $cleared ) { | ||
| WP_CLI::success( "Cleared cache for '{$themes[0]->get_stylesheet()}' theme." ); | ||
| } else { | ||
| WP_CLI::success( "Cleared cache for {$cleared} themes." ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Flushes the entire theme cache group. | ||
| * | ||
| * ## EXAMPLES | ||
| * | ||
| * # Flush the entire theme cache group | ||
| * $ wp theme cache flush | ||
| * Success: The theme cache was flushed. | ||
| * | ||
| * @param string[] $args Positional arguments. Unused. | ||
| * @param array $assoc_args Associative arguments. Unused. | ||
| */ | ||
| public function flush( $args, $assoc_args ) { | ||
| // Only added in WordPress 6.1. | ||
| if ( function_exists( 'wp_cache_flush_group' ) ) { | ||
| wp_cache_flush_group( 'themes' ); | ||
| WP_CLI::success( 'The theme cache was flushed.' ); | ||
| return; | ||
| } | ||
|
|
||
| // Fallback for WordPress versions prior to 6.1: clear cache for all themes. | ||
| if ( function_exists( 'wp_get_themes' ) ) { | ||
| $all_themes = wp_get_themes(); | ||
| foreach ( $all_themes as $theme ) { | ||
| $this->clear_theme_cache( $theme ); | ||
| } | ||
| WP_CLI::success( 'The theme cache was flushed.' ); | ||
| } else { | ||
| WP_CLI::warning( 'Your WordPress version does not support flushing the theme cache group.' ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clear cache for a specific theme. | ||
| * | ||
| * @param \WP_Theme $theme Theme object. | ||
| */ | ||
| private function clear_theme_cache( $theme ) { | ||
| $cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() ); | ||
| $cache_keys = [ 'theme', 'screenshot', 'headers', 'page_templates' ]; | ||
|
|
||
| foreach ( $cache_keys as $key ) { | ||
| wp_cache_delete( $key . '-' . $cache_hash, 'themes' ); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.