函数文档

_wp_batch_update_comment_type()

💡 云策文档标注

概述

_wp_batch_update_comment_type() 是一个 WordPress 内部函数,用于批量更新评论类型,将空评论类型设置为 'comment'。它通过数据库锁机制和分批处理来确保操作的安全性和效率。

关键要点

  • 函数通过全局 $wpdb 对象操作数据库,使用锁选项(update_comment_type.lock)防止并发执行。
  • 检查是否存在空评论类型(comment_type = ''),若无则标记完成并删除锁。
  • 使用 wp_update_comment_type_batch_size 过滤器可自定义批处理大小,默认 100。
  • 分批更新评论类型为 'comment',并清理评论缓存以保持数据一致性。
  • 通过 wp_schedule_single_event() 调度事件,支持异步处理,避免长时间阻塞。

代码示例

// 示例:函数内部核心代码片段
$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() ) );

$comment_batch_size = (int) apply_filters( 'wp_update_comment_type_batch_size', 100 );
$comment_ids = $wpdb->get_col(
    $wpdb->prepare(
        "SELECT comment_ID
        FROM {$wpdb->comments}
        WHERE comment_type = ''
        ORDER BY comment_ID DESC
        LIMIT %d",
        $comment_batch_size
    )
);

if ( $comment_ids ) {
    $comment_id_list = implode( ',', $comment_ids );
    $wpdb->query(
        "UPDATE {$wpdb->comments}
        SET comment_type = 'comment'
        WHERE comment_type = ''
        AND comment_ID IN ({$comment_id_list})"
    );
    clean_comment_cache( $comment_ids );
}

注意事项

  • 此函数是内部函数,通常由 WordPress 核心自动调用,开发者不应直接调用。
  • 锁机制基于 options 表,确保在分布式环境中不会重复执行。
  • 更新操作涉及数据库查询,批处理大小可通过过滤器调整以优化性能。
  • 函数在 WordPress 5.5.0 版本引入,用于处理历史数据中的空评论类型。

📄 原文内容

Updates the comment type for a batch of comments.

Source

function _wp_batch_update_comment_type() {
	global $wpdb;

	$lock_name = 'update_comment_type.lock';

	// Try to 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() ) );

	if ( ! $lock_result ) {
		$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' );
			return;
		}
	}

	// 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
		WHERE comment_type = ''
		LIMIT 1"
	);

	// No empty comment type, we're done here.
	if ( ! $empty_comment_type ) {
		update_option( 'finished_updating_comment_type', true );
		delete_option( $lock_name );
		return;
	}

	// 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.
	 *
	 * @since 5.5.0
	 *
	 * @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(
		$wpdb->prepare(
			"SELECT comment_ID
			FROM {$wpdb->comments}
			WHERE comment_type = ''
			ORDER BY comment_ID DESC
			LIMIT %d",
			$comment_batch_size
		)
	);

	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 );
}

Hooks

apply_filters( ‘wp_update_comment_type_batch_size’, int $comment_batch_size )

Filters the comment batch size for updating the comment type.

Changelog

Version Description
5.5.0 Introduced.