Skip to content

Commit b392114

Browse files
committed
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
1 parent 374b39d commit b392114

14 files changed

Lines changed: 4522 additions & 0 deletions

src/wp-includes/default-filters.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@
204204

205205
add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
206206

207+
// REST API filters.
208+
add_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
209+
add_action( 'wp_head', 'rest_output_link_wp_head', 10, 0 );
210+
add_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
211+
add_action( 'auth_cookie_malformed', 'rest_cookie_collect_status' );
212+
add_action( 'auth_cookie_expired', 'rest_cookie_collect_status' );
213+
add_action( 'auth_cookie_bad_username', 'rest_cookie_collect_status' );
214+
add_action( 'auth_cookie_bad_hash', 'rest_cookie_collect_status' );
215+
add_action( 'auth_cookie_valid', 'rest_cookie_collect_status' );
216+
add_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 );
217+
207218
// Actions
208219
add_action( 'wp_head', '_wp_render_title_tag', 1 );
209220
add_action( 'wp_head', 'wp_enqueue_scripts', 1 );
@@ -347,6 +358,11 @@
347358
add_action( 'register_new_user', 'wp_send_new_user_notifications' );
348359
add_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
349360

361+
// REST API actions.
362+
add_action( 'init', 'rest_api_init' );
363+
add_action( 'rest_api_init', 'rest_api_default_filters', 10, 1 );
364+
add_action( 'parse_request', 'rest_api_loaded' );
365+
350366
/**
351367
* Filters formerly mixed into wp-includes
352368
*/

src/wp-includes/rest-api.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* REST API functions.
4+
*
5+
* @package WordPress
6+
* @subpackage REST_API
7+
*/
8+
9+
/**
10+
* Version number for our API.
11+
*
12+
* @var string
13+
*/
14+
define( 'REST_API_VERSION', '2.0' );
15+
16+
/** WP_REST_Server class */
17+
require_once( ABSPATH . WPINC . '/rest-api/lib/class-wp-rest-server.php' );
18+
19+
/** WP_REST_Response class */
20+
require_once( ABSPATH . WPINC . '/rest-api/lib/class-wp-rest-response.php' );
21+
22+
/** WP_REST_Request class */
23+
require_once( ABSPATH . WPINC . '/rest-api/lib/class-wp-rest-request.php' );
24+
25+
/** REST functions */
26+
require_once( ABSPATH . WPINC . '/rest-api/rest-functions.php' );
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* REST API: WP_HTTP_Response class
4+
*
5+
* @package WordPress
6+
* @subpackage REST_API
7+
* @since 4.4.0
8+
*/
9+
10+
/**
11+
* Core class used to prepare HTTP responses.
12+
*
13+
* @since 4.4.0
14+
*/
15+
class WP_HTTP_Response {
16+
17+
/**
18+
* Response data.
19+
*
20+
* @since 4.4.0
21+
* @access public
22+
* @var mixed
23+
*/
24+
public $data;
25+
26+
/**
27+
* Response headers.
28+
*
29+
* @since 4.4.0
30+
* @access public
31+
* @var int
32+
*/
33+
public $headers;
34+
35+
/**
36+
* Response status.
37+
*
38+
* @since 4.4.0
39+
* @access public
40+
* @var array
41+
*/
42+
public $status;
43+
44+
/**
45+
* Constructor.
46+
*
47+
* @since 4.4.0
48+
* @access public
49+
*
50+
* @param mixed $data Response data. Default null.
51+
* @param int $status Optional. HTTP status code. Default 200.
52+
* @param array $headers Optional. HTTP header map. Default empty array.
53+
*/
54+
public function __construct( $data = null, $status = 200, $headers = array() ) {
55+
$this->data = $data;
56+
$this->set_status( $status );
57+
$this->set_headers( $headers );
58+
}
59+
60+
/**
61+
* Retrieves headers associated with the response.
62+
*
63+
* @since 4.4.0
64+
* @access public
65+
*
66+
* @return array Map of header name to header value.
67+
*/
68+
public function get_headers() {
69+
return $this->headers;
70+
}
71+
72+
/**
73+
* Sets all header values.
74+
*
75+
* @since 4.4.0
76+
* @access public
77+
*
78+
* @param array $headers Map of header name to header value.
79+
*/
80+
public function set_headers( $headers ) {
81+
$this->headers = $headers;
82+
}
83+
84+
/**
85+
* Sets a single HTTP header.
86+
*
87+
* @since 4.4.0
88+
* @access public
89+
*
90+
* @param string $key Header name.
91+
* @param string $value Header value.
92+
* @param bool $replace Optional. Whether to replace an existing header of the same name.
93+
* Default true.
94+
*/
95+
public function header( $key, $value, $replace = true ) {
96+
if ( $replace || ! isset( $this->headers[ $key ] ) ) {
97+
$this->headers[ $key ] = $value;
98+
} else {
99+
$this->headers[ $key ] .= ', ' . $value;
100+
}
101+
}
102+
103+
/**
104+
* Retrieves the HTTP return code for the response.
105+
*
106+
* @since 4.4.0
107+
* @access public
108+
*
109+
* @return int The 3-digit HTTP status code.
110+
*/
111+
public function get_status() {
112+
return $this->status;
113+
}
114+
115+
/**
116+
* Sets the 3-digit HTTP status code.
117+
*
118+
* @since 4.4.0
119+
* @access public
120+
*
121+
* @param int $code HTTP status.
122+
*/
123+
public function set_status( $code ) {
124+
$this->status = absint( $code );
125+
}
126+
127+
/**
128+
* Retrieves the response data.
129+
*
130+
* @since 4.4.0
131+
* @access public
132+
*
133+
* @return mixed Response data.
134+
*/
135+
public function get_data() {
136+
return $this->data;
137+
}
138+
139+
/**
140+
* Sets the response data.
141+
*
142+
* @since 4.4.0
143+
* @access public
144+
*
145+
* @param mixed $data Response data.
146+
*/
147+
public function set_data( $data ) {
148+
$this->data = $data;
149+
}
150+
151+
/**
152+
* Retrieves the response data for JSON serialization.
153+
*
154+
* It is expected that in most implementations, this will return the same as get_data(),
155+
* however this may be different if you want to do custom JSON data handling.
156+
*
157+
* @since 4.4.0
158+
* @access public
159+
*
160+
* @return mixed Any JSON-serializable value.
161+
*/
162+
public function jsonSerialize() {
163+
return $this->get_data();
164+
}
165+
}

0 commit comments

Comments
 (0)