PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0
Elementor Website Builder – more than just a page builder v4.2.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / data / base / endpoint.php

endpoint.php in Elementor Website Builder – more than just a page builder 4.2.0, at data/base/endpoint.php

352 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Data\Base;
4
5 use Elementor\Data\Manager;
6 use WP_REST_Server;
7
8 abstract class Endpoint {
9
10 const AVAILABLE_METHODS = [
11 WP_REST_Server::READABLE,
12 WP_REST_Server::CREATABLE,
13 WP_REST_Server::EDITABLE,
14 WP_REST_Server::DELETABLE,
15 WP_REST_Server::ALLMETHODS,
16 ];
17
18 /**
19 * Controller of current endpoint.
20 *
21 * @var \Elementor\Data\Base\Controller
22 */
23 protected $controller;
24
25 /**
26 * Loaded sub endpoint(s).
27 *
28 * @var \Elementor\Data\Base\SubEndpoint[]
29 */
30 private $sub_endpoints = [];
31
32 /**
33 * Get format suffix.
34 *
35 * Examples:
36 * '{one_parameter_name}'.
37 * '{one_parameter_name}/{two_parameter_name}/'.
38 * '{one_parameter_name}/whatever/anything/{two_parameter_name}/' and so on for each endpoint or sub-endpoint.
39 *
40 * @return string current location will later be added automatically.
41 */
42 public static function get_format() {
43 return '';
44 }
45
46 /**
47 * Endpoint constructor.
48 *
49 * @param \Elementor\Data\Base\Controller $controller
50 *
51 * @throws \Exception If invalid controller.
52 */
53 public function __construct( $controller ) {
54 if ( ! ( $controller instanceof Controller ) ) {
55 throw new \Exception( 'Invalid controller.' );
56 }
57
58 $this->controller = $controller;
59 $this->register();
60 }
61
62 /**
63 * Get endpoint name.
64 *
65 * @return string
66 */
67 abstract public function get_name();
68
69 /**
70 * Get base route.
71 *
72 * Removing 'index' from endpoint.
73 *
74 * @return string
75 */
76 public function get_base_route() {
77 $endpoint_name = $this->get_name();
78
79 // TODO: Allow this only for internal routes.
80 // TODO: Make difference between internal and external endpoints.
81 if ( 'index' === $endpoint_name ) {
82 $endpoint_name = '';
83 }
84
85 return '/' . $this->controller->get_rest_base() . '/' . $endpoint_name;
86 }
87
88 /**
89 * Register the endpoint.
90 *
91 * By default: register get items route.
92 *
93 * @throws \Exception If invalid endpoint registered.
94 */
95 protected function register() {
96 $this->register_items_route();
97 }
98
99 /**
100 * Register sub endpoint.
101 *
102 * @param string $route
103 * @param string $endpoint_class
104 *
105 * @return \Elementor\Data\Base\SubEndpoint
106 * @throws \Exception If invalid sub endpoint registered.
107 */
108 protected function register_sub_endpoint( $route, $endpoint_class ) {
109 $endpoint_instance = new $endpoint_class( $route, $this );
110
111 if ( ! ( $endpoint_instance instanceof SubEndpoint ) ) {
112 throw new \Exception( 'Invalid endpoint instance.' );
113 }
114
115 $endpoint_route = $route . '/' . $endpoint_instance->get_name();
116
117 $this->sub_endpoints[ $endpoint_route ] = $endpoint_instance;
118
119 $component_name = $endpoint_instance->controller->get_rest_base();
120 $parent_instance = $endpoint_instance->get_parent();
121 $parent_name = $endpoint_instance->get_name();
122 $parent_format_suffix = $parent_instance::get_format();
123 $current_format_suffix = $endpoint_instance::get_format();
124
125 $command = $component_name . '/' . $parent_name;
126 $format = $component_name . '/' . $parent_format_suffix . '/' . $parent_name . '/' . $current_format_suffix;
127
128 Manager::instance()->register_endpoint_format( $command, $format );
129
130 return $endpoint_instance;
131 }
132
133 /**
134 * Base callback.
135 *
136 * All reset requests from the client should pass this function.
137 *
138 * @param string $methods
139 * @param \WP_REST_Request $request
140 * @param bool $is_multi
141 *
142 * @return mixed|\WP_Error|\WP_HTTP_Response|\WP_REST_Response
143 * @throws \Exception If invalid method.
144 */
145 public function base_callback( $methods, $request, $is_multi = false ) {
146 // TODO: Find better solution.
147 $json_params = $request->get_json_params();
148
149 if ( $json_params ) {
150 $request->set_body_params( $json_params );
151 }
152
153 // TODO: Handle permission callback.
154 switch ( $methods ) {
155 case WP_REST_Server::READABLE:
156 $result = $is_multi ? $this->get_items( $request ) : $this->get_item( $request->get_param( 'id' ), $request );
157 break;
158
159 case WP_REST_Server::CREATABLE:
160 $result = $is_multi ? $this->create_items( $request ) : $this->create_item( $request->get_param( 'id' ), $request );
161 break;
162
163 case WP_REST_Server::EDITABLE:
164 $result = $is_multi ? $this->update_items( $request ) : $this->update_item( $request->get_param( 'id' ), $request );
165 break;
166
167 case WP_REST_Server::DELETABLE:
168 $result = $is_multi ? $this->delete_items( $request ) : $this->delete_item( $request->get_param( 'id' ), $request );
169 break;
170
171 default:
172 throw new \Exception( 'Invalid method.' );
173 }
174
175 return rest_ensure_response( $result );
176 }
177
178 /**
179 * Retrieves a collection of items.
180 *
181 * @param \WP_REST_Request $request Full data about the request.
182 *
183 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
184 */
185 public function get_items( $request ) {
186 return $this->controller->get_items( $request );
187 }
188
189 /**
190 * Retrieves one item from the collection.
191 *
192 * @param string $id
193 * @param \WP_REST_Request $request Full data about the request.
194 *
195 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
196 */
197 public function get_item( $id, $request ) {
198 return $this->controller->get_item( $request );
199 }
200
201 /**
202 * Get permission callback.
203 *
204 * By default get permission callback from the controller.
205 *
206 * @param \WP_REST_Request $request Full data about the request.
207 *
208 * @return boolean
209 */
210 public function get_permission_callback( $request ) {
211 return $this->controller->get_permission_callback( $request );
212 }
213
214 /**
215 * Creates one item.
216 *
217 * @param string $id id of request item.
218 * @param \WP_REST_Request $request Full data about the request.
219 *
220 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
221 */
222 public function create_item( $id, $request ) {
223 return $this->controller->create_item( $request );
224 }
225
226 /**
227 * Creates multiple items.
228 *
229 * @param \WP_REST_Request $request Full data about the request.
230 *
231 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
232 */
233 public function create_items( $request ) {
234 return $this->controller->create_items( $request );
235 }
236
237 /**
238 * Updates one item.
239 *
240 * @param string $id id of request item.
241 * @param \WP_REST_Request $request Full data about the request.
242 *
243 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
244 */
245 public function update_item( $id, $request ) {
246 return $this->controller->update_item( $request );
247 }
248
249 /**
250 * Updates multiple items.
251 *
252 * @param \WP_REST_Request $request Full data about the request.
253 *
254 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
255 */
256 public function update_items( $request ) {
257 return $this->controller->update_items( $request );
258 }
259
260 /**
261 * Delete one item.
262 *
263 * @param string $id id of request item.
264 * @param \WP_REST_Request $request Full data about the request.
265 *
266 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
267 */
268 public function delete_item( $id, $request ) {
269 return $this->controller->delete_item( $request );
270 }
271
272 /**
273 * Delete multiple items.
274 *
275 * @param \WP_REST_Request $request Full data about the request.
276 *
277 * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
278 */
279 public function delete_items( $request ) {
280 return $this->controller->delete_items( $request );
281 }
282
283 /**
284 * Register item route.
285 *
286 * @param string $methods
287 * @param array $args
288 * @param string $route
289 *
290 * @throws \Exception If invalid method.
291 */
292 public function register_item_route( $methods = WP_REST_Server::READABLE, $args = [], $route = '/' ) {
293 $args = array_merge( [
294 'id' => [
295 'description' => 'Unique identifier for the object.',
296 'type' => 'string',
297 ],
298 ], $args );
299
300 if ( isset( $args['id'] ) && $args['id'] ) {
301 $route .= '(?P<id>[\w]+)/';
302 }
303
304 $this->register_route( $route, $methods, function ( $request ) use ( $methods ) {
305 return $this->base_callback( $methods, $request );
306 }, $args );
307 }
308
309 /**
310 * Register items route.
311 *
312 * @param string $methods
313 *
314 * @throws \Exception If invalid method.
315 */
316 public function register_items_route( $methods = WP_REST_Server::READABLE ) {
317 $this->register_route( '', $methods, function ( $request ) use ( $methods ) {
318 return $this->base_callback( $methods, $request, true );
319 } );
320 }
321
322 /**
323 * Register route.
324 *
325 * @param string $route
326 * @param string $methods
327 * @param null $callback
328 * @param array $args
329 *
330 * @return bool
331 * @throws \Exception If invalid method.
332 */
333 public function register_route( $route = '', $methods = WP_REST_Server::READABLE, $callback = null, $args = [] ) {
334 if ( ! in_array( $methods, self::AVAILABLE_METHODS, true ) ) {
335 throw new \Exception( 'Invalid method.' );
336 }
337
338 $route = $this->get_base_route() . $route;
339
340 return register_rest_route( $this->controller->get_namespace(), $route, [
341 [
342 'args' => $args,
343 'methods' => $methods,
344 'callback' => $callback,
345 'permission_callback' => function ( $request ) {
346 return $this->get_permission_callback( $request );
347 },
348 ],
349 ] );
350 }
351 }
352