PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.4
Elementor Website Builder – more than just a page builder v3.20.4
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.20.4, at data/manager.php

419 lines 9.2 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 if ( ! $this->is_internal() ) {
121 $logger_manager = \Elementor\Core\Logger\Manager::instance();
122 $logger_manager->register_error_handler();
123 }
124 }
125
126 /**
127 * Find controller instance.
128 *
129 * By given command name.
130 *
131 * @param string $command
132 *
133 * @return false|\Elementor\Data\Base\Controller
134 */
135 public function find_controller_instance( $command ) {
136 $command_parts = explode( '/', $command );
137 $assumed_command_parts = [];
138
139 foreach ( $command_parts as $command_part ) {
140 $assumed_command_parts [] = $command_part;
141
142 foreach ( $this->controllers as $controller_name => $controller ) {
143 $assumed_command = implode( '/', $assumed_command_parts );
144
145 if ( $assumed_command === $controller_name ) {
146 return $controller;
147 }
148 }
149 }
150
151 return false;
152 }
153
154 /**
155 * Command extract args.
156 *
157 * @param string $command
158 * @param array $args
159 *
160 * @return \stdClass
161 */
162 public function command_extract_args( $command, $args = [] ) {
163 $result = new \stdClass();
164 $result->command = $command;
165 $result->args = $args;
166
167 if ( false !== strpos( $command, '?' ) ) {
168 $command_parts = explode( '?', $command );
169 $pure_command = $command_parts[0];
170 $query_string = $command_parts[1];
171
172 parse_str( $query_string, $temp );
173
174 $result->command = rtrim( $pure_command, '/' );
175 $result->args = array_merge( $args, $temp );
176 }
177
178 return $result;
179 }
180
181 /**
182 * Command to endpoint.
183 *
184 * Format is required otherwise $command will returned.
185 *
186 * @param string $command
187 * @param string $format
188 * @param array $args
189 *
190 * @return string endpoint
191 */
192 public function command_to_endpoint( $command, $format, $args ) {
193 $endpoint = $command;
194
195 if ( $format ) {
196 $formatted = $format;
197
198 array_walk( $args, function ( $val, $key ) use ( &$formatted ) {
199 $formatted = str_replace( '{' . $key . '}', $val, $formatted );
200 } );
201
202 // Remove remaining format if not requested via `$args`.
203 if ( strstr( $formatted, '/{' ) ) {
204 /**
205 * Example:
206 * $command = 'example/documents';
207 * $format = 'example/documents/{document_id}/elements/{element_id}';
208 * $formatted = 'example/documents/1618/elements/{element_id}';
209 * Result:
210 * $formatted = 'example/documents/1618/elements';
211 */
212 $formatted = substr( $formatted, 0, strpos( $formatted, '/{' ) );
213 }
214
215 $endpoint = $formatted;
216 }
217
218 return $endpoint;
219 }
220
221 /**
222 * Run server.
223 *
224 * Init WordPress reset api.
225 *
226 * @return \WP_REST_Server
227 */
228 public function run_server() {
229 /**
230 * If run_server() called means, that rest api is simulated from the backend.
231 */
232 $this->is_internal = true;
233
234 if ( ! $this->server ) {
235 // Remove all 'rest_api_init' actions.
236 remove_all_actions( 'rest_api_init' );
237
238 // Call custom reset api loader.
239 do_action( 'elementor_rest_api_before_init' );
240
241 $this->server = rest_get_server(); // Init API.
242 }
243
244 return $this->server;
245 }
246
247 /**
248 * Kill server.
249 *
250 * Free server and controllers.
251 */
252 public function kill_server() {
253 global $wp_rest_server;
254
255 $this->controllers = [];
256 $this->command_formats = [];
257 $this->server = false;
258 $this->is_internal = false;
259 $this->cache = [];
260 $wp_rest_server = false;
261 }
262
263 /**
264 * Run processor.
265 *
266 * @param \Elementor\Data\Base\Processor $processor
267 * @param array $data
268 *
269 * @return mixed
270 */
271 public function run_processor( $processor, $data ) {
272 if ( call_user_func_array( [ $processor, 'get_conditions' ], $data ) ) {
273 return call_user_func_array( [ $processor, 'apply' ], $data );
274 }
275
276 return null;
277 }
278
279 /**
280 * Run processors.
281 *
282 * Filter them by class.
283 *
284 * @param \Elementor\Data\Base\Processor[] $processors
285 * @param string $filter_by_class
286 * @param array $data
287 *
288 * @return false|array
289 */
290 public function run_processors( $processors, $filter_by_class, $data ) {
291 foreach ( $processors as $processor ) {
292 if ( $processor instanceof $filter_by_class ) {
293 if ( Processor\Before::class === $filter_by_class ) {
294 $this->run_processor( $processor, $data );
295 } elseif ( Processor\After::class === $filter_by_class ) {
296 $result = $this->run_processor( $processor, $data );
297 if ( $result ) {
298 $data[1] = $result;
299 }
300 } else {
301 // TODO: error
302 break;
303 }
304 }
305 }
306
307 return isset( $data[1] ) ? $data[1] : false;
308 }
309
310 /**
311 * Run request.
312 *
313 * Simulate rest API from within the backend.
314 * Use args as query.
315 *
316 * @param string $endpoint
317 * @param array $args
318 * @param string $method
319 *
320 * @return \WP_REST_Response
321 */
322 private function run_request( $endpoint, $args, $method ) {
323 $this->run_server();
324
325 $endpoint = '/' . self::ROOT_NAMESPACE . '/v' . self::VERSION . '/' . $endpoint;
326
327 // Run reset api.
328 $request = new \WP_REST_Request( $method, $endpoint );
329
330 if ( 'GET' === $method ) {
331 $request->set_query_params( $args );
332 } else {
333 $request->set_body_params( $args );
334 }
335
336 return rest_do_request( $request );
337 }
338
339 /**
340 * Run endpoint.
341 *
342 * Wrapper for `$this->run_request` return `$response->getData()` instead of `$response`.
343 *
344 * @param string $endpoint
345 * @param array $args
346 * @param string $method
347 *
348 * @return array
349 */
350 public function run_endpoint( $endpoint, $args = [], $method = 'GET' ) {
351 $response = $this->run_request( $endpoint, $args, $method );
352
353 return $response->get_data();
354 }
355
356 /**
357 * Run ( simulated reset api ).
358 *
359 * Do:
360 * Init reset server.
361 * Run before processors.
362 * Run command as reset api endpoint from internal.
363 * Run after processors.
364 *
365 * @param string $command
366 * @param array $args
367 * @param string $method
368 *
369 * @return array|false processed result
370 */
371 public function run( $command, $args = [], $method = 'GET' ) {
372 $key = crc32( $command . '-' . wp_json_encode( $args ) . '-' . $method );
373 $cache = $this->get_cache( $key );
374
375 if ( $cache ) {
376 return $cache;
377 }
378
379 $this->run_server();
380
381 $controller_instance = $this->find_controller_instance( $command );
382
383 if ( ! $controller_instance ) {
384 $this->set_cache( $key, [] );
385 return [];
386 }
387
388 $extracted_command = $this->command_extract_args( $command, $args );
389 $command = $extracted_command->command;
390 $args = $extracted_command->args;
391
392 $format = isset( $this->command_formats[ $command ] ) ? $this->command_formats[ $command ] : false;
393
394 $command_processors = $controller_instance->get_processors( $command );
395
396 $endpoint = $this->command_to_endpoint( $command, $format, $args );
397
398 $this->run_processors( $command_processors, Processor\Before::class, [ $args ] );
399
400 $response = $this->run_request( $endpoint, $args, $method );
401 $result = $response->get_data();
402
403 if ( $response->is_error() ) {
404 $this->set_cache( $key, [] );
405 return [];
406 }
407
408 $result = $this->run_processors( $command_processors, Processor\After::class, [ $args, $result ] );
409
410 $this->set_cache( $key, $result );
411
412 return $result;
413 }
414
415 public function is_internal() {
416 return $this->is_internal;
417 }
418 }
419