函数文档

wp_privacy_process_personal_data_erasure_page()

💡 云策文档标注

概述

wp_privacy_process_personal_data_erasure_page() 是一个 WordPress 函数,用于在个人数据擦除处理完成后将请求标记为完成。它拦截 Ajax 响应并监控请求状态,确保所有处理步骤正确执行。

关键要点

  • 函数拦截个人数据擦除页面的 Ajax 响应,验证响应结构并监控处理状态。
  • 当所有擦除器处理完成且响应格式正确时,调用 _wp_privacy_completed_request() 将请求标记为完成。
  • 触发 wp_privacy_personal_data_erased 动作钩子,允许开发者在擦除完成后执行自定义操作。
  • 参数包括 $response(擦除器响应数组)、$eraser_index(擦除器索引)、$email_address(用户邮箱)、$page(数据页码)和 $request_id(请求 ID)。
  • 返回过滤后的响应数组,确保与 Ajax 处理兼容。

代码示例

function wp_privacy_process_personal_data_erasure_page( $response, $eraser_index, $email_address, $page, $request_id ) {
    if ( ! is_array( $response ) ) {
        return $response;
    }
    if ( ! array_key_exists( 'done', $response ) ) {
        return $response;
    }
    if ( ! array_key_exists( 'items_removed', $response ) ) {
        return $response;
    }
    if ( ! array_key_exists( 'items_retained', $response ) ) {
        return $response;
    }
    if ( ! array_key_exists( 'messages', $response ) ) {
        return $response;
    }
    $request = wp_get_user_request( $request_id );
    if ( ! $request || 'remove_personal_data' !== $request->action_name ) {
        wp_send_json_error( __( 'Invalid request ID when processing personal data to erase.' ) );
    }
    $erasers        = apply_filters( 'wp_privacy_personal_data_erasers', array() );
    $is_last_eraser = count( $erasers ) === $eraser_index;
    $eraser_done    = $response['done'];
    if ( ! $is_last_eraser || ! $eraser_done ) {
        return $response;
    }
    _wp_privacy_completed_request( $request_id );
    do_action( 'wp_privacy_personal_data_erased', $request_id );
    return $response;
}

注意事项

  • 函数严格验证响应数组结构,包含 'done'、'items_removed'、'items_retained' 和 'messages' 键,否则直接返回原响应。
  • 使用 wp_get_user_request() 验证请求 ID 和操作类型,无效时发送 JSON 错误响应。
  • 依赖 wp_privacy_personal_data_erasers 过滤器获取擦除器列表,以确定是否为最后一个擦除器。
  • 仅在最后一个擦除器完成处理时才标记请求为完成,避免过早更新状态。

📄 原文内容

Mark erasure requests as completed after processing is finished.

Description

This intercepts the Ajax responses to personal data eraser page requests, and monitors the status of a request. Once all of the processing has finished, the request is marked as completed.

See also

Parameters

$responsearrayrequired
The response from the personal data eraser for the given page.
$eraser_indexintrequired
The index of the personal data eraser. Begins at 1.
$email_addressstringrequired
The email address of the user whose personal data this is.
$pageintrequired
The page of personal data for this eraser.
Begins at 1.
$request_idintrequired
The request ID for this personal data erasure.

Return

array The filtered response.

Source

function wp_privacy_process_personal_data_erasure_page( $response, $eraser_index, $email_address, $page, $request_id ) {
	/*
	 * If the eraser response is malformed, don't attempt to consume it; let it
	 * pass through, so that the default Ajax processing will generate a warning
	 * to the user.
	 */
	if ( ! is_array( $response ) ) {
		return $response;
	}

	if ( ! array_key_exists( 'done', $response ) ) {
		return $response;
	}

	if ( ! array_key_exists( 'items_removed', $response ) ) {
		return $response;
	}

	if ( ! array_key_exists( 'items_retained', $response ) ) {
		return $response;
	}

	if ( ! array_key_exists( 'messages', $response ) ) {
		return $response;
	}

	// Get the request.
	$request = wp_get_user_request( $request_id );

	if ( ! $request || 'remove_personal_data' !== $request->action_name ) {
		wp_send_json_error( __( 'Invalid request ID when processing personal data to erase.' ) );
	}

	/** This filter is documented in wp-admin/includes/ajax-actions.php */
	$erasers        = apply_filters( 'wp_privacy_personal_data_erasers', array() );
	$is_last_eraser = count( $erasers ) === $eraser_index;
	$eraser_done    = $response['done'];

	if ( ! $is_last_eraser || ! $eraser_done ) {
		return $response;
	}

	_wp_privacy_completed_request( $request_id );

	/**
	 * Fires immediately after a personal data erasure request has been marked completed.
	 *
	 * @since 4.9.6
	 *
	 * @param int $request_id The privacy request post ID associated with this request.
	 */
	do_action( 'wp_privacy_personal_data_erased', $request_id );

	return $response;
}

Hooks

do_action( ‘wp_privacy_personal_data_erased’, int $request_id )

Fires immediately after a personal data erasure request has been marked completed.

apply_filters( ‘wp_privacy_personal_data_erasers’, array $args )

Filters the array of personal data eraser callbacks.

Changelog

Version Description
4.9.6 Introduced.