函数文档

wp_comments_personal_data_eraser()

💡 云策文档标注

概述

wp_comments_personal_data_eraser() 是 WordPress 中用于从评论表中擦除与电子邮件地址关联的个人数据的函数。它通过匿名化评论作者信息来实现数据擦除,并支持分页处理以避免超时。

关键要点

  • 函数接受两个参数:必需的电子邮件地址字符串和可选的页码整数(默认为1)。
  • 返回一个数组,包含 items_removed(是否已移除项目)、items_retained(是否保留项目)、messages(消息数组)和 done(擦除是否完成)等键。
  • 使用 get_comments() 查询评论,每次最多处理500条,通过 wpdb::update() 更新数据库以匿名化数据。
  • 提供 wp_anonymize_comment 过滤器,允许自定义匿名化逻辑或消息。
  • 函数在 WordPress 4.9.6 版本中引入。

代码示例

function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
    global $wpdb;
    if ( empty( $email_address ) ) {
        return array(
            'items_removed'  => false,
            'items_retained' => false,
            'messages'       => array(),
            'done'           => true,
        );
    }
    $number = 500;
    $page = (int) $page;
    $items_removed = false;
    $items_retained = false;
    $comments = get_comments(
        array(
            'author_email'       => $email_address,
            'number'             => $number,
            'paged'              => $page,
            'orderby'            => 'comment_ID',
            'order'              => 'ASC',
            'include_unapproved' => true,
        )
    );
    $anon_author = __( 'Anonymous' );
    $messages = array();
    foreach ( (array) $comments as $comment ) {
        $anonymized_comment = array();
        $anonymized_comment['comment_agent'] = '';
        $anonymized_comment['comment_author'] = $anon_author;
        $anonymized_comment['comment_author_email'] = '';
        $anonymized_comment['comment_author_IP'] = wp_privacy_anonymize_data( 'ip', $comment->comment_author_IP );
        $anonymized_comment['comment_author_url'] = '';
        $anonymized_comment['user_id'] = 0;
        $comment_id = (int) $comment->comment_ID;
        $anon_message = apply_filters( 'wp_anonymize_comment', true, $comment, $anonymized_comment );
        if ( true !== $anon_message ) {
            if ( $anon_message && is_string( $anon_message ) ) {
                $messages[] = esc_html( $anon_message );
            } else {
                $messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id );
            }
            $items_retained = true;
            continue;
        }
        $args = array( 'comment_ID' => $comment_id );
        $updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args );
        if ( $updated ) {
            $items_removed = true;
            clean_comment_cache( $comment_id );
        } else {
            $items_retained = true;
        }
    }
    $done = count( $comments ) < $number;
    return array(
        'items_removed'  => $items_removed,
        'items_retained' => $items_retained,
        'messages'       => $messages,
        'done'           => $done,
    );
}

注意事项

  • 函数处理时,如果电子邮件地址为空,会直接返回完成状态,不执行任何擦除操作。
  • 匿名化包括清空 comment_agent、comment_author_email、comment_author_url,设置 comment_author 为“Anonymous”,并使用 wp_privacy_anonymize_data() 处理 IP 地址。
  • 通过 wp_anonymize_comment 过滤器,开发者可以干预匿名化过程,例如返回自定义消息或阻止匿名化。
  • 更新成功后,会调用 clean_comment_cache() 清理评论缓存,确保数据一致性。

📄 原文内容

Erases personal data associated with an email address from the comments table.

Parameters

$email_addressstringrequired
The comment author email address.
$pageintoptional
Comment page number.

Default:1

Return

array Data removal results.

  • items_removed bool
    Whether items were actually removed.
  • items_retained bool
    Whether items were retained.
  • messages string[]
    An array of messages to add to the personal data export file.
  • done bool
    Whether the eraser is finished.

Source

function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
	global $wpdb;

	if ( empty( $email_address ) ) {
		return array(
			'items_removed'  => false,
			'items_retained' => false,
			'messages'       => array(),
			'done'           => true,
		);
	}

	// Limit us to 500 comments at a time to avoid timing out.
	$number         = 500;
	$page           = (int) $page;
	$items_removed  = false;
	$items_retained = false;

	$comments = get_comments(
		array(
			'author_email'       => $email_address,
			'number'             => $number,
			'paged'              => $page,
			'orderby'            => 'comment_ID',
			'order'              => 'ASC',
			'include_unapproved' => true,
		)
	);

	/* translators: Name of a comment's author after being anonymized. */
	$anon_author = __( 'Anonymous' );
	$messages    = array();

	foreach ( (array) $comments as $comment ) {
		$anonymized_comment                         = array();
		$anonymized_comment['comment_agent']        = '';
		$anonymized_comment['comment_author']       = $anon_author;
		$anonymized_comment['comment_author_email'] = '';
		$anonymized_comment['comment_author_IP']    = wp_privacy_anonymize_data( 'ip', $comment->comment_author_IP );
		$anonymized_comment['comment_author_url']   = '';
		$anonymized_comment['user_id']              = 0;

		$comment_id = (int) $comment->comment_ID;

		/**
		 * Filters whether to anonymize the comment.
		 *
		 * @since 4.9.6
		 *
		 * @param bool|string $anon_message       Whether to apply the comment anonymization (bool) or a custom
		 *                                        message (string). Default true.
		 * @param WP_Comment  $comment            WP_Comment object.
		 * @param array       $anonymized_comment Anonymized comment data.
		 */
		$anon_message = apply_filters( 'wp_anonymize_comment', true, $comment, $anonymized_comment );

		if ( true !== $anon_message ) {
			if ( $anon_message && is_string( $anon_message ) ) {
				$messages[] = esc_html( $anon_message );
			} else {
				/* translators: %d: Comment ID. */
				$messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id );
			}

			$items_retained = true;

			continue;
		}

		$args = array(
			'comment_ID' => $comment_id,
		);

		$updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args );

		if ( $updated ) {
			$items_removed = true;
			clean_comment_cache( $comment_id );
		} else {
			$items_retained = true;
		}
	}

	$done = count( $comments ) < $number;

	return array(
		'items_removed'  => $items_removed,
		'items_retained' => $items_retained,
		'messages'       => $messages,
		'done'           => $done,
	);
}

Hooks

apply_filters( ‘wp_anonymize_comment’, bool|string $anon_message, WP_Comment $comment, array $anonymized_comment )

Filters whether to anonymize the comment.

Changelog

Version Description
4.9.6 Introduced.