| 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 |
* Default permission callback for this controller's routes. |
| 59 |
* |
| 60 |
* @param WP_REST_Request $request Full data about the request. |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
abstract public function permission_callback( WP_REST_Request $request ): bool; |
| 65 |
|
| 66 |
/** |
| 67 |
* Check if a given request has access to get items. |
| 68 |
* |
| 69 |
* @param WP_REST_Request $request Full data about the request. |
| 70 |
* |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public function get_items_permissions_check( $request ): bool { |
| 74 |
return $this->permission_callback( $request ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Check if a given request has access to get a specific item. |
| 79 |
* |
| 80 |
* @param WP_REST_Request $request Full data about the request. |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public function get_item_permissions_check( $request ): bool { |
| 85 |
return $this->permission_callback( $request ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Check if a given request has access to create items. |
| 90 |
* |
| 91 |
* @param WP_REST_Request $request Full data about the request. |
| 92 |
* |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
public function create_item_permissions_check( $request ): bool { |
| 96 |
return $this->permission_callback( $request ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Check if a given request has access to update a specific item. |
| 101 |
* |
| 102 |
* @param WP_REST_Request $request Full data about the request. |
| 103 |
* |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
public function update_item_permissions_check( $request ): bool { |
| 107 |
return $this->permission_callback( $request ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Check if a given request has access to delete a specific item. |
| 112 |
* |
| 113 |
* @param WP_REST_Request $request Full data about the request. |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public function delete_item_permissions_check( $request ): bool { |
| 118 |
return $this->permission_callback( $request ); |
| 119 |
} |
| 120 |
} |
| 121 |
|