REST API: Introduce baby API to the world.
Baby API was born at 2.8KLOC on October 8th at 2:30 UTC. API has lots of growing to do, so wish it the best of luck. Thanks to everyone who helped along the way: Props rmccue, rachelbaker, danielbachhuber, joehoyle, drewapicture, adamsilverstein, netweb, tlovett1, shelob9, kadamwhite, pento, westonruter, nikv, tobych, redsweater, alecuf, pollyplummer, hurtige, bpetty, oso96_2000, ericlewis, wonderboymusic, joshkadis, mordauk, jdgrimes, johnbillion, jeremyfelt, thiago-negri, jdolan, pkevan, iseulde, thenbrent, maxcutler, kwight, markoheijnen, phh, natewr, jjeaton, shprink, mattheu, quasel, jmusal, codebykat, hubdotcom, tapsboy, QWp6t, pushred, jaredcobb, justinsainton, japh, matrixik, jorbin, frozzare, codfish, michael-arestad, kellbot, ironpaperweight, simonlampen, alisspers, eliorivero, davidbhayes, JohnDittmar, dimadin, traversal, cmmarslender, Toddses, kokarn, welcher, and ericpedia. Fixes #33982. git-svn-id: https://develop.svn.wordpress.org/trunk@34928 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
374b39d6ba
commit
b39211475d
@ -204,6 +204,17 @@ add_filter( 'title_save_pre', 'trim' );
|
||||
|
||||
add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
|
||||
|
||||
// REST API filters.
|
||||
add_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
|
||||
add_action( 'wp_head', 'rest_output_link_wp_head', 10, 0 );
|
||||
add_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
|
||||
add_action( 'auth_cookie_malformed', 'rest_cookie_collect_status' );
|
||||
add_action( 'auth_cookie_expired', 'rest_cookie_collect_status' );
|
||||
add_action( 'auth_cookie_bad_username', 'rest_cookie_collect_status' );
|
||||
add_action( 'auth_cookie_bad_hash', 'rest_cookie_collect_status' );
|
||||
add_action( 'auth_cookie_valid', 'rest_cookie_collect_status' );
|
||||
add_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 );
|
||||
|
||||
// Actions
|
||||
add_action( 'wp_head', '_wp_render_title_tag', 1 );
|
||||
add_action( 'wp_head', 'wp_enqueue_scripts', 1 );
|
||||
@ -347,6 +358,11 @@ add_action( 'after_password_reset', 'wp_password_change_notification' );
|
||||
add_action( 'register_new_user', 'wp_send_new_user_notifications' );
|
||||
add_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
|
||||
|
||||
// REST API actions.
|
||||
add_action( 'init', 'rest_api_init' );
|
||||
add_action( 'rest_api_init', 'rest_api_default_filters', 10, 1 );
|
||||
add_action( 'parse_request', 'rest_api_loaded' );
|
||||
|
||||
/**
|
||||
* Filters formerly mixed into wp-includes
|
||||
*/
|
||||
|
26
src/wp-includes/rest-api.php
Normal file
26
src/wp-includes/rest-api.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API functions.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Version number for our API.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
define( 'REST_API_VERSION', '2.0' );
|
||||
|
||||
/** WP_REST_Server class */
|
||||
require_once( ABSPATH . WPINC . '/rest-api/lib/class-wp-rest-server.php' );
|
||||
|
||||
/** WP_REST_Response class */
|
||||
require_once( ABSPATH . WPINC . '/rest-api/lib/class-wp-rest-response.php' );
|
||||
|
||||
/** WP_REST_Request class */
|
||||
require_once( ABSPATH . WPINC . '/rest-api/lib/class-wp-rest-request.php' );
|
||||
|
||||
/** REST functions */
|
||||
require_once( ABSPATH . WPINC . '/rest-api/rest-functions.php' );
|
165
src/wp-includes/rest-api/class-wp-http-response.php
Normal file
165
src/wp-includes/rest-api/class-wp-http-response.php
Normal file
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API: WP_HTTP_Response class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used to prepare HTTP responses.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
class WP_HTTP_Response {
|
||||
|
||||
/**
|
||||
* Response data.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var mixed
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* Response headers.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $headers;
|
||||
|
||||
/**
|
||||
* Response status.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $status;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $data Response data. Default null.
|
||||
* @param int $status Optional. HTTP status code. Default 200.
|
||||
* @param array $headers Optional. HTTP header map. Default empty array.
|
||||
*/
|
||||
public function __construct( $data = null, $status = 200, $headers = array() ) {
|
||||
$this->data = $data;
|
||||
$this->set_status( $status );
|
||||
$this->set_headers( $headers );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves headers associated with the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Map of header name to header value.
|
||||
*/
|
||||
public function get_headers() {
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all header values.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $headers Map of header name to header value.
|
||||
*/
|
||||
public function set_headers( $headers ) {
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a single HTTP header.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Header name.
|
||||
* @param string $value Header value.
|
||||
* @param bool $replace Optional. Whether to replace an existing header of the same name.
|
||||
* Default true.
|
||||
*/
|
||||
public function header( $key, $value, $replace = true ) {
|
||||
if ( $replace || ! isset( $this->headers[ $key ] ) ) {
|
||||
$this->headers[ $key ] = $value;
|
||||
} else {
|
||||
$this->headers[ $key ] .= ', ' . $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the HTTP return code for the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return int The 3-digit HTTP status code.
|
||||
*/
|
||||
public function get_status() {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 3-digit HTTP status code.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param int $code HTTP status.
|
||||
*/
|
||||
public function set_status( $code ) {
|
||||
$this->status = absint( $code );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the response data.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return mixed Response data.
|
||||
*/
|
||||
public function get_data() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the response data.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $data Response data.
|
||||
*/
|
||||
public function set_data( $data ) {
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the response data for JSON serialization.
|
||||
*
|
||||
* It is expected that in most implementations, this will return the same as get_data(),
|
||||
* however this may be different if you want to do custom JSON data handling.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return mixed Any JSON-serializable value.
|
||||
*/
|
||||
public function jsonSerialize() {
|
||||
return $this->get_data();
|
||||
}
|
||||
}
|
930
src/wp-includes/rest-api/class-wp-rest-request.php
Normal file
930
src/wp-includes/rest-api/class-wp-rest-request.php
Normal file
@ -0,0 +1,930 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API: WP_REST_Request class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used to implement a REST request object.
|
||||
*
|
||||
* Contains data from the request, to be passed to the callback.
|
||||
*
|
||||
* Note: This implements ArrayAccess, and acts as an array of parameters when
|
||||
* used in that manner. It does not use ArrayObject (as we cannot rely on SPL),
|
||||
* so be aware it may have non-array behaviour in some cases.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see ArrayAccess
|
||||
*/
|
||||
class WP_REST_Request implements ArrayAccess {
|
||||
|
||||
/**
|
||||
* HTTP method.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $method = '';
|
||||
|
||||
/**
|
||||
* Parameters passed to the request.
|
||||
*
|
||||
* These typically come from the `$_GET`, `$_POST` and `$_FILES`
|
||||
* superglobals when being created from the global scope.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var array Contains GET, POST and FILES keys mapping to arrays of data.
|
||||
*/
|
||||
protected $params;
|
||||
|
||||
/**
|
||||
* HTTP headers for the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var array Map of key to value. Key is always lowercase, as per HTTP specification.
|
||||
*/
|
||||
protected $headers = array();
|
||||
|
||||
/**
|
||||
* Body data.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var string Binary data from the request.
|
||||
*/
|
||||
protected $body = null;
|
||||
|
||||
/**
|
||||
* Route matched for the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $route;
|
||||
|
||||
/**
|
||||
* Attributes (options) for the route that was matched.
|
||||
*
|
||||
* This is the options array used when the route was registered, typically
|
||||
* containing the callback as well as the valid methods for the route.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var array Attributes for the request.
|
||||
*/
|
||||
protected $attributes = array();
|
||||
|
||||
/**
|
||||
* Used to determine if the JSON data has been parsed yet.
|
||||
*
|
||||
* Allows lazy-parsing of JSON data where possible.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var bool
|
||||
*/
|
||||
protected $parsed_json = false;
|
||||
|
||||
/**
|
||||
* Used to determine if the body data has been parsed yet.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var bool
|
||||
*/
|
||||
protected $parsed_body = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $method Optional. Request method. Default empty.
|
||||
* @param string $route Optional. Request route. Default empty.
|
||||
* @param array $attributes Optional. Request attributes. Default empty array.
|
||||
*/
|
||||
public function __construct( $method = '', $route = '', $attributes = array() ) {
|
||||
$this->params = array(
|
||||
'URL' => array(),
|
||||
'GET' => array(),
|
||||
'POST' => array(),
|
||||
'FILES' => array(),
|
||||
|
||||
// See parse_json_params.
|
||||
'JSON' => null,
|
||||
|
||||
'defaults' => array(),
|
||||
);
|
||||
|
||||
$this->set_method( $method );
|
||||
$this->set_route( $route );
|
||||
$this->set_attributes( $attributes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the HTTP method for the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return string HTTP method.
|
||||
*/
|
||||
public function get_method() {
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets HTTP method for the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $method HTTP method.
|
||||
*/
|
||||
public function set_method( $method ) {
|
||||
$this->method = strtoupper( $method );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all headers from the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Map of key to value. Key is always lowercase, as per HTTP specification.
|
||||
*/
|
||||
public function get_headers() {
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Canonicalizes the header name.
|
||||
*
|
||||
* Ensures that header names are always treated the same regardless of
|
||||
* source. Header names are always case insensitive.
|
||||
*
|
||||
* Note that we treat `-` (dashes) and `_` (underscores) as the same
|
||||
* character, as per header parsing rules in both Apache and nginx.
|
||||
*
|
||||
* @link http://stackoverflow.com/q/18185366
|
||||
* @link http://wiki.nginx.org/Pitfalls#Missing_.28disappearing.29_HTTP_headers
|
||||
* @link http://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @static
|
||||
*
|
||||
* @param string $key Header name.
|
||||
* @return string Canonicalized name.
|
||||
*/
|
||||
public static function canonicalize_header_name( $key ) {
|
||||
$key = strtolower( $key );
|
||||
$key = str_replace( '-', '_', $key );
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the given header from the request.
|
||||
*
|
||||
* If the header has multiple values, they will be concatenated with a comma
|
||||
* as per the HTTP specification. Be aware that some non-compliant headers
|
||||
* (notably cookie headers) cannot be joined this way.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Header name, will be canonicalized to lowercase.
|
||||
* @return string|null String value if set, null otherwise.
|
||||
*/
|
||||
public function get_header( $key ) {
|
||||
$key = $this->canonicalize_header_name( $key );
|
||||
|
||||
if ( ! isset( $this->headers[ $key ] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return implode( ',', $this->headers[ $key ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves header values from the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Header name, will be canonicalized to lowercase.
|
||||
* @return array|null List of string values if set, null otherwise.
|
||||
*/
|
||||
public function get_header_as_array( $key ) {
|
||||
$key = $this->canonicalize_header_name( $key );
|
||||
|
||||
if ( ! isset( $this->headers[ $key ] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->headers[ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the header on request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Header name.
|
||||
* @param string $value Header value, or list of values.
|
||||
*/
|
||||
public function set_header( $key, $value ) {
|
||||
$key = $this->canonicalize_header_name( $key );
|
||||
$value = (array) $value;
|
||||
|
||||
$this->headers[ $key ] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a header value for the given header.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Header name.
|
||||
* @param string $value Header value, or list of values.
|
||||
*/
|
||||
public function add_header( $key, $value ) {
|
||||
$key = $this->canonicalize_header_name( $key );
|
||||
$value = (array) $value;
|
||||
|
||||
if ( ! isset( $this->headers[ $key ] ) ) {
|
||||
$this->headers[ $key ] = array();
|
||||
}
|
||||
|
||||
$this->headers[ $key ] = array_merge( $this->headers[ $key ], $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all values for a header.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Header name.
|
||||
*/
|
||||
public function remove_header( $key ) {
|
||||
unset( $this->headers[ $key ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets headers on the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $headers Map of header name to value.
|
||||
* @param bool $override If true, replace the request's headers. Otherwise, merge with existing.
|
||||
*/
|
||||
public function set_headers( $headers, $override = true ) {
|
||||
if ( true === $override ) {
|
||||
$this->headers = array();
|
||||
}
|
||||
|
||||
foreach ( $headers as $key => $value ) {
|
||||
$this->set_header( $key, $value );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the content-type of the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Map containing 'value' and 'parameters' keys.
|
||||
*/
|
||||
public function get_content_type() {
|
||||
$value = $this->get_header( 'content-type' );
|
||||
if ( empty( $value ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parameters = '';
|
||||
if ( strpos( $value, ';' ) ) {
|
||||
list( $value, $parameters ) = explode( ';', $value, 2 );
|
||||
}
|
||||
|
||||
$value = strtolower( $value );
|
||||
if ( strpos( $value, '/' ) === false ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Parse type and subtype out.
|
||||
list( $type, $subtype ) = explode( '/', $value, 2 );
|
||||
|
||||
$data = compact( 'value', 'type', 'subtype', 'parameters' );
|
||||
$data = array_map( 'trim', $data );
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the parameter priority order.
|
||||
*
|
||||
* Used when checking parameters in get_param().
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array List of types to check, in order of priority.
|
||||
*/
|
||||
protected function get_parameter_order() {
|
||||
$order = array();
|
||||
$order[] = 'JSON';
|
||||
|
||||
$this->parse_json_params();
|
||||
|
||||
// Ensure we parse the body data.
|
||||
$body = $this->get_body();
|
||||
if ( $this->method !== 'POST' && ! empty( $body ) ) {
|
||||
$this->parse_body_params();
|
||||
}
|
||||
|
||||
$accepts_body_data = array( 'POST', 'PUT', 'PATCH' );
|
||||
if ( in_array( $this->method, $accepts_body_data ) ) {
|
||||
$order[] = 'POST';
|
||||
}
|
||||
|
||||
$order[] = 'GET';
|
||||
$order[] = 'URL';
|
||||
$order[] = 'defaults';
|
||||
|
||||
/**
|
||||
* Filter the parameter order.
|
||||
*
|
||||
* The order affects which parameters are checked when using get_param() and family.
|
||||
* This acts similarly to PHP's `request_order` setting.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param array $order {
|
||||
* An array of types to check, in order of priority.
|
||||
*
|
||||
* @param string $type The type to check.
|
||||
* }
|
||||
* @param WP_REST_Request $this The request object.
|
||||
*/
|
||||
return apply_filters( 'rest_request_parameter_order', $order, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a parameter from the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Parameter name.
|
||||
* @return mixed|null Value if set, null otherwise.
|
||||
*/
|
||||
public function get_param( $key ) {
|
||||
$order = $this->get_parameter_order();
|
||||
|
||||
foreach ( $order as $type ) {
|
||||
// Determine if we have the parameter for this type.
|
||||
if ( isset( $this->params[ $type ][ $key ] ) ) {
|
||||
return $this->params[ $type ][ $key ];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a parameter on the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $key Parameter name.
|
||||
* @param mixed $value Parameter value.
|
||||
*/
|
||||
public function set_param( $key, $value ) {
|
||||
switch ( $this->method ) {
|
||||
case 'POST':
|
||||
$this->params['POST'][ $key ] = $value;
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->params['GET'][ $key ] = $value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves merged parameters from the request.
|
||||
*
|
||||
* The equivalent of get_param(), but returns all parameters for the request.
|
||||
* Handles merging all the available values into a single array.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Map of key to value.
|
||||
*/
|
||||
public function get_params() {
|
||||
$order = $this->get_parameter_order();
|
||||
$order = array_reverse( $order, true );
|
||||
|
||||
$params = array();
|
||||
foreach ( $order as $type ) {
|
||||
$params = array_merge( $params, (array) $this->params[ $type ] );
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves parameters from the route itself.
|
||||
*
|
||||
* These are parsed from the URL using the regex.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Parameter map of key to value.
|
||||
*/
|
||||
public function get_url_params() {
|
||||
return $this->params['URL'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets parameters from the route.
|
||||
*
|
||||
* Typically, this is set after parsing the URL.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $params Parameter map of key to value.
|
||||
*/
|
||||
public function set_url_params( $params ) {
|
||||
$this->params['URL'] = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves parameters from the query string.
|
||||
*
|
||||
* These are the parameters you'd typically find in `$_GET`.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Parameter map of key to value
|
||||
*/
|
||||
public function get_query_params() {
|
||||
return $this->params['GET'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets parameters from the query string.
|
||||
*
|
||||
* Typically, this is set from `$_GET`.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $params Parameter map of key to value.
|
||||
*/
|
||||
public function set_query_params( $params ) {
|
||||
$this->params['GET'] = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves parameters from the body.
|
||||
*
|
||||
* These are the parameters you'd typically find in `$_POST`.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Parameter map of key to value.
|
||||
*/
|
||||
public function get_body_params() {
|
||||
return $this->params['POST'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets parameters from the body.
|
||||
*
|
||||
* Typically, this is set from `$_POST`.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $params Parameter map of key to value.
|
||||
*/
|
||||
public function set_body_params( $params ) {
|
||||
$this->params['POST'] = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves multipart file parameters from the body.
|
||||
*
|
||||
* These are the parameters you'd typically find in `$_FILES`.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Parameter map of key to value
|
||||
*/
|
||||
public function get_file_params() {
|
||||
return $this->params['FILES'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets multipart file parameters from the body.
|
||||
*
|
||||
* Typically, this is set from `$_FILES`.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $params Parameter map of key to value.
|
||||
*/
|
||||
public function set_file_params( $params ) {
|
||||
$this->params['FILES'] = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the default parameters.
|
||||
*
|
||||
* These are the parameters set in the route registration.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Parameter map of key to value
|
||||
*/
|
||||
public function get_default_params() {
|
||||
return $this->params['defaults'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets default parameters.
|
||||
*
|
||||
* These are the parameters set in the route registration.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $params Parameter map of key to value.
|
||||
*/
|
||||
public function set_default_params( $params ) {
|
||||
$this->params['defaults'] = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the request body content.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return string Binary data from the request body.
|
||||
*/
|
||||
public function get_body() {
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets body content.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $data Binary data from the request body.
|
||||
*/
|
||||
public function set_body( $data ) {
|
||||
$this->body = $data;
|
||||
|
||||
// Enable lazy parsing.
|
||||
$this->parsed_json = false;
|
||||
$this->parsed_body = false;
|
||||
$this->params['JSON'] = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the parameters from a JSON-formatted body.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Parameter map of key to value.
|
||||
*/
|
||||
public function get_json_params() {
|
||||
// Ensure the parameters have been parsed out.
|
||||
$this->parse_json_params();
|
||||
|
||||
return $this->params['JSON'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the JSON parameters.
|
||||
*
|
||||
* Avoids parsing the JSON data until we need to access it.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function parse_json_params() {
|
||||
if ( $this->parsed_json ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->parsed_json = true;
|
||||
|
||||
// Check that we actually got JSON.
|
||||
$content_type = $this->get_content_type();
|
||||
|
||||
if ( empty( $content_type ) || 'application/json' !== $content_type['value'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$params = json_decode( $this->get_body(), true );
|
||||
|
||||
/*
|
||||
* Check for a parsing error.
|
||||
*
|
||||
* Note that due to WP's JSON compatibility functions, json_last_error
|
||||
* might not be defined: https://core.trac.wordpress.org/ticket/27799
|
||||
*/
|
||||
if ( null === $params && ( ! function_exists( 'json_last_error' ) || JSON_ERROR_NONE !== json_last_error() ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->params['JSON'] = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the request body parameters.
|
||||
*
|
||||
* Parses out URL-encoded bodies for request methods that aren't supported
|
||||
* natively by PHP. In PHP 5.x, only POST has these parsed automatically.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function parse_body_params() {
|
||||
if ( $this->parsed_body ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->parsed_body = true;
|
||||
|
||||
/*
|
||||
* Check that we got URL-encoded. Treat a missing content-type as
|
||||
* URL-encoded for maximum compatibility.
|
||||
*/
|
||||
$content_type = $this->get_content_type();
|
||||
|
||||
if ( ! empty( $content_type ) && 'application/x-www-form-urlencoded' !== $content_type['value'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
parse_str( $this->get_body(), $params );
|
||||
|
||||
/*
|
||||
* Amazingly, parse_str follows magic quote rules. Sigh.
|
||||
*
|
||||
* NOTE: Do not refactor to use `wp_unslash`.
|
||||
*/
|
||||
if ( get_magic_quotes_gpc() ) {
|
||||
$params = stripslashes_deep( $params );
|
||||
}
|
||||
|
||||
/*
|
||||
* Add to the POST parameters stored internally. If a user has already
|
||||
* set these manually (via `set_body_params`), don't override them.
|
||||
*/
|
||||
$this->params['POST'] = array_merge( $params, $this->params['POST'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the route that matched the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return string Route matching regex.
|
||||
*/
|
||||
public function get_route() {
|
||||
return $this->route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the route that matched the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $route Route matching regex.
|
||||
*/
|
||||
public function set_route( $route ) {
|
||||
$this->route = $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the attributes for the request.
|
||||
*
|
||||
* These are the options for the route that was matched.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Attributes for the request.
|
||||
*/
|
||||
public function get_attributes() {
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the attributes for the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $attributes Attributes for the request.
|
||||
*/
|
||||
public function set_attributes( $attributes ) {
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes (where possible) the params on the request.
|
||||
*
|
||||
* This is primarily based off the sanitize_callback param on each registered
|
||||
* argument.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return true|null True if there are no parameters to sanitize, null otherwise.
|
||||
*/
|
||||
public function sanitize_params() {
|
||||
|
||||
$attributes = $this->get_attributes();
|
||||
|
||||
// No arguments set, skip sanitizing.
|
||||
if ( empty( $attributes['args'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$order = $this->get_parameter_order();
|
||||
|
||||
foreach ( $order as $type ) {
|
||||
if ( empty( $this->params[ $type ] ) ) {
|
||||
continue;
|
||||
}
|
||||
foreach ( $this->params[ $type ] as $key => $value ) {
|
||||
// Check if this param has a sanitize_callback added.
|
||||
if ( isset( $attributes['args'][ $key ] ) && ! empty( $attributes['args'][ $key ]['sanitize_callback'] ) ) {
|
||||
$this->params[ $type ][ $key ] = call_user_func( $attributes['args'][ $key ]['sanitize_callback'], $value, $this, $key );
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this request is valid according to its attributes.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return bool|WP_Error True if there are no parameters to validate or if all pass validation,
|
||||
* WP_Error if required parameters are missing.
|
||||
*/
|
||||
public function has_valid_params() {
|
||||
|
||||
$attributes = $this->get_attributes();
|
||||
$required = array();
|
||||
|
||||
// No arguments set, skip validation.
|
||||
if ( empty( $attributes['args'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ( $attributes['args'] as $key => $arg ) {
|
||||
|
||||
$param = $this->get_param( $key );
|
||||
if ( isset( $arg['required'] ) && true === $arg['required'] && null === $param ) {
|
||||
$required[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $required ) ) {
|
||||
return new WP_Error( 'rest_missing_callback_param', sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ), array( 'status' => 400, 'params' => $required ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the validation callbacks for each registered arg.
|
||||
*
|
||||
* This is done after required checking as required checking is cheaper.
|
||||
*/
|
||||
$invalid_params = array();
|
||||
|
||||
foreach ( $attributes['args'] as $key => $arg ) {
|
||||
|
||||
$param = $this->get_param( $key );
|
||||
|
||||
if ( null !== $param && ! empty( $arg['validate_callback'] ) ) {
|
||||
$valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key );
|
||||
|
||||
if ( false === $valid_check ) {
|
||||
$invalid_params[ $key ] = __( 'Invalid param.' );
|
||||
}
|
||||
|
||||
if ( is_wp_error( $valid_check ) ) {
|
||||
$invalid_params[] = sprintf( '%s (%s)', $key, $valid_check->get_error_message() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $invalid_params ) {
|
||||
return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', $invalid_params ) ), array( 'status' => 400, 'params' => $invalid_params ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a parameter is set.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $offset Parameter name.
|
||||
* @return bool Whether the parameter is set.
|
||||
*/
|
||||
public function offsetExists( $offset ) {
|
||||
$order = $this->get_parameter_order();
|
||||
|
||||
foreach ( $order as $type ) {
|
||||
if ( isset( $this->params[ $type ][ $offset ] ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a parameter from the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $offset Parameter name.
|
||||
* @return mixed|null Value if set, null otherwise.
|
||||
*/
|
||||
public function offsetGet( $offset ) {
|
||||
return $this->get_param( $offset );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a parameter on the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $offset Parameter name.
|
||||
* @param mixed $value Parameter value.
|
||||
*/
|
||||
public function offsetSet( $offset, $value ) {
|
||||
$this->set_param( $offset, $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a parameter from the request.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $offset Parameter name.
|
||||
*/
|
||||
public function offsetUnset( $offset ) {
|
||||
$order = $this->get_parameter_order();
|
||||
|
||||
// Remove the offset from every group.
|
||||
foreach ( $order as $type ) {
|
||||
unset( $this->params[ $type ][ $offset ] );
|
||||
}
|
||||
}
|
||||
}
|
255
src/wp-includes/rest-api/class-wp-rest-response.php
Normal file
255
src/wp-includes/rest-api/class-wp-rest-response.php
Normal file
@ -0,0 +1,255 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API: WP_REST_Response class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used to implement a REST response object.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see WP_HTTP_Response
|
||||
*/
|
||||
class WP_REST_Response extends WP_HTTP_Response {
|
||||
|
||||
/**
|
||||
* Links related to the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var array
|
||||
*/
|
||||
protected $links = array();
|
||||
|
||||
/**
|
||||
* The route that was to create the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $matched_route = '';
|
||||
|
||||
/**
|
||||
* The handler that was used to create the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var null|array
|
||||
*/
|
||||
protected $matched_handler = null;
|
||||
|
||||
/**
|
||||
* Adds a link to the response.
|
||||
*
|
||||
* @internal The $rel parameter is first, as this looks nicer when sending multiple.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @link http://tools.ietf.org/html/rfc5988
|
||||
* @link http://www.iana.org/assignments/link-relations/link-relations.xml
|
||||
*
|
||||
* @param string $rel Link relation. Either an IANA registered type,
|
||||
* or an absolute URL.
|
||||
* @param string $href Target URI for the link.
|
||||
* @param array $attributes Optional. Link parameters to send along with the URL. Default empty array.
|
||||
*/
|
||||
public function add_link( $rel, $href, $attributes = array() ) {
|
||||
if ( empty( $this->links[ $rel ] ) ) {
|
||||
$this->links[ $rel ] = array();
|
||||
}
|
||||
|
||||
if ( isset( $attributes['href'] ) ) {
|
||||
// Remove the href attribute, as it's used for the main URL.
|
||||
unset( $attributes['href'] );
|
||||
}
|
||||
|
||||
$this->links[ $rel ][] = array(
|
||||
'href' => $href,
|
||||
'attributes' => $attributes,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a link from the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $rel Link relation. Either an IANA registered type, or an absolute URL.
|
||||
* @param string $href Optional. Only remove links for the relation matching the given href.
|
||||
* Default null.
|
||||
*/
|
||||
public function remove_link( $rel, $href = null ) {
|
||||
if ( ! isset( $this->links[ $rel ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $href ) {
|
||||
$this->links[ $rel ] = wp_list_filter( $this->links[ $rel ], array( 'href' => $href ), 'NOT' );
|
||||
} else {
|
||||
$this->links[ $rel ] = array();
|
||||
}
|
||||
|
||||
if ( ! $this->links[ $rel ] ) {
|
||||
unset( $this->links[ $rel ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple links to the response.
|
||||
*
|
||||
* Link data should be an associative array with link relation as the key.
|
||||
* The value can either be an associative array of link attributes
|
||||
* (including `href` with the URL for the response), or a list of these
|
||||
* associative arrays.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $links Map of link relation to list of links.
|
||||
*/
|
||||
public function add_links( $links ) {
|
||||
foreach ( $links as $rel => $set ) {
|
||||
// If it's a single link, wrap with an array for consistent handling.
|
||||
if ( isset( $set['href'] ) ) {
|
||||
$set = array( $set );
|
||||
}
|
||||
|
||||
foreach ( $set as $attributes ) {
|
||||
$this->add_link( $rel, $attributes['href'], $attributes );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves links for the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return array List of links.
|
||||
*/
|
||||
public function get_links() {
|
||||
return $this->links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a single link header.
|
||||
*
|
||||
* @internal The $rel parameter is first, as this looks nicer when sending multiple.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @link http://tools.ietf.org/html/rfc5988
|
||||
* @link http://www.iana.org/assignments/link-relations/link-relations.xml
|
||||
*
|
||||
* @param string $rel Link relation. Either an IANA registered type, or an absolute URL.
|
||||
* @param string $link Target IRI for the link.
|
||||
* @param array $other Optional. Other parameters to send, as an assocative array.
|
||||
* Default empty array.
|
||||
*/
|
||||
public function link_header( $rel, $link, $other = array() ) {
|
||||
$header = '<' . $link . '>; rel="' . $rel . '"';
|
||||
|
||||
foreach ( $other as $key => $value ) {
|
||||
if ( 'title' === $key ) {
|
||||
$value = '"' . $value . '"';
|
||||
}
|
||||
$header .= '; ' . $key . '=' . $value;
|
||||
}
|
||||
$this->header( 'Link', $header, false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the route that was used.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return string The matched route.
|
||||
*/
|
||||
public function get_matched_route() {
|
||||
return $this->matched_route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the route (regex for path) that caused the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $route Route name.
|
||||
*/
|
||||
public function set_matched_route( $route ) {
|
||||
$this->matched_route = $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the handler that was used to generate the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return null|array The handler that was used to create the response.
|
||||
*/
|
||||
public function get_matched_handler() {
|
||||
return $this->matched_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the handler that was responsible for generating the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $handler The matched handler.
|
||||
*/
|
||||
public function set_matched_handler( $handler ) {
|
||||
$this->matched_handler = $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the response is an error, i.e. >= 400 response code.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return bool Whether the response is an error.
|
||||
*/
|
||||
public function is_error() {
|
||||
return $this->get_status() >= 400;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a WP_Error object from the response.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @return WP_Error|null WP_Error or null on not an errored response.
|
||||
*/
|
||||
public function as_error() {
|
||||
if ( ! $this->is_error() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$error = new WP_Error;
|
||||
|
||||
if ( is_array( $this->get_data() ) ) {
|
||||
foreach ( $this->get_data() as $err ) {
|
||||
$error->add( $err['code'], $err['message'], $err['data'] );
|
||||
}
|
||||
} else {
|
||||
$error->add( $this->get_status(), '', array( 'status' => $this->get_status() ) );
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
}
|
1188
src/wp-includes/rest-api/class-wp-rest-server.php
Normal file
1188
src/wp-includes/rest-api/class-wp-rest-server.php
Normal file
File diff suppressed because it is too large
Load Diff
666
src/wp-includes/rest-api/rest-functions.php
Normal file
666
src/wp-includes/rest-api/rest-functions.php
Normal file
@ -0,0 +1,666 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API functions.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers a REST API route.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $namespace The first URL segment after core prefix. Should be unique to your package/plugin.
|
||||
* @param string $route The base URL for route you are adding.
|
||||
* @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for
|
||||
* multiple methods. Default empty array.
|
||||
* @param bool $override Optional. If the route already exists, should we override it? True overrides,
|
||||
* false merges (with newer overriding if duplicate keys exist). Default false.
|
||||
*/
|
||||
function register_rest_route( $namespace, $route, $args = array(), $override = false ) {
|
||||
|
||||
/** @var WP_REST_Server $wp_rest_server */
|
||||
global $wp_rest_server;
|
||||
|
||||
if ( isset( $args['callback'] ) ) {
|
||||
// Upgrade a single set to multiple.
|
||||
$args = array( $args );
|
||||
}
|
||||
|
||||
$defaults = array(
|
||||
'methods' => 'GET',
|
||||
'callback' => null,
|
||||
'args' => array(),
|
||||
);
|
||||
foreach ( $args as $key => &$arg_group ) {
|
||||
if ( ! is_numeric( $arg_group ) ) {
|
||||
// Route option, skip here.
|
||||
continue;
|
||||
}
|
||||
|
||||
$arg_group = array_merge( $defaults, $arg_group );
|
||||
}
|
||||
|
||||
if ( $namespace ) {
|
||||
$full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
|
||||
} else {
|
||||
/*
|
||||
* Non-namespaced routes are not allowed, with the exception of the main
|
||||
* and namespace indexes. If you really need to register a
|
||||
* non-namespaced route, call `WP_REST_Server::register_route` directly.
|
||||
*/
|
||||
_doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin name and version', 'WPAPI-2.0' );
|
||||
|
||||
$full_route = '/' . trim( $route, '/' );
|
||||
}
|
||||
|
||||
$wp_rest_server->register_route( $namespace, $full_route, $args, $override );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new field on an existing WordPress object type.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @global array $wp_rest_additional_fields Holds registered fields, organized
|
||||
* by object type.
|
||||
*
|
||||
* @param string|array $object_type Object(s) the field is being registered
|
||||
* to, "post"|"term"|"comment" etc.
|
||||
* @param string $attribute The attribute name.
|
||||
* @param array $args {
|
||||
* Optional. An array of arguments used to handle the registered field.
|
||||
*
|
||||
* @type string|array|null $get_callback Optional. The callback function used to retrieve the field
|
||||
* value. Default is 'null', the field will not be returned in
|
||||
* the response.
|
||||
* @type string|array|null $update_callback Optional. The callback function used to set and update the
|
||||
* field value. Default is 'null', the value cannot be set or
|
||||
* updated.
|
||||
* @type string|array|null schema Optional. The callback function used to create the schema for
|
||||
* this field. Default is 'null', no schema entry will be returned.
|
||||
* }
|
||||
*/
|
||||
function register_api_field( $object_type, $attribute, $args = array() ) {
|
||||
|
||||
$defaults = array(
|
||||
'get_callback' => null,
|
||||
'update_callback' => null,
|
||||
'schema' => null,
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
||||
global $wp_rest_additional_fields;
|
||||
|
||||
$object_types = (array) $object_type;
|
||||
|
||||
foreach ( $object_types as $object_type ) {
|
||||
$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers rewrite rules for the API.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see rest_api_register_rewrites()
|
||||
* @global WP $wp Current WordPress environment instance.
|
||||
*/
|
||||
function rest_api_init() {
|
||||
rest_api_register_rewrites();
|
||||
|
||||
global $wp;
|
||||
$wp->add_query_var( 'rest_route' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds REST rewrite rules.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see add_rewrite_rule()
|
||||
*/
|
||||
function rest_api_register_rewrites() {
|
||||
add_rewrite_rule( '^' . rest_get_url_prefix() . '/?$','index.php?rest_route=/','top' );
|
||||
add_rewrite_rule( '^' . rest_get_url_prefix() . '/(.*)?','index.php?rest_route=/$matches[1]','top' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the default REST API filters.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @internal This will live in default-filters.php
|
||||
*/
|
||||
function rest_api_default_filters() {
|
||||
// Deprecated reporting.
|
||||
add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 );
|
||||
add_filter( 'deprecated_function_trigger_error', '__return_false' );
|
||||
add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 );
|
||||
add_filter( 'deprecated_argument_trigger_error', '__return_false' );
|
||||
|
||||
// Default serving.
|
||||
add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
|
||||
add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
|
||||
|
||||
add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the REST API.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
function rest_api_loaded() {
|
||||
if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this is a REST Request.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
define( 'REST_REQUEST', true );
|
||||
|
||||
/** @var WP_REST_Server $wp_rest_server */
|
||||
global $wp_rest_server;
|
||||
|
||||
/**
|
||||
* Filter the REST Server Class.
|
||||
*
|
||||
* This filter allows you to adjust the server class used by the API, using a
|
||||
* different class to handle requests.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $class_name The name of the server class. Default 'WP_REST_Server'.
|
||||
*/
|
||||
$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
|
||||
$wp_rest_server = new $wp_rest_server_class;
|
||||
|
||||
/**
|
||||
* Fires when preparing to serve an API request.
|
||||
*
|
||||
* Endpoint objects should be created and register their hooks on this action rather
|
||||
* than another action to ensure they're only loaded when needed.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param WP_REST_Server $wp_rest_server Server object.
|
||||
*/
|
||||
do_action( 'rest_api_init', $wp_rest_server );
|
||||
|
||||
// Fire off the request.
|
||||
$wp_rest_server->serve_request( $GLOBALS['wp']->query_vars['rest_route'] );
|
||||
|
||||
// We're done.
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the URL prefix for any API resource.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @return string Prefix.
|
||||
*/
|
||||
function rest_get_url_prefix() {
|
||||
/**
|
||||
* Filter the REST URL prefix.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $prefix URL prefix. Default 'wp-json'.
|
||||
*/
|
||||
return apply_filters( 'rest_url_prefix', 'wp-json' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the URL to a REST endpoint on a site.
|
||||
*
|
||||
* Note: The returned URL is NOT escaped.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @todo Check if this is even necessary
|
||||
*
|
||||
* @param int $blog_id Optional. Blog ID. Default of null returns URL for current blog.
|
||||
* @param string $path Optional. REST route. Default '/'.
|
||||
* @param string $scheme Optional. Sanitization scheme. Default 'json'.
|
||||
* @return string Full URL to the endpoint.
|
||||
*/
|
||||
function get_rest_url( $blog_id = null, $path = '/', $scheme = 'json' ) {
|
||||
if ( empty( $path ) ) {
|
||||
$path = '/';
|
||||
}
|
||||
|
||||
if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
|
||||
$url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
|
||||
$url .= '/' . ltrim( $path, '/' );
|
||||
} else {
|
||||
$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
|
||||
|
||||
$path = '/' . ltrim( $path, '/' );
|
||||
|
||||
$url = add_query_arg( 'rest_route', $path, $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the REST URL.
|
||||
*
|
||||
* Use this filter to adjust the url returned by the `get_rest_url` function.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $url REST URL.
|
||||
* @param string $path REST route.
|
||||
* @param int $blod_ig Blog ID.
|
||||
* @param string $scheme Sanitization scheme.
|
||||
*/
|
||||
return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the URL to a REST endpoint.
|
||||
*
|
||||
* Note: The returned URL is NOT escaped.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $path Optional. REST route. Default empty.
|
||||
* @param string $scheme Optional. Sanitization scheme. Default 'json'.
|
||||
* @return string Full URL to the endpoint.
|
||||
*/
|
||||
function rest_url( $path = '', $scheme = 'json' ) {
|
||||
return get_rest_url( null, $path, $scheme );
|
||||
}
|
||||
|
||||
/**
|
||||
* Do a REST request.
|
||||
*
|
||||
* Used primarily to route internal requests through WP_REST_Server.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @global WP_REST_Server $wp_rest_server
|
||||
*
|
||||
* @param WP_REST_Request|string $request Request.
|
||||
* @return WP_REST_Response REST response.
|
||||
*/
|
||||
function rest_do_request( $request ) {
|
||||
global $wp_rest_server;
|
||||
$request = rest_ensure_request( $request );
|
||||
return $wp_rest_server->dispatch( $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures request arguments are a request object (for consistency).
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param array|WP_REST_Request $request Request to check.
|
||||
* @return WP_REST_Request REST request instance.
|
||||
*/
|
||||
function rest_ensure_request( $request ) {
|
||||
if ( $request instanceof WP_REST_Request ) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
return new WP_REST_Request( 'GET', '', $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures a REST response is a response object (for consistency).
|
||||
*
|
||||
* This implements WP_HTTP_Response, allowing usage of `set_status`/`header`/etc
|
||||
* without needing to double-check the object. Will also allow WP_Error to indicate error
|
||||
* responses, so users should immediately check for this value.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param WP_Error|WP_HTTP_Response|mixed $response Response to check.
|
||||
* @return mixed WP_Error if response generated an error, WP_HTTP_Response if response
|
||||
* is a already an instance, otherwise returns a new WP_REST_Response instance.
|
||||
*/
|
||||
function rest_ensure_response( $response ) {
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
if ( $response instanceof WP_HTTP_Response ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return new WP_REST_Response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles _deprecated_function() errors.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $function Function name.
|
||||
* @param string $replacement Replacement function name.
|
||||
* @param string $version Version.
|
||||
*/
|
||||
function rest_handle_deprecated_function( $function, $replacement, $version ) {
|
||||
if ( ! empty( $replacement ) ) {
|
||||
$string = sprintf( __( '%1$s (since %2$s; use %3$s instead)' ), $function, $version, $replacement );
|
||||
} else {
|
||||
$string = sprintf( __( '%1$s (since %2$s; no alternative available)' ), $function, $version );
|
||||
}
|
||||
|
||||
header( sprintf( 'X-WP-DeprecatedFunction: %s', $string ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles _deprecated_argument() errors.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $function Function name.
|
||||
* @param string $replacement Replacement function name.
|
||||
* @param string $version Version.
|
||||
*/
|
||||
function rest_handle_deprecated_argument( $function, $replacement, $version ) {
|
||||
if ( ! empty( $replacement ) ) {
|
||||
$string = sprintf( __( '%1$s (since %2$s; %3$s)' ), $function, $version, $replacement );
|
||||
} else {
|
||||
$string = sprintf( __( '%1$s (since %2$s; no alternative available)' ), $function, $version );
|
||||
}
|
||||
|
||||
header( sprintf( 'X-WP-DeprecatedParam: %s', $string ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends Cross-Origin Resource Sharing headers with API requests.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param mixed $value Response data.
|
||||
* @return mixed Response data.
|
||||
*/
|
||||
function rest_send_cors_headers( $value ) {
|
||||
$origin = get_http_origin();
|
||||
|
||||
if ( $origin ) {
|
||||
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) );
|
||||
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
|
||||
header( 'Access-Control-Allow-Credentials: true' );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles OPTIONS requests for the server.
|
||||
*
|
||||
* This is handled outside of the server code, as it doesn't obey normal route
|
||||
* mapping.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param mixed $response Current response, either response or `null` to indicate pass-through.
|
||||
* @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server).
|
||||
* @param WP_REST_Request $request The request that was used to make current response.
|
||||
* @return WP_REST_Response Modified response, either response or `null` to indicate pass-through.
|
||||
*/
|
||||
function rest_handle_options_request( $response, $handler, $request ) {
|
||||
if ( ! empty( $response ) || $request->get_method() !== 'OPTIONS' ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response = new WP_REST_Response();
|
||||
$data = array();
|
||||
|
||||
$accept = array();
|
||||
|
||||
foreach ( $handler->get_routes() as $route => $endpoints ) {
|
||||
$match = preg_match( '@^' . $route . '$@i', $request->get_route(), $args );
|
||||
|
||||
if ( ! $match ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = $handler->get_data_for_route( $route, $endpoints, 'help' );
|
||||
$accept = array_merge( $accept, $data['methods'] );
|
||||
break;
|
||||
}
|
||||
$response->header( 'Accept', implode( ', ', $accept ) );
|
||||
|
||||
$response->set_data( $data );
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the "Allow" header to state all methods that can be sent to the current route.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param WP_REST_Response $response Current response being served.
|
||||
* @param WP_REST_Server $server ResponseHandler instance (usually WP_REST_Server).
|
||||
* @param WP_REST_Request $request The request that was used to make current response.
|
||||
* @return WP_REST_Response Current response being served.
|
||||
*/
|
||||
function rest_send_allow_header( $response, $server, $request ) {
|
||||
|
||||
$matched_route = $response->get_matched_route();
|
||||
|
||||
if ( ! $matched_route ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$routes = $server->get_routes();
|
||||
|
||||
$allowed_methods = array();
|
||||
|
||||
// Get the allowed methods across the routes.
|
||||
foreach ( $routes[ $matched_route ] as $_handler ) {
|
||||
foreach ( $_handler['methods'] as $handler_method => $value ) {
|
||||
|
||||
if ( ! empty( $_handler['permission_callback'] ) ) {
|
||||
|
||||
$permission = call_user_func( $_handler['permission_callback'], $request );
|
||||
|
||||
$allowed_methods[ $handler_method ] = true === $permission;
|
||||
} else {
|
||||
$allowed_methods[ $handler_method ] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Strip out all the methods that are not allowed (false values).
|
||||
$allowed_methods = array_filter( $allowed_methods );
|
||||
|
||||
if ( $allowed_methods ) {
|
||||
$response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the REST API URL to the WP RSD endpoint.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see get_rest_url()
|
||||
*/
|
||||
function rest_output_rsd() {
|
||||
$api_root = get_rest_url();
|
||||
|
||||
if ( empty( $api_root ) ) {
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<api name="WP-API" blogID="1" preferred="false" apiLink="<?php echo esc_url( $api_root ); ?>" />
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the REST API link tag into page header.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see get_rest_url()
|
||||
*/
|
||||
function rest_output_link_wp_head() {
|
||||
$api_root = get_rest_url();
|
||||
|
||||
if ( empty( $api_root ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo "<link rel='https://github.com/WP-API/WP-API' href='" . esc_url( $api_root ) . "' />\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a Link header for the REST API.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
function rest_output_link_header() {
|
||||
if ( headers_sent() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$api_root = get_rest_url();
|
||||
|
||||
if ( empty( $api_root ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
header( 'Link: <' . esc_url_raw( $api_root ) . '>; rel="https://github.com/WP-API/WP-API"', false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for errors when using cookie-based authentication.
|
||||
*
|
||||
* WordPress' built-in cookie authentication is always active
|
||||
* for logged in users. However, the API has to check nonces
|
||||
* for each request to ensure users are not vulnerable to CSRF.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @global mixed $wp_rest_auth_cookie
|
||||
*
|
||||
* @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it,
|
||||
* or another value if not.
|
||||
* @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
|
||||
*/
|
||||
function rest_cookie_check_errors( $result ) {
|
||||
if ( ! empty( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
global $wp_rest_auth_cookie;
|
||||
|
||||
/*
|
||||
* Is cookie authentication being used? (If we get an auth
|
||||
* error, but we're still logged in, another authentication
|
||||
* must have been used).
|
||||
*/
|
||||
if ( true !== $wp_rest_auth_cookie && is_user_logged_in() ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Determine if there is a nonce.
|
||||
$nonce = null;
|
||||
|
||||
if ( isset( $_REQUEST['_wp_rest_nonce'] ) ) {
|
||||
$nonce = $_REQUEST['_wp_rest_nonce'];
|
||||
} elseif ( isset( $_SERVER['HTTP_X_WP_NONCE'] ) ) {
|
||||
$nonce = $_SERVER['HTTP_X_WP_NONCE'];
|
||||
}
|
||||
|
||||
if ( null === $nonce ) {
|
||||
// No nonce at all, so act as if it's an unauthenticated request.
|
||||
wp_set_current_user( 0 );
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check the nonce.
|
||||
$result = wp_verify_nonce( $nonce, 'wp_rest' );
|
||||
|
||||
if ( ! $result ) {
|
||||
return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects cookie authentication status.
|
||||
*
|
||||
* Collects errors from wp_validate_auth_cookie for use by rest_cookie_check_errors.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see current_action()
|
||||
* @global mixed $wp_rest_auth_cookie
|
||||
*/
|
||||
function rest_cookie_collect_status() {
|
||||
global $wp_rest_auth_cookie;
|
||||
|
||||
$status_type = current_action();
|
||||
|
||||
if ( 'auth_cookie_valid' !== $status_type ) {
|
||||
$wp_rest_auth_cookie = substr( $status_type, 12 );
|
||||
return;
|
||||
}
|
||||
|
||||
$wp_rest_auth_cookie = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an RFC3339 timestamp into a DateTime.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param string $date RFC3339 timestamp.
|
||||
* @param bool $force_utc Optional. Whether to force UTC timezone instead of using
|
||||
* the timestamp's timezone. Default false.
|
||||
* @return DateTime DateTime instance.
|
||||
*/
|
||||
function rest_parse_date( $date, $force_utc = false ) {
|
||||
if ( $force_utc ) {
|
||||
$date = preg_replace( '/[+-]\d+:?\d+$/', '+00:00', $date );
|
||||
}
|
||||
|
||||
$regex = '#^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}(?::\d{2})?)?$#';
|
||||
|
||||
if ( ! preg_match( $regex, $date, $matches ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strtotime( $date );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a local date with its GMT equivalent, in MySQL datetime format.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @see rest_parse_date()
|
||||
*
|
||||
* @param string $date RFC3339 timestamp.
|
||||
* @param bool $force_utc Whether a UTC timestamp should be forced. Default false.
|
||||
* @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
|
||||
* null on failure.
|
||||
*/
|
||||
function rest_get_date_with_gmt( $date, $force_utc = false ) {
|
||||
$date = rest_parse_date( $date, $force_utc );
|
||||
|
||||
if ( empty( $date ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$utc = date( 'Y-m-d H:i:s', $date );
|
||||
$local = get_date_from_gmt( $utc );
|
||||
|
||||
return array( $local, $utc );
|
||||
}
|
@ -154,6 +154,7 @@ require( ABSPATH . WPINC . '/widgets.php' );
|
||||
require( ABSPATH . WPINC . '/nav-menu.php' );
|
||||
require( ABSPATH . WPINC . '/nav-menu-template.php' );
|
||||
require( ABSPATH . WPINC . '/admin-bar.php' );
|
||||
require( ABSPATH . WPINC . '/rest-api.php' );
|
||||
|
||||
// Load multisite-specific files.
|
||||
if ( is_multisite() ) {
|
||||
|
@ -90,11 +90,13 @@ require_once ABSPATH . '/wp-settings.php';
|
||||
_delete_all_posts();
|
||||
|
||||
require dirname( __FILE__ ) . '/testcase.php';
|
||||
require dirname( __FILE__ ) . '/testcase-rest-api.php';
|
||||
require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
|
||||
require dirname( __FILE__ ) . '/testcase-ajax.php';
|
||||
require dirname( __FILE__ ) . '/testcase-canonical.php';
|
||||
require dirname( __FILE__ ) . '/exceptions.php';
|
||||
require dirname( __FILE__ ) . '/utils.php';
|
||||
require dirname( __FILE__ ) . '/spy-rest-server.php';
|
||||
|
||||
/**
|
||||
* A child class of the PHP test runner.
|
||||
|
23
tests/phpunit/includes/spy-rest-server.php
Normal file
23
tests/phpunit/includes/spy-rest-server.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
class Spy_REST_Server extends WP_REST_Server {
|
||||
/**
|
||||
* Get the raw $endpoints data from the server
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_raw_endpoint_data() {
|
||||
return $this->endpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow calling protected methods from tests
|
||||
*
|
||||
* @param string $method Method to call
|
||||
* @param array $args Arguments to pass to the method
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call( $method, $args ) {
|
||||
return call_user_func_array( array( $this, $method ), $args );
|
||||
}
|
||||
}
|
19
tests/phpunit/includes/testcase-rest-api.php
Normal file
19
tests/phpunit/includes/testcase-rest-api.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
abstract class WP_Test_REST_TestCase extends WP_UnitTestCase {
|
||||
protected function assertErrorResponse( $code, $response, $status = null ) {
|
||||
|
||||
if ( is_a( $response, 'WP_REST_Response' ) ) {
|
||||
$response = $response->as_error();
|
||||
}
|
||||
|
||||
$this->assertInstanceOf( 'WP_Error', $response );
|
||||
$this->assertEquals( $code, $response->get_error_code() );
|
||||
|
||||
if ( null !== $status ) {
|
||||
$data = $response->get_error_data();
|
||||
$this->assertArrayHasKey( 'status', $data );
|
||||
$this->assertEquals( $status, $data['status'] );
|
||||
}
|
||||
}
|
||||
}
|
255
tests/phpunit/tests/rest-api.php
Normal file
255
tests/phpunit/tests/rest-api.php
Normal file
@ -0,0 +1,255 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API functions.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/admin.php';
|
||||
require_once ABSPATH . WPINC . '/rest-api.php';
|
||||
|
||||
/**
|
||||
* @group restapi
|
||||
*/
|
||||
class Tests_REST_API extends WP_UnitTestCase {
|
||||
public function setUp() {
|
||||
// Override the normal server with our spying server.
|
||||
$GLOBALS['wp_rest_server'] = new Spy_REST_Server();
|
||||
parent::setup();
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin should be installed and activated.
|
||||
*/
|
||||
function test_rest_api_activated() {
|
||||
$this->assertTrue( class_exists( 'WP_REST_Server' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* The rest_api_init hook should have been registered with init, and should
|
||||
* have a default priority of 10.
|
||||
*/
|
||||
function test_init_action_added() {
|
||||
$this->assertEquals( 10, has_action( 'init', 'rest_api_init' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a single route is canonicalized.
|
||||
*
|
||||
* Ensures that single and multiple routes are handled correctly.
|
||||
*/
|
||||
public function test_route_canonicalized() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
|
||||
// Check the route was registered correctly.
|
||||
$endpoints = $GLOBALS['wp_rest_server']->get_raw_endpoint_data();
|
||||
$this->assertArrayHasKey( '/test-ns/test', $endpoints );
|
||||
|
||||
// Check the route was wrapped in an array.
|
||||
$endpoint = $endpoints['/test-ns/test'];
|
||||
$this->assertArrayNotHasKey( 'callback', $endpoint );
|
||||
$this->assertArrayHasKey( 'namespace', $endpoint );
|
||||
$this->assertEquals( 'test-ns', $endpoint['namespace'] );
|
||||
|
||||
// Grab the filtered data.
|
||||
$filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
|
||||
$this->assertArrayHasKey( '/test-ns/test', $filtered_endpoints );
|
||||
$endpoint = $filtered_endpoints['/test-ns/test'];
|
||||
$this->assertCount( 1, $endpoint );
|
||||
$this->assertArrayHasKey( 'callback', $endpoint[0] );
|
||||
$this->assertArrayHasKey( 'methods', $endpoint[0] );
|
||||
$this->assertArrayHasKey( 'args', $endpoint[0] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a single route is canonicalized.
|
||||
*
|
||||
* Ensures that single and multiple routes are handled correctly.
|
||||
*/
|
||||
public function test_route_canonicalized_multiple() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
),
|
||||
array(
|
||||
'methods' => array( 'POST' ),
|
||||
'callback' => '__return_null',
|
||||
),
|
||||
) );
|
||||
|
||||
// Check the route was registered correctly.
|
||||
$endpoints = $GLOBALS['wp_rest_server']->get_raw_endpoint_data();
|
||||
$this->assertArrayHasKey( '/test-ns/test', $endpoints );
|
||||
|
||||
// Check the route was wrapped in an array.
|
||||
$endpoint = $endpoints['/test-ns/test'];
|
||||
$this->assertArrayNotHasKey( 'callback', $endpoint );
|
||||
$this->assertArrayHasKey( 'namespace', $endpoint );
|
||||
$this->assertEquals( 'test-ns', $endpoint['namespace'] );
|
||||
|
||||
$filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
|
||||
$endpoint = $filtered_endpoints['/test-ns/test'];
|
||||
$this->assertCount( 2, $endpoint );
|
||||
|
||||
// Check for both methods.
|
||||
foreach ( array( 0, 1 ) as $key ) {
|
||||
$this->assertArrayHasKey( 'callback', $endpoint[ $key ] );
|
||||
$this->assertArrayHasKey( 'methods', $endpoint[ $key ] );
|
||||
$this->assertArrayHasKey( 'args', $endpoint[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that routes are merged by default.
|
||||
*/
|
||||
public function test_route_merge() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'POST' ),
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
|
||||
// Check both routes exist.
|
||||
$endpoints = $GLOBALS['wp_rest_server']->get_routes();
|
||||
$endpoint = $endpoints['/test-ns/test'];
|
||||
$this->assertCount( 2, $endpoint );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that we can override routes.
|
||||
*/
|
||||
public function test_route_override() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
) );
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'POST' ),
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => true,
|
||||
), true );
|
||||
|
||||
// Check we only have one route.
|
||||
$endpoints = $GLOBALS['wp_rest_server']->get_routes();
|
||||
$endpoint = $endpoints['/test-ns/test'];
|
||||
$this->assertCount( 1, $endpoint );
|
||||
|
||||
// Check it's the right one.
|
||||
$this->assertArrayHasKey( 'should_exist', $endpoint[0] );
|
||||
$this->assertTrue( $endpoint[0]['should_exist'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* The rest_route query variable should be registered.
|
||||
*/
|
||||
function test_rest_route_query_var() {
|
||||
rest_api_init();
|
||||
$this->assertTrue( in_array( 'rest_route', $GLOBALS['wp']->public_query_vars ) );
|
||||
}
|
||||
|
||||
public function test_route_method() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'methods' arg should accept a single value as well as array.
|
||||
*/
|
||||
public function test_route_method_string() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'methods' arg should accept a single value as well as array.
|
||||
*/
|
||||
public function test_route_method_array() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'GET', 'POST' ),
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true, 'POST' => true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'methods' arg should a comma seperated string.
|
||||
*/
|
||||
public function test_route_method_comma_seperated() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET,POST',
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true, 'POST' => true ) );
|
||||
}
|
||||
|
||||
public function test_options_request() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET,POST',
|
||||
'callback' => '__return_null',
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' );
|
||||
$response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request );
|
||||
|
||||
$headers = $response->get_headers();
|
||||
$this->assertArrayHasKey( 'Accept', $headers );
|
||||
|
||||
$this->assertEquals( 'GET, POST', $headers['Accept'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the OPTIONS handler doesn't kick in for non-OPTIONS requests.
|
||||
*/
|
||||
public function test_options_request_not_options() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET,POST',
|
||||
'callback' => '__return_true',
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test' );
|
||||
$response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request );
|
||||
|
||||
$this->assertNull( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* The get_rest_url function should return a URL consistently terminated with a "/",
|
||||
* whether the blog is configured with pretty permalink support or not.
|
||||
*/
|
||||
public function test_rest_url_generation() {
|
||||
// In pretty permalinks case, we expect a path of wp-json/ with no query.
|
||||
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
|
||||
$this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() );
|
||||
|
||||
update_option( 'permalink_structure', '' );
|
||||
// In non-pretty case, we get a query string to invoke the rest router.
|
||||
$this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/?rest_route=/', get_rest_url() );
|
||||
}
|
||||
}
|
394
tests/phpunit/tests/rest-api/rest-request.php
Normal file
394
tests/phpunit/tests/rest-api/rest-request.php
Normal file
@ -0,0 +1,394 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_REST_Request functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group restapi
|
||||
*/
|
||||
class Tests_REST_Request extends WP_UnitTestCase {
|
||||
public function setUp() {
|
||||
$this->request = new WP_REST_Request();
|
||||
}
|
||||
|
||||
public function test_header() {
|
||||
$value = 'application/x-wp-example';
|
||||
|
||||
$this->request->set_header( 'Content-Type', $value );
|
||||
|
||||
$this->assertEquals( $value, $this->request->get_header( 'Content-Type' ) );
|
||||
}
|
||||
|
||||
public function test_header_missing() {
|
||||
$this->assertNull( $this->request->get_header( 'missing' ) );
|
||||
$this->assertNull( $this->request->get_header_as_array( 'missing' ) );
|
||||
}
|
||||
|
||||
public function test_header_multiple() {
|
||||
$value1 = 'application/x-wp-example-1';
|
||||
$value2 = 'application/x-wp-example-2';
|
||||
$this->request->add_header( 'Accept', $value1 );
|
||||
$this->request->add_header( 'Accept', $value2 );
|
||||
|
||||
$this->assertEquals( $value1 . ',' . $value2, $this->request->get_header( 'Accept' ) );
|
||||
$this->assertEquals( array( $value1, $value2 ), $this->request->get_header_as_array( 'Accept' ) );
|
||||
}
|
||||
|
||||
public static function header_provider() {
|
||||
return array(
|
||||
array( 'Test', 'test' ),
|
||||
array( 'TEST', 'test' ),
|
||||
array( 'Test-Header', 'test_header' ),
|
||||
array( 'test-header', 'test_header' ),
|
||||
array( 'Test_Header', 'test_header' ),
|
||||
array( 'test_header', 'test_header' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider header_provider
|
||||
* @param string $original Original header key.
|
||||
* @param string $expected Expected canonicalized version.
|
||||
*/
|
||||
public function test_header_canonicalization( $original, $expected ) {
|
||||
$this->assertEquals( $expected, $this->request->canonicalize_header_name( $original ) );
|
||||
}
|
||||
|
||||
public static function content_type_provider() {
|
||||
return array(
|
||||
// Check basic parsing.
|
||||
array( 'application/x-wp-example', 'application/x-wp-example', 'application', 'x-wp-example', '' ),
|
||||
array( 'application/x-wp-example; charset=utf-8', 'application/x-wp-example', 'application', 'x-wp-example', 'charset=utf-8' ),
|
||||
|
||||
// Check case insensitivity.
|
||||
array( 'APPLICATION/x-WP-Example', 'application/x-wp-example', 'application', 'x-wp-example', '' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider content_type_provider
|
||||
*
|
||||
* @param string $header Header value.
|
||||
* @param string $value Full type value.
|
||||
* @param string $type Main type (application, text, etc).
|
||||
* @param string $subtype Subtype (json, etc).
|
||||
* @param string $parameters Parameters (charset=utf-8, etc).
|
||||
*/
|
||||
public function test_content_type_parsing( $header, $value, $type, $subtype, $parameters ) {
|
||||
// Check we start with nothing.
|
||||
$this->assertEmpty( $this->request->get_content_type() );
|
||||
|
||||
$this->request->set_header( 'Content-Type', $header );
|
||||
$parsed = $this->request->get_content_type();
|
||||
|
||||
$this->assertEquals( $value, $parsed['value'] );
|
||||
$this->assertEquals( $type, $parsed['type'] );
|
||||
$this->assertEquals( $subtype, $parsed['subtype'] );
|
||||
$this->assertEquals( $parameters, $parsed['parameters'] );
|
||||
}
|
||||
|
||||
protected function request_with_parameters() {
|
||||
$this->request->set_url_params( array(
|
||||
'source' => 'url',
|
||||
'has_url_params' => true,
|
||||
) );
|
||||
$this->request->set_query_params( array(
|
||||
'source' => 'query',
|
||||
'has_query_params' => true,
|
||||
) );
|
||||
$this->request->set_body_params( array(
|
||||
'source' => 'body',
|
||||
'has_body_params' => true,
|
||||
) );
|
||||
|
||||
$json_data = wp_json_encode( array(
|
||||
'source' => 'json',
|
||||
'has_json_params' => true,
|
||||
) );
|
||||
$this->request->set_body( $json_data );
|
||||
|
||||
$this->request->set_default_params( array(
|
||||
'source' => 'defaults',
|
||||
'has_default_params' => true,
|
||||
) );
|
||||
}
|
||||
|
||||
public function test_parameter_order() {
|
||||
$this->request_with_parameters();
|
||||
|
||||
$this->request->set_method( 'GET' );
|
||||
|
||||
// Check that query takes precedence.
|
||||
$this->assertEquals( 'query', $this->request->get_param( 'source' ) );
|
||||
|
||||
// Check that the correct arguments are parsed (and that falling through
|
||||
// the stack works).
|
||||
$this->assertTrue( $this->request->get_param( 'has_url_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_query_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_default_params' ) );
|
||||
|
||||
// POST and JSON parameters shouldn't be parsed.
|
||||
$this->assertEmpty( $this->request->get_param( 'has_body_params' ) );
|
||||
$this->assertEmpty( $this->request->get_param( 'has_json_params' ) );
|
||||
}
|
||||
|
||||
public function test_parameter_order_post() {
|
||||
$this->request_with_parameters();
|
||||
|
||||
$this->request->set_method( 'POST' );
|
||||
$this->request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' );
|
||||
$this->request->set_attributes( array( 'accept_json' => true ) );
|
||||
|
||||
// Check that POST takes precedence.
|
||||
$this->assertEquals( 'body', $this->request->get_param( 'source' ) );
|
||||
|
||||
// Check that the correct arguments are parsed (and that falling through
|
||||
// the stack works).
|
||||
$this->assertTrue( $this->request->get_param( 'has_url_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_query_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_body_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_default_params' ) );
|
||||
|
||||
// JSON shouldn't be parsed.
|
||||
$this->assertEmpty( $this->request->get_param( 'has_json_params' ) );
|
||||
}
|
||||
|
||||
public function test_parameter_order_json() {
|
||||
$this->request_with_parameters();
|
||||
|
||||
$this->request->set_method( 'POST' );
|
||||
$this->request->set_header( 'Content-Type', 'application/json' );
|
||||
$this->request->set_attributes( array( 'accept_json' => true ) );
|
||||
|
||||
// Check that JSON takes precedence.
|
||||
$this->assertEquals( 'json', $this->request->get_param( 'source' ) );
|
||||
|
||||
// Check that the correct arguments are parsed (and that falling through
|
||||
// the stack works).
|
||||
$this->assertTrue( $this->request->get_param( 'has_url_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_query_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_body_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_json_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_default_params' ) );
|
||||
}
|
||||
|
||||
public function test_parameter_order_json_invalid() {
|
||||
$this->request_with_parameters();
|
||||
|
||||
$this->request->set_method( 'POST' );
|
||||
$this->request->set_header( 'Content-Type', 'application/json' );
|
||||
$this->request->set_attributes( array( 'accept_json' => true ) );
|
||||
|
||||
// Use invalid JSON data.
|
||||
$this->request->set_body( '{ this is not json }' );
|
||||
|
||||
// Check that JSON is ignored.
|
||||
$this->assertEquals( 'body', $this->request->get_param( 'source' ) );
|
||||
|
||||
// Check that the correct arguments are parsed (and that falling through
|
||||
// the stack works).
|
||||
$this->assertTrue( $this->request->get_param( 'has_url_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_query_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_body_params' ) );
|
||||
$this->assertTrue( $this->request->get_param( 'has_default_params' ) );
|
||||
|
||||
// JSON should be ignored.
|
||||
$this->assertEmpty( $this->request->get_param( 'has_json_params' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT requests don't get $_POST automatically parsed, so ensure that
|
||||
* WP_REST_Request does it for us.
|
||||
*/
|
||||
public function test_parameters_for_put() {
|
||||
$data = array(
|
||||
'foo' => 'bar',
|
||||
'alot' => array(
|
||||
'of' => 'parameters',
|
||||
),
|
||||
'list' => array(
|
||||
'of',
|
||||
'cool',
|
||||
'stuff',
|
||||
),
|
||||
);
|
||||
|
||||
$this->request->set_method( 'PUT' );
|
||||
$this->request->set_body_params( array() );
|
||||
$this->request->set_body( http_build_query( $data ) );
|
||||
|
||||
foreach ( $data as $key => $expected_value ) {
|
||||
$this->assertEquals( $expected_value, $this->request->get_param( $key ) );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_parameters_for_json_put() {
|
||||
$data = array(
|
||||
'foo' => 'bar',
|
||||
'alot' => array(
|
||||
'of' => 'parameters',
|
||||
),
|
||||
'list' => array(
|
||||
'of',
|
||||
'cool',
|
||||
'stuff',
|
||||
),
|
||||
);
|
||||
|
||||
$this->request->set_method( 'PUT' );
|
||||
$this->request->add_header( 'content-type', 'application/json' );
|
||||
$this->request->set_body( wp_json_encode( $data ) );
|
||||
|
||||
foreach ( $data as $key => $expected_value ) {
|
||||
$this->assertEquals( $expected_value, $this->request->get_param( $key ) );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_parameters_for_json_post() {
|
||||
$data = array(
|
||||
'foo' => 'bar',
|
||||
'alot' => array(
|
||||
'of' => 'parameters',
|
||||
),
|
||||
'list' => array(
|
||||
'of',
|
||||
'cool',
|
||||
'stuff',
|
||||
),
|
||||
);
|
||||
|
||||
$this->request->set_method( 'POST' );
|
||||
$this->request->add_header( 'content-type', 'application/json' );
|
||||
$this->request->set_body( wp_json_encode( $data ) );
|
||||
|
||||
foreach ( $data as $key => $expected_value ) {
|
||||
$this->assertEquals( $expected_value, $this->request->get_param( $key ) );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_parameter_merging() {
|
||||
$this->request_with_parameters();
|
||||
|
||||
$this->request->set_method( 'POST' );
|
||||
|
||||
$expected = array(
|
||||
'source' => 'body',
|
||||
'has_url_params' => true,
|
||||
'has_query_params' => true,
|
||||
'has_body_params' => true,
|
||||
'has_default_params' => true,
|
||||
);
|
||||
$this->assertEquals( $expected, $this->request->get_params() );
|
||||
}
|
||||
|
||||
public function test_sanitize_params() {
|
||||
$this->request->set_url_params( array(
|
||||
'someinteger' => '123',
|
||||
'somestring' => 'hello',
|
||||
));
|
||||
|
||||
$this->request->set_attributes( array(
|
||||
'args' => array(
|
||||
'someinteger' => array(
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'somestring' => array(
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->request->sanitize_params();
|
||||
|
||||
$this->assertEquals( 123, $this->request->get_param( 'someinteger' ) );
|
||||
$this->assertEquals( 0, $this->request->get_param( 'somestring' ) );
|
||||
}
|
||||
|
||||
public function test_has_valid_params_required_flag() {
|
||||
$this->request->set_attributes( array(
|
||||
'args' => array(
|
||||
'someinteger' => array(
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$valid = $this->request->has_valid_params();
|
||||
|
||||
$this->assertWPError( $valid );
|
||||
$this->assertEquals( 'rest_missing_callback_param', $valid->get_error_code() );
|
||||
}
|
||||
|
||||
public function test_has_valid_params_required_flag_multiple() {
|
||||
$this->request->set_attributes( array(
|
||||
'args' => array(
|
||||
'someinteger' => array(
|
||||
'required' => true,
|
||||
),
|
||||
'someotherinteger' => array(
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
$valid = $this->request->has_valid_params();
|
||||
|
||||
$this->assertWPError( $valid );
|
||||
$this->assertEquals( 'rest_missing_callback_param', $valid->get_error_code() );
|
||||
|
||||
$data = $valid->get_error_data( 'rest_missing_callback_param' );
|
||||
|
||||
$this->assertTrue( in_array( 'someinteger', $data['params'] ) );
|
||||
$this->assertTrue( in_array( 'someotherinteger', $data['params'] ) );
|
||||
}
|
||||
|
||||
public function test_has_valid_params_validate_callback() {
|
||||
$this->request->set_url_params( array(
|
||||
'someinteger' => '123',
|
||||
));
|
||||
|
||||
$this->request->set_attributes( array(
|
||||
'args' => array(
|
||||
'someinteger' => array(
|
||||
'validate_callback' => '__return_false',
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
$valid = $this->request->has_valid_params();
|
||||
|
||||
$this->assertWPError( $valid );
|
||||
$this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
|
||||
}
|
||||
|
||||
public function test_has_multiple_invalid_params_validate_callback() {
|
||||
$this->request->set_url_params( array(
|
||||
'someinteger' => '123',
|
||||
'someotherinteger' => '123',
|
||||
));
|
||||
|
||||
$this->request->set_attributes( array(
|
||||
'args' => array(
|
||||
'someinteger' => array(
|
||||
'validate_callback' => '__return_false',
|
||||
),
|
||||
'someotherinteger' => array(
|
||||
'validate_callback' => '__return_false',
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
$valid = $this->request->has_valid_params();
|
||||
|
||||
$this->assertWPError( $valid );
|
||||
$this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
|
||||
|
||||
$data = $valid->get_error_data( 'rest_invalid_param' );
|
||||
|
||||
$this->assertArrayHasKey( 'someinteger', $data['params'] );
|
||||
$this->assertArrayHasKey( 'someotherinteger', $data['params'] );
|
||||
}
|
||||
}
|
582
tests/phpunit/tests/rest-api/rest-server.php
Normal file
582
tests/phpunit/tests/rest-api/rest-server.php
Normal file
@ -0,0 +1,582 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_REST_Server functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group restapi
|
||||
*/
|
||||
class Tests_REST_Server extends WP_Test_REST_TestCase {
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
/** @var WP_REST_Server $wp_rest_server */
|
||||
global $wp_rest_server;
|
||||
$this->server = $wp_rest_server = new Spy_REST_Server();
|
||||
|
||||
do_action( 'rest_api_init', $this->server );
|
||||
}
|
||||
|
||||
public function test_envelope() {
|
||||
$data = array(
|
||||
'amount of arbitrary data' => 'alot',
|
||||
);
|
||||
$status = 987;
|
||||
$headers = array(
|
||||
'Arbitrary-Header' => 'value',
|
||||
'Multiple' => 'maybe, yes',
|
||||
);
|
||||
|
||||
$response = new WP_REST_Response( $data, $status );
|
||||
$response->header( 'Arbitrary-Header', 'value' );
|
||||
|
||||
// Check header concatenation as well.
|
||||
$response->header( 'Multiple', 'maybe' );
|
||||
$response->header( 'Multiple', 'yes', false );
|
||||
|
||||
$envelope_response = $this->server->envelope_response( $response, false );
|
||||
|
||||
// The envelope should still be a response, but with defaults.
|
||||
$this->assertInstanceOf( 'WP_REST_Response', $envelope_response );
|
||||
$this->assertEquals( 200, $envelope_response->get_status() );
|
||||
$this->assertEmpty( $envelope_response->get_headers() );
|
||||
$this->assertEmpty( $envelope_response->get_links() );
|
||||
|
||||
$enveloped = $envelope_response->get_data();
|
||||
|
||||
$this->assertEquals( $data, $enveloped['body'] );
|
||||
$this->assertEquals( $status, $enveloped['status'] );
|
||||
$this->assertEquals( $headers, $enveloped['headers'] );
|
||||
}
|
||||
|
||||
public function test_default_param() {
|
||||
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
'args' => array(
|
||||
'foo' => array(
|
||||
'default' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 'bar', $request['foo'] );
|
||||
}
|
||||
|
||||
public function test_default_param_is_overridden() {
|
||||
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
'args' => array(
|
||||
'foo' => array(
|
||||
'default' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test' );
|
||||
$request->set_query_params( array( 'foo' => 123 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( '123', $request['foo'] );
|
||||
}
|
||||
|
||||
public function test_optional_param() {
|
||||
register_rest_route( 'optional', '/test', array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => '__return_null',
|
||||
'args' => array(
|
||||
'foo' => array(),
|
||||
),
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/optional/test' );
|
||||
$request->set_query_params( array() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertInstanceOf( 'WP_REST_Response', $response );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertArrayNotHasKey( 'foo', (array) $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass a capability which the user does not have, this should
|
||||
* result in a 403 error.
|
||||
*/
|
||||
function test_rest_route_capability_authorization_fails() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
'permission_callback' => array( $this, 'permission_denied' ),
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
|
||||
$result = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 403, $result->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* An editor should be able to get access to an route with the
|
||||
* edit_posts capability.
|
||||
*/
|
||||
function test_rest_route_capability_authorization() {
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
'permission_callback' => '__return_true',
|
||||
) );
|
||||
|
||||
$editor = $this->factory->user->create( array( 'role' => 'editor' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
|
||||
|
||||
wp_set_current_user( $editor );
|
||||
|
||||
$result = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 200, $result->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* An "Allow" HTTP header should be sent with a request
|
||||
* for all available methods on that route.
|
||||
*/
|
||||
function test_allow_header_sent() {
|
||||
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
|
||||
|
||||
$result = $this->server->dispatch( $request );
|
||||
$result = apply_filters( 'rest_post_dispatch', $result, $this->server, $request );
|
||||
|
||||
$this->assertFalse( $result->get_status() !== 200 );
|
||||
|
||||
$sent_headers = $result->get_headers();
|
||||
$this->assertEquals( $sent_headers['Allow'], 'GET' );
|
||||
}
|
||||
|
||||
/**
|
||||
* The "Allow" HTTP header should include all available
|
||||
* methods that can be sent to a route.
|
||||
*/
|
||||
function test_allow_header_sent_with_multiple_methods() {
|
||||
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
) );
|
||||
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'POST',
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
|
||||
|
||||
$result = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertFalse( $result->get_status() !== 200 );
|
||||
|
||||
$result = apply_filters( 'rest_post_dispatch', $result, $this->server, $request );
|
||||
|
||||
$sent_headers = $result->get_headers();
|
||||
$this->assertEquals( $sent_headers['Allow'], 'GET, POST' );
|
||||
}
|
||||
|
||||
/**
|
||||
* The "Allow" HTTP header should NOT include other methods
|
||||
* which the user does not have access to.
|
||||
*/
|
||||
function test_allow_header_send_only_permitted_methods() {
|
||||
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
'permission_callback' => array( $this, 'permission_denied' ),
|
||||
) );
|
||||
|
||||
register_rest_route( 'test-ns', '/test', array(
|
||||
'methods' => 'POST',
|
||||
'callback' => '__return_null',
|
||||
'should_exist' => false,
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/test', array() );
|
||||
|
||||
$result = $this->server->dispatch( $request );
|
||||
$result = apply_filters( 'rest_post_dispatch', $result, $this->server, $request );
|
||||
|
||||
$this->assertEquals( $result->get_status(), 403 );
|
||||
|
||||
$sent_headers = $result->get_headers();
|
||||
$this->assertEquals( $sent_headers['Allow'], 'POST' );
|
||||
}
|
||||
|
||||
public function permission_denied() {
|
||||
return new WP_Error( 'forbidden', 'You are not allowed to do this', array( 'status' => 403 ) );
|
||||
}
|
||||
|
||||
public function test_error_to_response() {
|
||||
$code = 'wp-api-test-error';
|
||||
$message = 'Test error message for the API';
|
||||
$error = new WP_Error( $code, $message );
|
||||
|
||||
$response = $this->server->error_to_response( $error );
|
||||
$this->assertInstanceOf( 'WP_REST_Response', $response );
|
||||
|
||||
// Make sure we default to a 500 error.
|
||||
$this->assertEquals( 500, $response->get_status() );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertCount( 1, $data );
|
||||
|
||||
$this->assertEquals( $code, $data[0]['code'] );
|
||||
$this->assertEquals( $message, $data[0]['message'] );
|
||||
}
|
||||
|
||||
public function test_error_to_response_with_status() {
|
||||
$code = 'wp-api-test-error';
|
||||
$message = 'Test error message for the API';
|
||||
$error = new WP_Error( $code, $message, array( 'status' => 400 ) );
|
||||
|
||||
$response = $this->server->error_to_response( $error );
|
||||
$this->assertInstanceOf( 'WP_REST_Response', $response );
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
$data = $response->get_data();
|
||||
$this->assertCount( 1, $data );
|
||||
|
||||
$this->assertEquals( $code, $data[0]['code'] );
|
||||
$this->assertEquals( $message, $data[0]['message'] );
|
||||
}
|
||||
|
||||
public function test_rest_error() {
|
||||
$data = array(
|
||||
array(
|
||||
'code' => 'wp-api-test-error',
|
||||
'message' => 'Message text',
|
||||
),
|
||||
);
|
||||
$expected = wp_json_encode( $data );
|
||||
$response = $this->server->json_error( 'wp-api-test-error', 'Message text' );
|
||||
|
||||
$this->assertEquals( $expected, $response );
|
||||
}
|
||||
|
||||
public function test_json_error_with_status() {
|
||||
$stub = $this->getMockBuilder( 'Spy_REST_Server' )
|
||||
->setMethods( array( 'set_status' ) )
|
||||
->getMock();
|
||||
|
||||
$stub->expects( $this->once() )
|
||||
->method( 'set_status' )
|
||||
->with( $this->equalTo( 400 ) );
|
||||
|
||||
$data = array(
|
||||
array(
|
||||
'code' => 'wp-api-test-error',
|
||||
'message' => 'Message text',
|
||||
),
|
||||
);
|
||||
$expected = wp_json_encode( $data );
|
||||
|
||||
$response = $stub->json_error( 'wp-api-test-error', 'Message text', 400 );
|
||||
|
||||
$this->assertEquals( $expected, $response );
|
||||
}
|
||||
|
||||
public function test_response_to_data_links() {
|
||||
$response = new WP_REST_Response();
|
||||
$response->add_link( 'self', 'http://example.com/' );
|
||||
$response->add_link( 'alternate', 'http://example.org/', array( 'type' => 'application/xml' ) );
|
||||
|
||||
$data = $this->server->response_to_data( $response, false );
|
||||
$this->assertArrayHasKey( '_links', $data );
|
||||
|
||||
$self = array(
|
||||
'href' => 'http://example.com/',
|
||||
);
|
||||
$this->assertEquals( $self, $data['_links']['self'][0] );
|
||||
|
||||
$alternate = array(
|
||||
'href' => 'http://example.org/',
|
||||
'type' => 'application/xml',
|
||||
);
|
||||
$this->assertEquals( $alternate, $data['_links']['alternate'][0] );
|
||||
}
|
||||
|
||||
public function test_link_embedding() {
|
||||
// Register our testing route.
|
||||
$this->server->register_route( 'test', '/test/embeddable', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this, 'embedded_response_callback' ),
|
||||
) );
|
||||
$response = new WP_REST_Response();
|
||||
|
||||
// External links should be ignored.
|
||||
$response->add_link( 'alternate', 'http://not-api.example.com/', array( 'embeddable' => true ) );
|
||||
|
||||
// All others should be embedded.
|
||||
$response->add_link( 'alternate', rest_url( '/test/embeddable' ), array( 'embeddable' => true ) );
|
||||
|
||||
$data = $this->server->response_to_data( $response, true );
|
||||
$this->assertArrayHasKey( '_embedded', $data );
|
||||
|
||||
$alternate = $data['_embedded']['alternate'];
|
||||
$this->assertCount( 2, $alternate );
|
||||
$this->assertEmpty( $alternate[0] );
|
||||
|
||||
$this->assertInternalType( 'array', $alternate[1] );
|
||||
$this->assertArrayNotHasKey( 'code', $alternate[1] );
|
||||
$this->assertTrue( $alternate[1]['hello'] );
|
||||
|
||||
// Ensure the context is set to embed when requesting.
|
||||
$this->assertEquals( 'embed', $alternate[1]['parameters']['context'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends test_link_embedding
|
||||
*/
|
||||
public function test_link_embedding_self() {
|
||||
// Register our testing route.
|
||||
$this->server->register_route( 'test', '/test/embeddable', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this, 'embedded_response_callback' ),
|
||||
) );
|
||||
$response = new WP_REST_Response();
|
||||
|
||||
// 'self' should be ignored.
|
||||
$response->add_link( 'self', rest_url( '/test/notembeddable' ), array( 'embeddable' => true ) );
|
||||
|
||||
$data = $this->server->response_to_data( $response, true );
|
||||
|
||||
$this->assertArrayNotHasKey( '_embedded', $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends test_link_embedding
|
||||
*/
|
||||
public function test_link_embedding_params() {
|
||||
// Register our testing route.
|
||||
$this->server->register_route( 'test', '/test/embeddable', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this, 'embedded_response_callback' ),
|
||||
) );
|
||||
|
||||
$response = new WP_REST_Response();
|
||||
$response->add_link( 'alternate', rest_url( '/test/embeddable?parsed_params=yes' ), array( 'embeddable' => true ) );
|
||||
|
||||
$data = $this->server->response_to_data( $response, true );
|
||||
|
||||
$this->assertArrayHasKey( '_embedded', $data );
|
||||
$this->assertArrayHasKey( 'alternate', $data['_embedded'] );
|
||||
$data = $data['_embedded']['alternate'][0];
|
||||
|
||||
$this->assertEquals( 'yes', $data['parameters']['parsed_params'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends test_link_embedding_params
|
||||
*/
|
||||
public function test_link_embedding_error() {
|
||||
// Register our testing route.
|
||||
$this->server->register_route( 'test', '/test/embeddable', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this, 'embedded_response_callback' ),
|
||||
) );
|
||||
|
||||
$response = new WP_REST_Response();
|
||||
$response->add_link( 'up', rest_url( '/test/embeddable?error=1' ), array( 'embeddable' => true ) );
|
||||
|
||||
$data = $this->server->response_to_data( $response, true );
|
||||
|
||||
$this->assertArrayHasKey( '_embedded', $data );
|
||||
$this->assertArrayHasKey( 'up', $data['_embedded'] );
|
||||
|
||||
// Check that errors are embedded correctly.
|
||||
$up = $data['_embedded']['up'];
|
||||
$this->assertCount( 1, $up );
|
||||
|
||||
$up_data = $up[0];
|
||||
$this->assertEquals( 'wp-api-test-error', $up_data[0]['code'] );
|
||||
$this->assertEquals( 'Test message', $up_data[0]['message'] );
|
||||
$this->assertEquals( 403, $up_data[0]['data']['status'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure embedding is a no-op without links in the data.
|
||||
*/
|
||||
public function test_link_embedding_without_links() {
|
||||
$data = array(
|
||||
'untouched' => 'data',
|
||||
);
|
||||
$result = $this->server->embed_links( $data );
|
||||
|
||||
$this->assertArrayNotHasKey( '_links', $data );
|
||||
$this->assertArrayNotHasKey( '_embedded', $data );
|
||||
$this->assertEquals( 'data', $data['untouched'] );
|
||||
}
|
||||
|
||||
public function embedded_response_callback( $request ) {
|
||||
$params = $request->get_params();
|
||||
|
||||
if ( isset( $params['error'] ) ) {
|
||||
return new WP_Error( 'wp-api-test-error', 'Test message', array( 'status' => 403 ) );
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'hello' => true,
|
||||
'parameters' => $params,
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function test_removing_links() {
|
||||
$response = new WP_REST_Response();
|
||||
$response->add_link( 'self', 'http://example.com/' );
|
||||
$response->add_link( 'alternate', 'http://example.org/', array( 'type' => 'application/xml' ) );
|
||||
|
||||
$response->remove_link( 'self' );
|
||||
|
||||
$data = $this->server->response_to_data( $response, false );
|
||||
$this->assertArrayHasKey( '_links', $data );
|
||||
|
||||
$this->assertArrayNotHasKey( 'self', $data['_links'] );
|
||||
|
||||
$alternate = array(
|
||||
'href' => 'http://example.org/',
|
||||
'type' => 'application/xml',
|
||||
);
|
||||
$this->assertEquals( $alternate, $data['_links']['alternate'][0] );
|
||||
}
|
||||
|
||||
public function test_removing_links_for_href() {
|
||||
$response = new WP_REST_Response();
|
||||
$response->add_link( 'self', 'http://example.com/' );
|
||||
$response->add_link( 'self', 'https://example.com/' );
|
||||
|
||||
$response->remove_link( 'self', 'https://example.com/' );
|
||||
|
||||
$data = $this->server->response_to_data( $response, false );
|
||||
$this->assertArrayHasKey( '_links', $data );
|
||||
|
||||
$this->assertArrayHasKey( 'self', $data['_links'] );
|
||||
|
||||
$self_not_filtered = array(
|
||||
'href' => 'http://example.com/',
|
||||
);
|
||||
$this->assertEquals( $self_not_filtered, $data['_links']['self'][0] );
|
||||
}
|
||||
|
||||
public function test_get_index() {
|
||||
$server = new WP_REST_Server();
|
||||
$server->register_route( 'test/example', '/test/example/some-route', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => '__return_true',
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => '__return_true',
|
||||
),
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/' );
|
||||
$index = $server->dispatch( $request );
|
||||
$data = $index->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'name', $data );
|
||||
$this->assertArrayHasKey( 'description', $data );
|
||||
$this->assertArrayHasKey( 'url', $data );
|
||||
$this->assertArrayHasKey( 'namespaces', $data );
|
||||
$this->assertArrayHasKey( 'authentication', $data );
|
||||
$this->assertArrayHasKey( 'routes', $data );
|
||||
|
||||
// Check namespace data.
|
||||
$this->assertContains( 'test/example', $data['namespaces'] );
|
||||
|
||||
// Check the route.
|
||||
$this->assertArrayHasKey( '/test/example/some-route', $data['routes'] );
|
||||
$route = $data['routes']['/test/example/some-route'];
|
||||
$this->assertEquals( 'test/example', $route['namespace'] );
|
||||
$this->assertArrayHasKey( 'methods', $route );
|
||||
$this->assertContains( 'GET', $route['methods'] );
|
||||
$this->assertContains( 'DELETE', $route['methods'] );
|
||||
$this->assertArrayHasKey( '_links', $route );
|
||||
}
|
||||
|
||||
public function test_get_namespace_index() {
|
||||
$server = new WP_REST_Server();
|
||||
$server->register_route( 'test/example', '/test/example/some-route', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => '__return_true',
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => '__return_true',
|
||||
),
|
||||
) );
|
||||
$server->register_route( 'test/another', '/test/another/route', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => '__return_false',
|
||||
),
|
||||
) );
|
||||
|
||||
$request = new WP_REST_Request();
|
||||
$request->set_param( 'namespace', 'test/example' );
|
||||
$index = rest_ensure_response( $server->get_namespace_index( $request ) );
|
||||
$data = $index->get_data();
|
||||
|
||||
// Check top-level.
|
||||
$this->assertEquals( 'test/example', $data['namespace'] );
|
||||
$this->assertArrayHasKey( 'routes', $data );
|
||||
|
||||
// Check we have the route we expect...
|
||||
$this->assertArrayHasKey( '/test/example/some-route', $data['routes'] );
|
||||
|
||||
// ...and none we don't.
|
||||
$this->assertArrayNotHasKey( '/test/another/route', $data['routes'] );
|
||||
}
|
||||
|
||||
public function test_get_namespaces() {
|
||||
$server = new WP_REST_Server();
|
||||
$server->register_route( 'test/example', '/test/example/some-route', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => '__return_true',
|
||||
),
|
||||
) );
|
||||
$server->register_route( 'test/another', '/test/another/route', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => '__return_false',
|
||||
),
|
||||
) );
|
||||
|
||||
$namespaces = $server->get_namespaces();
|
||||
$this->assertContains( 'test/example', $namespaces );
|
||||
$this->assertContains( 'test/another', $namespaces );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user