From 5bf66662a9bdd62c5bccab15e607cd95cfb8fcab Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 27 Jul 2020 10:05:23 +0200 Subject: Removed wordpress and phpmyadmin, my server doesn't handle it well and it brings shame on my familly --- .../wp-includes/rest-api/class-wp-rest-request.php | 1017 -------- .../rest-api/class-wp-rest-response.php | 290 --- .../wp-includes/rest-api/class-wp-rest-server.php | 1331 ---------- .../class-wp-rest-attachments-controller.php | 930 ------- .../class-wp-rest-autosaves-controller.php | 433 ---- .../class-wp-rest-block-renderer-controller.php | 183 -- .../endpoints/class-wp-rest-blocks-controller.php | 93 - .../class-wp-rest-comments-controller.php | 1710 ------------- .../endpoints/class-wp-rest-controller.php | 672 ----- .../class-wp-rest-post-statuses-controller.php | 352 --- .../class-wp-rest-post-types-controller.php | 337 --- .../endpoints/class-wp-rest-posts-controller.php | 2616 -------------------- .../class-wp-rest-revisions-controller.php | 789 ------ .../endpoints/class-wp-rest-search-controller.php | 364 --- .../class-wp-rest-settings-controller.php | 348 --- .../class-wp-rest-taxonomies-controller.php | 402 --- .../endpoints/class-wp-rest-terms-controller.php | 1059 -------- .../endpoints/class-wp-rest-themes-controller.php | 242 -- .../endpoints/class-wp-rest-users-controller.php | 1440 ----------- .../fields/class-wp-rest-comment-meta-fields.php | 51 - .../rest-api/fields/class-wp-rest-meta-fields.php | 590 ----- .../fields/class-wp-rest-post-meta-fields.php | 72 - .../fields/class-wp-rest-term-meta-fields.php | 70 - .../fields/class-wp-rest-user-meta-fields.php | 51 - .../search/class-wp-rest-post-search-handler.php | 206 -- .../search/class-wp-rest-search-handler.php | 97 - 26 files changed, 15745 deletions(-) delete mode 100644 srcs/wordpress/wp-includes/rest-api/class-wp-rest-request.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/class-wp-rest-response.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/class-wp-rest-server.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/fields/class-wp-rest-comment-meta-fields.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/fields/class-wp-rest-post-meta-fields.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/fields/class-wp-rest-term-meta-fields.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/fields/class-wp-rest-user-meta-fields.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php delete mode 100644 srcs/wordpress/wp-includes/rest-api/search/class-wp-rest-search-handler.php (limited to 'srcs/wordpress/wp-includes/rest-api') diff --git a/srcs/wordpress/wp-includes/rest-api/class-wp-rest-request.php b/srcs/wordpress/wp-includes/rest-api/class-wp-rest-request.php deleted file mode 100644 index 0910ff6..0000000 --- a/srcs/wordpress/wp-includes/rest-api/class-wp-rest-request.php +++ /dev/null @@ -1,1017 +0,0 @@ -params = array( - 'URL' => array(), - 'GET' => array(), - 'POST' => array(), - 'FILES' => array(), - - // See parse_json_params. - 'JSON' => null, - - 'defaults' => array(), - ); - - $this->set_method( $method ); - $this->set_route( $route ); - $this->set_attributes( $attributes ); - } - - /** - * Retrieves the HTTP method for the request. - * - * @since 4.4.0 - * - * @return string HTTP method. - */ - public function get_method() { - return $this->method; - } - - /** - * Sets HTTP method for the request. - * - * @since 4.4.0 - * - * @param string $method HTTP method. - */ - public function set_method( $method ) { - $this->method = strtoupper( $method ); - } - - /** - * Retrieves all headers from the request. - * - * @since 4.4.0 - * - * @return array Map of key to value. Key is always lowercase, as per HTTP specification. - */ - public function get_headers() { - return $this->headers; - } - - /** - * Canonicalizes the header name. - * - * Ensures that header names are always treated the same regardless of - * source. Header names are always case insensitive. - * - * Note that we treat `-` (dashes) and `_` (underscores) as the same - * character, as per header parsing rules in both Apache and nginx. - * - * @link https://stackoverflow.com/q/18185366 - * @link https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#missing-disappearing-http-headers - * @link https://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers - * - * @since 4.4.0 - * - * @param string $key Header name. - * @return string Canonicalized name. - */ - public static function canonicalize_header_name( $key ) { - $key = strtolower( $key ); - $key = str_replace( '-', '_', $key ); - - return $key; - } - - /** - * Retrieves the given header from the request. - * - * If the header has multiple values, they will be concatenated with a comma - * as per the HTTP specification. Be aware that some non-compliant headers - * (notably cookie headers) cannot be joined this way. - * - * @since 4.4.0 - * - * @param string $key Header name, will be canonicalized to lowercase. - * @return string|null String value if set, null otherwise. - */ - public function get_header( $key ) { - $key = $this->canonicalize_header_name( $key ); - - if ( ! isset( $this->headers[ $key ] ) ) { - return null; - } - - return implode( ',', $this->headers[ $key ] ); - } - - /** - * Retrieves header values from the request. - * - * @since 4.4.0 - * - * @param string $key Header name, will be canonicalized to lowercase. - * @return array|null List of string values if set, null otherwise. - */ - public function get_header_as_array( $key ) { - $key = $this->canonicalize_header_name( $key ); - - if ( ! isset( $this->headers[ $key ] ) ) { - return null; - } - - return $this->headers[ $key ]; - } - - /** - * Sets the header on request. - * - * @since 4.4.0 - * - * @param string $key Header name. - * @param string $value Header value, or list of values. - */ - public function set_header( $key, $value ) { - $key = $this->canonicalize_header_name( $key ); - $value = (array) $value; - - $this->headers[ $key ] = $value; - } - - /** - * Appends a header value for the given header. - * - * @since 4.4.0 - * - * @param string $key Header name. - * @param string $value Header value, or list of values. - */ - public function add_header( $key, $value ) { - $key = $this->canonicalize_header_name( $key ); - $value = (array) $value; - - if ( ! isset( $this->headers[ $key ] ) ) { - $this->headers[ $key ] = array(); - } - - $this->headers[ $key ] = array_merge( $this->headers[ $key ], $value ); - } - - /** - * Removes all values for a header. - * - * @since 4.4.0 - * - * @param string $key Header name. - */ - public function remove_header( $key ) { - $key = $this->canonicalize_header_name( $key ); - unset( $this->headers[ $key ] ); - } - - /** - * Sets headers on the request. - * - * @since 4.4.0 - * - * @param array $headers Map of header name to value. - * @param bool $override If true, replace the request's headers. Otherwise, merge with existing. - */ - public function set_headers( $headers, $override = true ) { - if ( true === $override ) { - $this->headers = array(); - } - - foreach ( $headers as $key => $value ) { - $this->set_header( $key, $value ); - } - } - - /** - * Retrieves the content-type of the request. - * - * @since 4.4.0 - * - * @return array|null Map containing 'value' and 'parameters' keys - * or null when no valid content-type header was - * available. - */ - public function get_content_type() { - $value = $this->get_header( 'content-type' ); - if ( empty( $value ) ) { - return null; - } - - $parameters = ''; - if ( strpos( $value, ';' ) ) { - list( $value, $parameters ) = explode( ';', $value, 2 ); - } - - $value = strtolower( $value ); - if ( strpos( $value, '/' ) === false ) { - return null; - } - - // Parse type and subtype out. - list( $type, $subtype ) = explode( '/', $value, 2 ); - - $data = compact( 'value', 'type', 'subtype', 'parameters' ); - $data = array_map( 'trim', $data ); - - return $data; - } - - /** - * Retrieves the parameter priority order. - * - * Used when checking parameters in get_param(). - * - * @since 4.4.0 - * - * @return array List of types to check, in order of priority. - */ - protected function get_parameter_order() { - $order = array(); - - $content_type = $this->get_content_type(); - if ( isset( $content_type['value'] ) && 'application/json' === $content_type['value'] ) { - $order[] = 'JSON'; - } - - $this->parse_json_params(); - - // Ensure we parse the body data. - $body = $this->get_body(); - - if ( 'POST' !== $this->method && ! empty( $body ) ) { - $this->parse_body_params(); - } - - $accepts_body_data = array( 'POST', 'PUT', 'PATCH', 'DELETE' ); - if ( in_array( $this->method, $accepts_body_data ) ) { - $order[] = 'POST'; - } - - $order[] = 'GET'; - $order[] = 'URL'; - $order[] = 'defaults'; - - /** - * Filters the parameter order. - * - * The order affects which parameters are checked when using get_param() and family. - * This acts similarly to PHP's `request_order` setting. - * - * @since 4.4.0 - * - * @param array $order { - * An array of types to check, in order of priority. - * - * @param string $type The type to check. - * } - * @param WP_REST_Request $this The request object. - */ - return apply_filters( 'rest_request_parameter_order', $order, $this ); - } - - /** - * Retrieves a parameter from the request. - * - * @since 4.4.0 - * - * @param string $key Parameter name. - * @return mixed|null Value if set, null otherwise. - */ - public function get_param( $key ) { - $order = $this->get_parameter_order(); - - foreach ( $order as $type ) { - // Determine if we have the parameter for this type. - if ( isset( $this->params[ $type ][ $key ] ) ) { - return $this->params[ $type ][ $key ]; - } - } - - return null; - } - - /** - * Checks if a parameter exists in the request. - * - * This allows distinguishing between an omitted parameter, - * and a parameter specifically set to null. - * - * @since 5.3.0 - * - * @param string $key Parameter name. - * - * @return bool True if a param exists for the given key. - */ - public function has_param( $key ) { - $order = $this->get_parameter_order(); - - foreach ( $order as $type ) { - if ( array_key_exists( $key, $this->params[ $type ] ) ) { - return true; - } - } - - return false; - } - - /** - * Sets a parameter on the request. - * - * @since 4.4.0 - * - * @param string $key Parameter name. - * @param mixed $value Parameter value. - */ - public function set_param( $key, $value ) { - $order = $this->get_parameter_order(); - $this->params[ $order[0] ][ $key ] = $value; - } - - /** - * Retrieves merged parameters from the request. - * - * The equivalent of get_param(), but returns all parameters for the request. - * Handles merging all the available values into a single array. - * - * @since 4.4.0 - * - * @return array Map of key to value. - */ - public function get_params() { - $order = $this->get_parameter_order(); - $order = array_reverse( $order, true ); - - $params = array(); - foreach ( $order as $type ) { - // array_merge / the "+" operator will mess up - // numeric keys, so instead do a manual foreach. - foreach ( (array) $this->params[ $type ] as $key => $value ) { - $params[ $key ] = $value; - } - } - - return $params; - } - - /** - * Retrieves parameters from the route itself. - * - * These are parsed from the URL using the regex. - * - * @since 4.4.0 - * - * @return array Parameter map of key to value. - */ - public function get_url_params() { - return $this->params['URL']; - } - - /** - * Sets parameters from the route. - * - * Typically, this is set after parsing the URL. - * - * @since 4.4.0 - * - * @param array $params Parameter map of key to value. - */ - public function set_url_params( $params ) { - $this->params['URL'] = $params; - } - - /** - * Retrieves parameters from the query string. - * - * These are the parameters you'd typically find in `$_GET`. - * - * @since 4.4.0 - * - * @return array Parameter map of key to value - */ - public function get_query_params() { - return $this->params['GET']; - } - - /** - * Sets parameters from the query string. - * - * Typically, this is set from `$_GET`. - * - * @since 4.4.0 - * - * @param array $params Parameter map of key to value. - */ - public function set_query_params( $params ) { - $this->params['GET'] = $params; - } - - /** - * Retrieves parameters from the body. - * - * These are the parameters you'd typically find in `$_POST`. - * - * @since 4.4.0 - * - * @return array Parameter map of key to value. - */ - public function get_body_params() { - return $this->params['POST']; - } - - /** - * Sets parameters from the body. - * - * Typically, this is set from `$_POST`. - * - * @since 4.4.0 - * - * @param array $params Parameter map of key to value. - */ - public function set_body_params( $params ) { - $this->params['POST'] = $params; - } - - /** - * Retrieves multipart file parameters from the body. - * - * These are the parameters you'd typically find in `$_FILES`. - * - * @since 4.4.0 - * - * @return array Parameter map of key to value - */ - public function get_file_params() { - return $this->params['FILES']; - } - - /** - * Sets multipart file parameters from the body. - * - * Typically, this is set from `$_FILES`. - * - * @since 4.4.0 - * - * @param array $params Parameter map of key to value. - */ - public function set_file_params( $params ) { - $this->params['FILES'] = $params; - } - - /** - * Retrieves the default parameters. - * - * These are the parameters set in the route registration. - * - * @since 4.4.0 - * - * @return array Parameter map of key to value - */ - public function get_default_params() { - return $this->params['defaults']; - } - - /** - * Sets default parameters. - * - * These are the parameters set in the route registration. - * - * @since 4.4.0 - * - * @param array $params Parameter map of key to value. - */ - public function set_default_params( $params ) { - $this->params['defaults'] = $params; - } - - /** - * Retrieves the request body content. - * - * @since 4.4.0 - * - * @return string Binary data from the request body. - */ - public function get_body() { - return $this->body; - } - - /** - * Sets body content. - * - * @since 4.4.0 - * - * @param string $data Binary data from the request body. - */ - public function set_body( $data ) { - $this->body = $data; - - // Enable lazy parsing. - $this->parsed_json = false; - $this->parsed_body = false; - $this->params['JSON'] = null; - } - - /** - * Retrieves the parameters from a JSON-formatted body. - * - * @since 4.4.0 - * - * @return array Parameter map of key to value. - */ - public function get_json_params() { - // Ensure the parameters have been parsed out. - $this->parse_json_params(); - - return $this->params['JSON']; - } - - /** - * Parses the JSON parameters. - * - * Avoids parsing the JSON data until we need to access it. - * - * @since 4.4.0 - * @since 4.7.0 Returns error instance if value cannot be decoded. - * @return true|WP_Error True if the JSON data was passed or no JSON data was provided, WP_Error if invalid JSON was passed. - */ - protected function parse_json_params() { - if ( $this->parsed_json ) { - return true; - } - - $this->parsed_json = true; - - // Check that we actually got JSON. - $content_type = $this->get_content_type(); - - if ( empty( $content_type ) || 'application/json' !== $content_type['value'] ) { - return true; - } - - $body = $this->get_body(); - if ( empty( $body ) ) { - return true; - } - - $params = json_decode( $body, true ); - - /* - * Check for a parsing error. - */ - if ( null === $params && JSON_ERROR_NONE !== json_last_error() ) { - // Ensure subsequent calls receive error instance. - $this->parsed_json = false; - - $error_data = array( - 'status' => WP_Http::BAD_REQUEST, - 'json_error_code' => json_last_error(), - 'json_error_message' => json_last_error_msg(), - ); - - return new WP_Error( 'rest_invalid_json', __( 'Invalid JSON body passed.' ), $error_data ); - } - - $this->params['JSON'] = $params; - return true; - } - - /** - * Parses the request body parameters. - * - * Parses out URL-encoded bodies for request methods that aren't supported - * natively by PHP. In PHP 5.x, only POST has these parsed automatically. - * - * @since 4.4.0 - */ - protected function parse_body_params() { - if ( $this->parsed_body ) { - return; - } - - $this->parsed_body = true; - - /* - * Check that we got URL-encoded. Treat a missing content-type as - * URL-encoded for maximum compatibility. - */ - $content_type = $this->get_content_type(); - - if ( ! empty( $content_type ) && 'application/x-www-form-urlencoded' !== $content_type['value'] ) { - return; - } - - parse_str( $this->get_body(), $params ); - - /* - * Add to the POST parameters stored internally. If a user has already - * set these manually (via `set_body_params`), don't override them. - */ - $this->params['POST'] = array_merge( $params, $this->params['POST'] ); - } - - /** - * Retrieves the route that matched the request. - * - * @since 4.4.0 - * - * @return string Route matching regex. - */ - public function get_route() { - return $this->route; - } - - /** - * Sets the route that matched the request. - * - * @since 4.4.0 - * - * @param string $route Route matching regex. - */ - public function set_route( $route ) { - $this->route = $route; - } - - /** - * Retrieves the attributes for the request. - * - * These are the options for the route that was matched. - * - * @since 4.4.0 - * - * @return array Attributes for the request. - */ - public function get_attributes() { - return $this->attributes; - } - - /** - * Sets the attributes for the request. - * - * @since 4.4.0 - * - * @param array $attributes Attributes for the request. - */ - public function set_attributes( $attributes ) { - $this->attributes = $attributes; - } - - /** - * Sanitizes (where possible) the params on the request. - * - * This is primarily based off the sanitize_callback param on each registered - * argument. - * - * @since 4.4.0 - * - * @return true|WP_Error True if parameters were sanitized, WP_Error if an error occurred during sanitization. - */ - public function sanitize_params() { - $attributes = $this->get_attributes(); - - // No arguments set, skip sanitizing. - if ( empty( $attributes['args'] ) ) { - return true; - } - - $order = $this->get_parameter_order(); - - $invalid_params = array(); - - foreach ( $order as $type ) { - if ( empty( $this->params[ $type ] ) ) { - continue; - } - foreach ( $this->params[ $type ] as $key => $value ) { - if ( ! isset( $attributes['args'][ $key ] ) ) { - continue; - } - $param_args = $attributes['args'][ $key ]; - - // If the arg has a type but no sanitize_callback attribute, default to rest_parse_request_arg. - if ( ! array_key_exists( 'sanitize_callback', $param_args ) && ! empty( $param_args['type'] ) ) { - $param_args['sanitize_callback'] = 'rest_parse_request_arg'; - } - // If there's still no sanitize_callback, nothing to do here. - if ( empty( $param_args['sanitize_callback'] ) ) { - continue; - } - - $sanitized_value = call_user_func( $param_args['sanitize_callback'], $value, $this, $key ); - - if ( is_wp_error( $sanitized_value ) ) { - $invalid_params[ $key ] = $sanitized_value->get_error_message(); - } else { - $this->params[ $type ][ $key ] = $sanitized_value; - } - } - } - - if ( $invalid_params ) { - return new WP_Error( - 'rest_invalid_param', - /* translators: %s: List of invalid parameters. */ - sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), - array( - 'status' => 400, - 'params' => $invalid_params, - ) - ); - } - - return true; - } - - /** - * Checks whether this request is valid according to its attributes. - * - * @since 4.4.0 - * - * @return bool|WP_Error True if there are no parameters to validate or if all pass validation, - * WP_Error if required parameters are missing. - */ - public function has_valid_params() { - // If JSON data was passed, check for errors. - $json_error = $this->parse_json_params(); - if ( is_wp_error( $json_error ) ) { - return $json_error; - } - - $attributes = $this->get_attributes(); - $required = array(); - - // No arguments set, skip validation. - if ( empty( $attributes['args'] ) ) { - return true; - } - - foreach ( $attributes['args'] as $key => $arg ) { - - $param = $this->get_param( $key ); - if ( isset( $arg['required'] ) && true === $arg['required'] && null === $param ) { - $required[] = $key; - } - } - - if ( ! empty( $required ) ) { - return new WP_Error( - 'rest_missing_callback_param', - /* translators: %s: List of required parameters. */ - sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ), - array( - 'status' => 400, - 'params' => $required, - ) - ); - } - - /* - * Check the validation callbacks for each registered arg. - * - * This is done after required checking as required checking is cheaper. - */ - $invalid_params = array(); - - foreach ( $attributes['args'] as $key => $arg ) { - - $param = $this->get_param( $key ); - - if ( null !== $param && ! empty( $arg['validate_callback'] ) ) { - $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key ); - - if ( false === $valid_check ) { - $invalid_params[ $key ] = __( 'Invalid parameter.' ); - } - - if ( is_wp_error( $valid_check ) ) { - $invalid_params[ $key ] = $valid_check->get_error_message(); - } - } - } - - if ( $invalid_params ) { - return new WP_Error( - 'rest_invalid_param', - /* translators: %s: List of invalid parameters. */ - sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), - array( - 'status' => 400, - 'params' => $invalid_params, - ) - ); - } - - return true; - - } - - /** - * Checks if a parameter is set. - * - * @since 4.4.0 - * - * @param string $offset Parameter name. - * @return bool Whether the parameter is set. - */ - public function offsetExists( $offset ) { - $order = $this->get_parameter_order(); - - foreach ( $order as $type ) { - if ( isset( $this->params[ $type ][ $offset ] ) ) { - return true; - } - } - - return false; - } - - /** - * Retrieves a parameter from the request. - * - * @since 4.4.0 - * - * @param string $offset Parameter name. - * @return mixed|null Value if set, null otherwise. - */ - public function offsetGet( $offset ) { - return $this->get_param( $offset ); - } - - /** - * Sets a parameter on the request. - * - * @since 4.4.0 - * - * @param string $offset Parameter name. - * @param mixed $value Parameter value. - */ - public function offsetSet( $offset, $value ) { - $this->set_param( $offset, $value ); - } - - /** - * Removes a parameter from the request. - * - * @since 4.4.0 - * - * @param string $offset Parameter name. - */ - public function offsetUnset( $offset ) { - $order = $this->get_parameter_order(); - - // Remove the offset from every group. - foreach ( $order as $type ) { - unset( $this->params[ $type ][ $offset ] ); - } - } - - /** - * Retrieves a WP_REST_Request object from a full URL. - * - * @since 4.5.0 - * - * @param string $url URL with protocol, domain, path and query args. - * @return WP_REST_Request|false WP_REST_Request object on success, false on failure. - */ - public static function from_url( $url ) { - $bits = parse_url( $url ); - $query_params = array(); - - if ( ! empty( $bits['query'] ) ) { - wp_parse_str( $bits['query'], $query_params ); - } - - $api_root = rest_url(); - if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) { - // Pretty permalinks on, and URL is under the API root. - $api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) ); - $route = parse_url( $api_url_part, PHP_URL_PATH ); - } elseif ( ! empty( $query_params['rest_route'] ) ) { - // ?rest_route=... set directly - $route = $query_params['rest_route']; - unset( $query_params['rest_route'] ); - } - - $request = false; - if ( ! empty( $route ) ) { - $request = new WP_REST_Request( 'GET', $route ); - $request->set_query_params( $query_params ); - } - - /** - * Filters the request generated from a URL. - * - * @since 4.5.0 - * - * @param WP_REST_Request|false $request Generated request object, or false if URL - * could not be parsed. - * @param string $url URL the request was generated from. - */ - return apply_filters( 'rest_request_from_url', $request, $url ); - } -} diff --git a/srcs/wordpress/wp-includes/rest-api/class-wp-rest-response.php b/srcs/wordpress/wp-includes/rest-api/class-wp-rest-response.php deleted file mode 100644 index b13e923..0000000 --- a/srcs/wordpress/wp-includes/rest-api/class-wp-rest-response.php +++ /dev/null @@ -1,290 +0,0 @@ -links[ $rel ] ) ) { - $this->links[ $rel ] = array(); - } - - if ( isset( $attributes['href'] ) ) { - // Remove the href attribute, as it's used for the main URL. - unset( $attributes['href'] ); - } - - $this->links[ $rel ][] = array( - 'href' => $href, - 'attributes' => $attributes, - ); - } - - /** - * Removes a link from the response. - * - * @since 4.4.0 - * - * @param string $rel Link relation. Either an IANA registered type, or an absolute URL. - * @param string $href Optional. Only remove links for the relation matching the given href. - * Default null. - */ - public function remove_link( $rel, $href = null ) { - if ( ! isset( $this->links[ $rel ] ) ) { - return; - } - - if ( $href ) { - $this->links[ $rel ] = wp_list_filter( $this->links[ $rel ], array( 'href' => $href ), 'NOT' ); - } else { - $this->links[ $rel ] = array(); - } - - if ( ! $this->links[ $rel ] ) { - unset( $this->links[ $rel ] ); - } - } - - /** - * Adds multiple links to the response. - * - * Link data should be an associative array with link relation as the key. - * The value can either be an associative array of link attributes - * (including `href` with the URL for the response), or a list of these - * associative arrays. - * - * @since 4.4.0 - * - * @param array $links Map of link relation to list of links. - */ - public function add_links( $links ) { - foreach ( $links as $rel => $set ) { - // If it's a single link, wrap with an array for consistent handling. - if ( isset( $set['href'] ) ) { - $set = array( $set ); - } - - foreach ( $set as $attributes ) { - $this->add_link( $rel, $attributes['href'], $attributes ); - } - } - } - - /** - * Retrieves links for the response. - * - * @since 4.4.0 - * - * @return array List of links. - */ - public function get_links() { - return $this->links; - } - - /** - * Sets a single link header. - * - * @internal The $rel parameter is first, as this looks nicer when sending multiple. - * - * @since 4.4.0 - * - * @link https://tools.ietf.org/html/rfc5988 - * @link https://www.iana.org/assignments/link-relations/link-relations.xml - * - * @param string $rel Link relation. Either an IANA registered type, or an absolute URL. - * @param string $link Target IRI for the link. - * @param array $other Optional. Other parameters to send, as an assocative array. - * Default empty array. - */ - public function link_header( $rel, $link, $other = array() ) { - $header = '<' . $link . '>; rel="' . $rel . '"'; - - foreach ( $other as $key => $value ) { - if ( 'title' === $key ) { - $value = '"' . $value . '"'; - } - $header .= '; ' . $key . '=' . $value; - } - $this->header( 'Link', $header, false ); - } - - /** - * Retrieves the route that was used. - * - * @since 4.4.0 - * - * @return string The matched route. - */ - public function get_matched_route() { - return $this->matched_route; - } - - /** - * Sets the route (regex for path) that caused the response. - * - * @since 4.4.0 - * - * @param string $route Route name. - */ - public function set_matched_route( $route ) { - $this->matched_route = $route; - } - - /** - * Retrieves the handler that was used to generate the response. - * - * @since 4.4.0 - * - * @return null|array The handler that was used to create the response. - */ - public function get_matched_handler() { - return $this->matched_handler; - } - - /** - * Sets the handler that was responsible for generating the response. - * - * @since 4.4.0 - * - * @param array $handler The matched handler. - */ - public function set_matched_handler( $handler ) { - $this->matched_handler = $handler; - } - - /** - * Checks if the response is an error, i.e. >= 400 response code. - * - * @since 4.4.0 - * - * @return bool Whether the response is an error. - */ - public function is_error() { - return $this->get_status() >= 400; - } - - /** - * Retrieves a WP_Error object from the response. - * - * @since 4.4.0 - * - * @return WP_Error|null WP_Error or null on not an errored response. - */ - public function as_error() { - if ( ! $this->is_error() ) { - return null; - } - - $error = new WP_Error; - - if ( is_array( $this->get_data() ) ) { - $data = $this->get_data(); - $error->add( $data['code'], $data['message'], $data['data'] ); - if ( ! empty( $data['additional_errors'] ) ) { - foreach ( $data['additional_errors'] as $err ) { - $error->add( $err['code'], $err['message'], $err['data'] ); - } - } - } else { - $error->add( $this->get_status(), '', array( 'status' => $this->get_status() ) ); - } - - return $error; - } - - /** - * Retrieves the CURIEs (compact URIs) used for relations. - * - * @since 4.5.0 - * - * @return array Compact URIs. - */ - public function get_curies() { - $curies = array( - array( - 'name' => 'wp', - 'href' => 'https://api.w.org/{rel}', - 'templated' => true, - ), - ); - - /** - * Filters extra CURIEs available on API responses. - * - * CURIEs allow a shortened version of URI relations. This allows a more - * usable form for custom relations than using the full URI. These work - * similarly to how XML namespaces work. - * - * Registered CURIES need to specify a name and URI template. This will - * automatically transform URI relations into their shortened version. - * The shortened relation follows the format `{name}:{rel}`. `{rel}` in - * the URI template will be replaced with the `{rel}` part of the - * shortened relation. - * - * For example, a CURIE with name `example` and URI template - * `http://w.org/{rel}` would transform a `http://w.org/term` relation - * into `example:term`. - * - * Well-behaved clients should expand and normalise these back to their - * full URI relation, however some naive clients may not resolve these - * correctly, so adding new CURIEs may break backward compatibility. - * - * @since 4.5.0 - * - * @param array $additional Additional CURIEs to register with the API. - */ - $additional = apply_filters( 'rest_response_link_curies', array() ); - return array_merge( $curies, $additional ); - } -} diff --git a/srcs/wordpress/wp-includes/rest-api/class-wp-rest-server.php b/srcs/wordpress/wp-includes/rest-api/class-wp-rest-server.php deleted file mode 100644 index 8b09076..0000000 --- a/srcs/wordpress/wp-includes/rest-api/class-wp-rest-server.php +++ /dev/null @@ -1,1331 +0,0 @@ -endpoints = array( - // Meta endpoints. - '/' => array( - 'callback' => array( $this, 'get_index' ), - 'methods' => 'GET', - 'args' => array( - 'context' => array( - 'default' => 'view', - ), - ), - ), - ); - } - - - /** - * Checks the authentication headers if supplied. - * - * @since 4.4.0 - * - * @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful - * or no authentication provided - */ - public function check_authentication() { - /** - * Filters REST authentication errors. - * - * This is used to pass a WP_Error from an authentication method back to - * the API. - * - * Authentication methods should check first if they're being used, as - * multiple authentication methods can be enabled on a site (cookies, - * HTTP basic auth, OAuth). If the authentication method hooked in is - * not actually being attempted, null should be returned to indicate - * another authentication method should check instead. Similarly, - * callbacks should ensure the value is `null` before checking for - * errors. - * - * A WP_Error instance can be returned if an error occurs, and this should - * match the format used by API methods internally (that is, the `status` - * data should be used). A callback can return `true` to indicate that - * the authentication method was used, and it succeeded. - * - * @since 4.4.0 - * - * @param WP_Error|null|bool WP_Error if authentication error, null if authentication - * method wasn't used, true if authentication succeeded. - */ - return apply_filters( 'rest_authentication_errors', null ); - } - - /** - * Converts an error to a response object. - * - * This iterates over all error codes and messages to change it into a flat - * array. This enables simpler client behaviour, as it is represented as a - * list in JSON rather than an object/map. - * - * @since 4.4.0 - * - * @param WP_Error $error WP_Error instance. - * @return WP_REST_Response List of associative arrays with code and message keys. - */ - protected function error_to_response( $error ) { - $error_data = $error->get_error_data(); - - if ( is_array( $error_data ) && isset( $error_data['status'] ) ) { - $status = $error_data['status']; - } else { - $status = 500; - } - - $errors = array(); - - foreach ( (array) $error->errors as $code => $messages ) { - foreach ( (array) $messages as $message ) { - $errors[] = array( - 'code' => $code, - 'message' => $message, - 'data' => $error->get_error_data( $code ), - ); - } - } - - $data = $errors[0]; - if ( count( $errors ) > 1 ) { - // Remove the primary error. - array_shift( $errors ); - $data['additional_errors'] = $errors; - } - - $response = new WP_REST_Response( $data, $status ); - - return $response; - } - - /** - * Retrieves an appropriate error representation in JSON. - * - * Note: This should only be used in WP_REST_Server::serve_request(), as it - * cannot handle WP_Error internally. All callbacks and other internal methods - * should instead return a WP_Error with the data set to an array that includes - * a 'status' key, with the value being the HTTP status to send. - * - * @since 4.4.0 - * - * @param string $code WP_Error-style code. - * @param string $message Human-readable message. - * @param int $status Optional. HTTP status code to send. Default null. - * @return string JSON representation of the error - */ - protected function json_error( $code, $message, $status = null ) { - if ( $status ) { - $this->set_status( $status ); - } - - $error = compact( 'code', 'message' ); - - return wp_json_encode( $error ); - } - - /** - * Handles serving an API request. - * - * Matches the current server URI to a route and runs the first matching - * callback then outputs a JSON representation of the returned value. - * - * @since 4.4.0 - * - * @see WP_REST_Server::dispatch() - * - * @param string $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used. - * Default null. - * @return false|null Null if not served and a HEAD request, false otherwise. - */ - public function serve_request( $path = null ) { - $content_type = isset( $_GET['_jsonp'] ) ? 'application/javascript' : 'application/json'; - $this->send_header( 'Content-Type', $content_type . '; charset=' . get_option( 'blog_charset' ) ); - $this->send_header( 'X-Robots-Tag', 'noindex' ); - - $api_root = get_rest_url(); - if ( ! empty( $api_root ) ) { - $this->send_header( 'Link', '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"' ); - } - - /* - * Mitigate possible JSONP Flash attacks. - * - * https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/ - */ - $this->send_header( 'X-Content-Type-Options', 'nosniff' ); - $this->send_header( 'Access-Control-Expose-Headers', 'X-WP-Total, X-WP-TotalPages' ); - $this->send_header( 'Access-Control-Allow-Headers', 'Authorization, Content-Type' ); - - /** - * Send nocache headers on authenticated requests. - * - * @since 4.4.0 - * - * @param bool $rest_send_nocache_headers Whether to send no-cache headers. - */ - $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() ); - if ( $send_no_cache_headers ) { - foreach ( wp_get_nocache_headers() as $header => $header_value ) { - if ( empty( $header_value ) ) { - $this->remove_header( $header ); - } else { - $this->send_header( $header, $header_value ); - } - } - } - - /** - * Filters whether the REST API is enabled. - * - * @since 4.4.0 - * @deprecated 4.7.0 Use the rest_authentication_errors filter to restrict access to the API - * - * @param bool $rest_enabled Whether the REST API is enabled. Default true. - */ - apply_filters_deprecated( - 'rest_enabled', - array( true ), - '4.7.0', - 'rest_authentication_errors', - __( 'The REST API can no longer be completely disabled, the rest_authentication_errors filter can be used to restrict access to the API, instead.' ) - ); - - /** - * Filters whether jsonp is enabled. - * - * @since 4.4.0 - * - * @param bool $jsonp_enabled Whether jsonp is enabled. Default true. - */ - $jsonp_enabled = apply_filters( 'rest_jsonp_enabled', true ); - - $jsonp_callback = null; - - if ( isset( $_GET['_jsonp'] ) ) { - if ( ! $jsonp_enabled ) { - echo $this->json_error( 'rest_callback_disabled', __( 'JSONP support is disabled on this site.' ), 400 ); - return false; - } - - $jsonp_callback = $_GET['_jsonp']; - if ( ! wp_check_jsonp_callback( $jsonp_callback ) ) { - echo $this->json_error( 'rest_callback_invalid', __( 'Invalid JSONP callback function.' ), 400 ); - return false; - } - } - - if ( empty( $path ) ) { - if ( isset( $_SERVER['PATH_INFO'] ) ) { - $path = $_SERVER['PATH_INFO']; - } else { - $path = '/'; - } - } - - $request = new WP_REST_Request( $_SERVER['REQUEST_METHOD'], $path ); - - $request->set_query_params( wp_unslash( $_GET ) ); - $request->set_body_params( wp_unslash( $_POST ) ); - $request->set_file_params( $_FILES ); - $request->set_headers( $this->get_headers( wp_unslash( $_SERVER ) ) ); - $request->set_body( self::get_raw_data() ); - - /* - * HTTP method override for clients that can't use PUT/PATCH/DELETE. First, we check - * $_GET['_method']. If that is not set, we check for the HTTP_X_HTTP_METHOD_OVERRIDE - * header. - */ - if ( isset( $_GET['_method'] ) ) { - $request->set_method( $_GET['_method'] ); - } elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) { - $request->set_method( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ); - } - - $result = $this->check_authentication(); - - if ( ! is_wp_error( $result ) ) { - $result = $this->dispatch( $request ); - } - - // Normalize to either WP_Error or WP_REST_Response... - $result = rest_ensure_response( $result ); - - // ...then convert WP_Error across. - if ( is_wp_error( $result ) ) { - $result = $this->error_to_response( $result ); - } - - /** - * Filters the API response. - * - * Allows modification of the response before returning. - * - * @since 4.4.0 - * @since 4.5.0 Applied to embedded responses. - * - * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. - * @param WP_REST_Server $this Server instance. - * @param WP_REST_Request $request Request used to generate the response. - */ - $result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request ); - - // Wrap the response in an envelope if asked for. - if ( isset( $_GET['_envelope'] ) ) { - $result = $this->envelope_response( $result, isset( $_GET['_embed'] ) ); - } - - // Send extra data from response objects. - $headers = $result->get_headers(); - $this->send_headers( $headers ); - - $code = $result->get_status(); - $this->set_status( $code ); - - /** - * Filters whether the request has already been served. - * - * Allow sending the request manually - by returning true, the API result - * will not be sent to the client. - * - * @since 4.4.0 - * - * @param bool $served Whether the request has already been served. - * Default false. - * @param WP_HTTP_Response $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 $this Server instance. - */ - $served = apply_filters( 'rest_pre_serve_request', false, $result, $request, $this ); - - if ( ! $served ) { - if ( 'HEAD' === $request->get_method() ) { - return null; - } - - // Embed links inside the request. - $result = $this->response_to_data( $result, isset( $_GET['_embed'] ) ); - - /** - * Filters the API response. - * - * Allows modification of the response data after inserting - * embedded data (if any) and before echoing the response data. - * - * @since 4.8.1 - * - * @param array $result Response data to send to the client. - * @param WP_REST_Server $this Server instance. - * @param WP_REST_Request $request Request used to generate the response. - */ - $result = apply_filters( 'rest_pre_echo_response', $result, $this, $request ); - - // The 204 response shouldn't have a body. - if ( 204 === $code || null === $result ) { - return null; - } - - $result = wp_json_encode( $result ); - - $json_error_message = $this->get_json_last_error(); - if ( $json_error_message ) { - $json_error_obj = new WP_Error( 'rest_encode_error', $json_error_message, array( 'status' => 500 ) ); - $result = $this->error_to_response( $json_error_obj ); - $result = wp_json_encode( $result->data[0] ); - } - - if ( $jsonp_callback ) { - // Prepend '/**/' to mitigate possible JSONP Flash attacks. - // https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/ - echo '/**/' . $jsonp_callback . '(' . $result . ')'; - } else { - echo $result; - } - } - return null; - } - - /** - * Converts a response to data to send. - * - * @since 4.4.0 - * - * @param WP_REST_Response $response Response object. - * @param bool $embed Whether links should be embedded. - * @return array { - * Data with sub-requests embedded. - * - * @type array [$_links] Links. - * @type array [$_embedded] Embeddeds. - * } - */ - public function response_to_data( $response, $embed ) { - $data = $response->get_data(); - $links = self::get_compact_response_links( $response ); - - if ( ! empty( $links ) ) { - // Convert links to part of the data. - $data['_links'] = $links; - } - if ( $embed ) { - // Determine if this is a numeric array. - if ( wp_is_numeric_array( $data ) ) { - $data = array_map( array( $this, 'embed_links' ), $data ); - } else { - $data = $this->embed_links( $data ); - } - } - - return $data; - } - - /** - * Retrieves links from a response. - * - * Extracts the links from a response into a structured hash, suitable for - * direct output. - * - * @since 4.4.0 - * - * @param WP_REST_Response $response Response to extract links from. - * @return array Map of link relation to list of link hashes. - */ - public static function get_response_links( $response ) { - $links = $response->get_links(); - if ( empty( $links ) ) { - return array(); - } - - // Convert links to part of the data. - $data = array(); - foreach ( $links as $rel => $items ) { - $data[ $rel ] = array(); - - foreach ( $items as $item ) { - $attributes = $item['attributes']; - $attributes['href'] = $item['href']; - $data[ $rel ][] = $attributes; - } - } - - return $data; - } - - /** - * Retrieves the CURIEs (compact URIs) used for relations. - * - * Extracts the links from a response into a structured hash, suitable for - * direct output. - * - * @since 4.5.0 - * - * @param WP_REST_Response $response Response to extract links from. - * @return array Map of link relation to list of link hashes. - */ - public static function get_compact_response_links( $response ) { - $links = self::get_response_links( $response ); - - if ( empty( $links ) ) { - return array(); - } - - $curies = $response->get_curies(); - $used_curies = array(); - - foreach ( $links as $rel => $items ) { - - // Convert $rel URIs to their compact versions if they exist. - foreach ( $curies as $curie ) { - $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); - if ( strpos( $rel, $href_prefix ) !== 0 ) { - continue; - } - - // Relation now changes from '$uri' to '$curie:$relation'. - $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); - preg_match( '!' . $rel_regex . '!', $rel, $matches ); - if ( $matches ) { - $new_rel = $curie['name'] . ':' . $matches[1]; - $used_curies[ $curie['name'] ] = $curie; - $links[ $new_rel ] = $items; - unset( $links[ $rel ] ); - break; - } - } - } - - // Push the curies onto the start of the links array. - if ( $used_curies ) { - $links['curies'] = array_values( $used_curies ); - } - - return $links; - } - - /** - * Embeds the links from the data into the request. - * - * @since 4.4.0 - * - * @param array $data Data from the request. - * @return array { - * Data with sub-requests embedded. - * - * @type array [$_links] Links. - * @type array [$_embedded] Embeddeds. - * } - */ - protected function embed_links( $data ) { - if ( empty( $data['_links'] ) ) { - return $data; - } - - $embedded = array(); - - foreach ( $data['_links'] as $rel => $links ) { - $embeds = array(); - - foreach ( $links as $item ) { - // Determine if the link is embeddable. - if ( empty( $item['embeddable'] ) ) { - // Ensure we keep the same order. - $embeds[] = array(); - continue; - } - - // Run through our internal routing and serve. - $request = WP_REST_Request::from_url( $item['href'] ); - if ( ! $request ) { - $embeds[] = array(); - continue; - } - - // Embedded resources get passed context=embed. - if ( empty( $request['context'] ) ) { - $request['context'] = 'embed'; - } - - $response = $this->dispatch( $request ); - - /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ - $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request ); - - $embeds[] = $this->response_to_data( $response, false ); - } - - // Determine if any real links were found. - $has_links = count( array_filter( $embeds ) ); - - if ( $has_links ) { - $embedded[ $rel ] = $embeds; - } - } - - if ( ! empty( $embedded ) ) { - $data['_embedded'] = $embedded; - } - - return $data; - } - - /** - * Wraps the response in an envelope. - * - * The enveloping technique is used to work around browser/client - * compatibility issues. Essentially, it converts the full HTTP response to - * data instead. - * - * @since 4.4.0 - * - * @param WP_REST_Response $response Response object. - * @param bool $embed Whether links should be embedded. - * @return WP_REST_Response New response with wrapped data - */ - public function envelope_response( $response, $embed ) { - $envelope = array( - 'body' => $this->response_to_data( $response, $embed ), - 'status' => $response->get_status(), - 'headers' => $response->get_headers(), - ); - - /** - * Filters the enveloped form of a response. - * - * @since 4.4.0 - * - * @param array $envelope Envelope data. - * @param WP_REST_Response $response Original response data. - */ - $envelope = apply_filters( 'rest_envelope_response', $envelope, $response ); - - // Ensure it's still a response and return. - return rest_ensure_response( $envelope ); - } - - /** - * Registers a route to the server. - * - * @since 4.4.0 - * - * @param string $namespace Namespace. - * @param string $route The REST route. - * @param array $route_args Route arguments. - * @param bool $override Optional. Whether the route should be overridden if it already exists. - * Default false. - */ - public function register_route( $namespace, $route, $route_args, $override = false ) { - if ( ! isset( $this->namespaces[ $namespace ] ) ) { - $this->namespaces[ $namespace ] = array(); - - $this->register_route( - $namespace, - '/' . $namespace, - array( - array( - 'methods' => self::READABLE, - 'callback' => array( $this, 'get_namespace_index' ), - 'args' => array( - 'namespace' => array( - 'default' => $namespace, - ), - 'context' => array( - 'default' => 'view', - ), - ), - ), - ) - ); - } - - // Associative to avoid double-registration. - $this->namespaces[ $namespace ][ $route ] = true; - $route_args['namespace'] = $namespace; - - if ( $override || empty( $this->endpoints[ $route ] ) ) { - $this->endpoints[ $route ] = $route_args; - } else { - $this->endpoints[ $route ] = array_merge( $this->endpoints[ $route ], $route_args ); - } - } - - /** - * Retrieves the route map. - * - * The route map is an associative array with path regexes as the keys. The - * value is an indexed array with the callback function/method as the first - * item, and a bitmask of HTTP methods as the second item (see the class - * constants). - * - * Each route can be mapped to more than one callback by using an array of - * the indexed arrays. This allows mapping e.g. GET requests to one callback - * and POST requests to another. - * - * Note that the path regexes (array keys) must have @ escaped, as this is - * used as the delimiter with preg_match() - * - * @since 4.4.0 - * - * @return array `'/path/regex' => array( $callback, $bitmask )` or - * `'/path/regex' => array( array( $callback, $bitmask ), ...)`. - */ - public function get_routes() { - - /** - * Filters the array of available endpoints. - * - * @since 4.4.0 - * - * @param array $endpoints The available endpoints. An array of matching regex patterns, each mapped - * to an array of callbacks for the endpoint. These take the format - * `'/path/regex' => array( $callback, $bitmask )` or - * `'/path/regex' => array( array( $callback, $bitmask ). - */ - $endpoints = apply_filters( 'rest_endpoints', $this->endpoints ); - - // Normalise the endpoints. - $defaults = array( - 'methods' => '', - 'accept_json' => false, - 'accept_raw' => false, - 'show_in_index' => true, - 'args' => array(), - ); - - foreach ( $endpoints as $route => &$handlers ) { - - if ( isset( $handlers['callback'] ) ) { - // Single endpoint, add one deeper. - $handlers = array( $handlers ); - } - - if ( ! isset( $this->route_options[ $route ] ) ) { - $this->route_options[ $route ] = array(); - } - - foreach ( $handlers as $key => &$handler ) { - - if ( ! is_numeric( $key ) ) { - // Route option, move it to the options. - $this->route_options[ $route ][ $key ] = $handler; - unset( $handlers[ $key ] ); - continue; - } - - $handler = wp_parse_args( $handler, $defaults ); - - // Allow comma-separated HTTP methods. - if ( is_string( $handler['methods'] ) ) { - $methods = explode( ',', $handler['methods'] ); - } elseif ( is_array( $handler['methods'] ) ) { - $methods = $handler['methods']; - } else { - $methods = array(); - } - - $handler['methods'] = array(); - - foreach ( $methods as $method ) { - $method = strtoupper( trim( $method ) ); - $handler['methods'][ $method ] = true; - } - } - } - - return $endpoints; - } - - /** - * Retrieves namespaces registered on the server. - * - * @since 4.4.0 - * - * @return array List of registered namespaces. - */ - public function get_namespaces() { - return array_keys( $this->namespaces ); - } - - /** - * Retrieves specified options for a route. - * - * @since 4.4.0 - * - * @param string $route Route pattern to fetch options for. - * @return array|null Data as an associative array if found, or null if not found. - */ - public function get_route_options( $route ) { - if ( ! isset( $this->route_options[ $route ] ) ) { - return null; - } - - return $this->route_options[ $route ]; - } - - /** - * Matches the request to a callback and call it. - * - * @since 4.4.0 - * - * @param WP_REST_Request $request Request to attempt dispatching. - * @return WP_REST_Response Response returned by the callback. - */ - public function dispatch( $request ) { - /** - * Filters the pre-calculated result of a REST dispatch request. - * - * Allow hijacking the request before dispatching by returning a non-empty. The returned value - * will be used to serve the request instead. - * - * @since 4.4.0 - * - * @param mixed $result Response to replace the requested version with. Can be anything - * a normal endpoint can return, or null to not hijack the request. - * @param WP_REST_Server $this Server instance. - * @param WP_REST_Request $request Request used to generate the response. - */ - $result = apply_filters( 'rest_pre_dispatch', null, $this, $request ); - - if ( ! empty( $result ) ) { - return $result; - } - - $method = $request->get_method(); - $path = $request->get_route(); - - foreach ( $this->get_routes() as $route => $handlers ) { - $match = preg_match( '@^' . $route . '$@i', $path, $matches ); - - if ( ! $match ) { - continue; - } - - $args = array(); - foreach ( $matches as $param => $value ) { - if ( ! is_int( $param ) ) { - $args[ $param ] = $value; - } - } - - foreach ( $handlers as $handler ) { - $callback = $handler['callback']; - $response = null; - - // Fallback to GET method if no HEAD method is registered. - $checked_method = $method; - if ( 'HEAD' === $method && empty( $handler['methods']['HEAD'] ) ) { - $checked_method = 'GET'; - } - if ( empty( $handler['methods'][ $checked_method ] ) ) { - continue; - } - - if ( ! is_callable( $callback ) ) { - $response = new WP_Error( 'rest_invalid_handler', __( 'The handler for the route is invalid' ), array( 'status' => 500 ) ); - } - - if ( ! is_wp_error( $response ) ) { - // Remove the redundant preg_match argument. - unset( $args[0] ); - - $request->set_url_params( $args ); - $request->set_attributes( $handler ); - - $defaults = array(); - - foreach ( $handler['args'] as $arg => $options ) { - if ( isset( $options['default'] ) ) { - $defaults[ $arg ] = $options['default']; - } - } - - $request->set_default_params( $defaults ); - - $check_required = $request->has_valid_params(); - if ( is_wp_error( $check_required ) ) { - $response = $check_required; - } else { - $check_sanitized = $request->sanitize_params(); - if ( is_wp_error( $check_sanitized ) ) { - $response = $check_sanitized; - } - } - } - - /** - * Filters the response before executing any REST API callbacks. - * - * Allows plugins to perform additional validation after a - * request is initialized and matched to a registered route, - * but before it is executed. - * - * Note that this filter will not be called for requests that - * fail to authenticate or match to a registered route. - * - * @since 4.7.0 - * - * @param WP_HTTP_Response|WP_Error $response Result to send to the client. Usually a WP_REST_Re