PHP Класс WC_API_Server

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

Открытые свойства

Свойство Тип Описание
$files array Request files (matches $_FILES)
$handler WC_API_Handler Request/Response handler, either JSON by default or XML if requested by client
$headers array Request headers
$method string Requested method (GET/HEAD/POST/PUT/PATCH/DELETE)
$method_map array Map of HTTP verbs to constants
$params array This acts as an abstraction of the superglobals (GET => $_GET, POST => $_POST)
$path string Requested path (relative to the API root, wp-json.php)

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

Метод Описание
__construct ( $path ) : WC_API_Server Setup class and set request/response handler
add_pagination_headers ( WP_Query | WP_User_Query $query ) Send pagination headers for resources
add_pagination_headers ( WP_Query | WP_User_Query | stdClass $query ) Send pagination headers for resources
check_authentication ( ) : WP_User | WP_Error Check authentication for the request
dispatch ( ) : mixed Match the request to a callback and call it
format_datetime ( integer | string $timestamp, boolean $convert_to_utc = false, boolean $convert_to_gmt = false ) : string Format a unix timestamp or MySQL datetime into an RFC3339 datetime
get_headers ( array $server ) : array Extract headers from a PHP-style $_SERVER array
get_index ( ) : array Get the site index.
get_raw_data ( ) : string Retrieve the raw request entity (body)
get_routes ( ) : array Retrieve the route map
header ( string $key, string $value, boolean $replace = true ) Send a HTTP header
link_header ( string $rel, string $link, array $other = [] ) Send a Link header
parse_datetime ( string $datetime ) : string Parse an RFC3339 datetime into a MySQl datetime
send_status ( integer $code ) Send a HTTP status code
serve_request ( ) Handle serving an API request

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

Метод Описание
error_to_array ( WP_Error $error ) : array Convert an error to an array
sort_callback_params ( callable | array $callback, array $provided ) : array Sort parameters by order specified in method declaration
urldecode_deep ( string/array $value ) : string/array urldecode deep.

Приватные методы

Метод Описание
get_paginated_url ( integer $page ) : string Returns the request URL with the page query parameter set to the specified page
is_json_request ( ) : boolean Check if the current request accepts a JSON response by checking the endpoint suffix (.json) or the HTTP ACCEPT header
is_xml_request ( ) : boolean Check if the current request accepts an XML response by checking the endpoint suffix (.xml) or the HTTP ACCEPT header

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

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

Setup class and set request/response handler
С версии: 2.1
public __construct ( $path ) : WC_API_Server
$path
Результат WC_API_Server

add_pagination_headers() публичный Метод

Send pagination headers for resources
С версии: 2.1
public add_pagination_headers ( WP_Query | WP_User_Query $query )
$query WP_Query | WP_User_Query

add_pagination_headers() публичный Метод

Send pagination headers for resources
С версии: 2.1
public add_pagination_headers ( WP_Query | WP_User_Query | stdClass $query )
$query WP_Query | WP_User_Query | stdClass

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

Check authentication for the request
С версии: 2.1
public check_authentication ( ) : WP_User | WP_Error
Результат WP_User | WP_Error WP_User object indicates successful login, WP_Error indicates unsuccessful login

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

Match the request to a callback and call it
С версии: 2.1
public dispatch ( ) : mixed
Результат mixed The value returned by the callback, or a WP_Error instance

error_to_array() защищенный Метод

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

format_datetime() публичный Метод

Format a unix timestamp or MySQL datetime into an RFC3339 datetime
С версии: 2.1
public format_datetime ( integer | string $timestamp, boolean $convert_to_utc = false, boolean $convert_to_gmt = false ) : string
$timestamp integer | string unix timestamp or MySQL datetime
$convert_to_utc boolean
$convert_to_gmt boolean Use GMT timezone.
Результат string RFC3339 datetime

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

Extract headers from a PHP-style $_SERVER array
С версии: 2.1
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.
С версии: 2.1
public get_index ( ) : array
Результат array Index entity

get_raw_data() публичный Метод

Retrieve the raw request entity (body)
С версии: 2.1
public get_raw_data ( ) : string
Результат string

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

header() публичный Метод

Send a HTTP header
С версии: 2.1
public header ( string $key, string $value, boolean $replace = true )
$key string Header key
$value string Header value
$replace boolean Should we replace the existing header?

parse_datetime() публичный Метод

Invalid dates default to unix epoch
С версии: 2.1
public parse_datetime ( string $datetime ) : string
$datetime string RFC3339 datetime
Результат string MySQl datetime (YYYY-MM-DD HH:MM:SS)

send_status() публичный Метод

Send a HTTP status code
С версии: 2.1
public send_status ( integer $code )
$code integer HTTP status

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.
С версии: 2.1
public serve_request ( )

sort_callback_params() защищенный Метод

Takes a callback and a list of available params, then filters and sorts by the parameters the method actually needs, using the Reflection API
С версии: 2.1
protected sort_callback_params ( callable | array $callback, array $provided ) : array
$callback callable | array the endpoint callback
$provided array the provided request parameters
Результат array

urldecode_deep() защищенный Метод

urldecode deep.
С версии: 2.2
protected urldecode_deep ( string/array $value ) : string/array
$value string/array
Результат string/array

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

$files публичное свойство

Request files (matches $_FILES)
public array $files
Результат array

$handler публичное свойство

Request/Response handler, either JSON by default or XML if requested by client
public WC_API_Handler $handler
Результат WC_API_Handler

$headers публичное свойство

Request headers
public array $headers
Результат array

$method публичное свойство

Requested method (GET/HEAD/POST/PUT/PATCH/DELETE)
public string $method
Результат string

$method_map публичное статическое свойство

Map of HTTP verbs to constants
public static array $method_map
Результат array

$params публичное свойство

This acts as an abstraction of the superglobals (GET => $_GET, POST => $_POST)
public array $params
Результат array

$path публичное свойство

Requested path (relative to the API root, wp-json.php)
public string $path
Результат string