wp_validate_user_request_key

函数
wp_validate_user_request_key ( $request_id, $key )
参数
  • (string) $request_id ID of the request being confirmed.
    Required:
  • (string) $key Provided key to validate.
    Required:
返回值
  • (true|WP_Error) True on success, WP_Error on failure.
定义位置
相关方法
wp_generate_user_request_keywp_create_user_requestwp_get_user_requestwp_send_user_requestwp_get_user_request_data
引入
4.9.6
弃用
-

wp_validate_user_request_key是一个WordPress的函数,用来验证一个用户的请求密钥: 这个函数与GDPR和CCPA一起使用,以确保用户请求是有效的,并且请求密钥没有被欺诈性地使用: 该函数将请求密钥作为其输入,如果密钥有效则返回真,如果无效则返回假。

通过比较密钥和请求的密钥来验证一个用户请求。

function wp_validate_user_request_key( $request_id, $key ) {
	global $wp_hasher;

	$request_id       = absint( $request_id );
	$request          = wp_get_user_request( $request_id );
	$saved_key        = $request->confirm_key;
	$key_request_time = $request->modified_timestamp;

	if ( ! $request || ! $saved_key || ! $key_request_time ) {
		return new WP_Error( 'invalid_request', __( 'Invalid personal data request.' ) );
	}

	if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) {
		return new WP_Error( 'expired_request', __( 'This personal data request has expired.' ) );
	}

	if ( empty( $key ) ) {
		return new WP_Error( 'missing_key', __( 'The confirmation key is missing from this personal data request.' ) );
	}

	if ( empty( $wp_hasher ) ) {
		require_once ABSPATH . WPINC . '/class-phpass.php';
		$wp_hasher = new PasswordHash( 8, true );
	}

	/**
	 * Filters the expiration time of confirm keys.
	 *
	 * @since 4.9.6
	 *
	 * @param int $expiration The expiration time in seconds.
	 */
	$expiration_duration = (int) apply_filters( 'user_request_key_expiration', DAY_IN_SECONDS );
	$expiration_time     = $key_request_time + $expiration_duration;

	if ( ! $wp_hasher->CheckPassword( $key, $saved_key ) ) {
		return new WP_Error( 'invalid_key', __( 'The confirmation key is invalid for this personal data request.' ) );
	}

	if ( ! $expiration_time || time() > $expiration_time ) {
		return new WP_Error( 'expired_key', __( 'The confirmation key has expired for this personal data request.' ) );
	}

	return true;
}

常见问题

FAQs
查看更多 >