PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev1
Elementor Website Builder – more than just a page builder v3.32.0-dev1
4.3.0-beta3 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 All 452 releases
elementor / app / modules / import-export-customization / runners / import / templates.php

templates.php in Elementor Website Builder – more than just a page builder 3.32.0-dev1, at app/modules/import-export-customization/runners/import/templates.php

136 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\App\Modules\ImportExportCustomization\Runners\Import;
3
4 use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils;
5 use Elementor\Plugin;
6 use Elementor\TemplateLibrary\Source_Local;
7 use Elementor\Utils;
8 use Elementor\Modules\Library\Documents\Library_Document;
9
10 class Templates extends Import_Runner_Base {
11 private $import_session_id;
12
13 public static function get_name(): string {
14 return 'templates';
15 }
16
17 public function should_import( array $data ) {
18 return (
19 Utils::has_pro() &&
20 isset( $data['include'] ) &&
21 in_array( 'templates', $data['include'], true ) &&
22 ! empty( $data['extracted_directory_path'] ) &&
23 ! empty( $data['manifest']['templates'] )
24 );
25 }
26
27 public function import( array $data, array $imported_data ) {
28 $customization = $data['customization']['templates'] ?? null;
29
30 if ( $customization ) {
31 return $this->import_with_customization( $data, $imported_data, $customization );
32 }
33
34 return $this->import_all( $data, $imported_data );
35 }
36
37 private function import_with_customization( array $data, array $imported_data, array $customization ) {
38 $result = apply_filters( 'elementor/import-export-customization/import/templates/customization', null, $data, $imported_data, $customization, $this );
39
40 if ( is_array( $result ) ) {
41 return $result;
42 }
43
44 return $this->import_all( $data, $imported_data );
45 }
46
47 private function import_all( array $data, array $imported_data ) {
48 $template_types = array_keys( Plugin::$instance->documents->get_document_types( [
49 'is_editable' => true,
50 'show_in_library' => true,
51 'export_group' => Library_Document::EXPORT_GROUP,
52 ] ) );
53
54 $result = $this->process_templates_import( $data, $template_types );
55
56 /**
57 * Filter the templates import result to allow 3rd parties to add their own imported templates.
58 *
59 * @param array $result The import result structure with 'templates' key containing succeed/failed/succeed_summary.
60 * @param array $data The full import data.
61 * @param array|null $customization The customization settings for templates.
62 */
63 $customization = $data['customization']['templates'] ?? null;
64 $result = apply_filters( 'elementor/import-export-customization/import/templates_result', $result, $data, $customization );
65
66 return $result;
67 }
68
69 public function process_templates_import( array $data, array $template_types ) {
70 $this->import_session_id = $data['session_id'];
71
72 $path = $data['extracted_directory_path'] . 'templates/';
73 $templates = $data['manifest']['templates'];
74
75 $result['templates'] = [
76 'succeed' => [],
77 'failed' => [],
78 'succeed_summary' => [],
79 ];
80
81 foreach ( $templates as $id => $template_settings ) {
82 if ( ! empty( $template_types ) && ! in_array( $template_settings['doc_type'], $template_types, true ) ) {
83 continue;
84 }
85
86 try {
87 $template_data = ImportExportUtils::read_json_file( $path . $id );
88 $import = $this->import_template( $id, $template_settings, $template_data );
89
90 $result['templates']['succeed'][ $id ] = $import;
91 $result['templates']['succeed_summary'][ $template_settings['doc_type'] ] = ( $result['templates']['succeed_summary'][ $template_settings['doc_type'] ] ?? 0 ) + 1;
92 } catch ( \Exception $error ) {
93 $result['templates']['failed'][ $id ] = $error->getMessage();
94 }
95 }
96
97 return $result;
98 }
99
100 public function import_template( $id, array $template_settings, array $template_data ) {
101 $doc_type = $template_settings['doc_type'];
102
103 $new_document = Plugin::$instance->documents->create(
104 $doc_type,
105 [
106 'post_title' => $template_settings['title'],
107 'post_type' => Source_Local::CPT,
108 'post_status' => 'publish',
109 ]
110 );
111
112 if ( is_wp_error( $new_document ) ) {
113 throw new \Exception( esc_html( $new_document->get_error_message() ) );
114 }
115
116 $template_data['import_settings'] = $template_settings;
117 $template_data['id'] = $id;
118
119 $new_attachment_callback = function( $attachment_id ) {
120 $this->set_session_post_meta( $attachment_id, $this->import_session_id );
121 };
122
123 add_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
124
125 $new_document->import( $template_data );
126
127 remove_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
128
129 $document_id = $new_document->get_main_id();
130
131 $this->set_session_post_meta( $document_id, $this->import_session_id );
132
133 return $document_id;
134 }
135 }
136