PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.1.8
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.1.8
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / core / api / class-importer.php

class-importer.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 1.1.8, at core/api/class-importer.php

129 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately;
4
5 use Elementor\TemplateLibrary\Source_Local;
6 use Elementor\TemplateLibrary\Classes\Images;
7 use Elementor\Core\Settings\Page\Model;
8 use Elementor\Plugin as ElementorPlugin;
9
10 class Importer extends Source_Local {
11 /**
12 * Get template data.
13 *
14 * @inheritDoc
15 *
16 * @param array $args Custom template arguments.
17 * @param string $context Optional. The context. Default is `display`.
18 *
19 * @return array Remote Template data.
20 */
21 public function get_data( array $args, $context = 'display' ) {
22 $data = API::get_template_content( $args, true );
23 if ( is_wp_error( $data ) ) {
24 return $data;
25 }
26 ElementorPlugin::$instance->editor->set_edit_mode( true );
27
28 $data['content'] = $this->replace_elements_ids( $data['content'] );
29 $data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
30
31 $post_id = $args['editor_post_id'];
32 $document = ElementorPlugin::$instance->documents->get( $post_id );
33 if ( $document ) {
34 $data['content'] = $document->get_elements_raw_data( $data['content'], true );
35 }
36 return $data;
37 }
38
39 public function import_templately( $data ) {
40 if ( empty( $data ) ) {
41 return new \WP_Error( 'file_error', 'Invalid File' );
42 }
43
44 $content = $data['content'];
45
46 if ( ! is_array( $content ) ) {
47 return new \WP_Error( 'file_error', 'Invalid File' );
48 }
49
50 $page_settings = [];
51
52 if ( ! empty( $data['page_settings'] ) ) {
53 $page = new Model( [
54 'id' => 0,
55 'settings' => $data['page_settings'],
56 ] );
57
58 $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
59
60 if ( ! empty( $page_settings_data['settings'] ) ) {
61 $page_settings = $page_settings_data['settings'];
62 }
63 }
64
65 $template_id = $this->save_item( [
66 'content' => $content,
67 'title' => $data['title'],
68 'type' => $data['type'],
69 'page_settings' => $page_settings,
70 ] );
71
72 if ( is_wp_error( $template_id ) ) {
73 return $template_id;
74 }
75
76 return $this->get_item( $template_id );
77 }
78
79 public function create_page( $template_data ){
80 $page_settings = [];
81
82 if ( ! empty( $template_data['page_settings'] ) ) {
83 $page = new Model( [
84 'id' => 0,
85 'settings' => $template_data['page_settings'],
86 ] );
87
88 $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
89
90 if ( ! empty( $page_settings_data['settings'] ) ) {
91 $page_settings = $page_settings_data['settings'];
92 }
93 }
94
95 $defaults = [
96 'post_title' => isset( $template_data['page_title'] ) ? $template_data['page_title'] : 'Templately: ' . $template_data['title'],
97 'page_settings' => $page_settings,
98 'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending',
99 ];
100
101 $template_data = wp_parse_args( $template_data, $defaults );
102
103 $document = ElementorPlugin::$instance->documents->create(
104 $template_data['type'],
105 [
106 'post_title' => $template_data['post_title'],
107 'post_status' => $template_data['status'],
108 'post_type' => 'page',
109 ]
110 );
111
112 if ( is_wp_error( $document ) ) {
113 /**
114 * @var \WP_Error $document
115 */
116 return $document;
117 }
118
119 $document->save( [
120 'elements' => $template_data['content'],
121 'settings' => $page_settings,
122 ] );
123
124 return $document->get_main_id();
125 }
126
127 }
128
129