PHP Class WC_API_Server

Datei anzeigen Open project: woocommerce/woocommerce Class Usage Examples

Public Properties

Property Type Description
$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)

Public Methods

Method Description
__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

Protected Methods

Method Description
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.

Private Methods

Method Description
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

Method Details

__construct() public method

Setup class and set request/response handler
Since: 2.1
public __construct ( $path ) : WC_API_Server
$path
return WC_API_Server

add_pagination_headers() public method

Send pagination headers for resources
Since: 2.1
public add_pagination_headers ( WP_Query | WP_User_Query $query )
$query WP_Query | WP_User_Query

add_pagination_headers() public method

Send pagination headers for resources
Since: 2.1
public add_pagination_headers ( WP_Query | WP_User_Query | stdClass $query )
$query WP_Query | WP_User_Query | stdClass

check_authentication() public method

Check authentication for the request
Since: 2.1
public check_authentication ( ) : WP_User | WP_Error
return WP_User | WP_Error WP_User object indicates successful login, WP_Error indicates unsuccessful login

dispatch() public method

Match the request to a callback and call it
Since: 2.1
public dispatch ( ) : mixed
return mixed The value returned by the callback, or a WP_Error instance

error_to_array() protected method

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: 2.1
protected error_to_array ( WP_Error $error ) : array
$error WP_Error
return array List of associative arrays with code and message keys

format_datetime() public method

Format a unix timestamp or MySQL datetime into an RFC3339 datetime
Since: 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.
return string RFC3339 datetime

get_headers() public method

Extract headers from a PHP-style $_SERVER array
Since: 2.1
public get_headers ( array $server ) : array
$server array Associative array similar to $_SERVER
return array Headers extracted from the input

get_index() public method

This endpoint describes the capabilities of the site.
Since: 2.1
public get_index ( ) : array
return array Index entity

get_raw_data() public method

Retrieve the raw request entity (body)
Since: 2.1
public get_raw_data ( ) : string
return string

get_routes() public method

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: 2.1
public get_routes ( ) : array
return array `'/path/regex' => array( $callback, $bitmask )` or `'/path/regex' => array( array( $callback, $bitmask ), ...)`

header() public method

Send a HTTP header
Since: 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() public method

Invalid dates default to unix epoch
Since: 2.1
public parse_datetime ( string $datetime ) : string
$datetime string RFC3339 datetime
return string MySQl datetime (YYYY-MM-DD HH:MM:SS)

send_status() public method

Send a HTTP status code
Since: 2.1
public send_status ( integer $code )
$code integer HTTP status

serve_request() public method

Matches the current server URI to a route and runs the first matching callback then outputs a JSON representation of the returned value.
Since: 2.1
public serve_request ( )

sort_callback_params() protected method

Takes a callback and a list of available params, then filters and sorts by the parameters the method actually needs, using the Reflection API
Since: 2.1
protected sort_callback_params ( callable | array $callback, array $provided ) : array
$callback callable | array the endpoint callback
$provided array the provided request parameters
return array

urldecode_deep() protected method

urldecode deep.
Since: 2.2
protected urldecode_deep ( string/array $value ) : string/array
$value string/array
return string/array

Property Details

$files public_oe property

Request files (matches $_FILES)
public array $files
return array

$handler public_oe property

Request/Response handler, either JSON by default or XML if requested by client
public WC_API_Handler $handler
return WC_API_Handler

$headers public_oe property

Request headers
public array $headers
return array

$method public_oe property

Requested method (GET/HEAD/POST/PUT/PATCH/DELETE)
public string $method
return string

$method_map public_oe static_oe property

Map of HTTP verbs to constants
public static array $method_map
return array

$params public_oe property

This acts as an abstraction of the superglobals (GET => $_GET, POST => $_POST)
public array $params
return array

$path public_oe property

Requested path (relative to the API root, wp-json.php)
public string $path
return string