函数文档

_wp_personal_data_cleanup_requests()

💡 云策文档标注

概述

_wp_personal_data_cleanup_requests() 函数用于在显示个人数据请求列表前,清理失败和过期的请求。它通过 WP_Query 查询过期的待处理请求,并将其状态更新为失败。

关键要点

  • 函数核心功能:清理失败和过期的用户数据请求,确保列表显示前数据状态正确。
  • 使用 WP_Query 查询 post_type 为 'user_request'、状态为 'request-pending' 的请求,基于 post_modified_gmt 时间过滤过期项。
  • 通过 wp_update_post 将过期请求的 post_status 更新为 'request-failed',并清空 post_password。
  • 涉及 Hook:apply_filters('user_request_key_expiration', $expiration) 用于过滤确认密钥的过期时间。

代码示例

$expires = (int) apply_filters( 'user_request_key_expiration', DAY_IN_SECONDS );
$requests_query = new WP_Query(
    array(
        'post_type'      => 'user_request',
        'posts_per_page' => -1,
        'post_status'    => 'request-pending',
        'fields'         => 'ids',
        'date_query'     => array(
            array(
                'column' => 'post_modified_gmt',
                'before' => $expires . ' seconds ago',
            ),
        ),
    )
);
foreach ( $request_ids as $request_id ) {
    wp_update_post(
        array(
            'ID'            => $request_id,
            'post_status'   => 'request-failed',
            'post_password' => '',
        )
    );
}

注意事项

  • 函数在 WordPress 4.9.6 版本引入,属于核心功能的一部分。
  • 依赖 WP_Query 和 wp_update_post 函数,确保正确配置查询参数以避免误操作。
  • 过期时间可通过 user_request_key_expiration 过滤器自定义,默认使用 DAY_IN_SECONDS。

📄 原文内容

Cleans up failed and expired requests before displaying the list table.

Source

function _wp_personal_data_cleanup_requests() {
	/** This filter is documented in wp-includes/user.php */
	$expires = (int) apply_filters( 'user_request_key_expiration', DAY_IN_SECONDS );

	$requests_query = new WP_Query(
		array(
			'post_type'      => 'user_request',
			'posts_per_page' => -1,
			'post_status'    => 'request-pending',
			'fields'         => 'ids',
			'date_query'     => array(
				array(
					'column' => 'post_modified_gmt',
					'before' => $expires . ' seconds ago',
				),
			),
		)
	);

	$request_ids = $requests_query->posts;

	foreach ( $request_ids as $request_id ) {
		wp_update_post(
			array(
				'ID'            => $request_id,
				'post_status'   => 'request-failed',
				'post_password' => '',
			)
		);
	}
}

Hooks

apply_filters( ‘user_request_key_expiration’, int $expiration )

Filters the expiration time of confirm keys.

Changelog

Version Description
4.9.6 Introduced.