function wp_cache_set_comments_last_changed() {
wp_cache_set_last_changed( 'comment' );
* Updates the comment type for a batch of comments.
* @global wpdb $wpdb WordPress database abstraction object.
function _wp_batch_update_comment_type() {
$lock_name = 'update_comment_type.lock';
$lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) );
$lock_result = get_option( $lock_name );
// Bail if we were unable to create a lock, or if the existing lock is still valid.
if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) {
wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );
// Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
update_option( $lock_name, time() );
// Check if there's still an empty comment type.
$empty_comment_type = $wpdb->get_var(
"SELECT comment_ID FROM $wpdb->comments
// No empty comment type, we're done here.
if ( ! $empty_comment_type ) {
update_option( 'finished_updating_comment_type', true );
delete_option( $lock_name );
// Empty comment type found? We'll need to run this script again.
wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );
* Filters the comment batch size for updating the comment type.
* @param int $comment_batch_size The comment batch size. Default 100.
$comment_batch_size = (int) apply_filters( 'wp_update_comment_type_batch_size', 100 );
// Get the IDs of the comments to update.
$comment_ids = $wpdb->get_col(
$comment_id_list = implode( ',', $comment_ids );
// Update the `comment_type` field value to be `comment` for the next batch of comments.
"UPDATE {$wpdb->comments}
SET comment_type = 'comment'
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 );
* In order to avoid the _wp_batch_update_comment_type() job being accidentally removed,
* check that it's still scheduled while we haven't finished updating comment types.
function _wp_check_for_scheduled_update_comment_type() {
if ( ! get_option( 'finished_updating_comment_type' ) && ! wp_next_scheduled( 'wp_update_comment_type_batch' ) ) {
wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wp_update_comment_type_batch' );