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

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