Class WC_API_Server

Package: WooCommerce\API
Author: WooThemes
Located at includes/api/class-wc-api-server.php

Methods summary

public WC_API_Server
# __construct( mixed $path )

Setup class and set request/response handler

Setup class and set request/response handler

Parameters

$path
mixed
$path

Returns

WC_API_Server

Since

2.1
public WP_User|WP_Error
# check_authentication( )

Check authentication for the request

Check authentication for the request

Returns

WP_User|WP_Error
WP_User object indicates successful login, WP_Error indicates unsuccessful login

Since

2.1
protected array
# error_to_array( WP_Error $error )

Convert an error to an array

Convert an error to an 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

Parameters

$error
WP_Error
$error

Returns

array
List of associative arrays with code and message keys

Since

2.1
public
# serve_request( )

Handle serving an API request

Handle 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

2.1

Uses

WC_API_Server::dispatch()
public array
# get_routes( )

Retrieve the route map

Retrieve 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()

Returns

array
'/path/regex' => array( $callback, $bitmask ) or '/path/regex' => array( array( $callback, $bitmask ), ...)

Since

2.1
public mixed
# dispatch( )

Match the request to a callback and call it

Match the request to a callback and call it

Returns

mixed
The value returned by the callback, or a WP_Error instance

Since

2.1

Used by

WC_API_Server::serve_request()
protected array
# sort_callback_params( callable|array $callback, array $provided )

Sort parameters by order specified in method declaration

Sort parameters by order specified in method declaration

Takes a callback and a list of available params, then filters and sorts by the parameters the method actually needs, using the Reflection API

Parameters

$callback
callable|array
$callback the endpoint callback
$provided
array
$provided the provided request parameters

Returns

array

Since

2.1
public array
# get_index( )

Get the site index.

Get the site index.

This endpoint describes the capabilities of the site.

Returns

array
Index entity

Since

2.1
public
# send_status( integer $code )

Send a HTTP status code

Send a HTTP status code

Parameters

$code
integer
$code HTTP status

Since

2.1
public
# header( string $key, string $value, boolean $replace = true )

Send a HTTP header

Send a HTTP header

Parameters

$key
string
$key Header key
$value
string
$value Header value
$replace
boolean
$replace Should we replace the existing header?

Since

2.1
public
# link_header( string $rel, string $link, array $other = array() )

Send a Link header

Send a Link header

Parameters

$rel
string
$rel Link relation. Either a registered type, or an absolute URL
$link
string
$link Target IRI for the link
$other
array
$other Other parameters to send, as an associative array

Since

2.1

Link

http://tools.ietf.org/html/rfc5988
http://www.iana.org/assignments/link-relations/link-relations.xml
public
# add_pagination_headers( WP_Query|WP_User_Query $query )

Send pagination headers for resources

Send pagination headers for resources

Parameters

$query
WP_Query|WP_User_Query
$query

Since

2.1
public string
# get_raw_data( )

Retrieve the raw request entity (body)

Retrieve the raw request entity (body)

Returns

string

Since

2.1
public string
# parse_datetime( string $datetime )

Parse an RFC3339 datetime into a MySQl datetime

Parse an RFC3339 datetime into a MySQl datetime

Invalid dates default to unix epoch

Parameters

$datetime
string
$datetime RFC3339 datetime

Returns

string
MySQl datetime (YYYY-MM-DD HH:MM:SS)

Since

2.1
public string
# format_datetime( integer|string $timestamp, boolean $convert_to_utc = false )

Format a unix timestamp or MySQL datetime into an RFC3339 datetime

Format a unix timestamp or MySQL datetime into an RFC3339 datetime

Parameters

$timestamp
integer|string
$timestamp unix timestamp or MySQL datetime
$convert_to_utc
boolean
$convert_to_utc

Returns

string
RFC3339 datetime

Since

2.1
public array
# get_headers( array $server )

Extract headers from a PHP-style $_SERVER array

Extract headers from a PHP-style $_SERVER array

Parameters

$server
array
$server Associative array similar to $_SERVER

Returns

array
Headers extracted from the input

Since

2.1

Magic methods summary

Constants summary

integer METHOD_GET 1
#
integer METHOD_POST 2
#
integer METHOD_PUT 4
#
integer METHOD_PATCH 8
#
integer METHOD_DELETE 16
#
integer READABLE 1
#
integer CREATABLE 2
#
integer EDITABLE 14
#
integer DELETABLE 16
#
integer ALLMETHODS 31
#
integer ACCEPT_RAW_DATA 64
#

Does the endpoint accept a raw request body?

Does the endpoint accept a raw request body?

integer ACCEPT_DATA 128
#

Does the endpoint accept a request body? (either JSON or XML)

Does the endpoint accept a request body? (either JSON or XML)

integer HIDDEN_ENDPOINT 256
#

Should we hide this endpoint from the index?

Should we hide this endpoint from the index?

Properties summary

public static array $method_map
#

Map of HTTP verbs to constants

Map of HTTP verbs to constants

public string $path
#

Requested path (relative to the API root, wp-json.php)

Requested path (relative to the API root, wp-json.php)

public string $method
#

Requested method (GET/HEAD/POST/PUT/PATCH/DELETE)

Requested method (GET/HEAD/POST/PUT/PATCH/DELETE)

public array $params
#

Request parameters

Request parameters

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

public array $headers
#

Request headers

Request headers

public array $files
#

Request files (matches $_FILES)

Request files (matches $_FILES)

public WC_API_Handler $handler
#

Request/Response handler, either JSON by default or XML if requested by client

Request/Response handler, either JSON by default or XML if requested by client

WooCommerce API documentation generated by ApiGen 2.8.0