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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API: WP_REST_Server class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage REST_API
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class used to implement the WordPress REST API server.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
class WP_REST_Server {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for GET transport method.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const READABLE = 'GET';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for POST transport method.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const CREATABLE = 'POST';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for POST, PUT, PATCH transport methods together.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const EDITABLE = 'POST, PUT, PATCH';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for DELETE transport method.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const DELETABLE = 'DELETE';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for GET, POST, PUT, PATCH & DELETE transport methods together.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const ALLMETHODS = 'GET, POST, PUT, PATCH, DELETE';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Namespaces registered to the server.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $namespaces = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoints registered to the server.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $endpoints = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options defined for the routes.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $route_options = array();
|
|
|
|
|
2020-01-30 15:22:07 -05:00
|
|
|
/**
|
|
|
|
* Caches embedded requests.
|
|
|
|
*
|
|
|
|
* @since 5.4.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $embed_cache = array();
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
/**
|
|
|
|
* Instantiates the REST server.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->endpoints = array(
|
|
|
|
// Meta endpoints.
|
2020-10-20 15:10:08 -04:00
|
|
|
'/' => array(
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
'callback' => array( $this, 'get_index' ),
|
2017-11-30 18:11:00 -05:00
|
|
|
'methods' => 'GET',
|
|
|
|
'args' => array(
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
'context' => array(
|
|
|
|
'default' => 'view',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-10-20 15:10:08 -04:00
|
|
|
'/batch/v1' => array(
|
|
|
|
'callback' => array( $this, 'serve_batch_request_v1' ),
|
|
|
|
'methods' => 'POST',
|
|
|
|
'args' => array(
|
|
|
|
'validation' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
'enum' => array( 'require-all-validate', 'normal' ),
|
|
|
|
'default' => 'normal',
|
|
|
|
),
|
|
|
|
'requests' => array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'array',
|
|
|
|
'maxItems' => $this->get_max_batch_size(),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'method' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
'enum' => array( 'POST', 'PUT', 'PATCH', 'DELETE' ),
|
|
|
|
'default' => 'POST',
|
|
|
|
),
|
|
|
|
'path' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
'required' => true,
|
|
|
|
),
|
|
|
|
'body' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(),
|
|
|
|
'additionalProperties' => true,
|
|
|
|
),
|
|
|
|
'headers' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(),
|
|
|
|
'additionalProperties' => array(
|
|
|
|
'type' => array( 'string', 'array' ),
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the authentication headers if supplied.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful
|
|
|
|
* or no authentication provided
|
|
|
|
*/
|
2019-09-19 11:50:59 -04:00
|
|
|
public function check_authentication() {
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
/**
|
2016-10-30 13:54:30 -04:00
|
|
|
* Filters REST authentication errors.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* This is used to pass a WP_Error from an authentication method back to
|
|
|
|
* the API.
|
|
|
|
*
|
|
|
|
* Authentication methods should check first if they're being used, as
|
|
|
|
* multiple authentication methods can be enabled on a site (cookies,
|
|
|
|
* HTTP basic auth, OAuth). If the authentication method hooked in is
|
|
|
|
* not actually being attempted, null should be returned to indicate
|
|
|
|
* another authentication method should check instead. Similarly,
|
|
|
|
* callbacks should ensure the value is `null` before checking for
|
|
|
|
* errors.
|
|
|
|
*
|
|
|
|
* A WP_Error instance can be returned if an error occurs, and this should
|
|
|
|
* match the format used by API methods internally (that is, the `status`
|
|
|
|
* data should be used). A callback can return `true` to indicate that
|
|
|
|
* the authentication method was used, and it succeeded.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
2019-11-12 17:42:02 -05:00
|
|
|
* @param WP_Error|null|true $errors WP_Error if authentication error, null if authentication
|
|
|
|
* method wasn't used, true if authentication succeeded.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
2019-09-19 11:50:59 -04:00
|
|
|
return apply_filters( 'rest_authentication_errors', null );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an error to a response object.
|
|
|
|
*
|
|
|
|
* This iterates over all error codes and messages to change it into a flat
|
|
|
|
* array. This enables simpler client behaviour, as it is represented as a
|
|
|
|
* list in JSON rather than an object/map.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_Error $error WP_Error instance.
|
|
|
|
* @return WP_REST_Response List of associative arrays with code and message keys.
|
|
|
|
*/
|
|
|
|
protected function error_to_response( $error ) {
|
|
|
|
$error_data = $error->get_error_data();
|
|
|
|
|
|
|
|
if ( is_array( $error_data ) && isset( $error_data['status'] ) ) {
|
|
|
|
$status = $error_data['status'];
|
|
|
|
} else {
|
|
|
|
$status = 500;
|
|
|
|
}
|
|
|
|
|
2015-11-16 23:12:26 -05:00
|
|
|
$errors = array();
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
foreach ( (array) $error->errors as $code => $messages ) {
|
|
|
|
foreach ( (array) $messages as $message ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$errors[] = array(
|
|
|
|
'code' => $code,
|
|
|
|
'message' => $message,
|
|
|
|
'data' => $error->get_error_data( $code ),
|
|
|
|
);
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 23:12:26 -05:00
|
|
|
$data = $errors[0];
|
|
|
|
if ( count( $errors ) > 1 ) {
|
|
|
|
// Remove the primary error.
|
|
|
|
array_shift( $errors );
|
|
|
|
$data['additional_errors'] = $errors;
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$response = new WP_REST_Response( $data, $status );
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves an appropriate error representation in JSON.
|
|
|
|
*
|
|
|
|
* Note: This should only be used in WP_REST_Server::serve_request(), as it
|
|
|
|
* cannot handle WP_Error internally. All callbacks and other internal methods
|
|
|
|
* should instead return a WP_Error with the data set to an array that includes
|
|
|
|
* a 'status' key, with the value being the HTTP status to send.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param string $code WP_Error-style code.
|
|
|
|
* @param string $message Human-readable message.
|
|
|
|
* @param int $status Optional. HTTP status code to send. Default null.
|
|
|
|
* @return string JSON representation of the error
|
|
|
|
*/
|
|
|
|
protected function json_error( $code, $message, $status = null ) {
|
|
|
|
if ( $status ) {
|
|
|
|
$this->set_status( $status );
|
|
|
|
}
|
|
|
|
|
|
|
|
$error = compact( 'code', 'message' );
|
|
|
|
|
2015-11-16 23:12:26 -05:00
|
|
|
return wp_json_encode( $error );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-16 06:04:12 -05:00
|
|
|
* Handles serving a REST API request.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* Matches the current server URI to a route and runs the first matching
|
|
|
|
* callback then outputs a JSON representation of the returned value.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @see WP_REST_Server::dispatch()
|
|
|
|
*
|
REST API: Introduce Application Passwords for API authentication.
In WordPress 4.4 the REST API was first introduced. A few releases later in WordPress 4.7, the Content API endpoints were added, paving the way for Gutenberg and countless in-site experiences. In the intervening years, numerous plugins have built on top of the REST API. Many developers shared a common frustration, the lack of external authentication to the REST API.
This commit introduces Application Passwords to allow users to connect to external applications to their WordPress website. Users can generate individual passwords for each application, allowing for easy revocation and activity monitoring. An authorization flow is introduced to make the connection flow simple for users and application developers.
Application Passwords uses Basic Authentication, and by default is only available over an SSL connection.
Props georgestephanis, kasparsd, timothyblynjacobs, afercia, akkspro, andraganescu, arippberger, aristath, austyfrosty, ayesh, batmoo, bradyvercher, brianhenryie, helen, ipstenu, jeffmatson, jeffpaul, joostdevalk, joshlevinson, kadamwhite, kjbenk, koke, michael-arestad, Otto42, pekz0r, salzano, spacedmonkey, valendesigns.
Fixes #42790.
Built from https://develop.svn.wordpress.org/trunk@49109
git-svn-id: http://core.svn.wordpress.org/trunk@48871 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-10-08 18:14:06 -04:00
|
|
|
* @global WP_User $current_user The currently authenticated user.
|
|
|
|
*
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* @param string $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used.
|
|
|
|
* Default null.
|
2020-01-11 13:32:05 -05:00
|
|
|
* @return null|false Null if not served and a HEAD request, false otherwise.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
public function serve_request( $path = null ) {
|
REST API: Introduce Application Passwords for API authentication.
In WordPress 4.4 the REST API was first introduced. A few releases later in WordPress 4.7, the Content API endpoints were added, paving the way for Gutenberg and countless in-site experiences. In the intervening years, numerous plugins have built on top of the REST API. Many developers shared a common frustration, the lack of external authentication to the REST API.
This commit introduces Application Passwords to allow users to connect to external applications to their WordPress website. Users can generate individual passwords for each application, allowing for easy revocation and activity monitoring. An authorization flow is introduced to make the connection flow simple for users and application developers.
Application Passwords uses Basic Authentication, and by default is only available over an SSL connection.
Props georgestephanis, kasparsd, timothyblynjacobs, afercia, akkspro, andraganescu, arippberger, aristath, austyfrosty, ayesh, batmoo, bradyvercher, brianhenryie, helen, ipstenu, jeffmatson, jeffpaul, joostdevalk, joshlevinson, kadamwhite, kjbenk, koke, michael-arestad, Otto42, pekz0r, salzano, spacedmonkey, valendesigns.
Fixes #42790.
Built from https://develop.svn.wordpress.org/trunk@49109
git-svn-id: http://core.svn.wordpress.org/trunk@48871 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-10-08 18:14:06 -04:00
|
|
|
/* @var WP_User|null $current_user */
|
|
|
|
global $current_user;
|
|
|
|
|
|
|
|
if ( $current_user instanceof WP_User && ! $current_user->exists() ) {
|
|
|
|
/*
|
|
|
|
* If there is no current user authenticated via other means, clear
|
|
|
|
* the cached lack of user, so that an authenticate check can set it
|
|
|
|
* properly.
|
|
|
|
*
|
|
|
|
* This is done because for authentications such as Application
|
|
|
|
* Passwords, we don't want it to be accepted unless the current HTTP
|
2020-11-16 06:04:12 -05:00
|
|
|
* request is a REST API request, which can't always be identified early
|
REST API: Introduce Application Passwords for API authentication.
In WordPress 4.4 the REST API was first introduced. A few releases later in WordPress 4.7, the Content API endpoints were added, paving the way for Gutenberg and countless in-site experiences. In the intervening years, numerous plugins have built on top of the REST API. Many developers shared a common frustration, the lack of external authentication to the REST API.
This commit introduces Application Passwords to allow users to connect to external applications to their WordPress website. Users can generate individual passwords for each application, allowing for easy revocation and activity monitoring. An authorization flow is introduced to make the connection flow simple for users and application developers.
Application Passwords uses Basic Authentication, and by default is only available over an SSL connection.
Props georgestephanis, kasparsd, timothyblynjacobs, afercia, akkspro, andraganescu, arippberger, aristath, austyfrosty, ayesh, batmoo, bradyvercher, brianhenryie, helen, ipstenu, jeffmatson, jeffpaul, joostdevalk, joshlevinson, kadamwhite, kjbenk, koke, michael-arestad, Otto42, pekz0r, salzano, spacedmonkey, valendesigns.
Fixes #42790.
Built from https://develop.svn.wordpress.org/trunk@49109
git-svn-id: http://core.svn.wordpress.org/trunk@48871 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-10-08 18:14:06 -04:00
|
|
|
* enough in evaluation.
|
|
|
|
*/
|
|
|
|
$current_user = null;
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$content_type = isset( $_GET['_jsonp'] ) ? 'application/javascript' : 'application/json';
|
|
|
|
$this->send_header( 'Content-Type', $content_type . '; charset=' . get_option( 'blog_charset' ) );
|
2016-06-16 10:59:28 -04:00
|
|
|
$this->send_header( 'X-Robots-Tag', 'noindex' );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2016-06-28 21:57:55 -04:00
|
|
|
$api_root = get_rest_url();
|
|
|
|
if ( ! empty( $api_root ) ) {
|
|
|
|
$this->send_header( 'Link', '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"' );
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
/*
|
|
|
|
* Mitigate possible JSONP Flash attacks.
|
|
|
|
*
|
2016-06-10 00:50:33 -04:00
|
|
|
* https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
$this->send_header( 'X-Content-Type-Options', 'nosniff' );
|
2020-06-20 19:56:12 -04:00
|
|
|
$expose_headers = array( 'X-WP-Total', 'X-WP-TotalPages', 'Link' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the list of response headers that are exposed to CORS requests.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*
|
|
|
|
* @param string[] $expose_headers The list of headers to expose.
|
|
|
|
*/
|
|
|
|
$expose_headers = apply_filters( 'rest_exposed_cors_headers', $expose_headers );
|
|
|
|
|
|
|
|
$this->send_header( 'Access-Control-Expose-Headers', implode( ', ', $expose_headers ) );
|
2020-07-12 15:37:12 -04:00
|
|
|
|
|
|
|
$allow_headers = array(
|
|
|
|
'Authorization',
|
|
|
|
'X-WP-Nonce',
|
|
|
|
'Content-Disposition',
|
|
|
|
'Content-MD5',
|
|
|
|
'Content-Type',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the list of request headers that are allowed for CORS requests.
|
|
|
|
*
|
|
|
|
* The allowed headers are passed to the browser to specify which
|
|
|
|
* headers can be passed to the REST API. By default, we allow the
|
|
|
|
* Content-* headers needed to upload files to the media endpoints.
|
|
|
|
* As well as the Authorization and Nonce headers for allowing authentication.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*
|
|
|
|
* @param string[] $allow_headers The list of headers to allow.
|
|
|
|
*/
|
|
|
|
$allow_headers = apply_filters( 'rest_allowed_cors_headers', $allow_headers );
|
|
|
|
|
|
|
|
$this->send_header( 'Access-Control-Allow-Headers', implode( ', ', $allow_headers ) );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2015-12-04 18:36:25 -05:00
|
|
|
/**
|
|
|
|
* Send nocache headers on authenticated requests.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param bool $rest_send_nocache_headers Whether to send no-cache headers.
|
|
|
|
*/
|
|
|
|
$send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
|
|
|
|
if ( $send_no_cache_headers ) {
|
|
|
|
foreach ( wp_get_nocache_headers() as $header => $header_value ) {
|
2017-05-19 16:27:44 -04:00
|
|
|
if ( empty( $header_value ) ) {
|
|
|
|
$this->remove_header( $header );
|
|
|
|
} else {
|
|
|
|
$this->send_header( $header, $header_value );
|
|
|
|
}
|
2015-12-04 18:36:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters whether the REST API is enabled.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* @since 4.4.0
|
2019-11-09 08:05:02 -05:00
|
|
|
* @deprecated 4.7.0 Use the {@see 'rest_authentication_errors'} filter to
|
2020-11-16 06:04:12 -05:00
|
|
|
* restrict access to the REST API.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* @param bool $rest_enabled Whether the REST API is enabled. Default true.
|
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
apply_filters_deprecated(
|
2018-08-16 21:51:36 -04:00
|
|
|
'rest_enabled',
|
|
|
|
array( true ),
|
|
|
|
'4.7.0',
|
|
|
|
'rest_authentication_errors',
|
2020-01-06 11:21:04 -05:00
|
|
|
sprintf(
|
|
|
|
/* translators: %s: rest_authentication_errors */
|
|
|
|
__( 'The REST API can no longer be completely disabled, the %s filter can be used to restrict access to the API, instead.' ),
|
|
|
|
'rest_authentication_errors'
|
|
|
|
)
|
2017-02-01 16:32:47 -05:00
|
|
|
);
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters whether jsonp is enabled.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param bool $jsonp_enabled Whether jsonp is enabled. Default true.
|
|
|
|
*/
|
|
|
|
$jsonp_enabled = apply_filters( 'rest_jsonp_enabled', true );
|
|
|
|
|
|
|
|
$jsonp_callback = null;
|
|
|
|
|
|
|
|
if ( isset( $_GET['_jsonp'] ) ) {
|
|
|
|
if ( ! $jsonp_enabled ) {
|
|
|
|
echo $this->json_error( 'rest_callback_disabled', __( 'JSONP support is disabled on this site.' ), 400 );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-06 17:34:28 -04:00
|
|
|
$jsonp_callback = $_GET['_jsonp'];
|
|
|
|
if ( ! wp_check_jsonp_callback( $jsonp_callback ) ) {
|
2016-11-17 10:53:33 -05:00
|
|
|
echo $this->json_error( 'rest_callback_invalid', __( 'Invalid JSONP callback function.' ), 400 );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $path ) ) {
|
|
|
|
if ( isset( $_SERVER['PATH_INFO'] ) ) {
|
|
|
|
$path = $_SERVER['PATH_INFO'];
|
|
|
|
} else {
|
|
|
|
$path = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( $_SERVER['REQUEST_METHOD'], $path );
|
|
|
|
|
2016-04-06 17:02:28 -04:00
|
|
|
$request->set_query_params( wp_unslash( $_GET ) );
|
|
|
|
$request->set_body_params( wp_unslash( $_POST ) );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$request->set_file_params( $_FILES );
|
2016-04-06 17:02:28 -04:00
|
|
|
$request->set_headers( $this->get_headers( wp_unslash( $_SERVER ) ) );
|
2019-06-26 14:07:54 -04:00
|
|
|
$request->set_body( self::get_raw_data() );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* HTTP method override for clients that can't use PUT/PATCH/DELETE. First, we check
|
|
|
|
* $_GET['_method']. If that is not set, we check for the HTTP_X_HTTP_METHOD_OVERRIDE
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
if ( isset( $_GET['_method'] ) ) {
|
|
|
|
$request->set_method( $_GET['_method'] );
|
|
|
|
} elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) {
|
|
|
|
$request->set_method( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] );
|
|
|
|
}
|
|
|
|
|
2019-09-19 11:50:59 -04:00
|
|
|
$result = $this->check_authentication();
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
if ( ! is_wp_error( $result ) ) {
|
|
|
|
$result = $this->dispatch( $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize to either WP_Error or WP_REST_Response...
|
|
|
|
$result = rest_ensure_response( $result );
|
|
|
|
|
|
|
|
// ...then convert WP_Error across.
|
|
|
|
if ( is_wp_error( $result ) ) {
|
|
|
|
$result = $this->error_to_response( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-16 06:04:12 -05:00
|
|
|
* Filters the REST API response.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* Allows modification of the response before returning.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
2016-02-16 01:06:27 -05:00
|
|
|
* @since 4.5.0 Applied to embedded responses.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
2020-11-16 06:04:12 -05:00
|
|
|
* @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`.
|
2021-01-08 09:30:14 -05:00
|
|
|
* @param WP_REST_Server $server Server instance.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
|
|
|
*/
|
|
|
|
$result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request );
|
|
|
|
|
|
|
|
// Wrap the response in an envelope if asked for.
|
|
|
|
if ( isset( $_GET['_envelope'] ) ) {
|
|
|
|
$result = $this->envelope_response( $result, isset( $_GET['_embed'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send extra data from response objects.
|
|
|
|
$headers = $result->get_headers();
|
|
|
|
$this->send_headers( $headers );
|
|
|
|
|
|
|
|
$code = $result->get_status();
|
|
|
|
$this->set_status( $code );
|
|
|
|
|
|
|
|
/**
|
2020-11-16 06:04:12 -05:00
|
|
|
* Filters whether the REST API request has already been served.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* Allow sending the request manually - by returning true, the API result
|
|
|
|
* will not be sent to the client.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param bool $served Whether the request has already been served.
|
|
|
|
* Default false.
|
2020-11-16 06:04:12 -05:00
|
|
|
* @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
2021-01-08 09:30:14 -05:00
|
|
|
* @param WP_REST_Server $server Server instance.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
$served = apply_filters( 'rest_pre_serve_request', false, $result, $request, $this );
|
|
|
|
|
|
|
|
if ( ! $served ) {
|
|
|
|
if ( 'HEAD' === $request->get_method() ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Embed links inside the request.
|
2020-02-09 15:54:05 -05:00
|
|
|
$embed = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false;
|
|
|
|
$result = $this->response_to_data( $result, $embed );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2017-06-29 09:17:43 -04:00
|
|
|
/**
|
2020-11-16 06:04:12 -05:00
|
|
|
* Filters the REST API response.
|
2017-06-29 09:17:43 -04:00
|
|
|
*
|
|
|
|
* Allows modification of the response data after inserting
|
|
|
|
* embedded data (if any) and before echoing the response data.
|
|
|
|
*
|
|
|
|
* @since 4.8.1
|
|
|
|
*
|
|
|
|
* @param array $result Response data to send to the client.
|
2021-01-08 09:30:14 -05:00
|
|
|
* @param WP_REST_Server $server Server instance.
|
2017-06-29 09:17:43 -04:00
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
|
|
|
*/
|
|
|
|
$result = apply_filters( 'rest_pre_echo_response', $result, $this, $request );
|
|
|
|
|
2019-08-15 15:56:55 -04:00
|
|
|
// The 204 response shouldn't have a body.
|
|
|
|
if ( 204 === $code || null === $result ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$result = wp_json_encode( $result );
|
|
|
|
|
|
|
|
$json_error_message = $this->get_json_last_error();
|
2020-01-28 19:45:18 -05:00
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
if ( $json_error_message ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
$json_error_obj = new WP_Error(
|
|
|
|
'rest_encode_error',
|
|
|
|
$json_error_message,
|
|
|
|
array( 'status' => 500 )
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->error_to_response( $json_error_obj );
|
2021-01-02 15:36:06 -05:00
|
|
|
$result = wp_json_encode( $result->data );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $jsonp_callback ) {
|
2016-10-30 13:54:30 -04:00
|
|
|
// Prepend '/**/' to mitigate possible JSONP Flash attacks.
|
2016-06-10 00:50:33 -04:00
|
|
|
// https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
echo '/**/' . $jsonp_callback . '(' . $result . ')';
|
|
|
|
} else {
|
|
|
|
echo $result;
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 19:45:18 -05:00
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a response to data to send.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
2020-02-09 15:54:05 -05:00
|
|
|
* @since 5.4.0 The $embed parameter can now contain a list of link relations to include.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response Response object.
|
2020-02-09 15:54:05 -05:00
|
|
|
* @param bool|string[] $embed Whether to embed all links, a filtered list of link relations, or no links.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* @return array {
|
|
|
|
* Data with sub-requests embedded.
|
|
|
|
*
|
2020-07-23 03:39:02 -04:00
|
|
|
* @type array $_links Links.
|
2020-09-30 09:54:06 -04:00
|
|
|
* @type array $_embedded Embedded objects.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* }
|
|
|
|
*/
|
|
|
|
public function response_to_data( $response, $embed ) {
|
|
|
|
$data = $response->get_data();
|
2019-06-26 14:07:54 -04:00
|
|
|
$links = self::get_compact_response_links( $response );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
if ( ! empty( $links ) ) {
|
|
|
|
// Convert links to part of the data.
|
|
|
|
$data['_links'] = $links;
|
|
|
|
}
|
2020-01-28 19:45:18 -05:00
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
if ( $embed ) {
|
2020-01-30 15:22:07 -05:00
|
|
|
$this->embed_cache = array();
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
// Determine if this is a numeric array.
|
|
|
|
if ( wp_is_numeric_array( $data ) ) {
|
2020-02-09 15:54:05 -05:00
|
|
|
foreach ( $data as $key => $item ) {
|
|
|
|
$data[ $key ] = $this->embed_links( $item, $embed );
|
|
|
|
}
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
} else {
|
2020-02-09 15:54:05 -05:00
|
|
|
$data = $this->embed_links( $data, $embed );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
2020-01-30 15:22:07 -05:00
|
|
|
$this->embed_cache = array();
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves links from a response.
|
|
|
|
*
|
|
|
|
* Extracts the links from a response into a structured hash, suitable for
|
|
|
|
* direct output.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response Response to extract links from.
|
|
|
|
* @return array Map of link relation to list of link hashes.
|
|
|
|
*/
|
|
|
|
public static function get_response_links( $response ) {
|
|
|
|
$links = $response->get_links();
|
2020-01-28 19:45:18 -05:00
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
if ( empty( $links ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert links to part of the data.
|
|
|
|
$data = array();
|
2016-03-21 20:16:27 -04:00
|
|
|
foreach ( $links as $rel => $items ) {
|
|
|
|
$data[ $rel ] = array();
|
|
|
|
|
|
|
|
foreach ( $items as $item ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$attributes = $item['attributes'];
|
2016-03-21 20:16:27 -04:00
|
|
|
$attributes['href'] = $item['href'];
|
2017-11-30 18:11:00 -05:00
|
|
|
$data[ $rel ][] = $attributes;
|
2016-03-21 20:16:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the CURIEs (compact URIs) used for relations.
|
|
|
|
*
|
|
|
|
* Extracts the links from a response into a structured hash, suitable for
|
|
|
|
* direct output.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response Response to extract links from.
|
|
|
|
* @return array Map of link relation to list of link hashes.
|
|
|
|
*/
|
|
|
|
public static function get_compact_response_links( $response ) {
|
|
|
|
$links = self::get_response_links( $response );
|
|
|
|
|
|
|
|
if ( empty( $links ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$curies = $response->get_curies();
|
2016-02-15 21:19:27 -05:00
|
|
|
$used_curies = array();
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
foreach ( $links as $rel => $items ) {
|
2016-02-15 21:19:27 -05:00
|
|
|
|
|
|
|
// Convert $rel URIs to their compact versions if they exist.
|
|
|
|
foreach ( $curies as $curie ) {
|
|
|
|
$href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) );
|
|
|
|
if ( strpos( $rel, $href_prefix ) !== 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-30 13:54:30 -04:00
|
|
|
// Relation now changes from '$uri' to '$curie:$relation'.
|
2016-03-21 20:16:27 -04:00
|
|
|
$rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) );
|
2016-02-15 21:19:27 -05:00
|
|
|
preg_match( '!' . $rel_regex . '!', $rel, $matches );
|
|
|
|
if ( $matches ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$new_rel = $curie['name'] . ':' . $matches[1];
|
2016-03-21 20:16:27 -04:00
|
|
|
$used_curies[ $curie['name'] ] = $curie;
|
2017-11-30 18:11:00 -05:00
|
|
|
$links[ $new_rel ] = $items;
|
2016-03-21 20:16:27 -04:00
|
|
|
unset( $links[ $rel ] );
|
|
|
|
break;
|
2016-02-15 21:19:27 -05:00
|
|
|
}
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 21:19:27 -05:00
|
|
|
// Push the curies onto the start of the links array.
|
|
|
|
if ( $used_curies ) {
|
2016-03-21 20:16:27 -04:00
|
|
|
$links['curies'] = array_values( $used_curies );
|
2016-02-15 21:19:27 -05:00
|
|
|
}
|
|
|
|
|
2016-03-21 20:16:27 -04:00
|
|
|
return $links;
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Embeds the links from the data into the request.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
2020-02-09 15:54:05 -05:00
|
|
|
* @since 5.4.0 The $embed parameter can now contain a list of link relations to include.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
2020-02-09 15:54:05 -05:00
|
|
|
* @param array $data Data from the request.
|
|
|
|
* @param bool|string[] $embed Whether to embed all links or a filtered list of link relations.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* @return array {
|
|
|
|
* Data with sub-requests embedded.
|
|
|
|
*
|
2020-07-23 03:39:02 -04:00
|
|
|
* @type array $_links Links.
|
2020-09-30 09:54:06 -04:00
|
|
|
* @type array $_embedded Embedded objects.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* }
|
|
|
|
*/
|
2020-02-09 15:54:05 -05:00
|
|
|
protected function embed_links( $data, $embed = true ) {
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
if ( empty( $data['_links'] ) ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$embedded = array();
|
|
|
|
|
|
|
|
foreach ( $data['_links'] as $rel => $links ) {
|
2020-06-28 07:53:04 -04:00
|
|
|
// If a list of relations was specified, and the link relation
|
|
|
|
// is not in the list of allowed relations, don't process the link.
|
2020-02-09 15:54:05 -05:00
|
|
|
if ( is_array( $embed ) && ! in_array( $rel, $embed, true ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$embeds = array();
|
|
|
|
|
|
|
|
foreach ( $links as $item ) {
|
|
|
|
// Determine if the link is embeddable.
|
2016-02-23 23:20:25 -05:00
|
|
|
if ( empty( $item['embeddable'] ) ) {
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
// Ensure we keep the same order.
|
|
|
|
$embeds[] = array();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-30 15:22:07 -05:00
|
|
|
if ( ! array_key_exists( $item['href'], $this->embed_cache ) ) {
|
|
|
|
// Run through our internal routing and serve.
|
|
|
|
$request = WP_REST_Request::from_url( $item['href'] );
|
|
|
|
if ( ! $request ) {
|
|
|
|
$embeds[] = array();
|
|
|
|
continue;
|
|
|
|
}
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-01-30 15:22:07 -05:00
|
|
|
// Embedded resources get passed context=embed.
|
|
|
|
if ( empty( $request['context'] ) ) {
|
|
|
|
$request['context'] = 'embed';
|
|
|
|
}
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-01-30 15:22:07 -05:00
|
|
|
$response = $this->dispatch( $request );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-01-30 15:22:07 -05:00
|
|
|
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
|
|
|
|
$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request );
|
|
|
|
|
|
|
|
$this->embed_cache[ $item['href'] ] = $this->response_to_data( $response, false );
|
|
|
|
}
|
2016-02-16 01:06:27 -05:00
|
|
|
|
2020-01-30 15:22:07 -05:00
|
|
|
$embeds[] = $this->embed_cache[ $item['href'] ];
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if any real links were found.
|
|
|
|
$has_links = count( array_filter( $embeds ) );
|
2016-10-30 13:54:30 -04:00
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
if ( $has_links ) {
|
|
|
|
$embedded[ $rel ] = $embeds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $embedded ) ) {
|
|
|
|
$data['_embedded'] = $embedded;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps the response in an envelope.
|
|
|
|
*
|
|
|
|
* The enveloping technique is used to work around browser/client
|
|
|
|
* compatibility issues. Essentially, it converts the full HTTP response to
|
|
|
|
* data instead.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response Response object.
|
|
|
|
* @param bool $embed Whether links should be embedded.
|
|
|
|
* @return WP_REST_Response New response with wrapped data
|
|
|
|
*/
|
|
|
|
public function envelope_response( $response, $embed ) {
|
|
|
|
$envelope = array(
|
|
|
|
'body' => $this->response_to_data( $response, $embed ),
|
|
|
|
'status' => $response->get_status(),
|
|
|
|
'headers' => $response->get_headers(),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters the enveloped form of a response.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param array $envelope Envelope data.
|
|
|
|
* @param WP_REST_Response $response Original response data.
|
|
|
|
*/
|
|
|
|
$envelope = apply_filters( 'rest_envelope_response', $envelope, $response );
|
|
|
|
|
|
|
|
// Ensure it's still a response and return.
|
|
|
|
return rest_ensure_response( $envelope );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a route to the server.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param string $namespace Namespace.
|
|
|
|
* @param string $route The REST route.
|
|
|
|
* @param array $route_args Route arguments.
|
2016-10-31 02:28:32 -04:00
|
|
|
* @param bool $override Optional. Whether the route should be overridden if it already exists.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* Default false.
|
|
|
|
*/
|
|
|
|
public function register_route( $namespace, $route, $route_args, $override = false ) {
|
|
|
|
if ( ! isset( $this->namespaces[ $namespace ] ) ) {
|
|
|
|
$this->namespaces[ $namespace ] = array();
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->register_route(
|
2018-08-16 21:51:36 -04:00
|
|
|
$namespace,
|
|
|
|
'/' . $namespace,
|
|
|
|
array(
|
2017-11-30 18:11:00 -05:00
|
|
|
array(
|
|
|
|
'methods' => self::READABLE,
|
|
|
|
'callback' => array( $this, 'get_namespace_index' ),
|
|
|
|
'args' => array(
|
|
|
|
'namespace' => array(
|
|
|
|
'default' => $namespace,
|
|
|
|
),
|
|
|
|
'context' => array(
|
|
|
|
'default' => 'view',
|
|
|
|
),
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
),
|
|
|
|
),
|
2017-11-30 18:11:00 -05:00
|
|
|
)
|
|
|
|
);
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Associative to avoid double-registration.
|
|
|
|
$this->namespaces[ $namespace ][ $route ] = true;
|
2017-11-30 18:11:00 -05:00
|
|
|
$route_args['namespace'] = $namespace;
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
if ( $override || empty( $this->endpoints[ $route ] ) ) {
|
|
|
|
$this->endpoints[ $route ] = $route_args;
|
|
|
|
} else {
|
|
|
|
$this->endpoints[ $route ] = array_merge( $this->endpoints[ $route ], $route_args );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the route map.
|
|
|
|
*
|
|
|
|
* The route map is an associative array with path regexes as the keys. The
|
|
|
|
* value is an indexed array with the callback function/method as the first
|
|
|
|
* item, and a bitmask of HTTP methods as the second item (see the class
|
|
|
|
* constants).
|
|
|
|
*
|
|
|
|
* Each route can be mapped to more than one callback by using an array of
|
|
|
|
* the indexed arrays. This allows mapping e.g. GET requests to one callback
|
|
|
|
* and POST requests to another.
|
|
|
|
*
|
|
|
|
* Note that the path regexes (array keys) must have @ escaped, as this is
|
|
|
|
* used as the delimiter with preg_match()
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
2020-02-10 22:22:05 -05:00
|
|
|
* @since 5.4.0 Add $namespace parameter.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
2020-02-10 22:22:05 -05:00
|
|
|
* @param string $namespace Optionally, only return routes in the given namespace.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* @return array `'/path/regex' => array( $callback, $bitmask )` or
|
|
|
|
* `'/path/regex' => array( array( $callback, $bitmask ), ...)`.
|
|
|
|
*/
|
2020-02-10 22:22:05 -05:00
|
|
|
public function get_routes( $namespace = '' ) {
|
|
|
|
$endpoints = $this->endpoints;
|
|
|
|
|
|
|
|
if ( $namespace ) {
|
|
|
|
$endpoints = wp_list_filter( $endpoints, array( 'namespace' => $namespace ) );
|
|
|
|
}
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters the array of available endpoints.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param array $endpoints The available endpoints. An array of matching regex patterns, each mapped
|
|
|
|
* to an array of callbacks for the endpoint. These take the format
|
|
|
|
* `'/path/regex' => array( $callback, $bitmask )` or
|
|
|
|
* `'/path/regex' => array( array( $callback, $bitmask ).
|
|
|
|
*/
|
2020-02-10 22:22:05 -05:00
|
|
|
$endpoints = apply_filters( 'rest_endpoints', $endpoints );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
// Normalise the endpoints.
|
|
|
|
$defaults = array(
|
|
|
|
'methods' => '',
|
|
|
|
'accept_json' => false,
|
|
|
|
'accept_raw' => false,
|
|
|
|
'show_in_index' => true,
|
|
|
|
'args' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $endpoints as $route => &$handlers ) {
|
|
|
|
|
|
|
|
if ( isset( $handlers['callback'] ) ) {
|
|
|
|
// Single endpoint, add one deeper.
|
|
|
|
$handlers = array( $handlers );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset( $this->route_options[ $route ] ) ) {
|
|
|
|
$this->route_options[ $route ] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $handlers as $key => &$handler ) {
|
|
|
|
|
|
|
|
if ( ! is_numeric( $key ) ) {
|
|
|
|
// Route option, move it to the options.
|
|
|
|
$this->route_options[ $route ][ $key ] = $handler;
|
|
|
|
unset( $handlers[ $key ] );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$handler = wp_parse_args( $handler, $defaults );
|
|
|
|
|
|
|
|
// Allow comma-separated HTTP methods.
|
|
|
|
if ( is_string( $handler['methods'] ) ) {
|
|
|
|
$methods = explode( ',', $handler['methods'] );
|
2016-10-30 13:54:30 -04:00
|
|
|
} elseif ( is_array( $handler['methods'] ) ) {
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$methods = $handler['methods'];
|
|
|
|
} else {
|
|
|
|
$methods = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$handler['methods'] = array();
|
|
|
|
|
|
|
|
foreach ( $methods as $method ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$method = strtoupper( trim( $method ) );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$handler['methods'][ $method ] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-30 13:54:30 -04:00
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
return $endpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves namespaces registered on the server.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
2019-11-05 16:23:02 -05:00
|
|
|
* @return string[] List of registered namespaces.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
public function get_namespaces() {
|
|
|
|
return array_keys( $this->namespaces );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves specified options for a route.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param string $route Route pattern to fetch options for.
|
|
|
|
* @return array|null Data as an associative array if found, or null if not found.
|
|
|
|
*/
|
|
|
|
public function get_route_options( $route ) {
|
|
|
|
if ( ! isset( $this->route_options[ $route ] ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->route_options[ $route ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches the request to a callback and call it.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Request to attempt dispatching.
|
|
|
|
* @return WP_REST_Response Response returned by the callback.
|
|
|
|
*/
|
|
|
|
public function dispatch( $request ) {
|
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters the pre-calculated result of a REST dispatch request.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* Allow hijacking the request before dispatching by returning a non-empty. The returned value
|
|
|
|
* will be used to serve the request instead.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param mixed $result Response to replace the requested version with. Can be anything
|
|
|
|
* a normal endpoint can return, or null to not hijack the request.
|
2021-01-08 09:30:14 -05:00
|
|
|
* @param WP_REST_Server $server Server instance.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
|
|
|
*/
|
|
|
|
$result = apply_filters( 'rest_pre_dispatch', null, $this, $request );
|
|
|
|
|
|
|
|
if ( ! empty( $result ) ) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
$error = null;
|
|
|
|
$matched = $this->match_request_to_handler( $request );
|
|
|
|
|
|
|
|
if ( is_wp_error( $matched ) ) {
|
|
|
|
return $this->error_to_response( $matched );
|
|
|
|
}
|
|
|
|
|
|
|
|
list( $route, $handler ) = $matched;
|
|
|
|
|
|
|
|
if ( ! is_callable( $handler['callback'] ) ) {
|
|
|
|
$error = new WP_Error(
|
|
|
|
'rest_invalid_handler',
|
2020-09-16 15:23:07 -04:00
|
|
|
__( 'The handler for the route is invalid.' ),
|
2020-09-05 17:52:07 -04:00
|
|
|
array( 'status' => 500 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $error ) ) {
|
|
|
|
$check_required = $request->has_valid_params();
|
|
|
|
if ( is_wp_error( $check_required ) ) {
|
|
|
|
$error = $check_required;
|
|
|
|
} else {
|
|
|
|
$check_sanitized = $request->sanitize_params();
|
|
|
|
if ( is_wp_error( $check_sanitized ) ) {
|
|
|
|
$error = $check_sanitized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->respond_to_request( $request, $route, $handler, $error );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-10 13:12:03 -05:00
|
|
|
* Matches a request object to its handler.
|
2020-09-05 17:52:07 -04:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
* @return array|WP_Error The route and request handler on success or a WP_Error instance if no handler was found.
|
|
|
|
*/
|
2020-10-20 15:10:08 -04:00
|
|
|
protected function match_request_to_handler( $request ) {
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$method = $request->get_method();
|
|
|
|
$path = $request->get_route();
|
|
|
|
|
2020-02-24 13:07:08 -05:00
|
|
|
$with_namespace = array();
|
2020-02-10 22:22:05 -05:00
|
|
|
|
|
|
|
foreach ( $this->get_namespaces() as $namespace ) {
|
2020-02-24 13:07:08 -05:00
|
|
|
if ( 0 === strpos( trailingslashit( ltrim( $path, '/' ) ), $namespace ) ) {
|
|
|
|
$with_namespace[] = $this->get_routes( $namespace );
|
2020-02-10 22:22:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 13:07:08 -05:00
|
|
|
if ( $with_namespace ) {
|
|
|
|
$routes = array_merge( ...$with_namespace );
|
|
|
|
} else {
|
2020-02-10 22:22:05 -05:00
|
|
|
$routes = $this->get_routes();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $routes as $route => $handlers ) {
|
2017-08-03 15:55:43 -04:00
|
|
|
$match = preg_match( '@^' . $route . '$@i', $path, $matches );
|
2015-11-04 16:23:25 -05:00
|
|
|
|
|
|
|
if ( ! $match ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-03 15:55:43 -04:00
|
|
|
$args = array();
|
2020-01-28 19:45:18 -05:00
|
|
|
|
2017-08-03 15:55:43 -04:00
|
|
|
foreach ( $matches as $param => $value ) {
|
|
|
|
if ( ! is_int( $param ) ) {
|
|
|
|
$args[ $param ] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
foreach ( $handlers as $handler ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$callback = $handler['callback'];
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$response = null;
|
|
|
|
|
2016-02-16 00:51:26 -05:00
|
|
|
// Fallback to GET method if no HEAD method is registered.
|
|
|
|
$checked_method = $method;
|
|
|
|
if ( 'HEAD' === $method && empty( $handler['methods']['HEAD'] ) ) {
|
|
|
|
$checked_method = 'GET';
|
|
|
|
}
|
2015-12-03 11:34:25 -05:00
|
|
|
if ( empty( $handler['methods'][ $checked_method ] ) ) {
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_callable( $callback ) ) {
|
2020-09-05 17:52:07 -04:00
|
|
|
return array( $route, $handler );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
$request->set_url_params( $args );
|
|
|
|
$request->set_attributes( $handler );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
$defaults = array();
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
foreach ( $handler['args'] as $arg => $options ) {
|
|
|
|
if ( isset( $options['default'] ) ) {
|
|
|
|
$defaults[ $arg ] = $options['default'];
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
$request->set_default_params( $defaults );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
return array( $route, $handler );
|
|
|
|
}
|
|
|
|
}
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
return new WP_Error(
|
|
|
|
'rest_no_route',
|
2020-09-16 15:23:07 -04:00
|
|
|
__( 'No route was found matching the URL and request method.' ),
|
2020-09-05 17:52:07 -04:00
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches the request to the callback handler.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
* @param array $handler The matched route handler.
|
|
|
|
* @param string $route The matched route regex.
|
|
|
|
* @param WP_Error|null $response The current error object if any.
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
2020-10-20 15:10:08 -04:00
|
|
|
protected function respond_to_request( $request, $route, $handler, $response ) {
|
2020-09-05 17:52:07 -04:00
|
|
|
/**
|
|
|
|
* Filters the response before executing any REST API callbacks.
|
|
|
|
*
|
|
|
|
* Allows plugins to perform additional validation after a
|
|
|
|
* request is initialized and matched to a registered route,
|
|
|
|
* but before it is executed.
|
|
|
|
*
|
|
|
|
* Note that this filter will not be called for requests that
|
|
|
|
* fail to authenticate or match to a registered route.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
|
|
|
|
* @param array $handler Route handler used for the request.
|
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
|
|
|
*/
|
|
|
|
$response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request );
|
|
|
|
|
|
|
|
// Check permission specified on the route.
|
|
|
|
if ( ! is_wp_error( $response ) && ! empty( $handler['permission_callback'] ) ) {
|
|
|
|
$permission = call_user_func( $handler['permission_callback'], $request );
|
|
|
|
|
|
|
|
if ( is_wp_error( $permission ) ) {
|
|
|
|
$response = $permission;
|
|
|
|
} elseif ( false === $permission || null === $permission ) {
|
|
|
|
$response = new WP_Error(
|
|
|
|
'rest_forbidden',
|
|
|
|
__( 'Sorry, you are not allowed to do that.' ),
|
|
|
|
array( 'status' => rest_authorization_required_code() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
if ( ! is_wp_error( $response ) ) {
|
|
|
|
/**
|
|
|
|
* Filters the REST dispatch request result.
|
|
|
|
*
|
|
|
|
* Allow plugins to override dispatching the request.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @since 4.5.0 Added `$route` and `$handler` parameters.
|
|
|
|
*
|
|
|
|
* @param mixed $dispatch_result Dispatch result, will be used if not empty.
|
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
|
|
|
* @param string $route Route matched for the request.
|
|
|
|
* @param array $handler Route handler used for the request.
|
|
|
|
*/
|
|
|
|
$dispatch_result = apply_filters( 'rest_dispatch_request', null, $request, $route, $handler );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
// Allow plugins to halt the request via this filter.
|
|
|
|
if ( null !== $dispatch_result ) {
|
|
|
|
$response = $dispatch_result;
|
|
|
|
} else {
|
|
|
|
$response = call_user_func( $handler['callback'], $request );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 17:52:07 -04:00
|
|
|
/**
|
|
|
|
* Filters the response immediately after executing any REST API
|
|
|
|
* callbacks.
|
|
|
|
*
|
|
|
|
* Allows plugins to perform any needed cleanup, for example,
|
|
|
|
* to undo changes made during the {@see 'rest_request_before_callbacks'}
|
|
|
|
* filter.
|
|
|
|
*
|
|
|
|
* Note that this filter will not be called for requests that
|
|
|
|
* fail to authenticate or match to a registered route.
|
|
|
|
*
|
|
|
|
* Note that an endpoint's `permission_callback` can still be
|
|
|
|
* called after this filter - see `rest_send_allow_header()`.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
|
|
|
|
* @param array $handler Route handler used for the request.
|
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
|
|
|
*/
|
|
|
|
$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );
|
|
|
|
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
$response = $this->error_to_response( $response );
|
|
|
|
} else {
|
|
|
|
$response = rest_ensure_response( $response );
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->set_matched_route( $route );
|
|
|
|
$response->set_matched_handler( $handler );
|
|
|
|
|
|
|
|
return $response;
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if an error occurred during most recent JSON encode/decode.
|
|
|
|
*
|
|
|
|
* Strings to be translated will be in format like
|
|
|
|
* "Encoding error: Maximum stack depth exceeded".
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
2021-01-03 17:04:04 -05:00
|
|
|
* @return false|string Boolean false or string error message.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
protected function get_json_last_error() {
|
|
|
|
$last_error_code = json_last_error();
|
|
|
|
|
2019-09-20 16:08:57 -04:00
|
|
|
if ( JSON_ERROR_NONE === $last_error_code || empty( $last_error_code ) ) {
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_last_error_msg();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the site index.
|
|
|
|
*
|
|
|
|
* This endpoint describes the capabilities of the site.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param array $request {
|
|
|
|
* Request.
|
|
|
|
*
|
|
|
|
* @type string $context Context.
|
|
|
|
* }
|
2019-11-05 16:23:02 -05:00
|
|
|
* @return WP_REST_Response The API root index data.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
public function get_index( $request ) {
|
|
|
|
// General site data.
|
|
|
|
$available = array(
|
2018-04-23 00:12:20 -04:00
|
|
|
'name' => get_option( 'blogname' ),
|
|
|
|
'description' => get_option( 'blogdescription' ),
|
|
|
|
'url' => get_option( 'siteurl' ),
|
|
|
|
'home' => home_url(),
|
|
|
|
'gmt_offset' => get_option( 'gmt_offset' ),
|
|
|
|
'timezone_string' => get_option( 'timezone_string' ),
|
|
|
|
'namespaces' => array_keys( $this->namespaces ),
|
|
|
|
'authentication' => array(),
|
|
|
|
'routes' => $this->get_data_for_routes( $this->get_routes(), $request['context'] ),
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = new WP_REST_Response( $available );
|
|
|
|
$response->add_link( 'help', 'http://v2.wp-api.org/' );
|
2021-01-03 16:47:05 -05:00
|
|
|
$this->add_active_theme_link_to_index( $response );
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
/**
|
2020-11-16 06:04:12 -05:00
|
|
|
* Filters the REST API root index data.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* This contains the data describing the API. This includes information
|
|
|
|
* about supported authentication schemes, supported namespaces, routes
|
|
|
|
* available on the API, and a small amount of data about the site.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response Response data.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'rest_index', $response );
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:47:05 -05:00
|
|
|
/**
|
|
|
|
* Adds a link to the active theme for users who have proper permissions.
|
|
|
|
*
|
|
|
|
* @since 5.7.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response REST API response.
|
|
|
|
*/
|
|
|
|
protected function add_active_theme_link_to_index( WP_REST_Response $response ) {
|
|
|
|
$should_add = current_user_can( 'switch_themes' ) || current_user_can( 'manage_network_themes' );
|
|
|
|
|
|
|
|
if ( ! $should_add && current_user_can( 'edit_posts' ) ) {
|
|
|
|
$should_add = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $should_add ) {
|
|
|
|
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
|
|
|
|
if ( current_user_can( $post_type->cap->edit_posts ) ) {
|
|
|
|
$should_add = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $should_add ) {
|
|
|
|
$theme = wp_get_theme();
|
|
|
|
$response->add_link( 'https://api.w.org/active-theme', rest_url( 'wp/v2/themes/' . $theme->get_stylesheet() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
/**
|
|
|
|
* Retrieves the index for a namespace.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request REST request instance.
|
|
|
|
* @return WP_REST_Response|WP_Error WP_REST_Response instance if the index was found,
|
|
|
|
* WP_Error if the namespace isn't set.
|
|
|
|
*/
|
|
|
|
public function get_namespace_index( $request ) {
|
|
|
|
$namespace = $request['namespace'];
|
|
|
|
|
|
|
|
if ( ! isset( $this->namespaces[ $namespace ] ) ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
return new WP_Error(
|
|
|
|
'rest_invalid_namespace',
|
|
|
|
__( 'The specified namespace could not be found.' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$routes = $this->namespaces[ $namespace ];
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$endpoints = array_intersect_key( $this->get_routes(), $routes );
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$data = array(
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
'namespace' => $namespace,
|
2017-11-30 18:11:00 -05:00
|
|
|
'routes' => $this->get_data_for_routes( $endpoints, $request['context'] ),
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
);
|
|
|
|
$response = rest_ensure_response( $data );
|
|
|
|
|
|
|
|
// Link to the root index.
|
|
|
|
$response->add_link( 'up', rest_url( '/' ) );
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters the namespace index data.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* This typically is just the route data for the namespace, but you can
|
|
|
|
* add any data you'd like here.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response Response data.
|
|
|
|
* @param WP_REST_Request $request Request data. The namespace is passed as the 'namespace' parameter.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'rest_namespace_index', $response, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the publicly-visible data for routes.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param array $routes Routes to get data for.
|
|
|
|
* @param string $context Optional. Context for data. Accepts 'view' or 'help'. Default 'view'.
|
2019-11-05 16:23:02 -05:00
|
|
|
* @return array[] Route data to expose in indexes, keyed by route.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
public function get_data_for_routes( $routes, $context = 'view' ) {
|
|
|
|
$available = array();
|
|
|
|
|
|
|
|
// Find the available routes.
|
|
|
|
foreach ( $routes as $route => $callbacks ) {
|
|
|
|
$data = $this->get_data_for_route( $route, $callbacks, $context );
|
|
|
|
if ( empty( $data ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters the REST endpoint data.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Request data. The namespace is passed as the 'namespace' parameter.
|
|
|
|
*/
|
|
|
|
$available[ $route ] = apply_filters( 'rest_endpoints_description', $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:08:29 -04:00
|
|
|
* Filters the publicly-visible data for routes.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*
|
|
|
|
* This data is exposed on indexes and can be used by clients or
|
|
|
|
* developers to investigate the site and find out how to use it. It
|
|
|
|
* acts as a form of self-documentation.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
2019-11-05 16:23:02 -05:00
|
|
|
* @param array[] $available Route data to expose in indexes, keyed by route.
|
|
|
|
* @param array $routes Internal route data as an associative array.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
*/
|
|
|
|
return apply_filters( 'rest_route_data', $available, $routes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves publicly-visible data for the route.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param string $route Route to get data for.
|
|
|
|
* @param array $callbacks Callbacks to convert to data.
|
|
|
|
* @param string $context Optional. Context for the data. Accepts 'view' or 'help'. Default 'view'.
|
|
|
|
* @return array|null Data for the route, or null if no publicly-visible data.
|
|
|
|
*/
|
|
|
|
public function get_data_for_route( $route, $callbacks, $context = 'view' ) {
|
|
|
|
$data = array(
|
|
|
|
'namespace' => '',
|
2017-11-30 18:11:00 -05:00
|
|
|
'methods' => array(),
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
'endpoints' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( isset( $this->route_options[ $route ] ) ) {
|
|
|
|
$options = $this->route_options[ $route ];
|
|
|
|
|
|
|
|
if ( isset( $options['namespace'] ) ) {
|
|
|
|
$data['namespace'] = $options['namespace'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $options['schema'] ) && 'help' === $context ) {
|
|
|
|
$data['schema'] = call_user_func( $options['schema'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 16:19:09 -04:00
|
|
|
$allowed_schema_keywords = array_flip( rest_get_allowed_schema_keywords() );
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$route = preg_replace( '#\(\?P<(\w+?)>.*?\)#', '{$1}', $route );
|
|
|
|
|
|
|
|
foreach ( $callbacks as $callback ) {
|
|
|
|
// Skip to the next route if any callback is hidden.
|
|
|
|
if ( empty( $callback['show_in_index'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['methods'] = array_merge( $data['methods'], array_keys( $callback['methods'] ) );
|
2017-11-30 18:11:00 -05:00
|
|
|
$endpoint_data = array(
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
'methods' => array_keys( $callback['methods'] ),
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( isset( $callback['args'] ) ) {
|
|
|
|
$endpoint_data['args'] = array();
|
2020-01-28 19:45:18 -05:00
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
foreach ( $callback['args'] as $key => $opts ) {
|
2020-10-20 16:19:09 -04:00
|
|
|
$arg_data = array_intersect_key( $opts, $allowed_schema_keywords );
|
|
|
|
$arg_data['required'] = ! empty( $opts['required'] );
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
$endpoint_data['args'][ $key ] = $arg_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['endpoints'][] = $endpoint_data;
|
|
|
|
|
|
|
|
// For non-variable routes, generate links.
|
|
|
|
if ( strpos( $route, '{' ) === false ) {
|
|
|
|
$data['_links'] = array(
|
2020-02-19 19:55:05 -05:00
|
|
|
'self' => array(
|
|
|
|
array(
|
|
|
|
'href' => rest_url( $route ),
|
|
|
|
),
|
|
|
|
),
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $data['methods'] ) ) {
|
|
|
|
// No methods supported, hide the route.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:10:08 -04:00
|
|
|
/**
|
|
|
|
* Gets the maximum number of requests that can be included in a batch.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
|
|
|
* @return int The maximum requests.
|
|
|
|
*/
|
|
|
|
protected function get_max_batch_size() {
|
|
|
|
/**
|
|
|
|
* Filters the maximum number of requests that can be included in a batch.
|
|
|
|
*
|
2020-12-10 18:27:06 -05:00
|
|
|
* @since 5.6.0
|
|
|
|
*
|
2020-10-20 15:10:08 -04:00
|
|
|
* @param int $max_size The maximum size.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'rest_get_max_batch_size', 25 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serves the batch/v1 request.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $batch_request The batch request object.
|
|
|
|
* @return WP_REST_Response The generated response object.
|
|
|
|
*/
|
|
|
|
public function serve_batch_request_v1( WP_REST_Request $batch_request ) {
|
|
|
|
$requests = array();
|
|
|
|
|
|
|
|
foreach ( $batch_request['requests'] as $args ) {
|
|
|
|
$parsed_url = wp_parse_url( $args['path'] );
|
|
|
|
|
|
|
|
if ( false === $parsed_url ) {
|
|
|
|
$requests[] = new WP_Error( 'parse_path_failed', __( 'Could not parse the path.' ), array( 'status' => 400 ) );
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$single_request = new WP_REST_Request( isset( $args['method'] ) ? $args['method'] : 'POST', $parsed_url['path'] );
|
|
|
|
|
|
|
|
if ( ! empty( $parsed_url['query'] ) ) {
|
|
|
|
$query_args = null; // Satisfy linter.
|
|
|
|
wp_parse_str( $parsed_url['query'], $query_args );
|
|
|
|
$single_request->set_query_params( $query_args );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $args['body'] ) ) {
|
|
|
|
$single_request->set_body_params( $args['body'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $args['headers'] ) ) {
|
|
|
|
$single_request->set_headers( $args['headers'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$requests[] = $single_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
$matches = array();
|
|
|
|
$validation = array();
|
|
|
|
$has_error = false;
|
|
|
|
|
|
|
|
foreach ( $requests as $single_request ) {
|
|
|
|
$match = $this->match_request_to_handler( $single_request );
|
|
|
|
$matches[] = $match;
|
|
|
|
$error = null;
|
|
|
|
|
|
|
|
if ( is_wp_error( $match ) ) {
|
|
|
|
$error = $match;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $error ) {
|
|
|
|
list( $route, $handler ) = $match;
|
|
|
|
|
|
|
|
if ( isset( $handler['allow_batch'] ) ) {
|
|
|
|
$allow_batch = $handler['allow_batch'];
|
|
|
|
} else {
|
|
|
|
$route_options = $this->get_route_options( $route );
|
|
|
|
$allow_batch = isset( $route_options['allow_batch'] ) ? $route_options['allow_batch'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_array( $allow_batch ) || empty( $allow_batch['v1'] ) ) {
|
|
|
|
$error = new WP_Error(
|
|
|
|
'rest_batch_not_allowed',
|
|
|
|
__( 'The requested route does not support batch requests.' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $error ) {
|
|
|
|
$check_required = $single_request->has_valid_params();
|
|
|
|
if ( is_wp_error( $check_required ) ) {
|
|
|
|
$error = $check_required;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $error ) {
|
|
|
|
$check_sanitized = $single_request->sanitize_params();
|
|
|
|
if ( is_wp_error( $check_sanitized ) ) {
|
|
|
|
$error = $check_sanitized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $error ) {
|
|
|
|
$has_error = true;
|
|
|
|
$validation[] = $error;
|
|
|
|
} else {
|
|
|
|
$validation[] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$responses = array();
|
|
|
|
|
|
|
|
if ( $has_error && 'require-all-validate' === $batch_request['validation'] ) {
|
|
|
|
foreach ( $validation as $valid ) {
|
|
|
|
if ( is_wp_error( $valid ) ) {
|
|
|
|
$responses[] = $this->envelope_response( $this->error_to_response( $valid ), false )->get_data();
|
|
|
|
} else {
|
|
|
|
$responses[] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_REST_Response(
|
|
|
|
array(
|
|
|
|
'failed' => 'validation',
|
|
|
|
'responses' => $responses,
|
|
|
|
),
|
|
|
|
WP_Http::MULTI_STATUS
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $requests as $i => $single_request ) {
|
|
|
|
$clean_request = clone $single_request;
|
|
|
|
$clean_request->set_url_params( array() );
|
|
|
|
$clean_request->set_attributes( array() );
|
|
|
|
$clean_request->set_default_params( array() );
|
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
|
|
|
|
$result = apply_filters( 'rest_pre_dispatch', null, $this, $clean_request );
|
|
|
|
|
|
|
|
if ( empty( $result ) ) {
|
|
|
|
$match = $matches[ $i ];
|
|
|
|
$error = null;
|
|
|
|
|
|
|
|
if ( is_wp_error( $validation[ $i ] ) ) {
|
|
|
|
$error = $validation[ $i ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_wp_error( $match ) ) {
|
|
|
|
$result = $this->error_to_response( $match );
|
|
|
|
} else {
|
|
|
|
list( $route, $handler ) = $match;
|
|
|
|
|
|
|
|
if ( ! $error && ! is_callable( $handler['callback'] ) ) {
|
|
|
|
$error = new WP_Error(
|
|
|
|
'rest_invalid_handler',
|
|
|
|
__( 'The handler for the route is invalid' ),
|
|
|
|
array( 'status' => 500 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->respond_to_request( $single_request, $route, $handler, $error );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
|
|
|
|
$result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $single_request );
|
|
|
|
|
|
|
|
$responses[] = $this->envelope_response( $result, false )->get_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_REST_Response( array( 'responses' => $responses ), WP_Http::MULTI_STATUS );
|
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
/**
|
|
|
|
* Sends an HTTP status code.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param int $code HTTP status.
|
|
|
|
*/
|
|
|
|
protected function set_status( $code ) {
|
|
|
|
status_header( $code );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an HTTP header.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param string $key Header key.
|
|
|
|
* @param string $value Header value.
|
|
|
|
*/
|
|
|
|
public function send_header( $key, $value ) {
|
|
|
|
/*
|
|
|
|
* Sanitize as per RFC2616 (Section 4.2):
|
|
|
|
*
|
|
|
|
* Any LWS that occurs between field-content MAY be replaced with a
|
|
|
|
* single SP before interpreting the field value or forwarding the
|
|
|
|
* message downstream.
|
|
|
|
*/
|
|
|
|
$value = preg_replace( '/\s+/', ' ', $value );
|
|
|
|
header( sprintf( '%s: %s', $key, $value ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends multiple HTTP headers.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param array $headers Map of header name to header value.
|
|
|
|
*/
|
|
|
|
public function send_headers( $headers ) {
|
|
|
|
foreach ( $headers as $key => $value ) {
|
|
|
|
$this->send_header( $key, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 16:27:44 -04:00
|
|
|
/**
|
|
|
|
* Removes an HTTP header from the current response.
|
|
|
|
*
|
|
|
|
* @since 4.8.0
|
|
|
|
*
|
|
|
|
* @param string $key Header key.
|
|
|
|
*/
|
|
|
|
public function remove_header( $key ) {
|
2019-07-09 01:45:58 -04:00
|
|
|
header_remove( $key );
|
2017-05-19 16:27:44 -04:00
|
|
|
}
|
|
|
|
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
/**
|
|
|
|
* Retrieves the raw request entity (body).
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @global string $HTTP_RAW_POST_DATA Raw post data.
|
|
|
|
*
|
|
|
|
* @return string Raw request data.
|
|
|
|
*/
|
2015-11-25 17:22:25 -05:00
|
|
|
public static function get_raw_data() {
|
2020-06-08 15:55:10 -04:00
|
|
|
// phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
global $HTTP_RAW_POST_DATA;
|
|
|
|
|
2020-06-08 15:55:10 -04:00
|
|
|
// $HTTP_RAW_POST_DATA was deprecated in PHP 5.6 and removed in PHP 7.0.
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
if ( ! isset( $HTTP_RAW_POST_DATA ) ) {
|
|
|
|
$HTTP_RAW_POST_DATA = file_get_contents( 'php://input' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $HTTP_RAW_POST_DATA;
|
2020-06-08 15:55:10 -04:00
|
|
|
// phpcs:enable
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts headers from a PHP-style $_SERVER array.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @param array $server Associative array similar to `$_SERVER`.
|
|
|
|
* @return array Headers extracted from the input.
|
|
|
|
*/
|
|
|
|
public function get_headers( $server ) {
|
|
|
|
$headers = array();
|
|
|
|
|
|
|
|
// CONTENT_* headers are not prefixed with HTTP_.
|
2017-11-30 18:11:00 -05:00
|
|
|
$additional = array(
|
|
|
|
'CONTENT_LENGTH' => true,
|
|
|
|
'CONTENT_MD5' => true,
|
|
|
|
'CONTENT_TYPE' => true,
|
|
|
|
);
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
|
|
|
|
foreach ( $server as $key => $value ) {
|
|
|
|
if ( strpos( $key, 'HTTP_' ) === 0 ) {
|
|
|
|
$headers[ substr( $key, 5 ) ] = $value;
|
2020-02-10 11:08:16 -05:00
|
|
|
} elseif ( 'REDIRECT_HTTP_AUTHORIZATION' === $key && empty( $server['HTTP_AUTHORIZATION'] ) ) {
|
|
|
|
/*
|
|
|
|
* In some server configurations, the authorization header is passed in this alternate location.
|
|
|
|
* Since it would not be passed in in both places we do not check for both headers and resolve.
|
|
|
|
*/
|
|
|
|
$headers['AUTHORIZATION'] = $value;
|
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.
Built from https://develop.svn.wordpress.org/trunk@34928
git-svn-id: http://core.svn.wordpress.org/trunk@34893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-07 22:31:25 -04:00
|
|
|
} elseif ( isset( $additional[ $key ] ) ) {
|
|
|
|
$headers[ $key ] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
}
|