示例#1
0
文件: plugin.php 项目: enraiser/engap
/**
 * Handle OPTIONS requests for the server
 *
 * This is handled outside of the server code, as it doesn't obey normal route
 * mapping.
 *
 * @param mixed $response Current response, either response or `null` to indicate pass-through
 * @param ENGAP_Server $handler ResponseHandler instance (usually ENGAP_Server)
 * @return ENGAP_ResponseHandler Modified response, either response or `null` to indicate pass-through
 */
function json_handle_options_request($response, $handler)
{
    if (!empty($response) || $handler->method !== 'OPTIONS') {
        return $response;
    }
    $response = new ENGAP_Response();
    $accept = array();
    $handler_class = get_class($handler);
    $class_vars = get_class_vars($handler_class);
    $map = $class_vars['method_map'];
    foreach ($handler->get_routes() as $route => $endpoints) {
        $match = preg_match('@^' . $route . '$@i', $handler->path, $args);
        if (!$match) {
            continue;
        }
        foreach ($endpoints as $endpoint) {
            foreach ($map as $type => $bitmask) {
                if ($endpoint[1] & $bitmask) {
                    $accept[] = $type;
                }
            }
        }
        break;
    }
    $accept = array_unique($accept);
    $response->header('Accept', implode(', ', $accept));
    return $response;
}
示例#2
0
 /**
  * Create a new user.
  *
  * @param $data
  * @return mixed
  */
 public function create_user($data)
 {
     if (!current_user_can('create_users')) {
         return new WP_Error('json_cannot_create', __('Sorry, you are not allowed to create users.'), array('status' => 403));
     }
     if (!empty($data['ID'])) {
         return new WP_Error('json_user_exists', __('Cannot create existing user.'), array('status' => 400));
     }
     $user_id = $this->insert_user($data);
     if (is_wp_error($user_id)) {
         return $user_id;
     }
     $response = $this->get_user($user_id);
     if (!$response instanceof ENGAP_ResponseInterface) {
         $response = new ENGAP_Response($response);
     }
     $response->set_status(201);
     $response->header('Location', json_url('/users/' . $user_id));
     return $response;
 }