get_data_for_route() public method

Retrieves publicly-visible data for the route.
Since: 4.4.0
public get_data_for_route ( string $route, array $callbacks, string $context = 'view' ) : array | null
$route string Route to get data for.
$callbacks array Callbacks to convert to data.
$context string Optional. Context for the data. Accepts 'view' or 'help'. Default 'view'.
return array | null Data for the route, or null if no publicly-visible data.
示例#1
0
/**
 * Handles OPTIONS requests for the server.
 *
 * This is handled outside of the server code, as it doesn't obey normal route
 * mapping.
 *
 * @since 4.4.0
 *
 * @param mixed           $response Current response, either response or `null` to indicate pass-through.
 * @param WP_REST_Server  $handler  ResponseHandler instance (usually WP_REST_Server).
 * @param WP_REST_Request $request  The request that was used to make current response.
 * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through.
 */
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());
        if (!$match) {
            continue;
        }
        $data = $handler->get_data_for_route($route, $endpoints, 'help');
        $response->set_matched_route($route);
        break;
    }
    $response->set_data($data);
    return $response;
}
示例#2
0
/**
 * Handles OPTIONS requests for the server.
 *
 * This is handled outside of the server code, as it doesn't obey normal route
 * mapping.
 *
 * @since 4.4.0
 *
 * @param mixed           $response Current response, either response or `null` to indicate pass-through.
 * @param WP_REST_Server  $handler  ResponseHandler instance (usually WP_REST_Server).
 * @param WP_REST_Request $request  The request that was used to make current response.
 * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through.
 */
function rest_handle_options_request($response, $handler, $request)
{
    if (!empty($response) || $request->get_method() !== 'OPTIONS') {
        return $response;
    }
    $response = new WP_REST_Response();
    $data = array();
    $accept = array();
    foreach ($handler->get_routes() as $route => $endpoints) {
        $match = preg_match('@^' . $route . '$@i', $request->get_route(), $args);
        if (!$match) {
            continue;
        }
        $data = $handler->get_data_for_route($route, $endpoints, 'help');
        $accept = array_merge($accept, $data['methods']);
        break;
    }
    $response->header('Accept', implode(', ', $accept));
    $response->set_data($data);
    return $response;
}