PluginProbe
Hello Plus / 1.2.0
Hello Plus v1.2.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / modules / template-parts / classes / runners / import.php

import.php in Hello Plus 1.2.0, at modules/template-parts/classes/runners/import.php

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