1 | <?php |
||
18 | abstract class Route extends \WP_REST_Controller { |
||
19 | /** |
||
20 | * Route Name |
||
21 | * |
||
22 | * @since 2.0 |
||
23 | * @access protected |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $route_name; |
||
27 | |||
28 | /** |
||
29 | * Sub type, forms {$namespace}/route_name/{id}/sub_type type endpoints |
||
30 | * |
||
31 | * @since 2.0 |
||
32 | * @access protected |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $sub_type; |
||
36 | |||
37 | /** |
||
38 | * Register the routes for the objects of the controller. |
||
39 | */ |
||
40 | 12 | public function register_routes() { |
|
164 | |||
165 | /** |
||
166 | * Get route name |
||
167 | * |
||
168 | * MUST SET route_name property in subclass! |
||
169 | * |
||
170 | * @since 2.0 |
||
171 | * @access protected |
||
172 | * @return string |
||
173 | */ |
||
174 | 12 | protected function get_route_name() { |
|
175 | 12 | if ( is_string( $this->route_name ) ) { |
|
176 | 12 | return $this->route_name; |
|
177 | } else { |
||
178 | _doing_it_wrong( __METHOD__, __( 'Must set route name in subclass.', 'gravityview' ), '2.0' ); |
||
179 | return ''; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get sub_type |
||
185 | * |
||
186 | * MUST SET sub_type property in subclass! |
||
187 | * |
||
188 | * @since 2.0 |
||
189 | * @access protected |
||
190 | * @return string |
||
191 | */ |
||
192 | 12 | protected function get_sub_type() { |
|
193 | 12 | if ( is_string( $this->sub_type ) ) { |
|
194 | 12 | return $this->sub_type; |
|
195 | } else { |
||
196 | _doing_it_wrong( __METHOD__, __( 'Must set route sub type in subclass.', 'gravityview' ), '2.0' ); |
||
197 | return ''; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Get a collection of items |
||
203 | * |
||
204 | * @param \WP_REST_Request $request Full data about the request. |
||
205 | * @return \WP_Error|\WP_REST_Response |
||
206 | */ |
||
207 | public function get_items( $request ) { |
||
208 | return $this->not_implemented(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get one item from the collection |
||
213 | * |
||
214 | * @param \WP_REST_Request $request Full data about the request. |
||
215 | * @return \WP_Error|\WP_REST_Response |
||
216 | */ |
||
217 | public function get_item( $request ) { |
||
218 | return $this->not_implemented(); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Create one item from the collection |
||
223 | * |
||
224 | * @param \WP_REST_Request $request Full data about the request. |
||
225 | * @return \WP_REST_Response |
||
226 | */ |
||
227 | public function create_item( $request ) { |
||
228 | return $this->not_implemented(); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Update one item from the collection |
||
233 | * |
||
234 | * @param \WP_REST_Request $request Full data about the request. |
||
235 | * @return \WP_REST_Response |
||
236 | */ |
||
237 | public function update_item( $request ) { |
||
238 | return $this->not_implemented(); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Delete one item from the collection |
||
243 | * |
||
244 | * @param \WP_REST_Request $request Full data about the request. |
||
245 | * @return \WP_REST_Response |
||
246 | */ |
||
247 | public function delete_item( $request ) { |
||
250 | |||
251 | |||
252 | /** |
||
253 | * Get a collection of items |
||
254 | * |
||
255 | * @param \WP_REST_Request $request Full data about the request. |
||
256 | * @return \WP_Error|\WP_REST_Response |
||
257 | */ |
||
258 | public function get_sub_items( $request ) { |
||
262 | |||
263 | /** |
||
264 | * Get one item from the collection |
||
265 | * |
||
266 | * @param \WP_REST_Request $request Full data about the request. |
||
267 | * @return \WP_Error|\WP_REST_Response |
||
268 | */ |
||
269 | public function get_sub_item( $request ) { |
||
270 | return $this->not_implemented(); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Create one item from the collection |
||
275 | * |
||
276 | * @param \WP_REST_Request $request Full data about the request. |
||
277 | * @return \WP_REST_Response |
||
278 | */ |
||
279 | public function create_sub_item( $request ) { |
||
280 | return $this->not_implemented(); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Update one item from the collection for sub items |
||
285 | * |
||
286 | * @param \WP_REST_Request $request Full data about the request. |
||
287 | * @return \WP_REST_Response |
||
288 | */ |
||
289 | public function update_sub_item( $request ) { |
||
290 | return $this->not_implemented(); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Delete one item from the collection for sub items |
||
295 | * |
||
296 | * @param \WP_REST_Request $request Full data about the request. |
||
297 | * @return \WP_REST_Response |
||
298 | */ |
||
299 | public function delete_sub_item( $request ) { |
||
300 | return $this->not_implemented(); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Check if a given request has access to get items |
||
305 | * |
||
306 | * @param \WP_REST_Request $request Full data about the request. |
||
307 | * @return \WP_REST_Response |
||
308 | */ |
||
309 | public function get_items_permissions_check( $request ) { |
||
310 | return $this->not_implemented(); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Check if a given request has access to get a specific item |
||
315 | * |
||
316 | * @param \WP_REST_Request $request Full data about the request. |
||
317 | * @return \WP_REST_Response |
||
318 | */ |
||
319 | public function get_item_permissions_check( $request ) { |
||
320 | return $this->not_implemented(); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Check if a given request has access to create items |
||
325 | * |
||
326 | * @param \WP_REST_Request $request Full data about the request. |
||
327 | * @return \WP_REST_Response |
||
328 | */ |
||
329 | public function create_item_permissions_check( $request ) { |
||
330 | return $this->not_implemented(); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Check if a given request has access to update a specific item |
||
335 | * |
||
336 | * @param \WP_REST_Request $request Full data about the request. |
||
337 | * @return \WP_REST_Response |
||
338 | */ |
||
339 | public function update_item_permissions_check( $request ) { |
||
340 | return $this->not_implemented(); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Check if a given request has access to delete a specific item |
||
345 | * |
||
346 | * @param \WP_REST_Request $request Full data about the request. |
||
347 | * @return \WP_REST_Response |
||
348 | */ |
||
349 | public function delete_item_permissions_check( $request ) { |
||
352 | |||
353 | /** |
||
354 | * Prepare the item for create or update operation |
||
355 | * |
||
356 | * @todo ZACK - Use this as genric prepare to save or remove from usage. |
||
357 | * @param \WP_REST_Request $request Request object |
||
358 | * @return \WP_REST_Response |
||
359 | */ |
||
360 | protected function prepare_item_for_database( $request ) { |
||
361 | return $this->not_implemented(); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Prepare the item for the REST response |
||
366 | * |
||
367 | * @todo ZACK - Use this as generic prepare for response or remove from usage |
||
368 | * |
||
369 | * @since 2.0 |
||
370 | * @param mixed $item WordPress representation of the item. |
||
371 | * @param \WP_REST_Request $request Request object. |
||
372 | * @return \WP_REST_Response |
||
373 | */ |
||
374 | public function prepare_item_for_response( $item, $request ) { |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Generic response for routes not yet implemented |
||
381 | * |
||
382 | * @since 2.0 |
||
383 | * @return \WP_REST_Response |
||
384 | */ |
||
385 | protected function not_implemented( ) { |
||
389 | |||
390 | /** |
||
391 | * Fallback if subclass doesn't define routes |
||
392 | * |
||
393 | * Returns empty array for args instead of making an error. |
||
394 | * |
||
395 | * @since 2.0 |
||
396 | * @param $method |
||
397 | * @return array |
||
398 | */ |
||
399 | 12 | public function __call( $method, $args ) { |
|
409 | } |
||
410 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.