PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev3
Elementor Website Builder – more than just a page builder v3.32.0-dev3
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 / runners / import / templates.php

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

87 lines 2.4 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\ImportExport\Runners\Import;
3
4 use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils;
5 use Elementor\Plugin;
6 use Elementor\TemplateLibrary\Source_Local;
7 use Elementor\Utils;
8
9 class Templates extends Import_Runner_Base {
10 private $import_session_id;
11
12 public static function get_name(): string {
13 return 'templates';
14 }
15
16 public function should_import( array $data ) {
17 return (
18 Utils::has_pro() &&
19 isset( $data['include'] ) &&
20 in_array( 'templates', $data['include'], true ) &&
21 ! empty( $data['extracted_directory_path'] ) &&
22 ! empty( $data['manifest']['templates'] )
23 );
24 }
25
26 public function import( array $data, array $imported_data ) {
27 $this->import_session_id = $data['session_id'];
28
29 $path = $data['extracted_directory_path'] . 'templates/';
30 $templates = $data['manifest']['templates'];
31
32 $result['templates'] = [
33 'succeed' => [],
34 'failed' => [],
35 ];
36
37 foreach ( $templates as $id => $template_settings ) {
38 try {
39 $template_data = ImportExportUtils::read_json_file( $path . $id );
40 $import = $this->import_template( $id, $template_settings, $template_data );
41
42 $result['templates']['succeed'][ $id ] = $import;
43 } catch ( \Exception $error ) {
44 $result['templates']['failed'][ $id ] = $error->getMessage();
45 }
46 }
47
48 return $result;
49 }
50
51 private function import_template( $id, array $template_settings, array $template_data ) {
52 $doc_type = $template_settings['doc_type'];
53
54 $new_document = Plugin::$instance->documents->create(
55 $doc_type,
56 [
57 'post_title' => $template_settings['title'],
58 'post_type' => Source_Local::CPT,
59 'post_status' => 'publish',
60 ]
61 );
62
63 if ( is_wp_error( $new_document ) ) {
64 throw new \Exception( esc_html( $new_document->get_error_message() ) );
65 }
66
67 $template_data['import_settings'] = $template_settings;
68 $template_data['id'] = $id;
69
70 $new_attachment_callback = function( $attachment_id ) {
71 $this->set_session_post_meta( $attachment_id, $this->import_session_id );
72 };
73
74 add_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
75
76 $new_document->import( $template_data );
77
78 remove_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback );
79
80 $document_id = $new_document->get_main_id();
81
82 $this->set_session_post_meta( $document_id, $this->import_session_id );
83
84 return $document_id;
85 }
86 }
87