PluginProbe
Elementor Website Builder – more than just a page builder / 3.10.1
Elementor Website Builder – more than just a page builder v3.10.1
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 / includes / template-library / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.10.1, at includes/template-library/manager.php

693 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\TemplateLibrary;
3
4 use Elementor\Api;
5 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
6 use Elementor\Core\Settings\Manager as SettingsManager;
7 use Elementor\Includes\TemplateLibrary\Data\Controller;
8 use Elementor\TemplateLibrary\Classes\Import_Images;
9 use Elementor\Plugin;
10 use Elementor\User;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 /**
17 * Elementor template library manager.
18 *
19 * Elementor template library manager handler class is responsible for
20 * initializing the template library.
21 *
22 * @since 1.0.0
23 */
24 class Manager {
25
26 /**
27 * Registered template sources.
28 *
29 * Holds a list of all the supported sources with their instances.
30 *
31 * @access protected
32 *
33 * @var Source_Base[]
34 */
35 protected $_registered_sources = [];
36
37 /**
38 * Imported template images.
39 *
40 * Holds an instance of `Import_Images` class.
41 *
42 * @access private
43 *
44 * @var Import_Images
45 */
46 private $_import_images = null;
47
48 /**
49 * Template library manager constructor.
50 *
51 * Initializing the template library manager by registering default template
52 * sources and initializing ajax calls.
53 *
54 * @since 1.0.0
55 * @access public
56 */
57 public function __construct() {
58 Plugin::$instance->data_manager_v2->register_controller( new Controller() );
59
60 $this->register_default_sources();
61
62 $this->add_actions();
63 }
64
65 /**
66 * @since 2.3.0
67 * @access public
68 */
69 public function add_actions() {
70 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
71 add_action( 'wp_ajax_elementor_library_direct_actions', [ $this, 'handle_direct_actions' ] );
72 }
73
74 /**
75 * Get `Import_Images` instance.
76 *
77 * Retrieve the instance of the `Import_Images` class.
78 *
79 * @since 1.0.0
80 * @access public
81 *
82 * @return Import_Images Imported images instance.
83 */
84 public function get_import_images_instance() {
85 if ( null === $this->_import_images ) {
86 $this->_import_images = new Import_Images();
87 }
88
89 return $this->_import_images;
90 }
91
92 /**
93 * Register template source.
94 *
95 * Used to register new template sources displayed in the template library.
96 *
97 * @since 1.0.0
98 * @access public
99 *
100 * @param string $source_class The name of source class.
101 * @param array $args Optional. Class arguments. Default is an
102 * empty array.
103 *
104 * @return \WP_Error|true True if the source was registered, `WP_Error`
105 * otherwise.
106 */
107 public function register_source( $source_class, $args = [] ) {
108 if ( ! class_exists( $source_class ) ) {
109 return new \WP_Error( 'source_class_name_not_exists' );
110 }
111
112 $source_instance = new $source_class( $args );
113
114 if ( ! $source_instance instanceof Source_Base ) {
115 return new \WP_Error( 'wrong_instance_source' );
116 }
117
118 $source_id = $source_instance->get_id();
119
120 if ( isset( $this->_registered_sources[ $source_id ] ) ) {
121 return new \WP_Error( 'source_exists' );
122 }
123
124 $this->_registered_sources[ $source_id ] = $source_instance;
125
126 return true;
127 }
128
129 /**
130 * Unregister template source.
131 *
132 * Remove an existing template sources from the list of registered template
133 * sources.
134 *
135 * @deprecated 2.7.0
136 *
137 * @since 1.0.0
138 * @access public
139 *
140 * @param string $id The source ID.
141 *
142 * @return bool Whether the source was unregistered.
143 */
144 public function unregister_source( $id ) {
145 return true;
146 }
147
148 /**
149 * Get registered template sources.
150 *
151 * Retrieve registered template sources.
152 *
153 * @since 1.0.0
154 * @access public
155 *
156 * @return Source_Base[] Registered template sources.
157 */
158 public function get_registered_sources() {
159 return $this->_registered_sources;
160 }
161
162 /**
163 * Get template source.
164 *
165 * Retrieve single template sources for a given template ID.
166 *
167 * @since 1.0.0
168 * @access public
169 *
170 * @param string $id The source ID.
171 *
172 * @return false|Source_Base Template sources if one exist, False otherwise.
173 */
174 public function get_source( $id ) {
175 $sources = $this->get_registered_sources();
176
177 if ( ! isset( $sources[ $id ] ) ) {
178 return false;
179 }
180
181 return $sources[ $id ];
182 }
183
184 /**
185 * Get templates.
186 *
187 * Retrieve all the templates from all the registered sources.
188 *
189 * @param array $filter_sources
190 * @param bool $force_update
191 * @return array
192 */
193 public function get_templates( array $filter_sources = [], bool $force_update = false ): array {
194 $templates = [];
195
196 foreach ( $this->get_registered_sources() as $source ) {
197 if ( ! empty( $filter_sources ) && ! in_array( $source->get_id(), $filter_sources, true ) ) {
198 continue;
199 }
200
201 $templates = array_merge( $templates, $source->get_items( [ 'force_update' => $force_update ] ) );
202 }
203
204 return $templates;
205 }
206
207 /**
208 * Get library data.
209 *
210 * Retrieve the library data.
211 *
212 * @since 1.9.0
213 * @access public
214 *
215 * @param array $args Library arguments.
216 *
217 * @return array Library data.
218 */
219 public function get_library_data( array $args ) {
220 $library_data = Api::get_library_data( ! empty( $args['sync'] ) );
221
222 if ( empty( $library_data ) ) {
223 return $library_data;
224 }
225
226 // Ensure all document are registered.
227 Plugin::$instance->documents->get_document_types();
228
229 $filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : [];
230 $force_update = ! empty( $args['sync'] );
231
232 return [
233 'templates' => $this->get_templates( $filter_sources, $force_update ),
234 'config' => $library_data['types_data'],
235 ];
236 }
237
238 /**
239 * Save template.
240 *
241 * Save new or update existing template on the database.
242 *
243 * @since 1.0.0
244 * @access public
245 *
246 * @param array $args Template arguments.
247 *
248 * @return \WP_Error|int The ID of the saved/updated template.
249 */
250 public function save_template( array $args ) {
251 $validate_args = $this->ensure_args( [ 'post_id', 'source', 'content', 'type' ], $args );
252
253 if ( is_wp_error( $validate_args ) ) {
254 return $validate_args;
255 }
256
257 $source = $this->get_source( $args['source'] );
258
259 if ( ! $source ) {
260 return new \WP_Error( 'template_error', 'Template source not found.' );
261 }
262
263 $args['content'] = json_decode( $args['content'], true );
264
265 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['post_id'] );
266
267 $args['page_settings'] = $page->get_data( 'settings' );
268
269 $template_id = $source->save_item( $args );
270
271 if ( is_wp_error( $template_id ) ) {
272 return $template_id;
273 }
274
275 return $source->get_item( $template_id );
276 }
277
278 /**
279 * Update template.
280 *
281 * Update template on the database.
282 *
283 * @since 1.0.0
284 * @access public
285 *
286 * @param array $template_data New template data.
287 *
288 * @return \WP_Error|Source_Base Template sources instance if the templates
289 * was updated, `WP_Error` otherwise.
290 */
291 public function update_template( array $template_data ) {
292 $validate_args = $this->ensure_args( [ 'source', 'content', 'type' ], $template_data );
293
294 if ( is_wp_error( $validate_args ) ) {
295 return $validate_args;
296 }
297
298 $source = $this->get_source( $template_data['source'] );
299
300 if ( ! $source ) {
301 return new \WP_Error( 'template_error', 'Template source not found.' );
302 }
303
304 $template_data['content'] = json_decode( $template_data['content'], true );
305
306 $update = $source->update_item( $template_data );
307
308 if ( is_wp_error( $update ) ) {
309 return $update;
310 }
311
312 return $source->get_item( $template_data['id'] );
313 }
314
315 /**
316 * Update templates.
317 *
318 * Update template on the database.
319 *
320 * @since 1.0.0
321 * @access public
322 *
323 * @param array $args Template arguments.
324 *
325 * @return \WP_Error|true True if templates updated, `WP_Error` otherwise.
326 */
327 public function update_templates( array $args ) {
328 foreach ( $args['templates'] as $template_data ) {
329 $result = $this->update_template( $template_data );
330
331 if ( is_wp_error( $result ) ) {
332 return $result;
333 }
334 }
335
336 return true;
337 }
338
339 /**
340 * Get template data.
341 *
342 * Retrieve the template data.
343 *
344 * @since 1.5.0
345 * @access public
346 *
347 * @param array $args Template arguments.
348 *
349 * @return \WP_Error|bool|array ??
350 */
351 public function get_template_data( array $args ) {
352 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
353
354 if ( is_wp_error( $validate_args ) ) {
355 return $validate_args;
356 }
357
358 if ( isset( $args['edit_mode'] ) ) {
359 Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
360 }
361
362 $source = $this->get_source( $args['source'] );
363
364 if ( ! $source ) {
365 return new \WP_Error( 'template_error', 'Template source not found.' );
366 }
367
368 do_action( 'elementor/template-library/before_get_source_data', $args, $source );
369
370 $data = $source->get_data( $args );
371
372 do_action( 'elementor/template-library/after_get_source_data', $args, $source );
373
374 return $data;
375 }
376
377 /**
378 * Delete template.
379 *
380 * Delete template from the database.
381 *
382 * @since 1.0.0
383 * @access public
384 *
385 * @param array $args Template arguments.
386 *
387 * @return \WP_Post|\WP_Error|false|null Post data on success, false or null
388 * or 'WP_Error' on failure.
389 */
390 public function delete_template( array $args ) {
391 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
392
393 if ( is_wp_error( $validate_args ) ) {
394 return $validate_args;
395 }
396
397 $source = $this->get_source( $args['source'] );
398
399 if ( ! $source ) {
400 return new \WP_Error( 'template_error', 'Template source not found.' );
401 }
402
403 return $source->delete_template( $args['template_id'] );
404 }
405
406 /**
407 * Export template.
408 *
409 * Export template to a file.
410 *
411 * @since 1.0.0
412 * @access public
413 *
414 * @param array $args Template arguments.
415 *
416 * @return mixed Whether the export succeeded or failed.
417 */
418 public function export_template( array $args ) {
419 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
420
421 if ( is_wp_error( $validate_args ) ) {
422 return $validate_args;
423 }
424
425 $source = $this->get_source( $args['source'] );
426
427 if ( ! $source ) {
428 return new \WP_Error( 'template_error', 'Template source not found' );
429 }
430
431 return $source->export_template( $args['template_id'] );
432 }
433
434 /**
435 * @since 2.3.0
436 * @access public
437 */
438 public function direct_import_template() {
439 /** @var Source_Local $source */
440 $source = $this->get_source( 'local' );
441
442 return $source->import_template( $_FILES['file']['name'], $_FILES['file']['tmp_name'] );
443 }
444
445 /**
446 * Import template.
447 *
448 * Import template from a file.
449 *
450 * @since 1.0.0
451 * @access public
452 *
453 * @param array $data
454 *
455 * @return mixed Whether the export succeeded or failed.
456 */
457 public function import_template( array $data ) {
458 // If the template is a JSON file, allow uploading it.
459 add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
460 add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
461
462 // Imported templates can be either JSON files, or Zip files containing multiple JSON files
463 $upload_result = Plugin::$instance->uploads_manager->handle_elementor_upload( $data, [ 'zip', 'json' ] );
464
465 remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
466 remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
467
468 if ( is_wp_error( $upload_result ) ) {
469 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
470
471 return $upload_result;
472 }
473
474 /** @var Source_Local $source_local */
475 $source_local = $this->get_source( 'local' );
476
477 $import_result = $source_local->import_template( $upload_result['name'], $upload_result['tmp_name'] );
478
479 // Remove the temporary directory generated for the stream-uploaded file.
480 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
481
482 return $import_result;
483 }
484
485 /**
486 * Enable JSON Template Upload
487 *
488 * Runs on the 'elementor/files/allow-file-type/json' Uploads Manager filter.
489 *
490 * @since 3.5.0
491 * @access public
492 *
493 * return bool
494 */
495 public function enable_json_template_upload() {
496 return true;
497 }
498
499 /**
500 * Mark template as favorite.
501 *
502 * Add the template to the user favorite templates.
503 *
504 * @since 1.9.0
505 * @access public
506 *
507 * @param array $args Template arguments.
508 *
509 * @return mixed Whether the template marked as favorite.
510 */
511 public function mark_template_as_favorite( $args ) {
512 $validate_args = $this->ensure_args( [ 'source', 'template_id', 'favorite' ], $args );
513
514 if ( is_wp_error( $validate_args ) ) {
515 return $validate_args;
516 }
517
518 $source = $this->get_source( $args['source'] );
519
520 return $source->mark_as_favorite( $args['template_id'], filter_var( $args['favorite'], FILTER_VALIDATE_BOOLEAN ) );
521 }
522
523 /**
524 * Register default template sources.
525 *
526 * Register the 'local' and 'remote' template sources that Elementor use by
527 * default.
528 *
529 * @since 1.0.0
530 * @access private
531 */
532 private function register_default_sources() {
533 $sources = [
534 'local',
535 'remote',
536 ];
537
538 foreach ( $sources as $source_filename ) {
539 $class_name = ucwords( $source_filename );
540 $class_name = str_replace( '-', '_', $class_name );
541
542 $this->register_source( __NAMESPACE__ . '\Source_' . $class_name );
543 }
544 }
545
546 /**
547 * Handle ajax request.
548 *
549 * Fire authenticated ajax actions for any given ajax request.
550 *
551 * @since 1.0.0
552 * @access private
553 *
554 * @param string $ajax_request Ajax request.
555 *
556 * @param array $data
557 *
558 * @return mixed
559 * @throws \Exception
560 */
561 private function handle_ajax_request( $ajax_request, array $data ) {
562 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
563 throw new \Exception( 'Access denied.' );
564 }
565
566 if ( ! empty( $data['editor_post_id'] ) ) {
567 $editor_post_id = absint( $data['editor_post_id'] );
568
569 if ( ! get_post( $editor_post_id ) ) {
570 throw new \Exception( 'Post not found.' );
571 }
572
573 Plugin::$instance->db->switch_to_post( $editor_post_id );
574 }
575
576 $result = call_user_func( [ $this, $ajax_request ], $data );
577
578 if ( is_wp_error( $result ) ) {
579 throw new \Exception( $result->get_error_message() );
580 }
581
582 return $result;
583 }
584
585 /**
586 * Init ajax calls.
587 *
588 * Initialize template library ajax calls for allowed ajax requests.
589 *
590 * @since 2.3.0
591 * @access public
592 *
593 * @param Ajax $ajax
594 */
595 public function register_ajax_actions( Ajax $ajax ) {
596 $library_ajax_requests = [
597 'get_library_data',
598 'get_template_data',
599 'save_template',
600 'update_templates',
601 'delete_template',
602 'import_template',
603 'mark_template_as_favorite',
604 ];
605
606 foreach ( $library_ajax_requests as $ajax_request ) {
607 $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
608 return $this->handle_ajax_request( $ajax_request, $data );
609 } );
610 }
611 }
612
613 /**
614 * @since 2.3.0
615 * @access public
616 */
617 public function handle_direct_actions() {
618 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
619 return;
620 }
621
622 /** @var Ajax $ajax */
623 $ajax = Plugin::$instance->common->get_component( 'ajax' );
624
625 if ( ! $ajax->verify_request_nonce() ) {
626 $this->handle_direct_action_error( 'Access Denied' );
627 }
628
629 $action = $_REQUEST['library_action'];
630
631 $result = $this->$action( $_REQUEST );
632
633 if ( is_wp_error( $result ) ) {
634 /** @var \WP_Error $result */
635 $this->handle_direct_action_error( $result->get_error_message() . '.' );
636 }
637
638 $callback = "on_{$action}_success";
639
640 if ( method_exists( $this, $callback ) ) {
641 $this->$callback( $result );
642 }
643
644 die;
645 }
646
647 /**
648 * On successful template import.
649 *
650 * Redirect the user to the template library after template import was
651 * successful finished.
652 *
653 * @since 2.3.0
654 * @access private
655 */
656 private function on_direct_import_template_success() {
657 wp_safe_redirect( admin_url( Source_Local::ADMIN_MENU_SLUG ) );
658 }
659
660 /**
661 * @since 2.3.0
662 * @access private
663 */
664 private function handle_direct_action_error( $message ) {
665 _default_wp_die_handler( $message, 'Elementor Library' );
666 }
667
668 /**
669 * Ensure arguments exist.
670 *
671 * Checks whether the required arguments exist in the specified arguments.
672 *
673 * @since 1.0.0
674 * @access private
675 *
676 * @param array $required_args Required arguments to check whether they
677 * exist.
678 * @param array $specified_args The list of all the specified arguments to
679 * check against.
680 *
681 * @return \WP_Error|true True on success, 'WP_Error' otherwise.
682 */
683 private function ensure_args( array $required_args, array $specified_args ) {
684 $not_specified_args = array_diff( $required_args, array_keys( $specified_args ) );
685
686 if ( $not_specified_args ) {
687 return new \WP_Error( 'arguments_not_specified', sprintf( 'The required argument(s) "%s" not specified.', implode( ', ', $not_specified_args ) ) );
688 }
689
690 return true;
691 }
692 }
693