PluginProbe
Code Snippets / trunk
Code Snippets vtrunk
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / REST_API / REST_Collection_Controller.php

REST_Collection_Controller.php in Code Snippets trunk, at php/REST_API/REST_Collection_Controller.php

120 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\REST_API;
4
5 use WP_REST_Controller;
6 use WP_REST_Request;
7 use const Code_Snippets\REST_API_NAMESPACE;
8
9 /**
10 * Base class for REST API controllers that manage collections of items.
11 */
12 abstract class REST_Collection_Controller extends WP_REST_Controller {
13
14 /**
15 * The version of the REST API this controller belongs to.
16 *
17 * @var string
18 */
19 public const VERSION = 0;
20
21 /**
22 * The base route for this controller, relative to the REST API namespace and version.
23 *
24 * @var string
25 */
26 public const BASE_ROUTE = '';
27
28 /**
29 * Retrieve this controller's REST API base path, including namespace.
30 *
31 * @return string
32 */
33 public static function get_base_route(): string {
34 return REST_API_NAMESPACE . static::VERSION . '/' . static::BASE_ROUTE;
35 }
36
37 /**
38 * Retrieve the full base route including the REST API prefix.
39 *
40 * @return string
41 */
42 public static function get_prefixed_base_route(): string {
43 return '/' . rtrim( rest_get_url_prefix(), '/\\' ) . '/' . self::get_base_route();
44 }
45
46 /**
47 * Class constrictor.
48 */
49 public function __construct() {
50 $this->namespace = REST_API_NAMESPACE . static::VERSION;
51 $this->rest_base = static::BASE_ROUTE;
52
53 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
54 }
55
56 /**
57 * Default permission callback for this controller's routes.
58 *
59 * @param WP_REST_Request $request Full data about the request.
60 *
61 * @return bool
62 */
63 abstract public function permission_callback( WP_REST_Request $request ): bool;
64
65 /**
66 * Check if a given request has access to get items.
67 *
68 * @param WP_REST_Request $request Full data about the request.
69 *
70 * @return bool
71 */
72 public function get_items_permissions_check( $request ): bool {
73 return $this->permission_callback( $request );
74 }
75
76 /**
77 * Check if a given request has access to get a specific item.
78 *
79 * @param WP_REST_Request $request Full data about the request.
80 *
81 * @return bool
82 */
83 public function get_item_permissions_check( $request ): bool {
84 return $this->permission_callback( $request );
85 }
86
87 /**
88 * Check if a given request has access to create items.
89 *
90 * @param WP_REST_Request $request Full data about the request.
91 *
92 * @return bool
93 */
94 public function create_item_permissions_check( $request ): bool {
95 return $this->permission_callback( $request );
96 }
97
98 /**
99 * Check if a given request has access to update a specific item.
100 *
101 * @param WP_REST_Request $request Full data about the request.
102 *
103 * @return bool
104 */
105 public function update_item_permissions_check( $request ): bool {
106 return $this->permission_callback( $request );
107 }
108
109 /**
110 * Check if a given request has access to delete a specific item.
111 *
112 * @param WP_REST_Request $request Full data about the request.
113 *
114 * @return bool
115 */
116 public function delete_item_permissions_check( $request ): bool {
117 return $this->permission_callback( $request );
118 }
119 }
120