REST API: Introduce baby API to the world.
Baby API was born at 2.8KLOC on October 8th at 2:30 UTC. API has lots
of growing to do, so wish it the best of luck.
Thanks to everyone who helped along the way:
Props rmccue, rachelbaker, danielbachhuber, joehoyle, drewapicture,
adamsilverstein, netweb, tlovett1, shelob9, kadamwhite, pento,
westonruter, nikv, tobych, redsweater, alecuf, pollyplummer, hurtige,
bpetty, oso96_2000, ericlewis, wonderboymusic, joshkadis, mordauk,
jdgrimes, johnbillion, jeremyfelt, thiago-negri, jdolan, pkevan,
iseulde, thenbrent, maxcutler, kwight, markoheijnen, phh, natewr,
jjeaton, shprink, mattheu, quasel, jmusal, codebykat, hubdotcom,
tapsboy, QWp6t, pushred, jaredcobb, justinsainton, japh, matrixik,
jorbin, frozzare, codfish, michael-arestad, kellbot, ironpaperweight,
simonlampen, alisspers, eliorivero, davidbhayes, JohnDittmar, dimadin,
traversal, cmmarslender, Toddses, kokarn, welcher, and ericpedia.
Fixes #33982.
git-svn-id: https://develop.svn.wordpress.org/trunk@34928 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-08 04:30:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Spy_REST_Server extends WP_REST_Server {
|
2015-12-05 00:35:54 +01:00
|
|
|
|
2017-12-01 00:09:33 +01:00
|
|
|
public $sent_headers = array();
|
|
|
|
public $sent_body = '';
|
|
|
|
public $last_request = null;
|
2016-11-23 03:46:42 +01:00
|
|
|
public $override_by_default = false;
|
2015-12-05 00:35:54 +01: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.
git-svn-id: https://develop.svn.wordpress.org/trunk@34928 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-08 04:30:18 +02:00
|
|
|
/**
|
2020-02-11 01:18:28 +01:00
|
|
|
* Gets the raw $endpoints data from the server.
|
REST API: Introduce baby API to the world.
Baby API was born at 2.8KLOC on October 8th at 2:30 UTC. API has lots
of growing to do, so wish it the best of luck.
Thanks to everyone who helped along the way:
Props rmccue, rachelbaker, danielbachhuber, joehoyle, drewapicture,
adamsilverstein, netweb, tlovett1, shelob9, kadamwhite, pento,
westonruter, nikv, tobych, redsweater, alecuf, pollyplummer, hurtige,
bpetty, oso96_2000, ericlewis, wonderboymusic, joshkadis, mordauk,
jdgrimes, johnbillion, jeremyfelt, thiago-negri, jdolan, pkevan,
iseulde, thenbrent, maxcutler, kwight, markoheijnen, phh, natewr,
jjeaton, shprink, mattheu, quasel, jmusal, codebykat, hubdotcom,
tapsboy, QWp6t, pushred, jaredcobb, justinsainton, japh, matrixik,
jorbin, frozzare, codfish, michael-arestad, kellbot, ironpaperweight,
simonlampen, alisspers, eliorivero, davidbhayes, JohnDittmar, dimadin,
traversal, cmmarslender, Toddses, kokarn, welcher, and ericpedia.
Fixes #33982.
git-svn-id: https://develop.svn.wordpress.org/trunk@34928 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-08 04:30:18 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_raw_endpoint_data() {
|
|
|
|
return $this->endpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-11 01:18:28 +01:00
|
|
|
* Allow calling protected methods from tests.
|
REST API: Introduce baby API to the world.
Baby API was born at 2.8KLOC on October 8th at 2:30 UTC. API has lots
of growing to do, so wish it the best of luck.
Thanks to everyone who helped along the way:
Props rmccue, rachelbaker, danielbachhuber, joehoyle, drewapicture,
adamsilverstein, netweb, tlovett1, shelob9, kadamwhite, pento,
westonruter, nikv, tobych, redsweater, alecuf, pollyplummer, hurtige,
bpetty, oso96_2000, ericlewis, wonderboymusic, joshkadis, mordauk,
jdgrimes, johnbillion, jeremyfelt, thiago-negri, jdolan, pkevan,
iseulde, thenbrent, maxcutler, kwight, markoheijnen, phh, natewr,
jjeaton, shprink, mattheu, quasel, jmusal, codebykat, hubdotcom,
tapsboy, QWp6t, pushred, jaredcobb, justinsainton, japh, matrixik,
jorbin, frozzare, codfish, michael-arestad, kellbot, ironpaperweight,
simonlampen, alisspers, eliorivero, davidbhayes, JohnDittmar, dimadin,
traversal, cmmarslender, Toddses, kokarn, welcher, and ericpedia.
Fixes #33982.
git-svn-id: https://develop.svn.wordpress.org/trunk@34928 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-08 04:30:18 +02:00
|
|
|
*
|
2020-02-11 01:18:28 +01:00
|
|
|
* @param string $method Method to call.
|
|
|
|
* @param array $args Arguments to pass to the 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.
git-svn-id: https://develop.svn.wordpress.org/trunk@34928 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-08 04:30:18 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __call( $method, $args ) {
|
|
|
|
return call_user_func_array( array( $this, $method ), $args );
|
|
|
|
}
|
2015-12-05 00:35:54 +01:00
|
|
|
|
2020-02-11 01:18:28 +01:00
|
|
|
/**
|
|
|
|
* Adds a header to the list of sent headers.
|
|
|
|
*
|
|
|
|
* @param string $header Header name.
|
|
|
|
* @param string $value Header value.
|
|
|
|
*/
|
2015-12-05 00:35:54 +01:00
|
|
|
public function send_header( $header, $value ) {
|
|
|
|
$this->sent_headers[ $header ] = $value;
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:18:28 +01:00
|
|
|
/**
|
|
|
|
* Removes a header from the list of sent headers.
|
|
|
|
*
|
|
|
|
* @param string $header Header name.
|
|
|
|
*/
|
2017-05-19 22:26:48 +02:00
|
|
|
public function remove_header( $header ) {
|
|
|
|
unset( $this->sent_headers[ $header ] );
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:01:11 +02:00
|
|
|
/**
|
2020-02-11 01:18:28 +01:00
|
|
|
* Overrides the dispatch method so we can get a handle on the request object.
|
2016-04-06 23:01:11 +02:00
|
|
|
*
|
2020-02-11 01:18:28 +01:00
|
|
|
* @param WP_REST_Request $request Request to attempt dispatching.
|
2016-04-06 23:01:11 +02:00
|
|
|
* @return WP_REST_Response Response returned by the callback.
|
|
|
|
*/
|
|
|
|
public function dispatch( $request ) {
|
|
|
|
$this->last_request = $request;
|
|
|
|
return parent::dispatch( $request );
|
|
|
|
}
|
|
|
|
|
2016-11-23 03:46:42 +01:00
|
|
|
/**
|
2020-02-11 01:18:28 +01:00
|
|
|
* Overrides the register_route method so we can re-register routes internally if needed.
|
2016-11-23 03:46:42 +01:00
|
|
|
*
|
|
|
|
* @param string $namespace Namespace.
|
|
|
|
* @param string $route The REST route.
|
|
|
|
* @param array $route_args Route arguments.
|
|
|
|
* @param bool $override Optional. Whether the route should be overridden if it already exists.
|
2020-02-11 01:18:28 +01:00
|
|
|
* Default false. Also set `$GLOBALS['wp_rest_server']->override_by_default = true`
|
2016-11-23 03:46:42 +01:00
|
|
|
* to set overrides when you don't have access to the caller context.
|
|
|
|
*/
|
|
|
|
public function register_route( $namespace, $route, $route_args, $override = false ) {
|
|
|
|
parent::register_route( $namespace, $route, $route_args, $override || $this->override_by_default );
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:18:28 +01:00
|
|
|
/**
|
|
|
|
* Serves the request and returns the result.
|
|
|
|
*
|
|
|
|
* @param string $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used.
|
|
|
|
* Default null.
|
|
|
|
* @return null|false Null if not served and a HEAD request, false otherwise.
|
|
|
|
*/
|
2015-12-05 00:35:54 +01:00
|
|
|
public function serve_request( $path = null ) {
|
|
|
|
|
|
|
|
ob_start();
|
2017-12-01 00:09:33 +01:00
|
|
|
$result = parent::serve_request( $path );
|
2015-12-05 00:35:54 +01:00
|
|
|
$this->sent_body = ob_get_clean();
|
|
|
|
return $result;
|
|
|
|
}
|
REST API: Introduce baby API to the world.
Baby API was born at 2.8KLOC on October 8th at 2:30 UTC. API has lots
of growing to do, so wish it the best of luck.
Thanks to everyone who helped along the way:
Props rmccue, rachelbaker, danielbachhuber, joehoyle, drewapicture,
adamsilverstein, netweb, tlovett1, shelob9, kadamwhite, pento,
westonruter, nikv, tobych, redsweater, alecuf, pollyplummer, hurtige,
bpetty, oso96_2000, ericlewis, wonderboymusic, joshkadis, mordauk,
jdgrimes, johnbillion, jeremyfelt, thiago-negri, jdolan, pkevan,
iseulde, thenbrent, maxcutler, kwight, markoheijnen, phh, natewr,
jjeaton, shprink, mattheu, quasel, jmusal, codebykat, hubdotcom,
tapsboy, QWp6t, pushred, jaredcobb, justinsainton, japh, matrixik,
jorbin, frozzare, codfish, michael-arestad, kellbot, ironpaperweight,
simonlampen, alisspers, eliorivero, davidbhayes, JohnDittmar, dimadin,
traversal, cmmarslender, Toddses, kokarn, welcher, and ericpedia.
Fixes #33982.
git-svn-id: https://develop.svn.wordpress.org/trunk@34928 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-08 04:30:18 +02:00
|
|
|
}
|