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

177 lines 4.5 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 // $post_id = false; // FIXME: need to check later on.
43 // $document = ElementorPlugin::$instance->documents->get( $post_id );
44 // if ( $document ) {
45 // $args['content'] = $document->get_elements_raw_data( $args['content'], true );
46 // }
47 return $args;
48 }
49
50 public function import_in_library( $data ) {
51 if ( empty( $data ) ) {
52 return new WP_Error( 'file_error', 'Invalid File' );
53 }
54
55 $content = $data['content'];
56
57 if ( ! is_array( $content ) ) {
58 return new WP_Error( 'file_error', 'Invalid File' );
59 }
60
61 $page_settings = $this->page_settings( $data );
62
63 $template_id = $this->save_item( [
64 'content' => $content,
65 'title' => $data['title'],
66 'type' => $data['type'],
67 'page_settings' => $page_settings,
68 ] );
69
70 if ( is_wp_error( $template_id ) ) {
71 return $template_id;
72 }
73
74 return $this->get_item( $template_id );
75 }
76
77 public function create_page( $template_data ){
78 $page_settings = $this->page_settings( $template_data );
79
80 $defaults = [
81 'post_title' => isset( $template_data['page_title'] ) ? $template_data['page_title'] : 'Templately: ' . $template_data['title'],
82 'page_settings' => $page_settings,
83 'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending',
84 ];
85
86 $template_data = wp_parse_args( $template_data, $defaults );
87
88 $document = ElementorPlugin::$instance->documents->create(
89 $template_data['type'],
90 [
91 'post_title' => $template_data['post_title'],
92 'post_status' => $template_data['status'],
93 'post_type' => 'page',
94 ]
95 );
96
97 if ( is_wp_error( $document ) ) {
98 /**
99 * @var WP_Error $document
100 */
101 return $document;
102 }
103
104 $document->save( [
105 'elements' => $template_data['content'],
106 'settings' => $page_settings,
107 ] );
108
109 return $document->get_main_id();
110 }
111
112 /**
113 * @param $template_data
114 * @noinspection DuplicatedCode
115 *
116 * @return array
117 */
118 private function page_settings( $template_data ) {
119 $page_settings = [];
120
121 if ( ! empty( $template_data['page_settings'] ) ) {
122 $page = new Model( [
123 'id' => 0,
124 'settings' => $template_data['page_settings'],
125 ] );
126
127 $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
128
129 if ( ! empty( $page_settings_data['settings'] ) ) {
130 $page_settings = $page_settings_data['settings'];
131 }
132 }
133
134 return $page_settings;
135 }
136
137 /**
138 * Iterate data.
139 *
140 * Accept any type of Elementor data and a callback function. The callback
141 * function runs recursively for each element and his child elements.
142 *
143 * @since 1.0.0
144 * @access public
145 *
146 * @param array $data_container Any type of elementor data.
147 * @param callable $callback A function to iterate data by.
148 * @param array $args Array of args pointers for passing parameters in & out of the callback
149 *
150 * @return mixed Iterated data.
151 */
152 public function iterate_data( $data_container, $callback, $args = [] ) {
153 if ( isset( $data_container['elType'] ) ) {
154 if ( ! empty( $data_container['elements'] ) ) {
155 $data_container['elements'] = $this->iterate_data( $data_container['elements'], $callback, $args );
156 }
157
158 return call_user_func( $callback, $data_container, $args );
159 }
160
161 foreach ( $data_container as $element_key => $element_value ) {
162 $element_data = $this->iterate_data( $data_container[ $element_key ], $callback, $args );
163
164 if ( null === $element_data ) {
165 unset($data_container[ $element_key ]);
166 continue;
167 }
168
169 $data_container[ $element_key ] = $element_data;
170 }
171
172 return array_values($data_container);
173 }
174
175 }
176
177