Beispiel #1
0
/**
 * Send the "Allow" header to state all methods that can be sen
 * to the current route
 *
 * @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.
 */
function vp_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;
}
 /**
  * Get the site index.
  *
  * This endpoint describes the capabilities of the site.
  *
  * @todo Should we generate text documentation too based on PHPDoc?
  *
  * @return array Index entity
  */
 public function get_index()
 {
     // General site data
     $available = array('name' => get_option('blogname'), 'description' => get_option('blogdescription'), 'url' => get_option('siteurl'), 'namespaces' => array_keys($this->namespaces), 'authentication' => array(), 'routes' => $this->get_route_data($this->get_routes()));
     $response = new WP_REST_Response($available);
     $response->add_link('help', 'http://v2.wp-api.org/');
     /**
      * Filter the API root index data.
      *
      * This contains the data describing the API. This includes information
      * about supported authentication schemes, supported namespaces, routes
      * available on the API, and a small amount of data about the site.
      *
      * @param WP_REST_Response $response Response data.
      */
     return apply_filters('vp_rest_index', $response);
 }