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_Controller.php

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

68 lines 1.5 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_Request;
6 use const Code_Snippets\REST_API_NAMESPACE;
7
8 /**
9 * Base class for REST API controllers.
10 */
11 abstract class REST_Controller {
12
13 /**
14 * The version of the REST API this controller belongs to.
15 *
16 * @var string
17 */
18 public const VERSION = 0;
19
20 /**
21 * The base route for this controller, relative to the REST API namespace and version.
22 *
23 * @var string
24 */
25 public const BASE_ROUTE = '';
26
27 /**
28 * The namespace of this controller's route.
29 *
30 * @var string
31 */
32 protected string $namespace;
33
34 /**
35 * Class constructor.
36 */
37 public function __construct() {
38 assert( ! empty( static::VERSION ), get_class( $this ) . '::VERSION must be set' );
39 assert( ! empty( static::BASE_ROUTE ), get_class( $this ) . '::BASE_ROUTE must be set' );
40
41 $this->namespace = REST_API_NAMESPACE . static::VERSION;
42 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
43 }
44
45 /**
46 * Retrieve this controller's REST API base path, including namespace.
47 *
48 * @return string
49 */
50 public static function get_base_route(): string {
51 return REST_API_NAMESPACE . static::VERSION . '/' . static::BASE_ROUTE;
52 }
53
54 /**
55 * Register REST routes.
56 */
57 abstract public function register_routes();
58
59 /**
60 * Default permission callback for this controller's routes.
61 *
62 * @param WP_REST_Request $request Full data about the request.
63 *
64 * @return bool
65 */
66 abstract public function permission_callback( WP_REST_Request $request ): bool;
67 }
68