From ed42560f33a79de6bec2f3a62b6ab4bc3042e64d Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 7 Aug 2020 16:33:39 +0000 Subject: [PATCH] Comments: Update comment cache in the upgrade routine for changing the `comment_type` DB field value in comments table. This ensures that comment object cache is cleared after changing the comment type to `comment` instead of an empty string. Add a unit test for `_wp_batch_update_comment_type()`. Follow-up to [47597], [47626], [48225], [48227]. Props imath, westonruter. Reviewed by desrosj, SergeyBiryukov. Merges [48748] and [48751] to the 5.5 branch. Fixes #49236. git-svn-id: https://develop.svn.wordpress.org/branches/5.5@48752 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/upgrade.php | 8 +-- src/wp-includes/comment.php | 23 +++++++-- src/wp-includes/version.php | 2 +- .../comment/wpBatchUpdateCommentType.php | 49 +++++++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 tests/phpunit/tests/comment/wpBatchUpdateCommentType.php diff --git a/src/wp-admin/includes/upgrade.php b/src/wp-admin/includes/upgrade.php index 77487ba2cb..8cc106f45d 100644 --- a/src/wp-admin/includes/upgrade.php +++ b/src/wp-admin/includes/upgrade.php @@ -2174,9 +2174,6 @@ function upgrade_550() { global $wp_current_db_version; if ( $wp_current_db_version < 48121 ) { - update_option( 'finished_updating_comment_type', 0 ); - wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); - $comment_previously_approved = get_option( 'comment_whitelist', '' ); update_option( 'comment_previously_approved', $comment_previously_approved ); delete_option( 'comment_whitelist' ); @@ -2198,6 +2195,11 @@ function upgrade_550() { delete_option( 'blacklist_keys' ); delete_option( 'blocklist_keys' ); } + + if ( $wp_current_db_version < 48748 ) { + update_option( 'finished_updating_comment_type', 0 ); + wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' ); + } } /** 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/src/wp-includes/version.php b/src/wp-includes/version.php index c2c000f9c0..6f860e305a 100644 --- a/src/wp-includes/version.php +++ b/src/wp-includes/version.php @@ -20,7 +20,7 @@ $wp_version = '5.5-RC2-48739-src'; * * @global int $wp_db_version */ -$wp_db_version = 48575; +$wp_db_version = 48748; /** * Holds the TinyMCE version. 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; + } +}