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

692 lines 16.4 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 *
191 * @return array
192 */
193 public function get_templates( $filter_sources = [] ) {
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() );
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
231 return [
232 'templates' => $this->get_templates( $filter_sources ),
233 'config' => $library_data['types_data'],
234 ];
235 }
236
237 /**
238 * Save template.
239 *
240 * Save new or update existing template on the database.
241 *
242 * @since 1.0.0
243 * @access public
244 *
245 * @param array $args Template arguments.
246 *
247 * @return \WP_Error|int The ID of the saved/updated template.
248 */
249 public function save_template( array $args ) {
250 $validate_args = $this->ensure_args( [ 'post_id', 'source', 'content', 'type' ], $args );
251
252 if ( is_wp_error( $validate_args ) ) {
253 return $validate_args;
254 }
255
256 $source = $this->get_source( $args['source'] );
257
258 if ( ! $source ) {
259 return new \WP_Error( 'template_error', 'Template source not found.' );
260 }
261
262 $args['content'] = json_decode( $args['content'], true );
263
264 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['post_id'] );
265
266 $args['page_settings'] = $page->get_data( 'settings' );
267
268 $template_id = $source->save_item( $args );
269
270 if ( is_wp_error( $template_id ) ) {
271 return $template_id;
272 }
273
274 return $source->get_item( $template_id );
275 }
276
277 /**
278 * Update template.
279 *
280 * Update template on the database.
281 *
282 * @since 1.0.0
283 * @access public
284 *
285 * @param array $template_data New template data.
286 *
287 * @return \WP_Error|Source_Base Template sources instance if the templates
288 * was updated, `WP_Error` otherwise.
289 */
290 public function update_template( array $template_data ) {
291 $validate_args = $this->ensure_args( [ 'source', 'content', 'type' ], $template_data );
292
293 if ( is_wp_error( $validate_args ) ) {
294 return $validate_args;
295 }
296
297 $source = $this->get_source( $template_data['source'] );
298
299 if ( ! $source ) {
300 return new \WP_Error( 'template_error', 'Template source not found.' );
301 }
302
303 $template_data['content'] = json_decode( $template_data['content'], true );
304
305 $update = $source->update_item( $template_data );
306
307 if ( is_wp_error( $update ) ) {
308 return $update;
309 }
310
311 return $source->get_item( $template_data['id'] );
312 }
313
314 /**
315 * Update templates.
316 *
317 * Update template on the database.
318 *
319 * @since 1.0.0
320 * @access public
321 *
322 * @param array $args Template arguments.
323 *
324 * @return \WP_Error|true True if templates updated, `WP_Error` otherwise.
325 */
326 public function update_templates( array $args ) {
327 foreach ( $args['templates'] as $template_data ) {
328 $result = $this->update_template( $template_data );
329
330 if ( is_wp_error( $result ) ) {
331 return $result;
332 }
333 }
334
335 return true;
336 }
337
338 /**
339 * Get template data.
340 *
341 * Retrieve the template data.
342 *
343 * @since 1.5.0
344 * @access public
345 *
346 * @param array $args Template arguments.
347 *
348 * @return \WP_Error|bool|array ??
349 */
350 public function get_template_data( array $args ) {
351 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
352
353 if ( is_wp_error( $validate_args ) ) {
354 return $validate_args;
355 }
356
357 if ( isset( $args['edit_mode'] ) ) {
358 Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
359 }
360
361 $source = $this->get_source( $args['source'] );
362
363 if ( ! $source ) {
364 return new \WP_Error( 'template_error', 'Template source not found.' );
365 }
366
367 do_action( 'elementor/template-library/before_get_source_data', $args, $source );
368
369 $data = $source->get_data( $args );
370
371 do_action( 'elementor/template-library/after_get_source_data', $args, $source );
372
373 return $data;
374 }
375
376 /**
377 * Delete template.
378 *
379 * Delete template from the database.
380 *
381 * @since 1.0.0
382 * @access public
383 *
384 * @param array $args Template arguments.
385 *
386 * @return \WP_Post|\WP_Error|false|null Post data on success, false or null
387 * or 'WP_Error' on failure.
388 */
389 public function delete_template( array $args ) {
390 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
391
392 if ( is_wp_error( $validate_args ) ) {
393 return $validate_args;
394 }
395
396 $source = $this->get_source( $args['source'] );
397
398 if ( ! $source ) {
399 return new \WP_Error( 'template_error', 'Template source not found.' );
400 }
401
402 return $source->delete_template( $args['template_id'] );
403 }
404
405 /**
406 * Export template.
407 *
408 * Export template to a file.
409 *
410 * @since 1.0.0
411 * @access public
412 *
413 * @param array $args Template arguments.
414 *
415 * @return mixed Whether the export succeeded or failed.
416 */
417 public function export_template( array $args ) {
418 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
419
420 if ( is_wp_error( $validate_args ) ) {
421 return $validate_args;
422 }
423
424 $source = $this->get_source( $args['source'] );
425
426 if ( ! $source ) {
427 return new \WP_Error( 'template_error', 'Template source not found' );
428 }
429
430 return $source->export_template( $args['template_id'] );
431 }
432
433 /**
434 * @since 2.3.0
435 * @access public
436 */
437 public function direct_import_template() {
438 /** @var Source_Local $source */
439 $source = $this->get_source( 'local' );
440
441 return $source->import_template( $_FILES['file']['name'], $_FILES['file']['tmp_name'] );
442 }
443
444 /**
445 * Import template.
446 *
447 * Import template from a file.
448 *
449 * @since 1.0.0
450 * @access public
451 *
452 * @param array $data
453 *
454 * @return mixed Whether the export succeeded or failed.
455 */
456 public function import_template( array $data ) {
457 // If the template is a JSON file, allow uploading it.
458 add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
459 add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
460
461 // Imported templates can be either JSON files, or Zip files containing multiple JSON files
462 $upload_result = Plugin::$instance->uploads_manager->handle_elementor_upload( $data, [ 'zip', 'json' ] );
463
464 remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
465 remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
466
467 if ( is_wp_error( $upload_result ) ) {
468 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
469
470 return $upload_result;
471 }
472
473 /** @var Source_Local $source_local */
474 $source_local = $this->get_source( 'local' );
475
476 $import_result = $source_local->import_template( $upload_result['name'], $upload_result['tmp_name'] );
477
478 // Remove the temporary directory generated for the stream-uploaded file.
479 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
480
481 return $import_result;
482 }
483
484 /**
485 * Enable JSON Template Upload
486 *
487 * Runs on the 'elementor/files/allow-file-type/json' Uploads Manager filter.
488 *
489 * @since 3.5.0
490 * @access public
491 *
492 * return bool
493 */
494 public function enable_json_template_upload() {
495 return true;
496 }
497
498 /**
499 * Mark template as favorite.
500 *
501 * Add the template to the user favorite templates.
502 *
503 * @since 1.9.0
504 * @access public
505 *
506 * @param array $args Template arguments.
507 *
508 * @return mixed Whether the template marked as favorite.
509 */
510 public function mark_template_as_favorite( $args ) {
511 $validate_args = $this->ensure_args( [ 'source', 'template_id', 'favorite' ], $args );
512
513 if ( is_wp_error( $validate_args ) ) {
514 return $validate_args;
515 }
516
517 $source = $this->get_source( $args['source'] );
518
519 return $source->mark_as_favorite( $args['template_id'], filter_var( $args['favorite'], FILTER_VALIDATE_BOOLEAN ) );
520 }
521
522 /**
523 * Register default template sources.
524 *
525 * Register the 'local' and 'remote' template sources that Elementor use by
526 * default.
527 *
528 * @since 1.0.0
529 * @access private
530 */
531 private function register_default_sources() {
532 $sources = [
533 'local',
534 'remote',
535 ];
536
537 foreach ( $sources as $source_filename ) {
538 $class_name = ucwords( $source_filename );
539 $class_name = str_replace( '-', '_', $class_name );
540
541 $this->register_source( __NAMESPACE__ . '\Source_' . $class_name );
542 }
543 }
544
545 /**
546 * Handle ajax request.
547 *
548 * Fire authenticated ajax actions for any given ajax request.
549 *
550 * @since 1.0.0
551 * @access private
552 *
553 * @param string $ajax_request Ajax request.
554 *
555 * @param array $data
556 *
557 * @return mixed
558 * @throws \Exception
559 */
560 private function handle_ajax_request( $ajax_request, array $data ) {
561 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
562 throw new \Exception( 'Access Denied' );
563 }
564
565 if ( ! empty( $data['editor_post_id'] ) ) {
566 $editor_post_id = absint( $data['editor_post_id'] );
567
568 if ( ! get_post( $editor_post_id ) ) {
569 throw new \Exception( esc_html__( 'Post not found.', 'elementor' ) );
570 }
571
572 Plugin::$instance->db->switch_to_post( $editor_post_id );
573 }
574
575 $result = call_user_func( [ $this, $ajax_request ], $data );
576
577 if ( is_wp_error( $result ) ) {
578 throw new \Exception( $result->get_error_message() );
579 }
580
581 return $result;
582 }
583
584 /**
585 * Init ajax calls.
586 *
587 * Initialize template library ajax calls for allowed ajax requests.
588 *
589 * @since 2.3.0
590 * @access public
591 *
592 * @param Ajax $ajax
593 */
594 public function register_ajax_actions( Ajax $ajax ) {
595 $library_ajax_requests = [
596 'get_library_data',
597 'get_template_data',
598 'save_template',
599 'update_templates',
600 'delete_template',
601 'import_template',
602 'mark_template_as_favorite',
603 ];
604
605 foreach ( $library_ajax_requests as $ajax_request ) {
606 $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
607 return $this->handle_ajax_request( $ajax_request, $data );
608 } );
609 }
610 }
611
612 /**
613 * @since 2.3.0
614 * @access public
615 */
616 public function handle_direct_actions() {
617 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
618 return;
619 }
620
621 /** @var Ajax $ajax */
622 $ajax = Plugin::$instance->common->get_component( 'ajax' );
623
624 if ( ! $ajax->verify_request_nonce() ) {
625 $this->handle_direct_action_error( 'Access Denied' );
626 }
627
628 $action = $_REQUEST['library_action'];
629
630 $result = $this->$action( $_REQUEST );
631
632 if ( is_wp_error( $result ) ) {
633 /** @var \WP_Error $result */
634 $this->handle_direct_action_error( $result->get_error_message() . '.' );
635 }
636
637 $callback = "on_{$action}_success";
638
639 if ( method_exists( $this, $callback ) ) {
640 $this->$callback( $result );
641 }
642
643 die;
644 }
645
646 /**
647 * On successful template import.
648 *
649 * Redirect the user to the template library after template import was
650 * successful finished.
651 *
652 * @since 2.3.0
653 * @access private
654 */
655 private function on_direct_import_template_success() {
656 wp_safe_redirect( admin_url( Source_Local::ADMIN_MENU_SLUG ) );
657 }
658
659 /**
660 * @since 2.3.0
661 * @access private
662 */
663 private function handle_direct_action_error( $message ) {
664 _default_wp_die_handler( $message, 'Elementor Library' );
665 }
666
667 /**
668 * Ensure arguments exist.
669 *
670 * Checks whether the required arguments exist in the specified arguments.
671 *
672 * @since 1.0.0
673 * @access private
674 *
675 * @param array $required_args Required arguments to check whether they
676 * exist.
677 * @param array $specified_args The list of all the specified arguments to
678 * check against.
679 *
680 * @return \WP_Error|true True on success, 'WP_Error' otherwise.
681 */
682 private function ensure_args( array $required_args, array $specified_args ) {
683 $not_specified_args = array_diff( $required_args, array_keys( $specified_args ) );
684
685 if ( $not_specified_args ) {
686 return new \WP_Error( 'arguments_not_specified', sprintf( 'The required argument(s) "%s" not specified.', implode( ', ', $not_specified_args ) ) );
687 }
688
689 return true;
690 }
691 }
692