/** * * Gets all the related entities (or if its a belongs-to relation just the one) * to the item with the given id * * @param \WP_REST_Request $request * @return \WP_REST_Response|\WP_Error */ public static function handle_request_get_related(\WP_REST_Request $request) { $controller = new Read(); try { $matches = $controller->parse_route($request->get_route(), '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . '(.*)/(.*)/(.*)~', array('version', 'model', 'id', 'related_model')); $controller->set_requested_version($matches['version']); $main_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['model']); if (!$controller->get_model_version_info()->is_model_name_in_this_version($main_model_name_singular)) { return $controller->send_response(new \WP_Error('endpoint_parsing_error', sprintf(__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), $main_model_name_singular))); } $main_model = $controller->get_model_version_info()->load_model($main_model_name_singular); //assume the related model name is plural and try to find the model's name $related_model_name_singular = \EEH_Inflector::singularize_and_upper($matches['related_model']); if (!$controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { //so the word didn't singularize well. Maybe that's just because it's a singular word? $related_model_name_singular = \EEH_Inflector::humanize($matches['related_model']); } if (!$controller->get_model_version_info()->is_model_name_in_this_version($related_model_name_singular)) { return $controller->send_response(new \WP_Error('endpoint_parsing_error', sprintf(__('There is no model for endpoint %s. Please contact event espresso support', 'event_espresso'), $related_model_name_singular))); } return $controller->send_response($controller->get_entities_from_relation($request->get_param('id'), $main_model->related_settings_for($related_model_name_singular), $request)); } catch (\Exception $e) { return $controller->send_response($e); } }
/** * Toggles whether the user is checked in or not. * * @param \WP_REST_Request $request * @return \WP_Error|\WP_REST_Response */ protected function _create_checkin_checkout_object(\WP_REST_Request $request) { $reg_id = $request->get_param('REG_ID'); $dtt_id = $request->get_param('DTT_ID'); $force = $request->get_param('force'); if ($force == 'true') { $force = true; } else { $force = false; } $reg = \EEM_Registration::instance()->get_one_by_ID($reg_id); if (!$reg instanceof \EE_Registration) { return $this->send_response(new \WP_Error('rest_registration_toggle_checkin_invalid_id', sprintf(__('You cannot checkin registration with ID %1$s because it doesn\'t exist.', 'event_espresso'), $reg_id), array('status' => 422))); } if (!\EE_Capabilities::instance()->current_user_can('ee_edit_checkin', 'rest_api_checkin_endpoint', $reg_id)) { return $this->send_response(new \WP_Error('rest_user_cannot_toggle_checkin', sprintf(__('You are not allowed to checkin registration with ID %1$s.', 'event_espresso'), $reg_id), array('status' => 403))); } $success = $reg->toggle_checkin_status($dtt_id, !$force); if ($success === false) { //rely on EE_Error::add_error messages to have been added to give more data about hwy it failed return $this->send_response(new \WP_Error('rest_toggle_checkin_failed', __('Registration checkin failed. Please see additional error data.', 'event_espresso'))); } $checkin = \EEM_Checkin::instance()->get_one(array(array('REG_ID' => $reg_id, 'DTT_ID' => $dtt_id), 'order_by' => array('CHK_timestamp' => 'DESC'))); if (!$checkin instanceof \EE_Checkin) { return $this->send_response(new \WP_Error('rest_toggle_checkin_error', sprintf(__('Supposedly we created a new checkin object for registration %1$s at datetime %2$s, but we can\'t find it.', 'event_espresso'), $reg_id, $dtt_id))); } $requested_version = $this->get_requested_version($request->get_route()); $get_request = new \WP_REST_Request('GET', \EED_Core_Rest_Api::ee_api_namespace . $requested_version . '/checkins/' . $checkin->ID()); $get_request->set_url_params(array('id' => $checkin->ID())); return Read::handle_request_get_one($get_request); }
public static function handle_request_models_meta(\WP_REST_Request $request) { $controller = new Meta(); $matches = $controller->parse_route($request->get_route(), '~' . \EED_Core_Rest_Api::ee_api_namespace_for_regex . 'resources~', array('version')); if ($matches instanceof \WP_REST_Response) { return $matches; } $controller->set_requested_version($matches['version']); return $controller->send_response($controller->_get_models_metadata_entity()); }
/** * Hooks into the REST API output to print XML instead of JSON. * * This is only done for the oEmbed API endpoint, * which supports both formats. * * @access private * @since 4.4.0 * * @param bool $served Whether the request has already been served. * @param WP_HTTP_ResponseInterface $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request($served, $result, $request, $server) { $params = $request->get_params(); if ('/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method()) { return $served; } if (!isset($params['format']) || 'xml' !== $params['format']) { return $served; } // Embed links inside the request. $data = $server->response_to_data($result, false); if (404 === $result->get_status()) { $data = $data[0]; } if (!class_exists('SimpleXMLElement')) { status_header(501); die(get_status_header_desc(501)); } $result = _oembed_create_xml($data); // Bail if there's no XML. if (!$result) { status_header(501); return get_status_header_desc(501); } if (!headers_sent()) { $server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset')); } echo $result; return true; }
/** * Hooks into the REST API output to print XML instead of JSON. * * @access private * * @param bool $served Whether the request has already been served. * @param WP_HTTP_ResponseInterface $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request($served, $result, $request, $server) { $params = $request->get_params(); if ('/wp/v2/oembed' !== $request->get_route() || 'GET' !== $request->get_method()) { return $served; } if (!isset($params['format']) || 'xml' !== $params['format']) { return $served; } // Embed links inside the request. $data = $server->response_to_data($result, false); if (404 === $result->get_status()) { $data = $data[0]; } /** * Filter the XML response. * * @param array $data The original oEmbed response data. */ $result = apply_filters('oembed_xml_response', $data); // Bail if there's no XML. if (!is_string($result)) { status_header(501); die('Not implemented'); } if (!headers_sent()) { $server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset')); } echo $result; return true; }
/** * 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; }
/** * 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; }
/** * Hooks into the REST API output to print XML instead of JSON. * * @param bool $served Whether the request has already been served. * @param WP_HTTP_ResponseInterface $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * * @return bool */ public function rest_pre_serve_request($served, $result, $request, $server) { $params = $request->get_params(); if ('/wp/v2/oembed' !== $request->get_route() || 'xml' !== $params['format']) { return $served; } if ('HEAD' === $request->get_method()) { return $served; } if (!headers_sent()) { $server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset')); } // Embed links inside the request. $result = $server->response_to_data($result, false); $oembed = new SimpleXMLElement('<oembed></oembed>'); foreach ($result as $key => $value) { if (is_array($value)) { $element = $oembed->addChild($key); foreach ($value as $k => $v) { $element->addChild($k, $v); } continue; } $oembed->addChild($key, $value); } echo $oembed->asXML(); return true; }
/** * Process API requests. * * @since 1.4 * @param arr $request API Request data. * @return arr */ public function process_request(WP_REST_Request $request) { $start = microtime(true); // Start time for logging. $route = $request->get_route(); $this->request = $request; // The request parameters. $this->validate_user(); if ($this->is_valid_user) { $status_code = 200; $this->data = do_action($route, $this); } return $this->data; }