diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index efcf384815..47e74bc3f2 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3839,11 +3839,11 @@ function _wp_batch_update_comment_type() { */ $comment_batch_size = (int) apply_filters( 'wp_update_comment_type_batch_size', 100 ); - // Update the `comment_type` field value to be `comment` for the next batch of comments. - $wpdb->query( + // Get the IDs of the comments to update. + $comment_ids = $wpdb->get_col( $wpdb->prepare( - "UPDATE {$wpdb->comments} - SET comment_type = 'comment' + "SELECT comment_ID + FROM {$wpdb->comments} WHERE comment_type = '' ORDER BY comment_ID DESC LIMIT %d", @@ -3851,6 +3851,21 @@ function _wp_batch_update_comment_type() { ) ); + if ( $comment_ids ) { + $comment_id_list = implode( ',', $comment_ids ); + + // Update the `comment_type` field value to be `comment` for the next batch of comments. + $wpdb->query( + "UPDATE {$wpdb->comments} + SET comment_type = 'comment' + WHERE comment_type = '' + AND comment_ID IN ({$comment_id_list})" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + ); + + // Make sure to clean the comment cache. + clean_comment_cache( $comment_ids ); + } + delete_option( $lock_name ); } diff --git a/tests/phpunit/tests/comment/wpBatchUpdateCommentType.php b/tests/phpunit/tests/comment/wpBatchUpdateCommentType.php new file mode 100644 index 0000000000..ec7dec9ff3 --- /dev/null +++ b/tests/phpunit/tests/comment/wpBatchUpdateCommentType.php @@ -0,0 +1,49 @@ +comment->create_many( 3 ); + $comment_id_list = implode( ',', $comment_ids ); + + $wpdb->query( + "UPDATE {$wpdb->comments} + SET comment_type = '' + WHERE comment_type = 'comment' + AND comment_ID in ({$comment_id_list})" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + ); + + clean_comment_cache( $comment_ids ); + + foreach ( $comment_ids as $comment_id ) { + $comment = get_comment( $comment_id ); + $this->assertEmpty( $comment->comment_type ); + } + + add_filter( 'wp_update_comment_type_batch_size', array( $this, 'filter_comment_type_batch_size' ) ); + add_filter( 'schedule_event', '__return_null' ); + + _wp_batch_update_comment_type(); + + remove_filter( 'wp_update_comment_type_batch_size', array( $this, 'filter_comment_type_batch_size' ) ); + remove_filter( 'schedule_event', '__return_null' ); + + foreach ( $comment_ids as $comment_id ) { + $updated_comment = get_comment( $comment_id ); + $this->assertEquals( 'comment', $updated_comment->comment_type ); + } + } + + public function filter_comment_type_batch_size() { + return 3; + } +}