From 5e8ea533730160b883898c734ba67039b499ca90 Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Wed, 24 Apr 2013 04:47:15 +0000 Subject: [PATCH] Move empty blog subcommand to blog command; create separate class for multi-site commands so that we can make blog available to single site installations --- php/commands/blog.php | 156 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 123 insertions(+), 33 deletions(-) diff --git a/php/commands/blog.php b/php/commands/blog.php index b7af9610..0eef48b7 100644 --- a/php/commands/blog.php +++ b/php/commands/blog.php @@ -1,13 +1,131 @@ ] [--keep_terms] [--keep_comments] + */ + public function truncate( $args, $assoc_args ) { + + $assoc_args = wp_parse_args( $assoc_args, array( + 'post_type' => 'all', + 'keep_terms' => 0, + 'keep_comments' => 0, + ) ); + + WP_CLI::confirm( 'Are you sure you want to empty the blog at ' . site_url() . '?', $assoc_args ); + + global $wpdb; + + // Empty posts and post cache + $posts_query = "SELECT ID FROM $wpdb->posts"; + if ( 'all' != $assoc_args['post_type'] ) + $posts_query .= " WHERE post_type='$assoc_args[post_type]'"; + + $taxonomies = get_taxonomies(); + $posts = new WP_CLI\Iterators\Query( $posts_query, 10000 ); + + while ( $posts->valid() ) { + $post_id = $posts->current()->ID; + + wp_cache_delete( $post_id, 'posts' ); + wp_cache_delete( $post_id, 'post_meta' ); + foreach ( $taxonomies as $taxonomy ) + wp_cache_delete( $post_id, "{$taxonomy}_relationships" ); + wp_cache_delete( $wpdb->blogid . '-' . $post_id, 'global-posts' ); + + $posts->next(); + } + $wpdb->query( "TRUNCATE $wpdb->posts" ); + + if ( 'all' == $assoc_args['post_type'] ) + $wpdb->query( "TRUNCATE $wpdb->postmeta" ); + + + // Empty comments and comment cache + if ( 0 == $assoc_args['keep_comments'] ) { + $comment_ids = $wpdb->get_col( "SELECT comment_ID FROM $wpdb->comments" ); + foreach ( $comment_ids as $comment_id ) { + wp_cache_delete( $comment_id, 'comment' ); + wp_cache_delete( $comment_id, 'comment_meta' ); + } + $wpdb->query( "TRUNCATE $wpdb->comments" ); + $wpdb->query( "TRUNCATE $wpdb->commentmeta" ); + } + + // Empty taxonomies and terms + if ( 0 == $assoc_args['keep_terms'] ) { + $terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy" ); + $ids = array(); + foreach ( (array) $terms as $term ) { + $taxonomies[] = $term->taxonomy; + $ids[] = $term->term_id; + wp_cache_delete( $term->term_id, $term->taxonomy ); + } + + $taxonomies = array_unique( $taxonomies ); + foreach ( $taxonomies as $taxonomy ) { + if ( isset( $cleaned[$taxonomy] ) ) + continue; + $cleaned[$taxonomy] = true; + + wp_cache_delete( 'all_ids', $taxonomy ); + wp_cache_delete( 'get', $taxonomy ); + delete_option( "{$taxonomy}_children" ); + } + $wpdb->query( "TRUNCATE $wpdb->terms" ); + $wpdb->query( "TRUNCATE $wpdb->term_taxonomy" ); + $wpdb->query( "TRUNCATE $wpdb->term_relationships" ); + } + + WP_CLI::success( 'The blog at ' . site_url() . ' was emptied.' ); + } +} + +/** + * Manage blogs in a multisite install + */ +class MS_Blog_Command extends Blog_Command { + + public function __construct() { } + + /** + * Delete a blog in a multisite install. + * + * @synopsis [] [--slug=] [--yes] [--keep-tables] + */ + function delete( $args, $assoc_args ) { + if ( isset( $assoc_args['slug'] ) ) { + $blog = get_blog_details( trim( $assoc_args['slug'], '/' ) ); + } else { + if ( empty( $args ) ) { + WP_CLI::error( "Need to specify a blog id." ); + } + + $blog_id = $args[0]; + + $blog = get_blog_details( $blog_id ); + } + + if ( !$blog ) { + WP_CLI::error( "Blog not found." ); + } + + WP_CLI::confirm( "Are you sure you want to delete the $blog->siteurl blog?", $assoc_args ); + + wpmu_delete_blog( $blog->blog_id, !isset( $assoc_args['keep-tables'] ) ); + + WP_CLI::success( "The blog at $blog->siteurl was deleted." ); + } + + /** * Get site (network) data for a given id * * @param int $site_id @@ -123,39 +241,11 @@ class Blog_Command extends WP_CLI_Command { else WP_CLI::success( "Blog $id created: $url" ); } - - /** - * Delete a blog in a multisite install. - * - * @synopsis [] [--slug=] [--yes] [--keep-tables] - */ - function delete( $args, $assoc_args ) { - if ( isset( $assoc_args['slug'] ) ) { - $blog = get_blog_details( trim( $assoc_args['slug'], '/' ) ); - } else { - if ( empty( $args ) ) { - WP_CLI::error( "Need to specify a blog id." ); - } - - $blog_id = $args[0]; - - $blog = get_blog_details( $blog_id ); - } - - if ( !$blog ) { - WP_CLI::error( "Blog not found." ); - } - - WP_CLI::confirm( "Are you sure you want to delete the $blog->siteurl blog?", $assoc_args ); - - wpmu_delete_blog( $blog->blog_id, !isset( $assoc_args['keep-tables'] ) ); - - WP_CLI::success( "The blog at $blog->siteurl was deleted." ); - } } -if ( function_exists( 'is_multisite' ) && !is_multisite() ) - return; +$command_class = 'Blog_Command'; +if ( function_exists( 'is_multisite' ) && is_multisite() ) + $command_class = 'MS_Blog_Command'; -WP_CLI::add_command( 'blog', 'Blog_Command' ); +WP_CLI::add_command( 'blog', $command_class ); -- 2.11.0