PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.2.13
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.2.13
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 / includes / Core / Importer / Elementor.php

Elementor.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 2.2.13, at includes/Core/Importer/Elementor.php

190 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Core\Importer;
4
5 use WP_Error;
6 use function wp_parse_args;
7 use function current_user_can;
8 use Elementor\Core\Settings\Page\Model;
9
10 use Elementor\Plugin as ElementorPlugin;
11 use Elementor\TemplateLibrary\Source_Local as ElementorLocal;
12
13 class Elementor extends ElementorLocal {
14 /**
15 * Get template data.
16 *
17 * @inheritDoc
18 *
19 * @param array $args Custom template arguments.
20 *
21 * @return array Remote Template data.
22 */
23 public function get_data( array $args ) {
24 ElementorPlugin::$instance->editor->set_edit_mode( true );
25
26 $args['content'] = $this->replace_elements_ids( $args['content'] );
27 $args['content'] = $this->process_export_import_content( $args['content'], 'on_import' );
28
29 $args['content'] = $this->iterate_data(
30 $args['content'], function( $element_data ) {
31 $element = ElementorPlugin::$instance->elements_manager->create_element_instance( $element_data );
32
33 // If the widget/element isn't exist, like a plugin that creates a widget but deactivated
34 if ( ! $element ) {
35 return null;
36 }
37
38 return $element_data;
39 }
40 );
41
42 if (!isset($args['page_settings'])) {
43 $args['page_settings'] = array();
44 }
45 if(!isset($args['page_settings']["template"])){
46 $args['page_settings']["template"] = "elementor_header_footer";
47 }
48
49 // $post_id = false; // FIXME: need to check later on.
50 // $document = ElementorPlugin::$instance->documents->get( $post_id );
51 // if ( $document ) {
52 // $args['content'] = $document->get_elements_raw_data( $args['content'], true );
53 // }
54 return $args;
55 }
56
57 public function import_in_library( $data ) {
58 if ( empty( $data ) ) {
59 return new WP_Error( 'file_error', 'Invalid File' );
60 }
61
62 $content = $data['content'];
63
64 if ( ! is_array( $content ) ) {
65 return new WP_Error( 'file_error', 'Invalid File' );
66 }
67
68 $page_settings = $this->page_settings( $data );
69
70 $template_id = $this->save_item( [
71 'content' => $content,
72 'title' => $data['title'],
73 'type' => $data['type'],
74 'page_settings' => $page_settings,
75 ] );
76
77 if ( is_wp_error( $template_id ) ) {
78 return $template_id;
79 }
80
81 return $this->get_item( $template_id );
82 }
83
84 public function create_page( $template_data ){
85 $page_settings = $this->page_settings( $template_data );
86
87 $defaults = [
88 'post_title' => isset( $template_data['page_title'] ) ? $template_data['page_title'] : 'Templately: ' . $template_data['title'],
89 'page_settings' => $page_settings,
90 'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending',
91 ];
92
93 $template_data = wp_parse_args( $template_data, $defaults );
94
95 $document = ElementorPlugin::$instance->documents->create(
96 $template_data['type'],
97 [
98 'post_title' => $template_data['post_title'],
99 'post_status' => $template_data['status'],
100 'post_type' => 'page',
101 ]
102 );
103
104 if ( is_wp_error( $document ) ) {
105 /**
106 * @var WP_Error $document
107 */
108 return $document;
109 }
110
111 $document->save( [
112 'elements' => $template_data['content'],
113 'settings' => $page_settings,
114 ] );
115
116 return $document->get_main_id();
117 }
118
119 /**
120 * @param $template_data
121 * @noinspection DuplicatedCode
122 *
123 * @return array
124 */
125 private function page_settings( $template_data ) {
126 $page_settings = [];
127
128 if (!isset($template_data['page_settings'])) {
129 $template_data['page_settings'] = array();
130 }
131
132 if(!isset($template_data['page_settings']["template"])){
133 $template_data['page_settings']["template"] = "elementor_header_footer";
134 }
135
136 $page = new Model([
137 'id' => 0,
138 'settings' => $template_data['page_settings'],
139 ]);
140
141 $page_settings_data = $this->process_element_export_import_content($page, 'on_import');
142
143 if (!empty($page_settings_data['settings'])) {
144 $page_settings = $page_settings_data['settings'];
145 }
146
147 return $page_settings;
148 }
149
150 /**
151 * Iterate data.
152 *
153 * Accept any type of Elementor data and a callback function. The callback
154 * function runs recursively for each element and his child elements.
155 *
156 * @since 1.0.0
157 * @access public
158 *
159 * @param array $data_container Any type of elementor data.
160 * @param callable $callback A function to iterate data by.
161 * @param array $args Array of args pointers for passing parameters in & out of the callback
162 *
163 * @return mixed Iterated data.
164 */
165 public function iterate_data( $data_container, $callback, $args = [] ) {
166 if ( isset( $data_container['elType'] ) ) {
167 if ( ! empty( $data_container['elements'] ) ) {
168 $data_container['elements'] = $this->iterate_data( $data_container['elements'], $callback, $args );
169 }
170
171 return call_user_func( $callback, $data_container, $args );
172 }
173
174 foreach ( $data_container as $element_key => $element_value ) {
175 $element_data = $this->iterate_data( $data_container[ $element_key ], $callback, $args );
176
177 if ( null === $element_data ) {
178 unset($data_container[ $element_key ]);
179 continue;
180 }
181
182 $data_container[ $element_key ] = $element_data;
183 }
184
185 return array_values($data_container);
186 }
187
188 }
189
190