PluginProbe
Elementor Website Builder – more than just a page builder / 3.6.0-dev10
Elementor Website Builder – more than just a page builder v3.6.0-dev10
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 / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.6.0-dev10, at data/manager.php

423 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Data;
3
4 use Elementor\Core\Base\Module as BaseModule;
5 use Elementor\Data\Base\Processor;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly
9 }
10
11 class Manager extends BaseModule {
12
13 const ROOT_NAMESPACE = 'elementor';
14
15 const REST_BASE = '';
16
17 const VERSION = '1';
18
19 /**
20 * @var \WP_REST_Server
21 */
22 private $server;
23
24 /**
25 * @var boolean
26 */
27 private $is_internal = false;
28
29 /**
30 * @var array
31 */
32 private $cache = [];
33
34 /**
35 * Loaded controllers.
36 *
37 * @var \Elementor\Data\Base\Controller[]
38 */
39 public $controllers = [];
40
41 /**
42 * Loaded command(s) format.
43 *
44 * @var string[]
45 */
46 public $command_formats = [];
47
48 /**
49 * Fix issue with 'Potentially polymorphic call. The code may be inoperable depending on the actual class instance passed as the argument.'.
50 *
51 * @return \Elementor\Core\Base\Module|\Elementor\Data\Manager
52 */
53 public static function instance() {
54 return ( parent::instance() );
55 }
56
57 public function __construct() {
58 add_action( 'rest_api_init', [ $this, 'register_rest_error_handler' ] );
59 }
60
61 public function get_name() {
62 return 'data-manager';
63 }
64
65 /**
66 * @return \Elementor\Data\Base\Controller[]
67 */
68 public function get_controllers() {
69 return $this->controllers;
70 }
71
72 private function get_cache( $key ) {
73 return self::get_items( $this->cache, $key );
74 }
75
76 private function set_cache( $key, $value ) {
77 $this->cache[ $key ] = $value;
78 }
79
80 /**
81 * Register controller.
82 *
83 * @param string $controller_class_name
84 *
85 * @return \Elementor\Data\Base\Controller
86 */
87 public function register_controller( $controller_class_name ) {
88 $controller_instance = new $controller_class_name();
89
90 return $this->register_controller_instance( $controller_instance );
91 }
92
93 /**
94 * Register controller instance.
95 *
96 * @param \Elementor\Data\Base\Controller $controller_instance
97 *
98 * @return \Elementor\Data\Base\Controller
99 */
100 public function register_controller_instance( $controller_instance ) {
101 // TODO: Validate instance.
102
103 $this->controllers[ $controller_instance->get_name() ] = $controller_instance;
104
105 return $controller_instance;
106 }
107
108 /**
109 * Register endpoint format.
110 *
111 * @param string $command
112 * @param string $format
113 *
114 */
115 public function register_endpoint_format( $command, $format ) {
116 $this->command_formats[ $command ] = rtrim( $format, '/' );
117 }
118
119 public function register_rest_error_handler() {
120 // TODO: Remove - Find better solution.
121 return;
122
123 if ( ! $this->is_internal() ) {
124 $logger_manager = \Elementor\Core\Logger\Manager::instance();
125
126 set_error_handler( [ $logger_manager, 'rest_error_handler' ], E_ALL );
127 }
128 }
129
130 /**
131 * Find controller instance.
132 *
133 * By given command name.
134 *
135 * @param string $command
136 *
137 * @return false|\Elementor\Data\Base\Controller
138 */
139 public function find_controller_instance( $command ) {
140 $command_parts = explode( '/', $command );
141 $assumed_command_parts = [];
142
143 foreach ( $command_parts as $command_part ) {
144 $assumed_command_parts [] = $command_part;
145
146 foreach ( $this->controllers as $controller_name => $controller ) {
147 $assumed_command = implode( '/', $assumed_command_parts );
148
149 if ( $assumed_command === $controller_name ) {
150 return $controller;
151 }
152 }
153 }
154
155 return false;
156 }
157
158 /**
159 * Command extract args.
160 *
161 * @param string $command
162 * @param array $args
163 *
164 * @return \stdClass
165 */
166 public function command_extract_args( $command, $args = [] ) {
167 $result = new \stdClass();
168 $result->command = $command;
169 $result->args = $args;
170
171 if ( false !== strpos( $command, '?' ) ) {
172 $command_parts = explode( '?', $command );
173 $pure_command = $command_parts[0];
174 $query_string = $command_parts[1];
175
176 parse_str( $query_string, $temp );
177
178 $result->command = rtrim( $pure_command, '/' );
179 $result->args = array_merge( $args, $temp );
180 }
181
182 return $result;
183 }
184
185 /**
186 * Command to endpoint.
187 *
188 * Format is required otherwise $command will returned.
189 *
190 * @param string $command
191 * @param string $format
192 * @param array $args
193 *
194 * @return string endpoint
195 */
196 public function command_to_endpoint( $command, $format, $args ) {
197 $endpoint = $command;
198
199 if ( $format ) {
200 $formatted = $format;
201
202 array_walk( $args, function ( $val, $key ) use ( &$formatted ) {
203 $formatted = str_replace( '{' . $key . '}', $val, $formatted );
204 } );
205
206 // Remove remaining format if not requested via `$args`.
207 if ( strstr( $formatted, '/{' ) ) {
208 /**
209 * Example:
210 * $command = 'example/documents';
211 * $format = 'example/documents/{document_id}/elements/{element_id}';
212 * $formatted = 'example/documents/1618/elements/{element_id}';
213 * Result:
214 * $formatted = 'example/documents/1618/elements';
215 */
216 $formatted = substr( $formatted, 0, strpos( $formatted, '/{' ) );
217 }
218
219 $endpoint = $formatted;
220 }
221
222 return $endpoint;
223 }
224
225 /**
226 * Run server.
227 *
228 * Init WordPress reset api.
229 *
230 * @return \WP_REST_Server
231 */
232 public function run_server() {
233 /**
234 * If run_server() called means, that rest api is simulated from the backend.
235 */
236 $this->is_internal = true;
237
238 if ( ! $this->server ) {
239 // Remove all 'rest_api_init' actions.
240 remove_all_actions( 'rest_api_init' );
241
242 // Call custom reset api loader.
243 do_action( 'elementor_rest_api_before_init' );
244
245 $this->server = rest_get_server(); // Init API.
246 }
247
248 return $this->server;
249 }
250
251 /**
252 * Kill server.
253 *
254 * Free server and controllers.
255 */
256 public function kill_server() {
257 global $wp_rest_server;
258
259 $this->controllers = [];
260 $this->command_formats = [];
261 $this->server = false;
262 $this->is_internal = false;
263 $this->cache = [];
264 $wp_rest_server = false;
265 }
266
267 /**
268 * Run processor.
269 *
270 * @param \Elementor\Data\Base\Processor $processor
271 * @param array $data
272 *
273 * @return mixed
274 */
275 public function run_processor( $processor, $data ) {
276 if ( call_user_func_array( [ $processor, 'get_conditions' ], $data ) ) {
277 return call_user_func_array( [ $processor, 'apply' ], $data );
278 }
279
280 return null;
281 }
282
283 /**
284 * Run processors.
285 *
286 * Filter them by class.
287 *
288 * @param \Elementor\Data\Base\Processor[] $processors
289 * @param string $filter_by_class
290 * @param array $data
291 *
292 * @return false|array
293 */
294 public function run_processors( $processors, $filter_by_class, $data ) {
295 foreach ( $processors as $processor ) {
296 if ( $processor instanceof $filter_by_class ) {
297 if ( Processor\Before::class === $filter_by_class ) {
298 $this->run_processor( $processor, $data );
299 } elseif ( Processor\After::class === $filter_by_class ) {
300 $result = $this->run_processor( $processor, $data );
301 if ( $result ) {
302 $data[1] = $result;
303 }
304 } else {
305 // TODO: error
306 break;
307 }
308 }
309 }
310
311 return isset( $data[1] ) ? $data[1] : false;
312 }
313
314 /**
315 * Run request.
316 *
317 * Simulate rest API from within the backend.
318 * Use args as query.
319 *
320 * @param string $endpoint
321 * @param array $args
322 * @param string $method
323 *
324 * @return \WP_REST_Response
325 */
326 private function run_request( $endpoint, $args, $method ) {
327 $this->run_server();
328
329 $endpoint = '/' . self::ROOT_NAMESPACE . '/v' . self::VERSION . '/' . $endpoint;
330
331 // Run reset api.
332 $request = new \WP_REST_Request( $method, $endpoint );
333
334 if ( 'GET' === $method ) {
335 $request->set_query_params( $args );
336 } else {
337 $request->set_body_params( $args );
338 }
339
340 return rest_do_request( $request );
341 }
342
343 /**
344 * Run endpoint.
345 *
346 * Wrapper for `$this->run_request` return `$response->getData()` instead of `$response`.
347 *
348 * @param string $endpoint
349 * @param array $args
350 * @param string $method
351 *
352 * @return array
353 */
354 public function run_endpoint( $endpoint, $args = [], $method = 'GET' ) {
355 $response = $this->run_request( $endpoint, $args, $method );
356
357 return $response->get_data();
358 }
359
360 /**
361 * Run ( simulated reset api ).
362 *
363 * Do:
364 * Init reset server.
365 * Run before processors.
366 * Run command as reset api endpoint from internal.
367 * Run after processors.
368 *
369 * @param string $command
370 * @param array $args
371 * @param string $method
372 *
373 * @return array|false processed result
374 */
375 public function run( $command, $args = [], $method = 'GET' ) {
376 $key = crc32( $command . '-' . wp_json_encode( $args ) . '-' . $method );
377 $cache = $this->get_cache( $key );
378
379 if ( $cache ) {
380 return $cache;
381 }
382
383 $this->run_server();
384
385 $controller_instance = $this->find_controller_instance( $command );
386
387 if ( ! $controller_instance ) {
388 $this->set_cache( $key, [] );
389 return [];
390 }
391
392 $extracted_command = $this->command_extract_args( $command, $args );
393 $command = $extracted_command->command;
394 $args = $extracted_command->args;
395
396 $format = isset( $this->command_formats[ $command ] ) ? $this->command_formats[ $command ] : false;
397
398 $command_processors = $controller_instance->get_processors( $command );
399
400 $endpoint = $this->command_to_endpoint( $command, $format, $args );
401
402 $this->run_processors( $command_processors, Processor\Before::class, [ $args ] );
403
404 $response = $this->run_request( $endpoint, $args, $method );
405 $result = $response->get_data();
406
407 if ( $response->is_error() ) {
408 $this->set_cache( $key, [] );
409 return [];
410 }
411
412 $result = $this->run_processors( $command_processors, Processor\After::class, [ $args, $result ] );
413
414 $this->set_cache( $key, $result );
415
416 return $result;
417 }
418
419 public function is_internal() {
420 return $this->is_internal;
421 }
422 }
423