[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/ -> class-wp-rest-server.php (summary)

REST API: WP_REST_Server class

File Size: 2206 lines (67 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

WP_REST_Server:: (39 methods):
  __construct()
  check_authentication()
  error_to_response()
  json_error()
  get_json_encode_options()
  serve_request()
  response_to_data()
  get_response_links()
  get_target_hints_for_link()
  get_compact_response_links()
  embed_links()
  envelope_response()
  register_route()
  get_routes()
  get_namespaces()
  get_route_options()
  resolve_data_processing()
  dispatch()
  is_dispatching()
  match_request_to_handler()
  find_handler_for_request()
  respond_to_request()
  get_json_last_error()
  get_index()
  add_active_theme_link_to_index()
  add_site_logo_to_index()
  add_site_icon_to_index()
  add_image_to_index()
  get_namespace_index()
  get_data_for_routes()
  get_data_for_route()
  get_max_batch_size()
  serve_batch_request_v1()
  set_status()
  send_header()
  send_headers()
  remove_header()
  get_raw_data()
  get_headers()


Class: WP_REST_Server  - X-Ref

Core class used to implement the WordPress REST API server.

The aliases below describe a route handler as {@see WP_REST_Server::get_routes()}
returns it, once the defaults have been filled in and the methods normalized.

An endpoint argument is a JSON Schema fragment, so the keys named here are only the
ones WordPress reads itself. The rest are schema keywords, left to the open end of
the shape and validated by {@see rest_validate_value_from_schema()}. See
{@see rest_get_allowed_schema_keywords()} for the vocabulary the REST API exposes.

__construct()   X-Ref
Instantiates the REST server.


check_authentication()   X-Ref
Checks the authentication headers if supplied.

return: WP_Error|null|true WP_Error if authentication error occurred, null if authentication

error_to_response( $error )   X-Ref
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 behavior, as it is represented as a
list in JSON rather than an object/map.

param: WP_Error $error WP_Error instance.
return: WP_REST_Response List of associative arrays with code and message keys.

json_error( $code, $message, $status = null )   X-Ref
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.

param: string   $code    WP_Error-style code.
param: string   $message Human-readable message.
param: int|null $status  Optional. HTTP status code to send. Default null.
return: string JSON representation of the error.

get_json_encode_options( WP_REST_Request $request )   X-Ref
Gets the encoding options passed to {@see wp_json_encode}.

param: \WP_REST_Request $request The current request object.
return: int The JSON encode options.

serve_request( $path = null )   X-Ref
Handles serving a REST 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.

param: string|null $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used.
return: null|false Null if not served and a HEAD request, false otherwise.

response_to_data( $response, $embed )   X-Ref
Converts a response to data to send.

param: WP_REST_Response $response Response object.
param: bool|string[]    $embed    Whether to embed all links, a filtered list of link relations, or no links.
return: mixed Data with sub-requests embedded: an array with the links under `_links` and the embedded

get_response_links( $response )   X-Ref
Retrieves links from a response.

Extracts the links from a response into a structured hash, suitable for
direct output.

param: WP_REST_Response $response Response to extract links from.
return: array Map of link relation to list of link hashes.

get_target_hints_for_link( $link )   X-Ref
Gets the target hints for a REST API Link.

param: array<string, mixed> $link The link to get target hints for.
return: array<string, string[]>|null Map of hint name to list of values, which is empty if the route

get_compact_response_links( $response )   X-Ref
Retrieves the CURIEs (compact URIs) used for relations.

Extracts the links from a response into a structured hash, suitable for
direct output.

param: WP_REST_Response $response Response to extract links from.
return: array Map of link relation to list of link hashes.

embed_links( $data, $embed = true )   X-Ref
Embeds the links from the data into the request.

param: array         $data  Data from the request.
param: bool|string[] $embed Whether to embed all links or a filtered list of link relations.
return: array {

envelope_response( $response, $embed )   X-Ref
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.

param: WP_REST_Response $response Response object.
param: bool|string[]    $embed    Whether to embed all links, a filtered list of link relations, or no links.
return: WP_REST_Response New response with wrapped data

register_route( $route_namespace, $route, $route_args, $override = false )   X-Ref
Registers a route to the server.

param: string $route_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.

get_routes( $route_namespace = '' )   X-Ref
Retrieves the route map.

The route map is an associative array with path regexes as the keys. The value
is an indexed array of the handlers registered for that route, each of them an
associative array of endpoint arguments: the callback, its permission callback,
the arguments it accepts, and the HTTP methods it responds to as a map of method
name to true.

Each route can be mapped to more than one handler. This allows mapping e.g. GET
requests to one callback and POST requests to another.

Route options, the non-numeric keys a route is registered with such as `schema`
and `namespace`, are not returned here. They are moved to
{@see WP_REST_Server::get_route_options()}.

Note that the path regexes (array keys) must have @ escaped, as this is
used as the delimiter with preg_match()

param: string $route_namespace Optionally, only return routes in the given namespace.
return: array Route map as `'/path/regex' => array( $handler, ... )`, where each

get_namespaces()   X-Ref
Retrieves namespaces registered on the server.

return: string[] List of registered namespaces.

get_route_options( $route )   X-Ref
Retrieves specified options for a route.

param: string $route Route pattern to fetch options for.
return: array|null Data as an associative array if found, or null if not found.

resolve_data_processing( $handler, $route_options = null )   X-Ref
Resolves the data processing settings of a route handler.

A route, or one of its handlers, sets the `data_processing` option to serve its data as returned.
The option is `false` to turn off every feature, or an array with any of these keys:

- `fields`: Whether the `_fields` parameter filters the response data.
- `embed`:  Whether the `_embed` parameter embeds linked resources in the response data.
- `links`:  Whether the response links are added to the response data as `_links`.

Each feature is resolved on its own: the handler setting applies, then the route setting, and the
feature stays on when neither sets it. Embedding needs the links, so `embed` is off whenever `links`
is off.

The option covers these three features only. Authentication, permission checks, status codes, headers
and JSON encoding apply as usual, and keys of the data itself, such as its own `_links`, stay as returned.

The server resolves the settings when it matches a request, and sets them on the request attributes,
on the matched handler and on the response, for a response returned by the `rest_pre_dispatch` filter too.

param: array      $handler       The route handler.
param: array|null $route_options Optional. The options of the handler's route. Default null.
return: array {

dispatch( $request )   X-Ref
Matches the request to a callback and call it.

param: WP_REST_Request $request Request to attempt dispatching.
return: WP_REST_Response Response returned by the callback.

is_dispatching()   X-Ref
Returns whether the REST server is currently dispatching / responding to a request.

This may be a standalone REST API request, or an internal request dispatched from within a regular page load.

return: bool Whether the REST server is currently handling a request.

match_request_to_handler( $request )   X-Ref
Matches a request object to its handler.

param: WP_REST_Request $request The request object.
return: array<int, string|array<string, mixed>>|WP_Error The route and request handler on success or a WP_Error instance if no handler was found.

find_handler_for_request( $request )   X-Ref
Finds the route and the handler that a request matches, without changing the request.

The handler carries its resolved data processing settings under `data_processing`.

param: WP_REST_Request $request The request object.
return: array|null The route, the handler with its resolved data processing settings, and the URL

respond_to_request( $request, $route, $handler, $response )   X-Ref
Dispatches the request to the callback handler.

param: WP_REST_Request      $request  The request object.
param: string               $route    The matched route regex.
param: array<string, mixed> $handler  The matched route handler.
param: WP_Error|null        $response The current error object if any.
return: WP_REST_Response

get_json_last_error()   X-Ref
Returns if an error occurred during most recent JSON encode/decode.

Strings to be translated will be in format like
"Encoding error: Maximum stack depth exceeded".

return: false|string Boolean false or string error message.

get_index( $request )   X-Ref
Retrieves the site index.

This endpoint describes the capabilities of the site.

param: WP_REST_Request $request Request data.
return: WP_REST_Response The API root index data.

add_active_theme_link_to_index( WP_REST_Response $response )   X-Ref
Adds a link to the active theme for users who have proper permissions.

param: WP_REST_Response $response REST API response.

add_site_logo_to_index( WP_REST_Response $response )   X-Ref
Exposes the site logo through the WordPress REST API.

This is used for fetching this information when user has no rights
to update settings.

param: WP_REST_Response $response REST API response.

add_site_icon_to_index( WP_REST_Response $response )   X-Ref
Exposes the site icon through the WordPress REST API.

This is used for fetching this information when user has no rights
to update settings.

param: WP_REST_Response $response REST API response.

add_image_to_index( WP_REST_Response $response, $image_id, $type )   X-Ref
Exposes an image through the WordPress REST API.
This is used for fetching this information when user has no rights
to update settings.

param: WP_REST_Response $response REST API response.
param: int              $image_id Image attachment ID.
param: string           $type     Type of Image.

get_namespace_index( $request )   X-Ref
Retrieves the index for a namespace.

param: WP_REST_Request $request REST request instance.
return: WP_REST_Response|WP_Error WP_REST_Response instance if the index was found,

get_data_for_routes( $routes, $context = 'view' )   X-Ref
Retrieves the publicly-visible data for routes.

param: array  $routes  Routes to get data for.
param: string $context Optional. Context for data. Accepts 'view' or 'help'. Default 'view'.
return: array[] Route data to expose in indexes, keyed by route.

get_data_for_route( $route, $callbacks, $context = 'view' )   X-Ref
Retrieves publicly-visible data for the route.

param: string $route     Route to get data for.
param: array  $callbacks Callbacks to convert to data.
param: string $context   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.

get_max_batch_size()   X-Ref
Gets the maximum number of requests that can be included in a batch.

return: int The maximum requests.

serve_batch_request_v1( WP_REST_Request $batch_request )   X-Ref
Serves the batch/v1 request.

param: WP_REST_Request $batch_request The batch request object.
return: WP_REST_Response The generated response object.

set_status( $code )   X-Ref
Sends an HTTP status code.

param: int $code HTTP status.

send_header( $key, $value )   X-Ref
Sends an HTTP header.

param: string $key   Header key.
param: string $value Header value.

send_headers( $headers )   X-Ref
Sends multiple HTTP headers.

param: array<string, string> $headers Map of header name to header value.

remove_header( $key )   X-Ref
Removes an HTTP header from the current response.

param: string $key Header key.

get_raw_data()   X-Ref
Retrieves the raw request entity (body).

return: string Raw request data.

get_headers( $server )   X-Ref
Extracts headers from a PHP-style $_SERVER array.

param: array $server Associative array similar to `$_SERVER`.
return: array Headers extracted from the input.



Generated : Sun Nov 22 08:20:01 2026 Cross-referenced by PHPXref