Skip to content

Commit 860bb2b

Browse files
committed
fix: defer comment counts
1 parent 000c832 commit 860bb2b

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

src/Comment_Command.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -451,23 +451,54 @@ public function delete( $args, $assoc_args ) {
451451
return;
452452
}
453453

454-
parent::_delete(
455-
$args,
456-
$assoc_args,
457-
function ( $comment_id, $assoc_args ) {
458-
$force = (bool) Utils\get_flag_value( $assoc_args, 'force' );
454+
$defer_term_counting = Utils\get_flag_value( $assoc_args, 'defer-term-counting', false );
455+
456+
if ( $all && ! $defer_term_counting ) {
457+
$assoc_args['defer-term-counting'] = true;
458+
}
459459

460-
$status = wp_get_comment_status( $comment_id );
461-
$result = wp_delete_comment( $comment_id, $force );
460+
$status = 0;
462461

463-
if ( ! $result ) {
464-
return [ 'error', "Failed deleting comment {$comment_id}." ];
465-
}
462+
if ( $defer_term_counting ) {
463+
wp_defer_term_counting( true );
464+
}
466465

467-
$verb = ( $force || 'trash' === $status ) ? 'Deleted' : 'Trashed';
468-
return [ 'success', "{$verb} comment {$comment_id}." ];
466+
$total = count( $args );
467+
$successfully_deleted = 0;
468+
469+
$force = (bool) Utils\get_flag_value( $assoc_args, 'force' );
470+
471+
foreach ( $args as $comment_id ) {
472+
473+
$comment_status = wp_get_comment_status( $comment_id );
474+
$result = wp_delete_comment( $comment_id, $force );
475+
476+
if ( ! $result ) {
477+
$response = [ 'error', "Failed deleting comment {$comment_id}." ];
478+
} else {
479+
$verb = ( $force || 'trash' === $comment_status ) ? 'Deleted' : 'Trashed';
480+
$response = [ 'success', "{$verb} comment {$comment_id}." ];
481+
++$successfully_deleted;
469482
}
470-
);
483+
484+
$status = $this->success_or_failure( $response );
485+
if ( $status ) {
486+
++$successfully_deleted;
487+
}
488+
}
489+
490+
if ( $defer_term_counting ) {
491+
wp_defer_term_counting( false );
492+
}
493+
494+
if ( $status ) {
495+
WP_CLI::success( "Deleted {$successfully_deleted} comments." );
496+
} else {
497+
$error_count = $total - $successfully_deleted;
498+
WP_CLI::error( "Failed deleting {$error_count} comments." );
499+
}
500+
501+
exit( 0 === $status ? 0 : 1 );
471502
}
472503

473504
private function call( $args, $status, $success, $failure ) {

0 commit comments

Comments
 (0)