本文整理汇总了PHP中WP_REST_Response::get_matched_route方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Response::get_matched_route方法的具体用法?PHP WP_REST_Response::get_matched_route怎么用?PHP WP_REST_Response::get_matched_route使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Response
的用法示例。
在下文中一共展示了WP_REST_Response::get_matched_route方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: rest_send_allow_header
/**
* Sends the "Allow" header to state all methods that can be sent to the current route.
*
* @since 4.4.0
*
* @param WP_REST_Response $response Current response being served.
* @param WP_REST_Server $server ResponseHandler instance (usually WP_REST_Server).
* @param WP_REST_Request $request The request that was used to make current response.
* @return WP_REST_Response Response to be served, with "Allow" header if route has allowed methods.
*/
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;
}