From 4ff859d9d650dab2cb797717cc8d731244a51a48 Mon Sep 17 00:00:00 2001 From: Caspar Green Date: Wed, 2 Dec 2020 14:16:05 -0500 Subject: [PATCH 01/10] Add nginx parameter with check and set global (#38) * Add nginx parameter with check and set global * Add example to docblock using new parameter * Add new command example to readme --- README.md | 5 +++-- command.php | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 469f872..b4a1b81 100755 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This repository contains a [WP-CLI command](https://github.com/wp-cli/wp-cli) for the [WP Rocket](http://wp-rocket.me) plugin. After installing this plugin, you will have access to a `wp rocket` command. -Currently supported commands: +Supported commands: * `wp rocket activate` -- Set WP_CACHE to true. * `wp rocket deactivate` -- Set WP_CACHE to false. @@ -11,7 +11,8 @@ Currently supported commands: * `wp rocket clean --confirm` -- Purge cache files without prompting for confirmation (usefull for automation tools/scripts) * `wp rocket preload` -- Preload cache files. * `wp rocket regenerate --file=` -- Regenerate .htaccess, advanced-cache.php or the WP Rocket config file. - +* `wp rocket regenerate --file=config --nginx=true` -- regenerate the config file on Nginx hosts. + ## Installing If you're using WP-CLI v0.23.0 or later, you can install this package with: diff --git a/command.php b/command.php index a8cabe7..1058fae 100644 --- a/command.php +++ b/command.php @@ -262,9 +262,13 @@ public function preload( $args = array(), $assoc_args = array() ) { * - advanced-cache * - config (It's the config file stored in the wp-rocket-config folder) * + * [--nginx=] + * : The command should run as if on nginx (setting the $is_nginx global to true) + * * ## EXAMPLES * * wp rocket regenerate --file=htaccess + * wp rocket regenerate --file=config --nginx=true * * @subcommand regenerate */ @@ -276,6 +280,9 @@ public function regenerate( $args = array(), $assoc_args = array() ) { WP_CLI::success( 'The advanced-cache.php file has just been regenerated.' ); break; case 'config': + if ( ! empty( $assoc_args['nginx'] ) && $assoc_args = true ) { + $GLOBALS['is_nginx'] = true; + } rocket_generate_config_file(); WP_CLI::success( 'The config file has just been regenerated.' ); break; From 3d9196ccd8df2e979bca42b6fe39f6c1f8cd84c6 Mon Sep 17 00:00:00 2001 From: Amaury Balmer Date: Wed, 2 Dec 2020 20:40:11 +0100 Subject: [PATCH 02/10] Fix clear command errors out when plugin is not activated --- command.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/command.php b/command.php index 1058fae..cb3a2ee 100644 --- a/command.php +++ b/command.php @@ -91,6 +91,9 @@ public function deactivate() { * @subcommand clean */ public function clean( $args = array(), $assoc_args = array() ) { + if ( ! function_exists( 'rocket_clean_domain' ) ) { + WP_CLI::error( ' The plugin WP-Rocket seems not enabled on this site.' ); + } if( ! empty( $assoc_args['blog_id'] ) ) { From d0a1c881abdc35bfbbecd79b14c4f82f89176802 Mon Sep 17 00:00:00 2001 From: Ramon Fincken Date: Wed, 2 Dec 2020 21:50:27 +0100 Subject: [PATCH 03/10] Some grammar and WP code styling fixes (PR #35) --- command.php | 14 +++++++------- wp-rocket-cli.php | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/command.php b/command.php index cb3a2ee..cd13a21 100644 --- a/command.php +++ b/command.php @@ -19,13 +19,13 @@ public function activate() { if( is_writable( rocket_find_wpconfig_path() ) ) { set_rocket_wp_cache_define( true ); - WP_CLI::success( 'WP Rocket is enable, WP_CACHE is set to true.' ); + WP_CLI::success( 'WP Rocket is now enabled, WP_CACHE is set to true.' ); } else { WP_CLI::error( 'It seems we don\'t have writing permissions on wp-config.php file.' ); } } else { - WP_CLI::error( 'WP Rocket is already enable.' ); + WP_CLI::error( 'WP Rocket is already enabled.' ); } } @@ -45,13 +45,13 @@ public function deactivate() { if( is_writable( rocket_find_wpconfig_path() ) ) { set_rocket_wp_cache_define( false ); - WP_CLI::success( 'WP Rocket is disable, WP_CACHE is set to false.' ); + WP_CLI::success( 'WP Rocket is now disabled, WP_CACHE is set to false.' ); } else { WP_CLI::error( 'It seems we don\'t have writing permissions on wp-config.php file.' ); } } else { - WP_CLI::error( 'WP Rocket is already disable.' ); + WP_CLI::error( 'WP Rocket is already disabled.' ); } } @@ -98,7 +98,7 @@ public function clean( $args = array(), $assoc_args = array() ) { if( ! empty( $assoc_args['blog_id'] ) ) { if ( ! defined( 'MULTISITE' ) || ! MULTISITE ) { - WP_CLI::error( 'This installation doesn\'t multisite support.' ); + WP_CLI::error( 'This installation doesn\'t support multisite.' ); } $blog_ids = explode( ',' , $assoc_args['blog_id'] ); @@ -295,12 +295,12 @@ public function regenerate( $args = array(), $assoc_args = array() ) { WP_CLI::success( 'The .htaccess file has just been regenerated.' ); break; default: - WP_CLI::error( 'You don\'t specify a good value for the "file" argument. It should be: advanced-cache, config or htaccess.' ); + WP_CLI::error( 'You didn\'t specify a good value for the "file" argument. It should be: advanced-cache, config or htaccess.' ); break; } } else { - WP_CLI::error( 'You don\'t specify the "file" argument.' ); + WP_CLI::error( 'You didn\'t specify the "file" argument.' ); } } } diff --git a/wp-rocket-cli.php b/wp-rocket-cli.php index 311781a..c2d35b3 100644 --- a/wp-rocket-cli.php +++ b/wp-rocket-cli.php @@ -11,6 +11,6 @@ */ -if ( defined('WP_CLI') && WP_CLI ) { - require('command.php'); -} \ No newline at end of file +if ( defined( 'WP_CLI' ) && WP_CLI ) { + require( 'command.php' ); +} From 8338a53c8676d769129221a0c7182fed2c1b6d28 Mon Sep 17 00:00:00 2001 From: CEnnis91 Date: Wed, 2 Dec 2020 15:52:16 -0500 Subject: [PATCH 04/10] Add option to trigger sitemap-based preloading (PR #36) --- command.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/command.php b/command.php index cd13a21..66dc435 100644 --- a/command.php +++ b/command.php @@ -242,14 +242,27 @@ public function clean( $args = array(), $assoc_args = array() ) { /** * Run WP Rocket Bot for preload cache files * + * ## OPTIONS + * + * [--sitemap] + * : Trigger sitemap-based preloading + * * ## EXAMPLES * * wp rocket preload + * wp rocket preload --sitemap * * @subcommand preload */ public function preload( $args = array(), $assoc_args = array() ) { - run_rocket_bot( 'cache-preload' ); + + if ( ! empty( $assoc_args['sitemap'] ) && $assoc_args['sitemap'] ) { + WP_CLI::line( 'Triggering sitemap-based preloading.' ); + run_rocket_sitemap_preload(); + } else { + WP_CLI::line( 'Triggering homepage-based preloading.' ); + run_rocket_bot( 'cache-preload' ); + } WP_CLI::success( 'Finished WP Rocket preload cache files.' ); } From e8488b33c1101e00fe339573979f13d2d1a57916 Mon Sep 17 00:00:00 2001 From: Ahmed Saed Date: Fri, 22 Jan 2021 16:10:50 +0200 Subject: [PATCH 05/10] Remove deprecated functions & update methods (PR #37) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * solve duplications on activate and deactivate * upgrade to php 5.6 and refactor the remaining methods * update require PHP and authors * import function and update copy Co-authored-by: Rémy Perona --- command.php | 111 +++++++++++++++++++++++--------------------------- composer.json | 7 ++-- 2 files changed, 55 insertions(+), 63 deletions(-) diff --git a/command.php b/command.php index 66dc435..9099bca 100644 --- a/command.php +++ b/command.php @@ -1,4 +1,8 @@ set_wp_cache_constant( true ) ) { WP_CLI::success( 'WP Rocket is now enabled, WP_CACHE is set to true.' ); } else { - WP_CLI::error( 'It seems we don\'t have writing permissions on wp-config.php file.' ); + WP_CLI::error( 'Error while setting WP_CACHE constant into wp-config.php!' ); } } else { @@ -43,11 +48,12 @@ public function deactivate() { if( defined( 'WP_CACHE' ) && WP_CACHE ) { - if( is_writable( rocket_find_wpconfig_path() ) ) { - set_rocket_wp_cache_define( false ); + $wp_cache = new WPCache( rocket_direct_filesystem() ); + + if ( $wp_cache->set_wp_cache_constant( false ) ) { WP_CLI::success( 'WP Rocket is now disabled, WP_CACHE is set to false.' ); } else { - WP_CLI::error( 'It seems we don\'t have writing permissions on wp-config.php file.' ); + WP_CLI::error( 'Error while setting WP_CACHE constant into wp-config.php!' ); } } else { @@ -90,24 +96,23 @@ public function deactivate() { * * @subcommand clean */ - public function clean( $args = array(), $assoc_args = array() ) { + public function clean( array $args = [], array $assoc_args = [] ) { if ( ! function_exists( 'rocket_clean_domain' ) ) { WP_CLI::error( ' The plugin WP-Rocket seems not enabled on this site.' ); } if( ! empty( $assoc_args['blog_id'] ) ) { - if ( ! defined( 'MULTISITE' ) || ! MULTISITE ) { WP_CLI::error( 'This installation doesn\'t support multisite.' ); } $blog_ids = explode( ',' , $assoc_args['blog_id'] ); - $blog_ids = array_map( 'trim' , $blog_ids ); $total = 0; $notify = \WP_CLI\Utils\make_progress_bar( 'Delete cache files', count( $blog_ids ) ); foreach ( $blog_ids as $blog_id ) { + $blog_id = trim( $blog_id ); if ( $bloginfo = get_blog_details( (int) $blog_id, false ) ) { @@ -119,7 +124,6 @@ public function clean( $args = array(), $assoc_args = array() ) { restore_current_blog(); $total++; - } else { WP_CLI::line( 'This blog ID "' . $blog_id . '" doesn\'t exist.' ); } @@ -132,9 +136,8 @@ public function clean( $args = array(), $assoc_args = array() ) { WP_CLI::success( 'Cache cleared for ' . $total . ' blog(s).' ); } else if( ! empty( $assoc_args['lang'] ) ) { - if( ! rocket_has_i18n() ) { - WP_CLI::error( 'No WPML or qTranslate in this website.' ); + WP_CLI::error( 'No WPML, Polylang or qTranslate in this website.' ); } $langs = explode( ',' , $assoc_args['lang'] ); @@ -145,7 +148,7 @@ public function clean( $args = array(), $assoc_args = array() ) { foreach ( $langs as $lang ) { - rocket_clean_domain_for_selected_lang( $lang ); + rocket_clean_domain( $lang ); $notify->tick(); } @@ -154,56 +157,37 @@ public function clean( $args = array(), $assoc_args = array() ) { WP_CLI::success( 'Cache files cleared for ' . $total . ' lang(s).' ); } else if( ! empty( $assoc_args['permalink'] ) ) { - $permalinks = explode( ',' , $assoc_args['permalink'] ); - $permalinks = array_map( 'trim' , $permalinks ); $total = count( $permalinks ); - $notify = \WP_CLI\Utils\make_progress_bar( 'Delete cache files', $total ); + $notify = make_progress_bar( 'Delete cache files', $total ); foreach ( $permalinks as $permalink ) { + $permalink = trim( $permalink ); rocket_clean_files( $permalink ); WP_CLI::line( 'Cache cleared for "' . $permalink . '".' ); $notify->tick(); - } $notify->finish(); WP_CLI::success( 'Cache files cleared for ' . $total . ' permalink(s).' ); } else if( ! empty( $assoc_args['post_id'] ) ) { - $total = 0; $post_ids = explode( ',' , $assoc_args['post_id'] ); - $post_ids = array_map( 'trim' , $post_ids ); - $notify = \WP_CLI\Utils\make_progress_bar( 'Delete cache files', count( $post_ids ) ); + $notify = make_progress_bar( 'Delete cache files', count( $post_ids ) ); foreach ( $post_ids as $post_id ) { + $post_id = trim( $post_id ); - global $wpdb; - $post_exists = $wpdb->get_row( "SELECT ID FROM $wpdb->posts WHERE id = '" . (int) $post_id . "'"); - - if( $post_exists ) { - - if( get_post_type( $post_id ) == 'attachment' ) { - - WP_CLI::line( 'This post ID "' . $post_id . '" is an attachment.' ); - - } else { - - rocket_clean_post( $post_id ); - WP_CLI::line( 'Cache cleared for post ID "' . $post_id . '".' ); - $total++; - - } - + if( rocket_clean_post( $post_id ) ) { + WP_CLI::line( 'Cache cleared for post ID "' . $post_id . '".' ); + $total++; } else { - - WP_CLI::line( 'This post ID "' . $post_id . '" doesn\'t exist.' ); - + WP_CLI::line( 'This post ID "' . $post_id . '" is not a valid public post.' ); } $notify->tick(); @@ -211,7 +195,6 @@ public function clean( $args = array(), $assoc_args = array() ) { } if( $total ) { - $notify->finish(); if( $total == 1 ) { @@ -221,20 +204,20 @@ public function clean( $args = array(), $assoc_args = array() ) { } } else { - WP_CLI::error( 'No cache files are cleared.' ); + WP_CLI::error( 'No cache files cleared.' ); } - } else { - if ( ! empty( $assoc_args['confirm'] ) && $assoc_args['confirm'] ) { WP_CLI::line( 'Deleting all cache files.' ); } else { WP_CLI::confirm( 'Delete all cache files ?' ); } - rocket_clean_domain(); - WP_CLI::success( 'All cache files cleared.' ); - + if ( rocket_clean_domain() ) { + WP_CLI::success( 'All cache files cleared.' ); + }else{ + WP_CLI::error( 'No cache files are cleared.' ); + } } } @@ -246,7 +229,7 @@ public function clean( $args = array(), $assoc_args = array() ) { * * [--sitemap] * : Trigger sitemap-based preloading - * + * * ## EXAMPLES * * wp rocket preload @@ -254,14 +237,17 @@ public function clean( $args = array(), $assoc_args = array() ) { * * @subcommand preload */ - public function preload( $args = array(), $assoc_args = array() ) { - + public function preload( array $args = [], array $assoc_args = [] ) { + if ( ! empty( $assoc_args['sitemap'] ) && $assoc_args['sitemap'] ) { WP_CLI::line( 'Triggering sitemap-based preloading.' ); run_rocket_sitemap_preload(); } else { - WP_CLI::line( 'Triggering homepage-based preloading.' ); - run_rocket_bot( 'cache-preload' ); + if ( run_rocket_bot() ) { + WP_CLI::success( 'Triggering homepage-based preloading.' ); + }else{ + WP_CLI::error( 'Cannot start preload cache, please check preload option is activated.' ); + } } WP_CLI::success( 'Finished WP Rocket preload cache files.' ); @@ -288,32 +274,39 @@ public function preload( $args = array(), $assoc_args = array() ) { * * @subcommand regenerate */ - public function regenerate( $args = array(), $assoc_args = array() ) { + public function regenerate( array $args = [], array $assoc_args = [] ) { if( !empty( $assoc_args['file'] ) ) { switch( $assoc_args['file'] ) { case 'advanced-cache': - rocket_generate_advanced_cache_file(); - WP_CLI::success( 'The advanced-cache.php file has just been regenerated.' ); + if ( rocket_generate_advanced_cache_file() ) { + WP_CLI::success( 'The advanced-cache.php file has just been regenerated.' ); + }else{ + WP_CLI::error( 'Cannot generate advanced-cache.php file, please check folder permissions.' ); + } break; case 'config': if ( ! empty( $assoc_args['nginx'] ) && $assoc_args = true ) { $GLOBALS['is_nginx'] = true; } + rocket_generate_config_file(); WP_CLI::success( 'The config file has just been regenerated.' ); break; case 'htaccess': $GLOBALS['is_apache'] = true; - flush_rocket_htaccess(); - WP_CLI::success( 'The .htaccess file has just been regenerated.' ); + if ( flush_rocket_htaccess() ) { + WP_CLI::success( 'The .htaccess file has just been regenerated.' ); + }else{ + WP_CLI::error( 'Cannot generate .htaccess file.' ); + } break; default: - WP_CLI::error( 'You didn\'t specify a good value for the "file" argument. It should be: advanced-cache, config or htaccess.' ); + WP_CLI::error( 'You did not specify a good value for the "file" argument. It should be: advanced-cache, config or htaccess.' ); break; } } else { - WP_CLI::error( 'You didn\'t specify the "file" argument.' ); + WP_CLI::error( 'You did not specify the "file" argument.' ); } } } diff --git a/composer.json b/composer.json index 3f175bd..e17280b 100644 --- a/composer.json +++ b/composer.json @@ -6,13 +6,12 @@ "homepage": "https://github.com/wp-media/wp-rocket-cli", "license": "MIT", "require": { - "php": ">=5.3.0" + "php": ">=5.6" }, "authors": [{ "name": "WP Rocket Team", - "homepage": "http://wp-rocket.me", - "email": "jonathan@wp-rocket.me", - "role": "Developer" + "homepage": "https://wp-rocket.me", + "email": "contact@wp-rocket.me" }], "support": { "issues": "https://github.com/wp-media/wp-rocket-cli/issues", From b51c7ca1d5b85cdea695ab96dacc30898a5d34df Mon Sep 17 00:00:00 2001 From: Cristina Soponar <45258937+crystinutzaa@users.noreply.github.com> Date: Fri, 22 Jan 2021 16:15:18 +0200 Subject: [PATCH 06/10] Closes #21 - Enable / Disable CDN with the specified host and zone (PR #39) --- README.md | 1 + command.php | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/README.md b/README.md index b4a1b81..efbc3e1 100755 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Supported commands: * `wp rocket preload` -- Preload cache files. * `wp rocket regenerate --file=` -- Regenerate .htaccess, advanced-cache.php or the WP Rocket config file. * `wp rocket regenerate --file=config --nginx=true` -- regenerate the config file on Nginx hosts. +* `wp rocket cdn --enable= --host= --zone=` -- Enable / Disable CDN with the specified host and zone. ## Installing diff --git a/command.php b/command.php index 9099bca..ea50ab3 100644 --- a/command.php +++ b/command.php @@ -309,6 +309,92 @@ public function regenerate( array $args = [], array $assoc_args = [] ) { WP_CLI::error( 'You did not specify the "file" argument.' ); } } + + /** + * Enable / Disable CDN option and set the CDN URL. + * + * ## OPTIONS + * + * [--enable=] + * : Option to enable / disable = boolean. + * + * [--host=] + * : CDN host. + * + * [--zone=] + * : CDN zone -> [ all, images, css_and_js, js, css ] + * + * ## EXAMPLES + * + * wp rocket cdn --enable=false + * wp rocket cdn --enable=true --host=http://cdn.example.com + * wp rocket cdn --enable=true --host=http://cdn.example.com --zone=all + * + * @subcommand cdn + */ + public function cdn( $args = array(), $assoc_args = array() ) { + if ( empty( $assoc_args['enable'] ) ) { + WP_CLI::error( 'The "enable" argument must be specified.' ); + return; + } + + switch( $assoc_args['enable'] ) { + case 'true': + update_rocket_option( 'cdn', true ); + + if ( ! empty( $assoc_args['host'] ) ) { + $cdn_cnames = get_rocket_option( 'cdn_cnames', true ); + $cdn_zones = get_rocket_option( 'cdn_zone', true ); + $cname = $assoc_args['host']; + $zone = ( ! empty( $assoc_args['zone'] ) ? $assoc_args['zone'] : 'all' ); + $exists = array_search( $cname, $cdn_cnames ); + if ( false === $exists ) { + // CNAME does not exist. Set it the last element. + $exists = count( $cdn_cnames ); + } + + $cdn_cnames[ $exists ] = $cname; + $cdn_zones[ $exists ] = $zone; + + update_rocket_option( 'cdn_cnames', $cdn_cnames ); + update_rocket_option( 'cdn_zone', $cdn_zones ); + $this->clean_wp_rocket_cache( true ); + + WP_CLI::success( 'CDN enabled successfully with CNAME=<' . $cname .'> and zone=<' . $zone . '>' ); + break; + } + + $this->clean_wp_rocket_cache( true ); + WP_CLI::success( 'CDN enabled successfully without CNAME' ); + break; + case 'false': + update_rocket_option( 'cdn', false ); + $this->clean_wp_rocket_cache( true ); + WP_CLI::success( 'CDN disabled successfully!' ); + break; + default: + WP_CLI::error( 'The "enable" argument must contain either true or false value.' ); + break; + } + } + + /** + * Clean WP Rocket domain and additional cache files. + * + * @param boolean $minify Clean also minify cache files. + * @return void + */ + private function clean_wp_rocket_cache( $minify = false ) { + rocket_clean_domain(); + + if ( $minify ) { + // Remove all minify cache files. + rocket_clean_minify(); + // Generate a new random key for minify cache file. + update_rocket_option( 'minify_css_key', create_rocket_uniqid() ); + update_rocket_option( 'minify_js_key', create_rocket_uniqid() ); + } + } } WP_CLI::add_command( 'rocket', 'WPRocket_CLI' ); From 326c01f1dfcf38684ea6205ada06ea75ba275f01 Mon Sep 17 00:00:00 2001 From: Cristina Soponar <45258937+crystinutzaa@users.noreply.github.com> Date: Fri, 22 Jan 2021 21:43:53 +0200 Subject: [PATCH 07/10] Closes #25 Clean command needs to clean minify folder (PR #40) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémy Perona --- command.php | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/command.php b/command.php index ea50ab3..d59fde4 100644 --- a/command.php +++ b/command.php @@ -88,10 +88,10 @@ public function deactivate() { * wp rocket clean --post_id=2 * wp rocket clean --post_id=2,4,6,8 * wp rocket clean --permalink=http://example.com - * wp rocket clean --permalink=http://example.com, http://example.com/category/(.*) - * wp rocket clean --lang=fr + * wp rocket clean --permalink=http://example.com,http://example.com/category/(.*) + * wp rocket clean --lang=fr * wp rocket clean --lang=fr,de,en,it - * wp rocket clean --blog_id=2 + * wp rocket clean --blog_id=2 * wp rocket clean --blog_id=2,4,6,8 * * @subcommand clean @@ -118,9 +118,19 @@ public function clean( array $args = [], array $assoc_args = [] ) { switch_to_blog( $blog_id ); + if ( ! is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) { + continue; + } + rocket_clean_domain(); WP_CLI::line( 'Cache cleared for "' . esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ) . '".' ); + // Remove all minify cache files. + rocket_clean_minify(); + // Generate a new random key for minify cache file. + update_rocket_option( 'minify_css_key', create_rocket_uniqid() ); + update_rocket_option( 'minify_js_key', create_rocket_uniqid() ); + restore_current_blog(); $total++; @@ -213,7 +223,15 @@ public function clean( array $args = [], array $assoc_args = [] ) { WP_CLI::confirm( 'Delete all cache files ?' ); } - if ( rocket_clean_domain() ) { + $success = rocket_clean_domain(); + + // Remove all minify cache files. + rocket_clean_minify(); + // Generate a new random key for minify cache file. + update_rocket_option( 'minify_css_key', create_rocket_uniqid() ); + update_rocket_option( 'minify_js_key', create_rocket_uniqid() ); + + if ( $success ) { WP_CLI::success( 'All cache files cleared.' ); }else{ WP_CLI::error( 'No cache files are cleared.' ); @@ -260,16 +278,16 @@ public function preload( array $args = [], array $assoc_args = [] ) { * * [--file=] * : The file to regenerate. It could be: - * - htaccess - * - advanced-cache - * - config (It's the config file stored in the wp-rocket-config folder) + * - htaccess + * - advanced-cache + * - config (It's the config file stored in the wp-rocket-config folder) * * [--nginx=] * : The command should run as if on nginx (setting the $is_nginx global to true) * * ## EXAMPLES * - * wp rocket regenerate --file=htaccess + * wp rocket regenerate --file=htaccess * wp rocket regenerate --file=config --nginx=true * * @subcommand regenerate From 7f8b17f301845db88c4c0f4052770e9e36d8f2dd Mon Sep 17 00:00:00 2001 From: Cristina Soponar <45258937+crystinutzaa@users.noreply.github.com> Date: Tue, 26 Jan 2021 17:23:55 +0200 Subject: [PATCH 08/10] Fixes #22 Activate/Deactivate does not trigger htaccess regeneration (PR #41) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémy Perona --- README.md | 4 +- command.php | 103 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 89 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index efbc3e1..ccebba1 100755 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ This repository contains a [WP-CLI command](https://github.com/wp-cli/wp-cli) f Supported commands: -* `wp rocket activate` -- Set WP_CACHE to true. -* `wp rocket deactivate` -- Set WP_CACHE to false. +* `wp rocket activate-cache` -- Set WP_CACHE to true, flush htaccess rules and clean WP Rocket cache. +* `wp rocket deactivate-cache` -- Set WP_CACHE to false, flush htaccess rules and clean WP Rocket cache. * `wp rocket clean --post_id= --permalink= --lang= --blog_id=` -- Purge cache files. * `wp rocket clean --confirm` -- Purge cache files without prompting for confirmation (usefull for automation tools/scripts) * `wp rocket preload` -- Preload cache files. diff --git a/command.php b/command.php index d59fde4..27a0ea7 100644 --- a/command.php +++ b/command.php @@ -7,59 +7,116 @@ * Manage Revisions */ class WPRocket_CLI extends WP_CLI_Command { + /** + * Backward compatibility method + * + * @return void + */ + public function activate() { + $this->activate_cache(); + } /** - * Set WP_CACHE constant in wp-config.php to true + * Set WP_CACHE constant in wp-config.php to true and update htaccess + * + * ## OPTIONS + * + * [--htaccess=] + * : Enable update of the htaccess file. * * ## EXAMPLES * - * wp rocket activate + * wp rocket activate-cache * - * @subcommand activate + * @subcommand activate-cache */ - public function activate() { - - if( defined( 'WP_CACHE' ) && ! WP_CACHE ) { + public function activate_cache( array $args = [], array $assoc_args = [] ) { + if ( ! is_plugin_active( 'wp-rocket/wp-rocket.php') ) { + WP_CLI::error( 'WP Rocket is not enabled.' ); + } + if ( defined( 'WP_CACHE' ) && ! WP_CACHE ) { $wp_cache = new WPCache( rocket_direct_filesystem() ); if ( $wp_cache->set_wp_cache_constant( true ) ) { + if ( rocket_valid_key() ) { + // Add All WP Rocket rules of the .htaccess file. + if ( isset( $assoc_args['htaccess'] ) && 'true' === $assoc_args['htaccess'] ) { + self::set_apache(); + + if ( ! flush_rocket_htaccess() ) { + WP_CLI::warning( 'Adding WP Rocket rules to the htaccess file failed.'); + } else { + WP_CLI::success( 'WP Rocket rules added to the htaccess file.'); + } + } + } + // Clean WP Rocket Cache and Minified files. + $this->clean_wp_rocket_cache( true ); + WP_CLI::success( 'WP Rocket is now enabled, WP_CACHE is set to true.' ); } else { WP_CLI::error( 'Error while setting WP_CACHE constant into wp-config.php!' ); } - } else { WP_CLI::error( 'WP Rocket is already enabled.' ); } + } + /** + * Backward compatibility method + * + * @return void + */ + public function deactivate() { + $this->deactivate_cache(); } /** - * Set WP_CACHE constant in wp-config.php to false + * Set WP_CACHE constant in wp-config.php to false and update htaccess + * + * ## OPTIONS + * + * [--htaccess=] + * : Enable update of the htaccess file. * * ## EXAMPLES * - * wp rocket deactivate + * wp rocket deactivate-cache * - * @subcommand deactivate + * @subcommand deactivate-cache */ - public function deactivate() { - - if( defined( 'WP_CACHE' ) && WP_CACHE ) { + public function deactivate_cache( array $args = [], array $assoc_args = [] ) { + if ( ! is_plugin_active( 'wp-rocket/wp-rocket.php') ) { + WP_CLI::error( 'WP Rocket is not enabled.' ); + } + if ( defined( 'WP_CACHE' ) && WP_CACHE ) { $wp_cache = new WPCache( rocket_direct_filesystem() ); if ( $wp_cache->set_wp_cache_constant( false ) ) { + if ( rocket_valid_key() ) { + // Remove All WP Rocket rules from the .htaccess file. + if ( isset( $assoc_args['htaccess'] ) && 'true' === $assoc_args['htaccess'] ) { + self::set_apache(); + + if ( ! flush_rocket_htaccess( true ) ) { + WP_CLI::warning( 'Removing WP Rocket rules from the htaccess file failed.'); + } else { + WP_CLI::success( 'WP Rocket rules removed from the htaccess file.'); + } + } + } + // Clean WP Rocket Cache and Minified files. + $this->clean_wp_rocket_cache( true ); + WP_CLI::success( 'WP Rocket is now disabled, WP_CACHE is set to false.' ); } else { WP_CLI::error( 'Error while setting WP_CACHE constant into wp-config.php!' ); } - } else { WP_CLI::error( 'WP Rocket is already disabled.' ); } - } /** @@ -311,7 +368,7 @@ public function regenerate( array $args = [], array $assoc_args = [] ) { WP_CLI::success( 'The config file has just been regenerated.' ); break; case 'htaccess': - $GLOBALS['is_apache'] = true; + self::set_apache(); if ( flush_rocket_htaccess() ) { WP_CLI::success( 'The .htaccess file has just been regenerated.' ); }else{ @@ -413,6 +470,20 @@ private function clean_wp_rocket_cache( $minify = false ) { update_rocket_option( 'minify_js_key', create_rocket_uniqid() ); } } + + /** + * Set global Apache variable to true + * + * @return void + */ + private static function set_apache() { + global $is_apache; + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $is_apache = true; + + // needed for get_home_path() and .htaccess location + $_SERVER['SCRIPT_FILENAME'] = ABSPATH; + } } WP_CLI::add_command( 'rocket', 'WPRocket_CLI' ); From 4fef271d1d3abf661bc14b82836f30ac91e948a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 26 Jan 2021 12:09:53 -0500 Subject: [PATCH 09/10] remove docblock --- wp-rocket-cli.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/wp-rocket-cli.php b/wp-rocket-cli.php index c2d35b3..3b32c39 100644 --- a/wp-rocket-cli.php +++ b/wp-rocket-cli.php @@ -1,15 +1,4 @@ Date: Tue, 26 Jan 2021 12:10:00 -0500 Subject: [PATCH 10/10] update readme content --- README.md | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ccebba1..b39c5f9 100755 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -## CLI interface for the WP Rocket +## WP CLI interface for WP Rocket This repository contains a [WP-CLI command](https://github.com/wp-cli/wp-cli) for the [WP Rocket](http://wp-rocket.me) plugin. After installing this plugin, you will have access to a `wp rocket` command. -Supported commands: +## Supported commands -* `wp rocket activate-cache` -- Set WP_CACHE to true, flush htaccess rules and clean WP Rocket cache. -* `wp rocket deactivate-cache` -- Set WP_CACHE to false, flush htaccess rules and clean WP Rocket cache. +* `wp rocket activate-cache --htaccess=` -- Set WP_CACHE to true and clean WP Rocket cache. To also update the htaccess on Apache, use the additional `htaccess` parameter. +* `wp rocket deactivate-cache --htaccess=` -- Set WP_CACHE to false and clean WP Rocket cache. To also update the htaccess on Apache, use the additional `htaccess` parameter. * `wp rocket clean --post_id= --permalink= --lang= --blog_id=` -- Purge cache files. * `wp rocket clean --confirm` -- Purge cache files without prompting for confirmation (usefull for automation tools/scripts) * `wp rocket preload` -- Preload cache files. @@ -18,16 +18,4 @@ Supported commands: If you're using WP-CLI v0.23.0 or later, you can install this package with: -``` -wp package install wp-media/wp-rocket-cli -``` - -## Changelog - -### 1.1 - -* Add `regenerate` command. - -### 1.0 - -* Initial release +`wp package install wp-media/wp-rocket-cli`