函数文档

rest_cookie_collect_status()

💡 云策文档标注

概述

rest_cookie_collect_status() 函数用于收集 cookie 认证状态,主要从 wp_validate_auth_cookie 获取错误信息,供 rest_cookie_check_errors 使用。

关键要点

  • 函数收集 cookie 认证状态,基于 current_action() 返回的钩子名称进行判断。
  • 如果钩子名称不是 'auth_cookie_valid',则将错误类型存储在全局变量 $wp_rest_auth_cookie 中。
  • 如果钩子名称是 'auth_cookie_valid',则将 $wp_rest_auth_cookie 设置为 true。
  • 此函数自 WordPress 4.4.0 版本引入。

代码示例

function rest_cookie_collect_status() {
	global $wp_rest_auth_cookie;

	$status_type = current_action();

	if ( 'auth_cookie_valid' !== $status_type ) {
		$wp_rest_auth_cookie = substr( $status_type, 12 );
		return;
	}

	$wp_rest_auth_cookie = true;
}

注意事项

  • 函数依赖于 current_action() 来获取当前动作钩子名称,确保在正确的钩子上下文中调用。
  • 全局变量 $wp_rest_auth_cookie 用于存储认证状态或错误信息,需注意其作用域和生命周期。

📄 原文内容

Collects cookie authentication status.

Description

Collects errors from wp_validate_auth_cookie for use by rest_cookie_check_errors.

See also

Source

function rest_cookie_collect_status() {
	global $wp_rest_auth_cookie;

	$status_type = current_action();

	if ( 'auth_cookie_valid' !== $status_type ) {
		$wp_rest_auth_cookie = substr( $status_type, 12 );
		return;
	}

	$wp_rest_auth_cookie = true;
}

Changelog

Version Description
4.4.0 Introduced.