PHP Класс WP_REST_Server, wordpress

С версии: 4.4.0
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$endpoints array Endpoints registered to the server.
$namespaces array Namespaces registered to the server.
$route_options array Options defined for the routes.

Открытые методы

Метод Описание
__construct ( ) Instantiates the REST server.
check_authentication ( ) : WP_Error | null Checks the authentication headers if supplied.
dispatch ( WP_REST_Request $request ) : WP_REST_Response Matches the request to a callback and call it.
envelope_response ( WP_REST_Response $response, boolean $embed ) : WP_REST_Response Wraps the response in an envelope.
get_compact_response_links ( WP_REST_Response $response ) : array Retrieves the CURIEs (compact URIs) used for relations.
get_data_for_route ( string $route, array $callbacks, string $context = 'view' ) : array | null Retrieves publicly-visible data for the route.
get_data_for_routes ( array $routes, string $context = 'view' ) : array Retrieves the publicly-visible data for routes.
get_headers ( array $server ) : array Extracts headers from a PHP-style $_SERVER array.
get_index ( array $request ) : array Retrieves the site index.
get_namespace_index ( WP_REST_Request $request ) : WP_REST_Response | WP_Error Retrieves the index for a namespace.
get_namespaces ( ) : array Retrieves namespaces registered on the server.
get_raw_data ( ) : string Retrieves the raw request entity (body).
get_response_links ( WP_REST_Response $response ) : array Retrieves links from a response.
get_route_options ( string $route ) : array | null Retrieves specified options for a route.
get_routes ( ) : array Retrieves the route map.
register_route ( string $namespace, string $route, array $route_args, boolean $override = false ) Registers a route to the server.
response_to_data ( WP_REST_Response $response, boolean $embed ) : array Converts a response to data to send.
send_header ( string $key, string $value ) Sends an HTTP header.
send_headers ( array $headers ) Sends multiple HTTP headers.
serve_request ( string $path = null ) : false | null Handles serving an API request.

Защищенные методы

Метод Описание
embed_links ( array $data ) : array Embeds the links from the data into the request.
error_to_response ( WP_Error $error ) : WP_REST_Response Converts an error to a response object.
get_json_last_error ( ) : boolean | string Returns if an error occurred during most recent JSON encode/decode.
json_error ( string $code, string $message, integer $status = null ) : string Retrieves an appropriate error representation in JSON.
set_status ( integer $code ) Sends an HTTP status code.

Описание методов

__construct() публичный метод

Instantiates the REST server.
С версии: 4.4.0
public __construct ( )

check_authentication() публичный метод

Checks the authentication headers if supplied.
С версии: 4.4.0
public check_authentication ( ) : WP_Error | null
Результат WP_Error | null WP_Error indicates unsuccessful login, null indicates successful or no authentication provided

dispatch() публичный метод

Matches the request to a callback and call it.
С версии: 4.4.0
public dispatch ( WP_REST_Request $request ) : WP_REST_Response
$request WP_REST_Request Request to attempt dispatching.
Результат WP_REST_Response Response returned by the callback.

envelope_response() публичный метод

The enveloping technique is used to work around browser/client compatibility issues. Essentially, it converts the full HTTP response to data instead.
С версии: 4.4.0
public envelope_response ( WP_REST_Response $response, boolean $embed ) : WP_REST_Response
$response WP_REST_Response Response object.
$embed boolean Whether links should be embedded.
Результат WP_REST_Response New response with wrapped data

error_to_response() защищенный метод

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.
С версии: 4.4.0
protected error_to_response ( WP_Error $error ) : WP_REST_Response
$error WP_Error WP_Error instance.
Результат WP_REST_Response List of associative arrays with code and message keys.

get_data_for_route() публичный метод

Retrieves publicly-visible data for the route.
С версии: 4.4.0
public get_data_for_route ( string $route, array $callbacks, string $context = 'view' ) : array | null
$route string Route to get data for.
$callbacks array Callbacks to convert to data.
$context string Optional. Context for the data. Accepts 'view' or 'help'. Default 'view'.
Результат array | null Data for the route, or null if no publicly-visible data.

get_data_for_routes() публичный метод

Retrieves the publicly-visible data for routes.
С версии: 4.4.0
public get_data_for_routes ( array $routes, string $context = 'view' ) : array
$routes array Routes to get data for.
$context string Optional. Context for data. Accepts 'view' or 'help'. Default 'view'.
Результат array Route data to expose in indexes.

get_headers() публичный метод

Extracts headers from a PHP-style $_SERVER array.
С версии: 4.4.0
public get_headers ( array $server ) : array
$server array Associative array similar to `$_SERVER`.
Результат array Headers extracted from the input.

get_index() публичный метод

This endpoint describes the capabilities of the site.
С версии: 4.4.0
public get_index ( array $request ) : array
$request array { Request. @type string $context Context. }
Результат array Index entity

get_json_last_error() защищенный метод

Strings to be translated will be in format like "Encoding error: Maximum stack depth exceeded".
С версии: 4.4.0
protected get_json_last_error ( ) : boolean | string
Результат boolean | string Boolean false or string error message.

get_namespace_index() публичный метод

Retrieves the index for a namespace.
С версии: 4.4.0
public get_namespace_index ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request REST request instance.
Результат WP_REST_Response | WP_Error WP_REST_Response instance if the index was found, WP_Error if the namespace isn't set.

get_namespaces() публичный метод

Retrieves namespaces registered on the server.
С версии: 4.4.0
public get_namespaces ( ) : array
Результат array List of registered namespaces.

get_raw_data() публичный статический метод

Retrieves the raw request entity (body).
С версии: 4.4.0
public static get_raw_data ( ) : string
Результат string Raw request data.

get_route_options() публичный метод

Retrieves specified options for a route.
С версии: 4.4.0
public get_route_options ( string $route ) : array | null
$route string Route pattern to fetch options for.
Результат array | null Data as an associative array if found, or null if not found.

get_routes() публичный метод

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()
С версии: 4.4.0
public get_routes ( ) : array
Результат array `'/path/regex' => array( $callback, $bitmask )` or `'/path/regex' => array( array( $callback, $bitmask ), ...)`.

json_error() защищенный метод

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.
С версии: 4.4.0
protected json_error ( string $code, string $message, integer $status = null ) : string
$code string WP_Error-style code.
$message string Human-readable message.
$status integer Optional. HTTP status code to send. Default null.
Результат string JSON representation of the error

register_route() публичный метод

Registers a route to the server.
С версии: 4.4.0
public register_route ( string $namespace, string $route, array $route_args, boolean $override = false )
$namespace string Namespace.
$route string The REST route.
$route_args array Route arguments.
$override boolean Optional. Whether the route should be overridden if it already exists. Default false.

response_to_data() публичный метод

Converts a response to data to send.
С версии: 4.4.0
public response_to_data ( WP_REST_Response $response, boolean $embed ) : array
$response WP_REST_Response Response object.
$embed boolean Whether links should be embedded.
Результат array { Data with sub-requests embedded. @type array [$_links] Links. @type array [$_embedded] Embeddeds. }

send_header() публичный метод

Sends an HTTP header.
С версии: 4.4.0
public send_header ( string $key, string $value )
$key string Header key.
$value string Header value.

send_headers() публичный метод

Sends multiple HTTP headers.
С версии: 4.4.0
public send_headers ( array $headers )
$headers array Map of header name to header value.

serve_request() публичный метод

Matches the current server URI to a route and runs the first matching callback then outputs a JSON representation of the returned value.
См. также: WP_REST_Server::dispatch()
С версии: 4.4.0
public serve_request ( string $path = null ) : false | null
$path string Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used. Default null.
Результат false | null Null if not served and a HEAD request, false otherwise.

set_status() защищенный метод

Sends an HTTP status code.
С версии: 4.4.0
protected set_status ( integer $code )
$code integer HTTP status.

Описание свойств

$endpoints защищенное свойство

Endpoints registered to the server.
С версии: 4.4.0
protected array $endpoints
Результат array

$namespaces защищенное свойство

Namespaces registered to the server.
С версии: 4.4.0
protected array $namespaces
Результат array

$route_options защищенное свойство

Options defined for the routes.
С версии: 4.4.0
protected array $route_options
Результат array