rest_send_allow_header

函数
rest_send_allow_header ( $response, $server, $request )
参数
  • (WP_REST_Response) $response Current response being served.
    Required:
  • (WP_REST_Server) $server ResponseHandler instance (usually WP_REST_Server).
    Required:
  • (WP_REST_Request) $request The request that was used to make current response.
    Required:
返回值
  • (WP_REST_Response) Response to be served, with "Allow" header if route has allowed methods.
定义位置
相关方法
rest_send_cors_headersrest_get_allowed_schema_keywordsget_space_allowedget_site_allowed_themesregister_default_headers
引入
4.4.0
弃用
-

rest_send_allow_header: 这是一个WordPress的函数,发送一个HTTP允许头,以表明REST API端点允许的HTTP方法: 该函数接收一个允许的方法数组,并发送相应的允许头: 这个函数是用来向客户表明一个给定的REST API端点允许哪些HTTP方法。

发送"Allow"头,说明所有可以发送到当前路由的方法。

function rest_send_allow_header( $response, $server, $request ) {
	$matched_route = $response->get_matched_route();

	if ( ! $matched_route ) {
		return $response;
	}

	$routes = $server->get_routes();

	$allowed_methods = array();

	// Get the allowed methods across the routes.
	foreach ( $routes[ $matched_route ] as $_handler ) {
		foreach ( $_handler['methods'] as $handler_method => $value ) {

			if ( ! empty( $_handler['permission_callback'] ) ) {

				$permission = call_user_func( $_handler['permission_callback'], $request );

				$allowed_methods[ $handler_method ] = true === $permission;
			} else {
				$allowed_methods[ $handler_method ] = true;
			}
		}
	}

	// Strip out all the methods that are not allowed (false values).
	$allowed_methods = array_filter( $allowed_methods );

	if ( $allowed_methods ) {
		$response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) );
	}

	return $response;
}

常见问题

FAQs
查看更多 >