This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
/
class-wp-json-customposttype.php
executable file
·215 lines (183 loc) · 6.14 KB
/
class-wp-json-customposttype.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Custom Post Type base class
*
* A sensible base for custom post type APIs
*/
/**
* Custom Post Type base class
*
* A sensible base for custom post type APIs
*/
abstract class WP_JSON_CustomPostType extends WP_JSON_Posts {
/**
* Base route name
*
* @var string Route base (e.g. /my-plugin/my-type)
*/
protected $base = null;
/**
* Associated post type
*
* @var string Type slug
*/
protected $type = null;
/**
* Construct the API handler object
*/
public function __construct(WP_JSON_ResponseHandler $server) {
if ( empty( $this->base ) ) {
_doing_it_wrong( 'WP_JSON_CustomPostType::__construct', __( 'The route base must be overridden' ), 'WPAPI-0.6' );
return;
}
if ( empty( $this->type ) ) {
_doing_it_wrong( 'WP_JSON_CustomPostType::__construct', __( 'The post type must be overridden' ), 'WPAPI-0.6' );
return;
}
add_filter( 'json_endpoints', array( $this, 'registerRoutes' ) );
add_filter( 'json_post_type_data', array( $this, 'type_archive_link' ), 10, 2 );
parent::__construct($server);
}
/**
* Register the routes for the post type
*
* @param array $routes Routes for the post type
* @return array Modified routes
*/
public function registerRoutes( $routes ) {
$routes[ $this->base ] = array(
array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
array( array( $this, 'newPost' ), WP_JSON_Server::CREATABLE ),
);
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
array( array( $this, 'getPost' ), WP_JSON_Server::READABLE ),
array( array( $this, 'editPost' ), WP_JSON_Server::EDITABLE ),
array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
);
return $routes;
}
/**
* Register revision-related routes for the post type
*
* @param array $routes Routes for the post type
* @return array Modified routes
*/
public function registerRevisionRoutes( $routes ) {
$routes[ $this->base . '/(?P<id>\d+)/revisions' ] = array(
array( '__return_null', WP_JSON_Server::READABLE ),
);
return $routes;
}
/**
* Register comment-related routes for the post type
*
* @param array $routes Routes for the post type
* @return array Modified routes
*/
public function registerCommentRoutes( $routes ) {
$routes[ $this->base . '/(?P<id>\d+)/comments'] = array(
array( array( $this, 'getComments' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
);
$routes[ $this->base . '/(?P<id>\d+)/comments/(?P<comment>\d+)' ] = array(
array( array( $this, 'getComment' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
array( '__return_null', WP_JSON_Server::DELETABLE ),
);
return $routes;
}
/**
* Retrieve posts
*
* Overrides the $type to set to $this->type, then passes through to the
* post endpoints.
*
* @see WP_JSON_Posts::getPosts()
*/
public function getPosts( $filter = array(), $context = 'view', $type = null, $page = 1 ) {
if ( !empty( $type ) && $type !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
return parent::getPosts( $filter, $context, $this->type, $page );
}
/**
* Retrieve a post
*
* @see WP_JSON_Posts::getPost()
*/
public function getPost( $id, $context = 'view' ) {
$id = (int) $id;
if ( empty( $id ) )
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
$post = get_post( $id, ARRAY_A );
if ( $post['post_type'] !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
return parent::getPost( $id, $context );
}
/**
* Edit a post
*
* @see WP_JSON_Posts::editPost()
*/
function editPost( $id, $data, $_headers = array() ) {
$id = (int) $id;
if ( empty( $id ) )
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
$post = get_post( $id, ARRAY_A );
if ( empty( $post['ID'] ) )
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
if ( $post['post_type'] !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
return parent::editPost( $id, $data, $_headers );
}
/**
* Delete a post
*
* @see WP_JSON_Posts::deletePost()
*/
public function deletePost( $id, $force = false ) {
$id = (int) $id;
if ( empty( $id ) )
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
$post = get_post( $id, ARRAY_A );
if ( $post['post_type'] !== $this->type )
return new WP_Error( 'json_post_invalid_type', __( 'Invalid post type' ), array( 'status' => 400 ) );
return parent::deletePost( $id, $force );
}
/**
* Prepare post data
*
* @param array $post The unprepared post data
* @param array $fields The subset of post type fields to return
* @return array The prepared post data
*/
protected function prepare_post( $post, $context = 'view' ) {
$_post = parent::prepare_post( $post, $context );
// Override entity meta keys with the correct links
$_post['meta'] = array(
'links' => array(
'self' => json_url( $this->base . '/' . $post['ID'] ),
'author' => json_url( '/users/' . $post['post_author'] ),
'collection' => json_url( $this->base ),
'replies' => json_url( $this->base . '/' . $post['ID'] . '/comments' ),
'version-history' => json_url( $this->base . '/' . $post['ID'] . '/revisions' ),
),
);
if ( ! empty( $post['post_parent'] ) )
$_post['meta']['links']['up'] = json_url( $this->base . '/' . $post['ID'] );
return apply_filters( 'json_prepare_{$this->type}', $_post, $post, $context );
}
/**
* Filter the post type archive link
*
* @param array $data Post type data
* @param stdClass $type Internal post type data
* @return array Filtered data
*/
public function type_archive_link( $data, $type ) {
if ( $type->name !== $this->type ) {
return $data;
}
$data['meta']['links']['archives'] = json_url( $this->base );
return $data;
}
}