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

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

318 lines 10.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\Core\Importer\Runners;
4
5 use Elementor\Plugin;
6 use Elementor\TemplateLibrary\Source_Local;
7 use Exception;
8 use Templately\Core\Importer\Utils\Utils;
9 use Templately\Utils\Helper;
10
11 class ElementorContent extends BaseRunner {
12 public function get_name(): string {
13 return 'content';
14 }
15
16 public function get_label(): string {
17 return __( 'Elementor', 'templately' );
18 }
19
20 public function should_run( $data, $imported_data = [] ): bool {
21 return $this->manifest['platform'] === 'elementor' && ! empty( $this->manifest['content'] );
22 }
23
24 public function should_log(): bool {
25 return true;
26 }
27
28 public function get_action(): string {
29 return 'updateLog';
30 }
31
32 public function log_message(): string {
33 return __( 'Importing Elementor Page and Post Templates', 'templately' );
34 }
35
36 /**
37 * Get kit by ID with backward compatibility.
38 *
39 * The get_kit() method was introduced in Elementor 3.13.0.
40 * For older versions, we fall back to using documents->get() directly.
41 *
42 * @param \Elementor\Core\Kits\Manager $kits_manager The kits manager instance.
43 * @param int $kit_id The kit ID to retrieve.
44 * @return \Elementor\Core\Kits\Documents\Kit|null The kit document or null.
45 */
46 private function get_kit_with_fallback( $kits_manager, $kit_id ) {
47 // Check if the get_kit method exists (Elementor 3.13.0+)
48 if ( method_exists( $kits_manager, 'get_kit' ) ) {
49 return $kits_manager->get_kit( $kit_id );
50 }
51
52 // Fallback for older Elementor versions (< 3.13.0)
53 // This replicates the old get_active_kit() behavior
54 return Plugin::$instance->documents->get( $kit_id );
55 }
56
57 /**
58 * Create a new kit with backward compatibility.
59 *
60 * The create_new_kit() method was introduced in Elementor 3.13.0.
61 * For older versions, we fall back to using documents->create() directly.
62 *
63 * @param \Elementor\Core\Kits\Manager $kits_manager The kits manager instance.
64 * @param string $kit_name The name for the new kit.
65 * @param array $settings The kit settings.
66 * @param bool $active Whether to set this kit as active.
67 * @return int The created kit ID.
68 */
69 private function create_new_kit_with_fallback( $kits_manager, $kit_name = '', $settings = [], $active = true ) {
70 // Check if the create_new_kit method exists (Elementor 3.13.0+)
71 if ( method_exists( $kits_manager, 'create_new_kit' ) ) {
72 return $kits_manager->create_new_kit( $kit_name, $settings, $active );
73 }
74
75 // Fallback for older Elementor versions (< 3.13.0)
76 $kit_name = $kit_name ? $kit_name : esc_html__( 'Custom', 'templately' );
77
78 $kit = Plugin::$instance->documents->create( 'kit', [
79 'post_type' => Source_Local::CPT,
80 'post_title' => $kit_name,
81 'post_status' => 'publish',
82 ] );
83
84 $kit_id = $kit->get_id();
85
86 // Apply settings if provided
87 if ( ! empty( $settings ) ) {
88 $kit->save( [ 'settings' => $settings ] );
89 }
90
91 // Set as active if requested
92 if ( $active ) {
93 update_option( $kits_manager::OPTION_ACTIVE, $kit_id );
94 }
95
96 return $kit_id;
97 }
98
99 /**
100 * Create a default kit with backward compatibility.
101 *
102 * The create_default() method was made public in Elementor 3.12.0.
103 * For older versions, we fall back to using documents->create() directly.
104 *
105 * @param \Elementor\Core\Kits\Manager $kits_manager The kits manager instance.
106 * @return int The created kit ID.
107 */
108 private function create_default_kit_with_fallback( $kits_manager ) {
109 // Check if create_default is publicly accessible (Elementor 3.12.0+)
110 if ( method_exists( $kits_manager, 'create_default' ) && is_callable( [ $kits_manager, 'create_default' ] ) ) {
111 return $kits_manager->create_default();
112 }
113
114 // Fallback for older Elementor versions (< 3.12.0)
115 $kit = Plugin::$instance->documents->create( 'kit', [
116 'post_type' => Source_Local::CPT,
117 'post_title' => esc_html__( 'Default Kit', 'templately' ),
118 'post_status' => 'publish',
119 ] );
120
121 return $kit->get_id();
122 }
123
124 /**
125 * @throws Exception
126 */
127 public function import( $data, $imported_data ): array {
128 $results = $data["imported_data"]["content"] ?? [];
129 $contents = $this->manifest['content'];
130 $path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR;
131 $processed_templates = $this->get_progress();
132
133 // $total = array_reduce( $contents, function ( $carry, $item ) {
134 // return $carry + count( $item );
135 // }, 0 );
136 // $processed = 0;
137
138 /**
139 * Check if there is any active kit?
140 * If not, create one.
141 */
142
143 $kits_manager = Plugin::$instance->kits_manager;
144
145 $active_kit = $kits_manager->get_active_id();
146 $kit = $this->get_kit_with_fallback( $kits_manager, $active_kit );
147 $old_logo = $kit->get_settings('site_logo');
148
149 if(isset($this->manifest['has_settings']) && $this->manifest['has_settings'] && !in_array("global_colors", $processed_templates)){
150 // backing up the active kit id before updating the new one
151 if(!get_option("__templately_" . $kits_manager::OPTION_ACTIVE)){
152 add_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, '', 'no');
153 }
154 else{
155 update_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, 'no');
156 }
157
158 $file = $this->dir_path . "settings.json";
159 $settings = Utils::read_json_file( $file );
160
161 if(isset($settings['site_name'])){
162 unset($settings['site_name']);
163 }
164 if(isset($settings['site_description'])){
165 unset($settings['site_description']);
166 }
167 if(!empty($data['color'])){
168 if (!empty($settings['system_colors'])) {
169 foreach ($settings['system_colors'] as $key => $color) {
170 $settings['system_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
171 }
172 }
173 if (!empty($settings['custom_colors'])) {
174 foreach ($settings['custom_colors'] as $key => $color) {
175 $settings['custom_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
176 }
177 }
178 }
179
180 if(!empty($data['typography'])){
181 if (!empty($settings['system_typography'])) {
182 foreach ($settings['system_typography'] as $key => &$typography) {
183 if(!empty($data['typography'][$typography['_id']])){
184 $typography = array_merge($typography, $data['typography'][$typography['_id']]);
185 }
186 }
187 }
188 if (!empty($settings['custom_typography'])) {
189 foreach ($settings['custom_typography'] as $key => &$typography) {
190 if(!empty($data['typography'][$typography['_id']])){
191 $typography = array_merge($typography, $data['typography'][$typography['_id']]);
192 }
193 }
194 }
195 }
196
197 if (!empty($data['logo']['id'])) {
198 $settings['site_logo'] = $data['logo'];
199 Utils::backup_option_value( 'site_logo' );
200 $this->origin->update_imported_list('attachment', $data['logo']['id']);
201 } elseif (!empty($data['logo'])) {
202 $settings['site_logo'] = $old_logo;
203
204 // If there's no old logo id, try to upload a new logo
205 if (empty($old_logo['id'])) {
206 $site_logo = Utils::upload_logo($data['logo'], $this->session_id);
207
208 // If the upload was successful, use the new logo, otherwise use the old one
209 if(!empty($site_logo['id'])){
210 $settings['site_logo'] = $site_logo;
211 Utils::backup_option_value( 'site_logo' );
212 $this->origin->update_imported_list('attachment', $site_logo['id']);
213 }
214 }
215 }
216
217
218 $kit_id = $this->create_new_kit_with_fallback( $kits_manager, $this->manifest['name'], $settings, true );
219
220 $kit = $this->get_kit_with_fallback( $kits_manager, $kit_id );
221
222 // $kit->update_settings( ['site_logo' => $settings['site_logo']] );
223
224 // Create an array with the post ID and the new title
225 $post_data = array(
226 'ID' => $kit_id,
227 'post_title' => $this->manifest['name'] . " Kit",
228 );
229 // Update the post
230 wp_update_post( $post_data );
231
232 $processed_templates[] = "global_colors";
233 $this->update_progress( $processed_templates);
234 }
235
236 $active_kit = $kits_manager->get_active_id();
237 $kit = $this->get_kit_with_fallback( $kits_manager, $active_kit );
238
239 if ( ! $kit->get_id() ) {
240 $kit_id = $this->create_default_kit_with_fallback( $kits_manager );
241 update_option( $kits_manager::OPTION_ACTIVE, $kit_id );
242 $kit = $this->get_kit_with_fallback( $kits_manager, $kit_id );
243 }
244
245 // $processed = 0;
246 $total = array_reduce($contents, function($carry, $item) {
247 return $carry + count($item);
248 }, 0);
249
250 $results = $this->loop( $contents, function($post_type, $post, $results ) use($path, $imported_data, $total) {
251 return $this->loop( $post, function($id, $content_settings, $result ) use ($post_type, $results, $path, $imported_data, $total) {
252 if ( post_type_exists( $post_type ) ) {
253
254 $import = $this->import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings );
255
256 if ( ! $import ) {
257 $result[ $post_type ]['failed'][ $id ] = $import;
258 } else {
259 Utils::import_page_settings( $import, $content_settings );
260 $result[ $post_type ]['succeed'][ $id ] = $import;
261 }
262
263 // Broadcast Log
264 $processed = 0;
265 $results = Helper::recursive_wp_parse_args($result, $results);
266 array_walk_recursive($results, function($item) use (&$processed) {
267 $processed++;
268 });
269 $progress = floor( ( 100 * $processed ) / $total );
270 $this->log( $progress );
271 }
272
273 return $result;
274 }, $post_type); //, true
275 });
276
277 return [ 'content' => $results ];
278 }
279
280 /**
281 * @throws Exception
282 */
283 private function import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ) {
284 try {
285 $template = $this->factory->create( $content_settings['doc_type'], [
286 'post_title' => $content_settings['title'],
287 'post_status' => 'publish',
288 'post_type' => $post_type,
289 ] );
290
291 $file = $path . $post_type . DIRECTORY_SEPARATOR . "{$id}.json";
292 $post_data = Utils::read_json_file( $file );
293
294 if ( ! empty( $content_settings['data'] ) || $this->is_ai_content($id) ) {
295 /**
296 * TODO:
297 *
298 * We can check if there is any data for settings.
299 * if yes: ignore content from insert.
300 *
301 * Process the content while finalizing.
302 */
303 // $this->json->prepare( $post_data['content'], $id, $content_settings['data'], $imported_data );
304
305 $post_data['content'] = [];
306 }
307
308 unset($content_settings['conditions']);
309 $post_data['import_settings'] = $content_settings;
310
311 $template->import( $post_data );
312
313 return $template->get_main_id();
314 } catch ( Exception $e ) {
315 return false;
316 }
317 }
318 }