io = $io; } /** * Prints how many comments `purge` would delete with the same flags. * * ## OPTIONS * * [--approved] * : Also count approved and pending comments, not only spam and trash. * * [--woo] * : Also count WooCommerce order notes, webhook logs and product reviews. * * ## EXAMPLES * * $ wp adminkeep comments count * 1204 * * @param array $args Positional arguments. * @param array $assoc_args Flags. * @return void */ public function count( $args, $assoc_args ) { $this->io->line( (string) CommentPurger::count( ! empty( $assoc_args['approved'] ), ! empty( $assoc_args['woo'] ) ) ); } /** * Permanently deletes comments. Spam and trash only, unless told otherwise. * * Deleted comments cannot be recovered. Runs in batches, so a very * large site will not time out. Works whether or not the Disable * Comments feature is on. * * ## OPTIONS * * [--approved] * : Also delete approved and pending comments. * * [--woo] * : Also delete WooCommerce order notes, webhook logs and product reviews. * * [--yes] * : Do not ask for confirmation. * * ## EXAMPLES * * # Clear out spam and trash. * $ wp adminkeep comments purge --yes * * # Delete every comment on the site. * $ wp adminkeep comments purge --approved * * @param array $args Positional arguments. * @param array $assoc_args Flags. * @return void */ public function purge( $args, $assoc_args ) { $approved = ! empty( $assoc_args['approved'] ); $woo = ! empty( $assoc_args['woo'] ); $total = CommentPurger::count( $approved, $woo ); if ( 0 === $total ) { $this->io->success( 'Nothing to delete.' ); return; } if ( empty( $assoc_args['yes'] ) ) { $this->io->confirm( sprintf( 'Permanently delete %d comments? This cannot be undone.', $total ) ); } $this->io->progress_start( 'Deleting comments', $total ); $deleted = 0; do { $batch = CommentPurger::delete_batch( CommentPurger::BATCH_SIZE, $approved, $woo ); $deleted += $batch; // A batch that deletes nothing ends the run even if the count is not zero, the same // guard the REST route uses: a comment that refuses to die must not loop forever. if ( $batch > 0 ) { $this->io->progress_tick( $batch ); } } while ( $batch > 0 ); $this->io->progress_finish(); $remaining = CommentPurger::count( $approved, $woo ); if ( $remaining > 0 ) { $this->io->warning( sprintf( '%d comments could not be deleted.', $remaining ) ); } $this->io->success( sprintf( 'Deleted %d comments.', $deleted ) ); } }