PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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 4.0.0-beta.1, at php/REST_API/REST_Collection_Controller.php

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