1 | <?php |
||
6 | class WP_REST_React_Controller extends WP_REST_Controller { |
||
7 | /** |
||
8 | * The namespace of this controller's route. |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | public $namespace; |
||
13 | |||
14 | /** |
||
15 | * The base of this controller's route. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $rest_base; |
||
20 | |||
21 | /** |
||
22 | * Constructor. |
||
23 | */ |
||
24 | public function __construct() { |
||
28 | |||
29 | /** |
||
30 | * Register the routes for the objects of the controller. |
||
31 | */ |
||
32 | public function register_routes() { |
||
33 | register_rest_route( $this->namespace, $this->rest_base, array( |
||
34 | array( |
||
35 | 'methods' => WP_Rest_Server::READABLE, |
||
36 | 'callback' => array( $this, 'get_items' ), |
||
37 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
38 | 'args' => $this->get_collection_params(), |
||
39 | ), |
||
40 | array( |
||
41 | 'methods' => WP_Rest_Server::CREATABLE, |
||
42 | 'callback' => array( $this, 'create_item' ), |
||
43 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
44 | 'args' => $this->get_creation_params(), |
||
45 | ), |
||
46 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
47 | ) ); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Check if a given request has access to read reactions. |
||
52 | * |
||
53 | * @param WP_REST_Request $request Full details about the request. |
||
54 | * @return WP_Error|boolean |
||
55 | */ |
||
56 | public function get_items_permissions_check( $request ) { |
||
70 | |||
71 | /** |
||
72 | * Get a list of reactions. |
||
73 | * |
||
74 | * @param WP_REST_Request $request Full details about the request. |
||
75 | * @return WP_Error|WP_REST_Response |
||
76 | */ |
||
77 | public function get_items( $request ) { |
||
129 | |||
130 | /** |
||
131 | * Check if a given request has access to create a reaction |
||
132 | * |
||
133 | * @param WP_REST_Request $request Full details about the request. |
||
134 | * @return WP_Error|boolean |
||
135 | */ |
||
136 | public function create_item_permissions_check( $request ) { |
||
148 | |||
149 | /** |
||
150 | * Create a reaction. |
||
151 | * |
||
152 | * @param WP_REST_Request $request Full details about the request. |
||
153 | * @return WP_Error|WP_REST_Response |
||
154 | */ |
||
155 | public function create_item( $request ) { |
||
166 | |||
167 | /** |
||
168 | * Check if we can read a post. |
||
169 | * |
||
170 | * Correctly handles posts with the inherit status. |
||
171 | * |
||
172 | * @param object $post Post object. |
||
173 | * @return boolean Can we read it? |
||
174 | */ |
||
175 | public function check_read_post_permission( $post ) { |
||
180 | |||
181 | /** |
||
182 | * Prepare a reaction group output for response. |
||
183 | * |
||
184 | * @param array $reaction Reaction data. |
||
185 | * @param WP_REST_Request $request Request object. |
||
186 | * @return WP_REST_Response $response |
||
187 | */ |
||
188 | public function prepare_item_for_response( $reaction, $request ) { |
||
211 | |||
212 | /** |
||
213 | * Prepare a response for inserting into a collection. |
||
214 | * |
||
215 | * @param WP_REST_Response $response Response object. |
||
216 | * @return array Response data, ready for insertion into collection data. |
||
217 | */ |
||
218 | public function prepare_response_for_collection( $response ) { |
||
231 | |||
232 | /** |
||
233 | * Prepare links for the request. |
||
234 | * |
||
235 | * @param array $reaction Reaction. |
||
236 | * @return array Links for the given reaction. |
||
237 | */ |
||
238 | protected function prepare_links( $reaction ) { |
||
263 | |||
264 | /** |
||
265 | * Get the query params for collections |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public function get_collection_params() { |
||
282 | /** |
||
283 | * Get the query params for collections |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | public function get_creation_params() { |
||
307 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.