PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0-dev4
Elementor Website Builder – more than just a page builder v3.35.0-dev4
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.35.0-dev4, at app/modules/import-export-customization/runners/import/templates.php

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