rest_handle_options_request

函数
rest_handle_options_request ( $response, $handler, $request )
参数
  • (mixed) $response Current response, either response or `null` to indicate pass-through.
    Required:
  • (WP_REST_Server) $handler ResponseHandler instance (usually WP_REST_Server).
    Required:
  • (WP_REST_Request) $request The request that was used to make current response.
    Required:
返回值
  • (WP_REST_Response) Modified response, either response or `null` to indicate pass-through.
定义位置
相关方法
rest_do_requestrest_ensure_requestrest_sanitize_request_argrest_validate_request_arg_delete_option_fresh_site
引入
4.4.0
弃用
-

rest_handle_options_request: 这是一个WordPress的函数,用于处理一个REST API端点的HTTP OPTIONS请求: 该函数在HTTP允许头中为端点发送允许的HTTP方法,以及该端点支持的任何额外的头。

处理服务器的OPTIONS请求。

这是在服务器代码之外处理的,因为它不服从正常的路由映射。

function rest_handle_options_request( $response, $handler, $request ) {
	if ( ! empty( $response ) || $request->get_method() !== 'OPTIONS' ) {
		return $response;
	}

	$response = new WP_REST_Response();
	$data     = array();

	foreach ( $handler->get_routes() as $route => $endpoints ) {
		$match = preg_match( '@^' . $route . '$@i', $request->get_route(), $matches );

		if ( ! $match ) {
			continue;
		}

		$args = array();
		foreach ( $matches as $param => $value ) {
			if ( ! is_int( $param ) ) {
				$args[ $param ] = $value;
			}
		}

		foreach ( $endpoints as $endpoint ) {
			// Remove the redundant preg_match() argument.
			unset( $args[0] );

			$request->set_url_params( $args );
			$request->set_attributes( $endpoint );
		}

		$data = $handler->get_data_for_route( $route, $endpoints, 'help' );
		$response->set_matched_route( $route );
		break;
	}

	$response->set_data( $data );
	return $response;
}

常见问题

FAQs
查看更多 >