Skip to content
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

Add multisite support #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions inc/class-revisions-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,32 @@ class Revisions_CLI extends WP_CLI_Command { // phpcs:ignore WordPressVIPMinimum
* [--yes]
* : Answer yes to the confirmation message.
*
* [--url=<site>]
* : Operate on the supplied network site of a multisite installation.
*
* ## EXAMPLES
*
* wp revisions dump
*/
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;
}

WP_CLI::confirm( sprintf( 'Remove all %d revisions?', $revs ), $assoc_args );

if ( isset( $assoc_args['hard'] ) ) {
WP_CLI::run_command( array( 'revisions', 'clean', -1 ), array( 'hard' => '' ) );
$this->reset_if_multisite( $assoc_args );
return;
}

Expand Down Expand Up @@ -72,6 +80,9 @@ public function dump( $args = array(), $assoc_args = array() ) {
* [--yes]
* : Answer yes to the confirmation message.
*
* [--url=<site>]
* : Operate on the supplied network site of a multisite installation.
*
* [--format=<format>]
* : Format to use for the output. One of table, csv or json.
*
Expand Down Expand Up @@ -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'] ) ) {

Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -221,10 +234,10 @@ public function status( $args = array(), $assoc_args = array() ) {
* : Clean revisions for given post type(s). Default: any
*
* [--after-date=<yyyy-mm-dd>]
* : Clean revisions on posts published on or after this date. Default: none.
* : Clean revisions published on or after this date. Default: none.
*
* [--before-date=<yyyy-mm-dd>]
* : Clean revisions on posts published on or before this date. Default: none.
* : Clean revisions published on or before this date. Default: none.
*
* [--post_id=<post-id>]
* : Clean revisions for given post.
Expand All @@ -235,6 +248,9 @@ public function status( $args = array(), $assoc_args = array() ) {
* [--dry-run]
* : Dry run, just a test, no actual cleaning done.
*
* [--url=<site>]
* : Operate on the supplied network site of a multisite installation.
*
* ## EXAMPLES
*
* wp revisions clean
Expand All @@ -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'] );
Expand Down Expand Up @@ -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 );


}

Expand All @@ -399,6 +419,9 @@ public function clean( $args = array(), $assoc_args = array() ) {
* [--post_id=<post-id>]
* : Generate revisions for given post.
*
* [--url=<site>]
* : Operate on the supplied network site of a multisite installation.
*
* ## EXAMPLES
*
* wp revisions generate 10
Expand All @@ -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'] );
Expand Down Expand Up @@ -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 );

}

Expand All @@ -484,7 +510,7 @@ private function supports_revisions() {

}

/*
/**
* Clear all of the caches for memory management
*/
protected function stop_the_insanity() {
Expand Down Expand Up @@ -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' );