From fbd26810fef2b1a2a60491b72c11574a1b01188b Mon Sep 17 00:00:00 2001 From: theMikeD Date: Sat, 13 Apr 2024 18:33:02 -0400 Subject: [PATCH 1/2] Add multisite support --- inc/class-revisions-cli.php | 71 ++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/inc/class-revisions-cli.php b/inc/class-revisions-cli.php index dc611d6..f9f62ba 100644 --- a/inc/class-revisions-cli.php +++ b/inc/class-revisions-cli.php @@ -23,6 +23,9 @@ class Revisions_CLI extends WP_CLI_Command { // phpcs:ignore WordPressVIPMinimum * [--yes] * : Answer yes to the confirmation message. * + * [--url=] + * : Operate on the supplied network site of a multisite installation. + * * ## EXAMPLES * * wp revisions dump @@ -30,10 +33,14 @@ class Revisions_CLI extends WP_CLI_Command { // phpcs:ignore WordPressVIPMinimum public function dump( $args = array(), $assoc_args = array() ) { global $wpdb; + + $this->handle_multisite( $assoc_args ); + $revs = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'revision'" ); if ( $revs < 1 ) { WP_CLI::success( 'No revisions.' ); + $this->reset_if_multisite( $assoc_args ); return; } @@ -41,7 +48,8 @@ public function dump( $args = array(), $assoc_args = array() ) { if ( isset( $assoc_args['hard'] ) ) { WP_CLI::run_command( array( 'revisions', 'clean', -1 ), array( 'hard' => '' ) ); - return; + $this->reset_if_multisite( $assoc_args ); + return; } $wpdb->query( "DELETE FROM $wpdb->posts WHERE post_type = 'revision'" ); @@ -72,6 +80,9 @@ public function dump( $args = array(), $assoc_args = array() ) { * [--yes] * : Answer yes to the confirmation message. * + * [--url=] + * : Operate on the supplied network site of a multisite installation. + * * [--format=] * : Format to use for the output. One of table, csv or json. * @@ -135,6 +146,8 @@ public function list_( $args = array(), $assoc_args = array() ) { $fields = array_map( 'esc_sql', $fields ); $fields = implode( ',', $fields ); + $this->handle_multisite( $assoc_args ); + global $wpdb; if ( ! empty( $assoc_args['post_id'] ) ) { @@ -180,7 +193,7 @@ public function list_( $args = array(), $assoc_args = array() ) { $formatter = new \WP_CLI\Formatter( $assoc_args, array( 'ID', 'post_parent', 'post_title' ), 'revisions' ); $formatter->display_items( $revs ); - + $this->reset_if_multisite( $assoc_args ); } /** @@ -221,10 +234,10 @@ public function status( $args = array(), $assoc_args = array() ) { * : Clean revisions for given post type(s). Default: any * * [--after-date=] - * : Clean revisions on posts published on or after this date. Default: none. + * : Clean revisions published on or after this date. Default: none. * * [--before-date=] - * : Clean revisions on posts published on or before this date. Default: none. + * : Clean revisions published on or before this date. Default: none. * * [--post_id=] * : Clean revisions for given post. @@ -235,6 +248,9 @@ public function status( $args = array(), $assoc_args = array() ) { * [--dry-run] * : Dry run, just a test, no actual cleaning done. * + * [--url=] + * : Operate on the supplied network site of a multisite installation. + * * ## EXAMPLES * * wp revisions clean @@ -248,6 +264,8 @@ public function clean( $args = array(), $assoc_args = array() ) { global $wpdb; + $this->handle_multisite( $assoc_args ); + if ( ! empty( $assoc_args['post_id'] ) ) { $posts = array( $assoc_args['post_id'] ); @@ -382,6 +400,8 @@ public function clean( $args = array(), $assoc_args = array() ) { } else { WP_CLI::success( sprintf( 'Finished removing %d old revisions.', $total_deleted ) ); } + $this->reset_if_multisite( $assoc_args ); + } @@ -399,6 +419,9 @@ public function clean( $args = array(), $assoc_args = array() ) { * [--post_id=] * : Generate revisions for given post. * + * [--url=] + * : Operate on the supplied network site of a multisite installation. + * * ## EXAMPLES * * wp revisions generate 10 @@ -409,6 +432,8 @@ public function generate( $args = array(), $assoc_args = array() ) { global $wpdb; + $this->handle_multisite( $assoc_args ); + if ( ! empty( $assoc_args['post_id'] ) ) { $posts = array( $assoc_args['post_id'] ); @@ -460,6 +485,7 @@ public function generate( $args = array(), $assoc_args = array() ) { $notify->finish(); WP_CLI::success( 'Finished generating revisions.' ); + $this->reset_if_multisite( $assoc_args ); } @@ -484,7 +510,7 @@ private function supports_revisions() { } - /* + /** * Clear all of the caches for memory management */ protected function stop_the_insanity() { @@ -519,4 +545,39 @@ protected function end_bulk_operation() { // This will also trigger a term count. wp_defer_term_counting( false ); } + + /** + * Handles the --url param to act on the targets blog, if supplied and valid + * + * @param $assoc_args + * + * @return void + */ + private function handle_multisite( $assoc_args ) { + if ( isset( $assoc_args['url'] ) && is_multisite() ) { + $url = untrailingslashit( $assoc_args['url'] ); + $current_site = get_site_by_path( $url ); + if ( ! $current_site ) { + WP_CLI::error( "Site with URL '{$url}' not found." ); + return; + } + switch_to_blog( $current_site->blog_id ); + } + } + + /** + * Reset after a --url option. + * + * @param $assoc_args + * + * @return void + */ + private function reset_if_multisite( $assoc_args ) { + if ( isset( $assoc_args['url'] ) && is_multisite() ) { + restore_current_blog(); + } + } + } + +WP_CLI::add_command( 'revisions', 'Revisions_CLI' ); From eff7fda939502a7cf52e18bca53805cf0e19e4e4 Mon Sep 17 00:00:00 2001 From: theMikeD Date: Sat, 13 Apr 2024 18:35:47 -0400 Subject: [PATCH 2/2] Update class-revisions-cli.php --- inc/class-revisions-cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-revisions-cli.php b/inc/class-revisions-cli.php index f9f62ba..e725d4c 100644 --- a/inc/class-revisions-cli.php +++ b/inc/class-revisions-cli.php @@ -49,7 +49,7 @@ public function dump( $args = array(), $assoc_args = array() ) { if ( isset( $assoc_args['hard'] ) ) { WP_CLI::run_command( array( 'revisions', 'clean', -1 ), array( 'hard' => '' ) ); $this->reset_if_multisite( $assoc_args ); - return; + return; } $wpdb->query( "DELETE FROM $wpdb->posts WHERE post_type = 'revision'" );