├── .actrc ├── .editorconfig ├── .github ├── dependabot.yml └── workflows │ ├── code-quality.yml │ ├── regenerate-readme.yml │ └── testing.yml ├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── features └── manage-cache.feature ├── phpcs.xml.dist ├── src ├── WP_Super_Cache_CLI_Loader.php └── WP_Super_Cache_Command.php ├── wp-cli.yml └── wp-super-cache-cli.php /.actrc: -------------------------------------------------------------------------------- 1 | # Configuration file for nektos/act. 2 | # See https://github.com/nektos/act#configuration 3 | -P ubuntu-latest=shivammathur/node:latest 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | # WordPress Coding Standards 5 | # https://make.wordpress.org/core/handbook/coding-standards/ 6 | 7 | # From https://github.com/WordPress/wordpress-develop/blob/trunk/.editorconfig with a couple of additions. 8 | 9 | root = true 10 | 11 | [*] 12 | charset = utf-8 13 | end_of_line = lf 14 | insert_final_newline = true 15 | trim_trailing_whitespace = true 16 | indent_style = tab 17 | 18 | [{*.yml,*.feature,.jshintrc,*.json}] 19 | indent_style = space 20 | indent_size = 2 21 | 22 | [*.md] 23 | trim_trailing_whitespace = false 24 | 25 | [{*.txt,wp-config-sample.php}] 26 | end_of_line = crlf 27 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: composer 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | labels: 9 | - scope:distribution 10 | - package-ecosystem: github-actions 11 | directory: "/" 12 | schedule: 13 | interval: daily 14 | open-pull-requests-limit: 10 15 | labels: 16 | - scope:distribution 17 | 18 | -------------------------------------------------------------------------------- /.github/workflows/code-quality.yml: -------------------------------------------------------------------------------- 1 | name: Code Quality Checks 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | - master 9 | 10 | jobs: 11 | code-quality: 12 | uses: wp-cli/.github/.github/workflows/reusable-code-quality.yml@main 13 | -------------------------------------------------------------------------------- /.github/workflows/regenerate-readme.yml: -------------------------------------------------------------------------------- 1 | name: Regenerate README file 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - main 8 | - master 9 | paths-ignore: 10 | - "features/**" 11 | - "README.md" 12 | 13 | jobs: 14 | regenerate-readme: 15 | uses: wp-cli/.github/.github/workflows/reusable-regenerate-readme.yml@main 16 | -------------------------------------------------------------------------------- /.github/workflows/testing.yml: -------------------------------------------------------------------------------- 1 | name: Testing 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | - master 9 | schedule: 10 | - cron: '17 1 * * *' # Run every day on a seemly random time. 11 | 12 | jobs: 13 | test: 14 | uses: wp-cli/.github/.github/workflows/reusable-testing.yml@main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock 3 | *.log 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | dist: trusty 3 | 4 | language: php 5 | php: 7.2 6 | 7 | notifications: 8 | email: 9 | on_success: never 10 | on_failure: change 11 | 12 | branches: 13 | only: 14 | - master 15 | 16 | cache: 17 | directories: 18 | - $HOME/.composer/cache 19 | 20 | env: 21 | global: 22 | - PATH="$TRAVIS_BUILD_DIR/vendor/bin:$PATH" 23 | - WP_CLI_BIN_DIR="$TRAVIS_BUILD_DIR/vendor/bin" 24 | 25 | before_install: 26 | - | 27 | # Remove Xdebug for a huge performance increase: 28 | if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then 29 | phpenv config-rm xdebug.ini 30 | else 31 | echo "xdebug.ini does not exist" 32 | fi 33 | - | 34 | # Raise PHP memory limit to 2048MB 35 | echo 'memory_limit = 2048M' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini 36 | - composer validate 37 | 38 | install: 39 | - composer install 40 | - composer prepare-tests 41 | 42 | script: 43 | - composer phpunit 44 | - composer behat 45 | 46 | jobs: 47 | include: 48 | - stage: sniff 49 | script: 50 | - composer lint 51 | - composer phpcs 52 | env: BUILD=sniff 53 | - stage: test 54 | php: 7.2 55 | env: WP_VERSION=latest 56 | - stage: test 57 | php: 7.1 58 | env: WP_VERSION=latest 59 | - stage: test 60 | php: 7.0 61 | env: WP_VERSION=latest 62 | - stage: test 63 | php: 5.6 64 | env: WP_VERSION=latest 65 | - stage: test 66 | php: 5.6 67 | env: WP_VERSION=3.7.11 68 | - stage: test 69 | php: 5.6 70 | env: WP_VERSION=trunk 71 | - stage: test 72 | php: 5.4 73 | env: WP_VERSION=5.1 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CLI interface for the WP Super Cache 2 | -------------------------------------------------- 3 | This repository contains a [WP-CLI plugin](https://github.com/wp-cli/wp-cli) for the [WP Super Cache Wordpress plugin](https://wordpress.org/plugins/wp-super-cache/). After installing this plugin, a Wordpress administrator will have access to a `wp super-cache` command 4 | 5 | $ wp super-cache 6 | usage: wp super-cache disable 7 | or: wp super-cache enable 8 | or: wp super-cache flush [--post_id=] [--permalink=] 9 | or: wp super-cache preload [--status] [--cancel] 10 | or: wp super-cache status 11 | 12 | See 'wp help super-cache ' for more information on a specific command. 13 | 14 | Installing 15 | -------------------------------------------------- 16 | For instructions on installing this, and other, WP-CLI community packages, read the [Community Packages](https://github.com/wp-cli/wp-cli/wiki/Community-Packages) section of the WP-CLI Wiki. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wp-cli/wp-super-cache-cli", 3 | "type": "wp-cli-package", 4 | "description": "Add a `wp super-cache` command to support the WP Super Cache plug-in", 5 | "keywords": [ 6 | "wp-cli", 7 | "super-cache", 8 | "cache" 9 | ], 10 | "homepage": "https://github.com/wp-cli/wp-super-cache-cli", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "WP-CLI Team", 15 | "homepage": "http://github.com/wp-cli", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php": ">=5.4", 21 | "wp-cli/wp-cli": "^2" 22 | }, 23 | "require-dev": { 24 | "wp-cli/extension-command": "^2", 25 | "wp-cli/wp-cli-tests": "^2.0.7" 26 | }, 27 | "config": { 28 | "platform": { 29 | "php": "5.4" 30 | }, 31 | "process-timeout": 7200, 32 | "sort-packages": true, 33 | "allow-plugins": { 34 | "dealerdirect/phpcodesniffer-composer-installer": true 35 | } 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "": "src/" 40 | }, 41 | "files": [ 42 | "wp-super-cache-cli.php" 43 | ] 44 | }, 45 | "minimum-stability": "dev", 46 | "prefer-stable": true, 47 | "scripts": { 48 | "behat": "run-behat-tests", 49 | "lint": "run-linter-tests", 50 | "phpcs": "run-phpcs-tests", 51 | "phpunit": "run-php-unit-tests", 52 | "prepare-tests": "install-package-tests", 53 | "test": [ 54 | "@lint", 55 | "@phpcs", 56 | "@phpunit", 57 | "@behat" 58 | ] 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /features/manage-cache.feature: -------------------------------------------------------------------------------- 1 | Feature: Generate cache 2 | 3 | Scenario: Manage wp-super-cache via CLI 4 | Given a WP install 5 | 6 | When I try `wp super-cache status` 7 | Then STDERR should contain: 8 | """ 9 | Error: WP Super Cache needs to be installed to use its WP-CLI commands. 10 | """ 11 | 12 | When I run `wp plugin install wp-super-cache` 13 | Then STDOUT should contain: 14 | """ 15 | Plugin installed successfully. 16 | """ 17 | And the wp-content/plugins/wp-super-cache directory should exist 18 | 19 | When I try `wp super-cache enable` 20 | Then STDERR should contain: 21 | """ 22 | Error: WP Super Cache needs to be activated to use its WP-CLI commands. 23 | """ 24 | 25 | When I run `wp plugin activate wp-super-cache` 26 | And I run `wp super-cache enable` 27 | Then STDOUT should contain: 28 | """ 29 | Success: The WP Super Cache is enabled. 30 | """ 31 | -------------------------------------------------------------------------------- /phpcs.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | Custom ruleset for WP-CLI 4 | 5 | 6 | 7 | 8 | 9 | . 10 | 12 | */.git/* 13 | */ci/* 14 | */features/* 15 | */packages/* 16 | */tests/* 17 | */utils/* 18 | */vendor/* 19 | 20 | 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | */php/commands/src/CLI_Command.php 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/WP_Super_Cache_CLI_Loader.php: -------------------------------------------------------------------------------- 1 | register_hooks(); 31 | return; 32 | } 33 | 34 | $error_msg = ''; 35 | 36 | // Before loading files check is plugin installed/activated. 37 | if ( $this->get_wpsc_version() === '' ) { 38 | $error_msg = 'WP Super Cache needs to be installed to use its WP-CLI commands.'; 39 | } elseif ( version_compare( $this->get_wpsc_version(), '1.5.2', '<' ) ) { 40 | $error_msg = 'Minimum required version of WP Super Cache is 1.5.2'; 41 | } elseif ( ! $this->is_wpsc_plugin_active() ) { 42 | $error_msg = 'WP Super Cache needs to be activated to use its WP-CLI commands.'; 43 | } elseif ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) { 44 | $error_msg = 'WP_CACHE constant is false or not defined'; 45 | } elseif ( defined( 'WP_CACHE' ) && WP_CACHE && defined( 'ADVANCEDCACHEPROBLEM' ) ) { 46 | $error_msg = 'WP Super Cache caching is broken'; 47 | } 48 | 49 | if ( $error_msg ) { 50 | WP_CLI::error( $error_msg ); 51 | } 52 | 53 | // Initialization of cache-base. 54 | $this->init_cache_base(); 55 | 56 | // Load dependencies if they aren't loaded. 57 | $this->maybe_load_files(); 58 | } 59 | 60 | /** 61 | * Registers the hooks. 62 | * 63 | * @return void 64 | */ 65 | private function register_hooks() { 66 | WP_CLI::add_wp_hook( 'muplugins_loaded', array( $this, 'init_cache_base' ) ); 67 | WP_CLI::add_wp_hook( 'plugins_loaded', array( $this, 'maybe_load_files' ) ); 68 | } 69 | 70 | /** 71 | * Initialization of cache-base. 72 | * 73 | * @global string $WPSC_HTTP_HOST 74 | * 75 | * @return void 76 | */ 77 | public function init_cache_base() { 78 | global $WPSC_HTTP_HOST; 79 | 80 | if ( ! defined( 'WPCACHEHOME' ) ) { 81 | return; 82 | } 83 | 84 | // Loads config file. 85 | $this->maybe_load_config(); 86 | 87 | // If the parameter --url doesn't exist then gets HTTP_HOST from WordPress Address. 88 | if ( empty( $_SERVER['HTTP_HOST'] ) ) { 89 | $_SERVER['HTTP_HOST'] = $this->parse_home_url( PHP_URL_HOST ); 90 | } 91 | 92 | if ( empty( $WPSC_HTTP_HOST ) ) { 93 | $this->maybe_include_file( 'include', 'wp-cache-base.php' ); 94 | } 95 | } 96 | 97 | /** 98 | * Loads config file and populates globals. 99 | * 100 | * @return void 101 | */ 102 | private function maybe_load_config() { 103 | global $cache_enabled, $super_cache_enabled, $cache_path, $wp_cache_mod_rewrite, $wp_cache_debug_log; 104 | global $wp_cache_config_file, $wp_cache_config_file_sample, $wp_cache_home_path; 105 | 106 | if ( empty( $wp_cache_config_file ) ) { 107 | return; 108 | } 109 | 110 | if ( ! isset( $cache_enabled, $super_cache_enabled, $cache_path, $wp_cache_mod_rewrite, $wp_cache_debug_log ) 111 | && ! $this->maybe_include_file( 'include', $wp_cache_config_file ) 112 | ) { 113 | if ( ! defined( 'WPCACHEHOME' ) 114 | || empty( $wp_cache_config_file_sample ) 115 | || ! $this->maybe_include_file( 'include', $wp_cache_config_file_sample ) 116 | ) { 117 | WP_CLI::error( 'Cannot load cache config file.' ); 118 | } 119 | 120 | WP_CLI::warning( 'Default cache config file loaded - ' . str_replace( ABSPATH, '', $wp_cache_config_file_sample ) ); 121 | } 122 | 123 | $wp_cache_home_path = trailingslashit( $this->parse_home_url( PHP_URL_PATH ) ); 124 | } 125 | 126 | /** 127 | * Loads config file, PHP files and overrides multisite settings. 128 | * 129 | * @return void 130 | */ 131 | public function maybe_load_files() { 132 | // WPSC >= 1.5.2 and it's active? 133 | if ( ! defined( 'WPCACHEHOME' ) || ! function_exists( 'wpsc_init' ) ) { 134 | return; 135 | } 136 | 137 | if ( version_compare( $this->get_wpsc_version(), '1.5.9', '>=' ) ) { 138 | // In rare cases, loading of wp-cache-phase2.php may be necessary. 139 | $this->maybe_include_file( 'wp_cache_phase2', 'wp-cache-phase2.php' ); 140 | } else { 141 | // Prevents creation of output buffer or serving file for older versions. 142 | $request_method = $_SERVER['REQUEST_METHOD']; 143 | $_SERVER['REQUEST_METHOD'] = 'POST'; 144 | } 145 | 146 | // List of required files. 147 | $include_files = array( 148 | 'wp_cache_postload' => array( 149 | 'file' => 'wp-cache-phase1.php', 150 | 'run' => '', 151 | ), 152 | 'domain_mapping_actions' => array( 153 | 'file' => 'plugins/domain-mapping.php', 154 | 'run' => 'domain_mapping_actions', 155 | ), 156 | 'wp_super_cache_multisite_init' => array( 157 | 'file' => 'plugins/multisite.php', 158 | 'run' => 'wp_super_cache_override_on_flag', 159 | ), 160 | ); 161 | 162 | foreach ( $include_files as $func => $file ) { 163 | $this->maybe_include_file( $func, $file['file'], $file['run'] ); 164 | } 165 | 166 | if ( ! empty( $request_method ) ) { 167 | $_SERVER['REQUEST_METHOD'] = $request_method; 168 | } 169 | 170 | $this->multisite_override_settings(); 171 | } 172 | 173 | /** 174 | * Overrides multisite settings. 175 | * 176 | * @global string $cache_path Absolute path to cache directory. 177 | * @global string $blogcacheid 178 | * @global string $blog_cache_dir 179 | * @global object $current_site The current site. 180 | * 181 | * @return void 182 | */ 183 | private function multisite_override_settings() { 184 | global $cache_path, $blogcacheid, $blog_cache_dir, $current_site; 185 | 186 | if ( ! is_multisite() ) { 187 | // Prevents PHP notices for single site installation. 188 | if ( ! isset( $blog_cache_dir ) ) { 189 | $blog_cache_dir = $cache_path; 190 | } 191 | 192 | return; 193 | } 194 | 195 | if ( is_object( $current_site ) ) { 196 | $blogcacheid = trim( is_subdomain_install() ? $current_site->domain : $current_site->path, '/' ); 197 | $blog_cache_dir = $cache_path . 'blogs/' . $blogcacheid . '/'; 198 | } 199 | } 200 | 201 | /** 202 | * Gets absolute path for file if file exists. 203 | * Returns empty string if file doesn't exist or isn't readable. 204 | * 205 | * @param string $filename File name. 206 | * 207 | * @return string 208 | */ 209 | private function get_wpsc_filename( $filename ) { 210 | if ( 0 !== strpos( $filename, ABSPATH ) ) { 211 | $filename = WPCACHEHOME . $filename; 212 | } 213 | 214 | if ( ! is_file( $filename ) || ! is_readable( $filename ) ) { 215 | return ''; 216 | } 217 | 218 | return $filename; 219 | } 220 | 221 | /** 222 | * If function doesn't exist then loads file and ivokes function if it needs. 223 | * Explicitly declares all globals which WPSC uses. 224 | * 225 | * @param string $func Function name. 226 | * @param string $filename File name. 227 | * @param string $run Optional function will be called if file is included. 228 | * 229 | * @return boolean True if file is included or false if it isn't included. 230 | */ 231 | private function maybe_include_file( $func, $filename, $run = '' ) { 232 | // Globals from wp-cache-config.php. 233 | global $super_cache_enabled, $cache_enabled, $wp_cache_mod_rewrite, $wp_cache_home_path, $cache_path, $file_prefix; 234 | global $wp_cache_mutex_disabled, $mutex_filename, $sem_id, $wp_super_cache_late_init; 235 | global $cache_compression, $cache_max_time, $wp_cache_shutdown_gc, $cache_rebuild_files; 236 | global $wp_super_cache_debug, $wp_super_cache_advanced_debug, $wp_cache_debug_level, $wp_cache_debug_to_file; 237 | global $wp_cache_debug_log, $wp_cache_debug_ip, $wp_cache_debug_username, $wp_cache_debug_email; 238 | global $cache_time_interval, $cache_scheduled_time, $cache_schedule_interval, $cache_schedule_type, $cache_gc_email_me; 239 | global $wp_cache_preload_on, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_taxonomies; 240 | global $wp_cache_preload_email_me, $wp_cache_preload_email_volume; 241 | global $wp_cache_mobile, $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes; 242 | // Globals from other files. 243 | global $wp_cache_config_file, $wp_cache_config_file_sample, $cache_domain_mapping; 244 | global $WPSC_HTTP_HOST, $blogcacheid, $blog_cache_dir; 245 | 246 | $file = $this->get_wpsc_filename( $filename ); 247 | 248 | if ( empty( $file ) || 249 | ( ! in_array( $func, array( 'require', 'require_once', 'include', 'include_once' ), true ) 250 | && function_exists( $func ) 251 | ) 252 | ) { 253 | return false; 254 | } 255 | 256 | switch ( $func ) { 257 | case 'require': 258 | $loaded = require $file; 259 | break; 260 | case 'require_once': 261 | $loaded = require_once $file; 262 | break; 263 | case 'include': 264 | $loaded = include $file; 265 | break; 266 | case 'include_once': 267 | default: 268 | $loaded = include_once $file; 269 | break; 270 | } 271 | 272 | if ( $loaded && ! empty( $run ) && function_exists( $run ) ) { 273 | call_user_func( $run ); 274 | } 275 | 276 | return $loaded; 277 | } 278 | 279 | /** 280 | * Gets version of WP Super Cache. 281 | * 282 | * @global string $wp_cache_config_file_sample Absolute path to wp-cache config sample file. 283 | * 284 | * @return string 285 | */ 286 | public function get_wpsc_version() { 287 | global $wp_cache_config_file_sample; 288 | 289 | if ( isset( $this->wpsc_version ) ) { 290 | return $this->wpsc_version; 291 | } 292 | 293 | if ( ! function_exists( 'get_file_data' ) ) { 294 | return ''; 295 | } 296 | 297 | if ( ! function_exists( 'get_plugin_data' ) ) { 298 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; 299 | } 300 | 301 | $this->wpsc_version = ''; 302 | $this->wpsc_plugin_file = empty( $wp_cache_config_file_sample ) 303 | ? trailingslashit( WP_PLUGIN_DIR ) . 'wp-super-cache/wp-cache.php' 304 | : plugin_dir_path( $wp_cache_config_file_sample ) . 'wp-cache.php'; 305 | 306 | if ( ! is_file( $this->wpsc_plugin_file ) || ! is_readable( $this->wpsc_plugin_file ) ) { 307 | return $this->wpsc_version; 308 | } 309 | 310 | $plugin_details = get_plugin_data( $this->wpsc_plugin_file ); 311 | if ( ! empty( $plugin_details['Version'] ) ) { 312 | $this->wpsc_version = $plugin_details['Version']; 313 | } 314 | 315 | return $this->wpsc_version; 316 | } 317 | 318 | /** 319 | * Check whether wp-super-cache plugin is active. 320 | * 321 | * @return bool 322 | */ 323 | private function is_wpsc_plugin_active() { 324 | if ( $this->get_wpsc_version() && is_plugin_active( plugin_basename( $this->wpsc_plugin_file ) ) ) { 325 | return true; 326 | } 327 | 328 | return false; 329 | } 330 | 331 | /** 332 | * Retrieves the component (PHP_URL_HOST or PHP_URL_PATH) from home URL. 333 | * 334 | * @param int $component The component to retrieve. 335 | * 336 | * @return string 337 | */ 338 | private function parse_home_url( $component ) { 339 | return function_exists( 'wp_parse_url' ) 340 | ? (string) wp_parse_url( get_option( 'home' ), $component ) 341 | : (string) parse_url( get_option( 'home' ), $component ); 342 | } 343 | } 344 | -------------------------------------------------------------------------------- /src/WP_Super_Cache_Command.php: -------------------------------------------------------------------------------- 1 | load(); 26 | $this->wpsc_version = $cli_loader->get_wpsc_version(); 27 | 28 | // Check if basic global variables are populated. 29 | if ( ! isset( $cache_enabled, $super_cache_enabled, $cache_path, $wp_cache_mod_rewrite, $wp_cache_debug_log ) ) { 30 | WP_CLI::error( 'WP Super Cache plugin is not properly loaded' ); 31 | } 32 | } 33 | 34 | /** 35 | * Clear something from the cache. 36 | * 37 | * @synopsis [--post_id=] [--permalink=] 38 | * 39 | * @when after_wp_load 40 | */ 41 | public function flush( $args = array(), $assoc_args = array() ) { 42 | global $file_prefix; 43 | 44 | $this->load(); 45 | 46 | if ( isset( $assoc_args['post_id'] ) ) { 47 | if ( is_numeric( $assoc_args['post_id'] ) ) { 48 | wp_cache_post_change( $assoc_args['post_id'] ); 49 | } else { 50 | WP_CLI::error( 'This is not a valid post id.' ); 51 | } 52 | 53 | wp_cache_post_change( $assoc_args['post_id'] ); 54 | } elseif ( isset( $assoc_args['permalink'] ) ) { 55 | $id = url_to_postid( $assoc_args['permalink'] ); 56 | 57 | if ( is_numeric( $id ) ) { 58 | wp_cache_post_change( $id ); 59 | } else { 60 | WP_CLI::error( 'There is no post with this permalink.' ); 61 | } 62 | } else { 63 | wp_cache_clean_cache( $file_prefix, true ); 64 | WP_CLI::success( 'Cache cleared.' ); 65 | } 66 | } 67 | 68 | /** 69 | * Get the status of the cache. 70 | * 71 | * @when after_wp_load 72 | */ 73 | public function status( $args = array(), $assoc_args = array() ) { 74 | global $cache_enabled, $super_cache_enabled, $wp_cache_mod_rewrite; 75 | 76 | $this->load(); 77 | 78 | WP_CLI::line( 'Version of WP Super Cache: ' . $this->wpsc_version ); 79 | WP_CLI::line(); 80 | 81 | $cache_method = 'WP-Cache'; 82 | if ( $cache_enabled && $super_cache_enabled ) { 83 | $cache_method = $wp_cache_mod_rewrite ? 'Expert' : 'Simple'; 84 | } 85 | 86 | $cache_status = 'Cache status: ' . WP_CLI::colorize( $cache_enabled ? '%gOn%n' : '%rOff%n' ) . PHP_EOL; 87 | $cache_status .= $cache_enabled 88 | ? 'Cache Delivery Method: ' . $cache_method . PHP_EOL 89 | : ''; 90 | WP_CLI::line( $cache_status ); 91 | 92 | $cache_stats = get_option( 'supercache_stats' ); 93 | if ( ! empty( $cache_stats ) ) { 94 | if ( $cache_stats['generated'] > time() - 3600 * 24 ) { 95 | WP_CLI::line( 'Cache content on ' . date( 'r', $cache_stats['generated'] ) . ': ' ); 96 | WP_CLI::line(); 97 | WP_CLI::line( ' WordPress cache:' ); 98 | WP_CLI::line( ' Cached: ' . $cache_stats['wpcache']['cached'] ); 99 | WP_CLI::line( ' Expired: ' . $cache_stats['wpcache']['expired'] ); 100 | WP_CLI::line(); 101 | WP_CLI::line( ' WP Super Cache:' ); 102 | WP_CLI::line( ' Cached: ' . $cache_stats['supercache']['cached'] ); 103 | WP_CLI::line( ' Expired: ' . $cache_stats['supercache']['expired'] ); 104 | } else { 105 | WP_CLI::error( 'The WP Super Cache stats are too old to work with (older than 24 hours).' ); 106 | } 107 | } else { 108 | WP_CLI::error( 'No WP Super Cache stats found.' ); 109 | } 110 | } 111 | 112 | /** 113 | * Enable the WP Super Cache. 114 | * 115 | * @when after_wp_load 116 | */ 117 | public function enable( $args = array(), $assoc_args = array() ) { 118 | global $cache_enabled, $wp_cache_mod_rewrite; 119 | 120 | $this->load(); 121 | 122 | wp_cache_enable(); 123 | if ( ! defined( 'DISABLE_SUPERCACHE' ) ) { 124 | wp_super_cache_enable(); 125 | } 126 | 127 | if ( $wp_cache_mod_rewrite ) { 128 | add_mod_rewrite_rules(); 129 | } 130 | 131 | if ( $cache_enabled ) { 132 | WP_CLI::success( 'The WP Super Cache is enabled.' ); 133 | } else { 134 | WP_CLI::error( 'The WP Super Cache is not enabled, check its settings page for more info.' ); 135 | } 136 | } 137 | 138 | /** 139 | * Disable the WP Super Cache. 140 | * 141 | * @when after_wp_load 142 | */ 143 | public function disable( $args = array(), $assoc_args = array() ) { 144 | global $cache_enabled; 145 | 146 | $this->load(); 147 | 148 | wp_cache_disable(); 149 | wp_super_cache_disable(); 150 | 151 | if ( ! $cache_enabled ) { 152 | WP_CLI::success( 'The WP Super Cache is disabled.' ); 153 | } else { 154 | WP_CLI::error( 'The WP Super Cache is still enabled, check its settings page for more info.' ); 155 | } 156 | } 157 | 158 | /** 159 | * Primes the cache by creating static pages before users visit them 160 | * 161 | * @synopsis [--status] [--cancel] 162 | * 163 | * @when after_wp_load 164 | */ 165 | public function preload( $args = array(), $assoc_args = array() ) { 166 | global $super_cache_enabled; 167 | 168 | $this->load(); 169 | 170 | $preload_counter = get_option( 'preload_cache_counter' ); 171 | $preloading = is_array( $preload_counter ) && $preload_counter['c'] > 0; 172 | $pending_cancel = get_option( 'preload_cache_stop' ); 173 | 174 | // Bail early if caching or preloading is disabled 175 | if ( ! $super_cache_enabled ) { 176 | WP_CLI::error( 'The WP Super Cache is not enabled.' ); 177 | } 178 | 179 | if ( defined( 'DISABLESUPERCACHEPRELOADING' ) && true == DISABLESUPERCACHEPRELOADING ) { 180 | WP_CLI::error( 'Cache preloading is not enabled.' ); 181 | } 182 | 183 | // Display status 184 | if ( isset( $assoc_args['status'] ) ) { 185 | $this->preload_status( $preload_counter, $pending_cancel ); 186 | exit(); 187 | } 188 | 189 | // Cancel preloading if in progress 190 | if ( isset( $assoc_args['cancel'] ) ) { 191 | if ( $preloading ) { 192 | if ( $pending_cancel ) { 193 | WP_CLI::error( 'There is already a pending preload cancel. It may take up to a minute for it to cancel completely.' ); 194 | } else { 195 | update_option( 'preload_cache_stop', true ); 196 | WP_CLI::success( 'Scheduled preloading of cache almost cancelled. It may take up to a minute for it to cancel completely.' ); 197 | exit(); 198 | } 199 | } else { 200 | WP_CLI::error( 'Not currently preloading.' ); 201 | } 202 | } 203 | 204 | // Start preloading if not already in progress 205 | if ( $preloading ) { 206 | WP_CLI::warning( 'Cache preloading is already in progress.' ); 207 | $this->preload_status( $preload_counter, $pending_cancel ); 208 | exit(); 209 | } else { 210 | wp_schedule_single_event( time(), 'wp_cache_full_preload_hook' ); 211 | WP_CLI::success( 'Scheduled preload for next cron run.' ); 212 | } 213 | } 214 | 215 | /** 216 | * Outputs the status of preloading 217 | * 218 | * @param $preload_counter 219 | * @param $pending_cancel 220 | */ 221 | protected function preload_status( $preload_counter, $pending_cancel ) { 222 | if ( is_array( $preload_counter ) && $preload_counter['c'] > 0 ) { 223 | WP_CLI::line( sprintf( 'Currently caching from post %d to %d.', $preload_counter['c'] - 100, $preload_counter['c'] ) ); 224 | 225 | if ( $pending_cancel ) { 226 | WP_CLI::warning( 'Pending preload cancel. It may take up to a minute for it to cancel completely.' ); 227 | } 228 | } else { 229 | WP_CLI::line( 'Not currently preloading.' ); 230 | } 231 | } 232 | } 233 | -------------------------------------------------------------------------------- /wp-cli.yml: -------------------------------------------------------------------------------- 1 | require: 2 | - wp-super-cache-cli.php 3 | -------------------------------------------------------------------------------- /wp-super-cache-cli.php: -------------------------------------------------------------------------------- 1 |