|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Manages theme cache. |
| 5 | + * |
| 6 | + * ## EXAMPLES |
| 7 | + * |
| 8 | + * # Clear cache for a specific theme |
| 9 | + * $ wp theme cache clear twentytwentyfour |
| 10 | + * Success: Cleared cache for 'twentytwentyfour' theme. |
| 11 | + * |
| 12 | + * # Flush the entire theme cache group |
| 13 | + * $ wp theme cache flush |
| 14 | + * Success: The theme cache was flushed. |
| 15 | + */ |
| 16 | +class Theme_Cache_Command extends WP_CLI_Command { |
| 17 | + |
| 18 | + /** |
| 19 | + * Clears the cache for one or more themes. |
| 20 | + * |
| 21 | + * ## OPTIONS |
| 22 | + * |
| 23 | + * [<theme>...] |
| 24 | + * : One or more themes to clear the cache for. |
| 25 | + * |
| 26 | + * [--all] |
| 27 | + * : If set, clear cache for all installed themes. |
| 28 | + * |
| 29 | + * ## EXAMPLES |
| 30 | + * |
| 31 | + * # Clear cache for a single theme |
| 32 | + * $ wp theme cache clear twentytwentyfour |
| 33 | + * Success: Cleared cache for 'twentytwentyfour' theme. |
| 34 | + * |
| 35 | + * # Clear cache for multiple themes |
| 36 | + * $ wp theme cache clear twentytwentythree twentytwentyfour |
| 37 | + * Success: Cleared cache for 2 themes. |
| 38 | + * |
| 39 | + * # Clear cache for all themes |
| 40 | + * $ wp theme cache clear --all |
| 41 | + * Success: Cleared cache for all themes. |
| 42 | + * |
| 43 | + * @param string[] $args Positional arguments. |
| 44 | + * @param array{all?: bool} $assoc_args Associative arguments. |
| 45 | + */ |
| 46 | + public function clear( $args, $assoc_args ) { |
| 47 | + if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) { |
| 48 | + WP_CLI::error( 'Please specify one or more themes, or use --all.' ); |
| 49 | + } |
| 50 | + |
| 51 | + $themes = []; |
| 52 | + |
| 53 | + if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { |
| 54 | + $all_themes = wp_get_themes(); |
| 55 | + foreach ( $all_themes as $theme_slug => $theme ) { |
| 56 | + $themes[] = $theme; |
| 57 | + } |
| 58 | + } else { |
| 59 | + foreach ( $args as $theme_slug ) { |
| 60 | + $theme = wp_get_theme( $theme_slug ); |
| 61 | + if ( ! $theme->exists() ) { |
| 62 | + WP_CLI::warning( "Theme '{$theme_slug}' not found." ); |
| 63 | + continue; |
| 64 | + } |
| 65 | + $themes[] = $theme; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if ( empty( $themes ) ) { |
| 70 | + WP_CLI::error( 'No valid themes to clear cache for.' ); |
| 71 | + } |
| 72 | + |
| 73 | + $cleared = 0; |
| 74 | + foreach ( $themes as $theme ) { |
| 75 | + $this->clear_theme_cache( $theme ); |
| 76 | + ++$cleared; |
| 77 | + } |
| 78 | + |
| 79 | + if ( 1 === $cleared ) { |
| 80 | + WP_CLI::success( "Cleared cache for '{$themes[0]->get_stylesheet()}' theme." ); |
| 81 | + } else { |
| 82 | + WP_CLI::success( "Cleared cache for {$cleared} themes." ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Flushes the entire theme cache group. |
| 88 | + * |
| 89 | + * ## EXAMPLES |
| 90 | + * |
| 91 | + * # Flush the entire theme cache group |
| 92 | + * $ wp theme cache flush |
| 93 | + * Success: The theme cache was flushed. |
| 94 | + * |
| 95 | + * @param string[] $args Positional arguments. Unused. |
| 96 | + * @param array $assoc_args Associative arguments. Unused. |
| 97 | + */ |
| 98 | + public function flush( $args, $assoc_args ) { |
| 99 | + wp_cache_flush_group( 'themes' ); |
| 100 | + WP_CLI::success( 'The theme cache was flushed.' ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Clear cache for a specific theme. |
| 105 | + * |
| 106 | + * @param \WP_Theme $theme Theme object. |
| 107 | + */ |
| 108 | + private function clear_theme_cache( $theme ) { |
| 109 | + $cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() ); |
| 110 | + $cache_keys = [ 'theme', 'screenshot', 'headers', 'page_templates' ]; |
| 111 | + |
| 112 | + foreach ( $cache_keys as $key ) { |
| 113 | + wp_cache_delete( $key . '-' . $cache_hash, 'themes' ); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments