PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.1
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / includes / ExportImport / TemplateExport.php
kirki / includes / ExportImport Last commit date
TemplateExport.php 3 days ago TemplateImport.php 3 weeks ago
TemplateExport.php
764 lines
1 <?php
2
3 /**
4 * FormValidator class
5 *
6 * This class is responsible for validating form data
7 *
8 * @package kirki
9 */
10
11 namespace Kirki\ExportImport;
12
13 use Kirki\Ajax\UserData;
14 use Kirki\API\ContentManager\ContentManagerHelper;
15 use Kirki\HelperFunctions;
16 use Kirki\Staging;
17
18 if (!defined('ABSPATH')) {
19 exit; // Exit if accessed directly.
20 }
21
22 class TemplateExport
23 {
24
25
26 private $discarded_meta = array('_edit_lock', '_edit_last');
27 public $asset_urls = array();
28 private $asset_urls_tracker = array();
29 private $only_published_pages = true;
30
31 public function __construct()
32 {
33
34 }
35
36 public function export($is_assets_with, $only_published_pages = true)
37 {
38 $environment = array(
39 'asset_urls_tracker' => $this->asset_urls_tracker,
40 'asset_urls' => $this->asset_urls,
41 'template_data' => array('exportId' => uniqid('', true)),
42 'only_published_pages' => $only_published_pages,
43 'queue' => array(
44 'pages_symbols_popups_templates_utility_pages',
45 'viewPorts_customFonts',
46 'globalStyleBlocks_variables',
47 'contentManager',
48 'contentManagerRefFields',
49 'siteInfo',
50 ),
51 );
52
53 if ($is_assets_with) {
54 $environment['queue'][] = 'assetUrls';
55 }
56
57 HelperFunctions::update_global_data_using_key('kirki_project_export', $environment);
58 return array(
59 'status' => 'init',
60 'queue' => $environment['queue'],
61 );
62 }
63
64 public function process()
65 {
66 $environment = HelperFunctions::get_global_data_using_key('kirki_project_export');
67 if (!$environment) {
68 return array(
69 'status' => false,
70 'message' => 'Nothing found to export',
71 );
72 }
73 $site_name = get_bloginfo('name');
74 $template_data = $environment['template_data'];
75 $template_data['variable_prefix'] = $site_name;
76 $this->only_published_pages = $environment['only_published_pages'];
77
78 $formattedString = preg_replace('/[^a-zA-Z ]/', '', $site_name); // Remove numbers & special symbols
79 $formattedString = str_replace(' ', '_', strtolower($formattedString));
80 $template_data['batch_id'] = $formattedString;
81 $template_data['prefix'] = $formattedString;
82
83 $queue = $environment['queue'];
84 if (count($queue) > 0) {
85 $this->asset_urls_tracker = $environment['asset_urls_tracker'];
86 $this->asset_urls = $environment['asset_urls'];
87
88 $current_task = array_shift($queue);
89 switch ($current_task) {
90 case 'pages_symbols_popups_templates_utility_pages': {
91 $template_data['pages'] = $this->get_all_pages();
92 $template_data['symbols'] = $this->get_all_symbols();
93 $template_data['popups'] = $this->get_all_popups();
94 $template_data['templates'] = $this->get_all_templates();
95 $template_data['utility_pages'] = $this->get_all_utility_pages();
96 break;
97 }
98 case 'viewPorts_customFonts': {
99 $template_data['viewPorts'] = $this->get_view_ports();
100 $template_data['customFonts'] = $this->get_custom_fonts();
101 break;
102 }
103 case 'globalStyleBlocks_variables': {
104 $template_data['globalStyleBlocks'] = $this->get_global_style_blocks();
105 $template_data['variables'] = UserData::get_kirki_variable_data();
106 break;
107 }
108 case 'contentManager': {
109 $template_data['contentManager'] = $this->get_all_content_manager_data();
110 break;
111 }
112 case 'contentManagerRefFields': {
113 $template_data['contentManagerRefFields'] = $this->get_all_content_manager_ref_fields();
114 break;
115 }
116 case 'assetUrls': {
117 $this->extract_asset_url_from_template_data($template_data);
118 $template_data['assetUrls'] = $this->asset_urls_tracker;
119 break;
120 }
121
122 case 'siteInfo': {
123 $template_data['siteInfo'] = array(
124 'siteName' => get_bloginfo('name'),
125 'siteUrl' => get_bloginfo('url'),
126 'siteDescription' => get_bloginfo('description'),
127 'siteLogo' => get_site_icon_url(),
128 'page_on_front' => get_option('page_on_front'),
129 'show_on_front' => get_option('show_on_front'),
130 );
131 break;
132 }
133 default:
134 // code...
135 break;
136 }
137
138 $environment['queue'] = $queue;
139 $environment['asset_urls_tracker'] = $this->asset_urls_tracker;
140 $environment['asset_urls'] = $this->asset_urls;
141 $environment['template_data'] = $template_data;
142 HelperFunctions::update_global_data_using_key('kirki_project_export', $environment);
143 return array(
144 'status' => 'exporting',
145 'queue' => $queue,
146 'done' => $current_task,
147 );
148 } else {
149 HelperFunctions::update_global_data_using_key('kirki_project_export', false);
150 $zip_file_url = $this->export_and_get_url($template_data);
151 return array(
152 'status' => 'done',
153 'queue' => $queue,
154 'zip_file_url' => $zip_file_url,
155 );
156 }
157 }
158
159 private function export_and_get_url($template_data)
160 {
161 // creating a zip file name for template export.
162 $site_name = get_bloginfo('name');
163 // Check if $site_name is empty or not found
164 if (empty($site_name)) {
165 $site_name = 'kirki-template'; // Fallback name
166 }
167 // Convert the site name to a slug format
168 $zip_filename = strtolower(str_replace(' ', '-', $site_name));
169 $zip_file_url = $this->make_zip_and_get_url($zip_filename, $template_data);
170
171 return $zip_file_url;
172 }
173 private function extract_asset_url_from_template_data($template_data)
174 {
175 $this->extract_asset_url_from_content_manager($template_data['contentManager']);
176 $this->extract_asset_url_from_pages($template_data['pages']);
177 $this->extract_asset_url_from_template($template_data['templates']);
178 $this->extract_asset_url_from_popups($template_data['popups']);
179 $this->extract_asset_url_from_symbols($template_data['symbols']);
180 $this->extract_asset_url_from_string(json_encode($template_data['globalStyleBlocks']));
181 }
182
183 private function extract_asset_url_from_string($string)
184 {
185 if (!$string) {
186 return;
187 }
188 $string = stripslashes($string);
189 // Define a regular expression pattern to match any image URLs
190 $pattern = '/https?:\/\/[^\s]+\/[^\s]+\.(jpg|jpeg|png|gif|svg|webp)/i';
191 // Find all image URLs matching the pattern in the string
192 preg_match_all($pattern, $string, $matches);
193 // Output the found image URLs
194 $image_urls = $matches[0];
195 foreach ($image_urls as $key => $url) {
196 $this->push_to_asset_urls_tracker(false, $url);
197 }
198 }
199
200 private function extract_asset_url_from_content_manager($contentManager)
201 {
202 $types = array('image', 'video', 'gallery');
203
204 foreach ($contentManager as $key => $collection) {
205 $asset_field_ids = array(); // [{type: 'image', id: 'kirki_cm_field_952_dp23nqfx'}]
206 $meta = $collection->meta;
207
208 if (isset($meta['kirki_cm_fields']) && !empty($meta['kirki_cm_fields'])) {
209 $fields = maybe_unserialize($meta['kirki_cm_fields'][0]);
210
211 // kirki_cm_field_952_dp23nqfx
212 if (is_array($fields) && !empty($fields)) {
213 foreach ($fields as $field) {
214 if (isset($field['type']) && in_array($field['type'], $types)) {
215 $asset_field_ids[] = array(
216 'type' => $field['type'],
217 'id' => 'kirki_cm_field_' . $collection->ID . '_' . $field['id'],
218 );
219 }
220 }
221 }
222 }
223
224 if (count($collection->children) > 0) {
225 foreach ($collection->children as $collection_data) {
226 $meta = $collection_data->meta;
227
228 foreach ($asset_field_ids as $asset_field) {
229 if (isset($meta[$asset_field['id']]) && !empty($meta[$asset_field['id']])) {
230 $asset = maybe_unserialize($meta[$asset_field['id']][0]);
231 $this->track_asset_and_push_to_tracker($asset, $asset_field['type']);
232 }
233 }
234 }
235 }
236 }
237 }
238
239 private function track_asset_and_push_to_tracker($asset, $type = 'image')
240 {
241 if ($type === 'gallery' && is_array($asset)) {
242 foreach ($asset as $item) {
243 if (!empty($item) && isset($item['id'], $item['url'])) {
244 $this->push_to_asset_urls_tracker($item['id'], $item['url']);
245 }
246 }
247 } elseif (!empty($asset) && isset($asset['id'], $asset['url'])) {
248 $this->push_to_asset_urls_tracker($asset['id'], $asset['url']);
249 }
250 }
251
252 private function extract_asset_url_from_pages($pages)
253 {
254 foreach ($pages as $page) {
255 $meta = $page->meta;
256 if (isset($meta['kirki']) && !empty($meta['kirki'])) {
257 $kirki_data = maybe_unserialize($meta['kirki'][0]);
258
259 if (is_array($kirki_data) && !empty($kirki_data)) {
260 $this->exact_asset_url_from_blocks($kirki_data['blocks'], 'pages');
261 }
262 }
263 if (!empty($meta['kirki_global_style_block_random'])) {
264 $kirki_global_style_block_random = maybe_unserialize($meta['kirki_global_style_block_random'][0]);
265 $this->extract_asset_url_from_string(json_encode($kirki_global_style_block_random));
266 }
267 }
268 }
269
270 private function extract_asset_url_from_template($templates)
271 {
272 foreach ($templates as $template) {
273 $meta = $template->meta;
274 if (isset($meta['kirki']) && !empty($meta['kirki'])) {
275 $kirki_data = maybe_unserialize($meta['kirki'][0]);
276
277 if (is_array($kirki_data) && !empty($kirki_data)) {
278 $this->exact_asset_url_from_blocks($kirki_data['blocks'], 'templates');
279 }
280 }
281 if (!empty($meta['kirki_global_style_block_random'])) {
282 $kirki_global_style_block_random = maybe_unserialize($meta['kirki_global_style_block_random'][0]);
283 $this->extract_asset_url_from_string(json_encode($kirki_global_style_block_random));
284 }
285 }
286 }
287
288 private function extract_asset_url_from_popups($popups)
289 {
290 foreach ($popups as $popup) {
291 $meta = $popup->meta;
292
293 if (isset($meta['kirki']) && !empty($meta['kirki'])) {
294 $kirki_data = maybe_unserialize($meta['kirki'][0]);
295 if (is_array($kirki_data) && !empty($kirki_data)) {
296 $this->exact_asset_url_from_blocks($kirki_data, 'popups');
297 }
298 }
299 }
300 }
301
302 private function extract_asset_url_from_symbols($symbols)
303 {
304 foreach ($symbols as $symbol) {
305
306 $meta = $symbol->meta;
307 if (isset($meta['kirki']) && !empty($meta['kirki'])) {
308 $kirki_data = maybe_unserialize($meta['kirki'][0]);
309
310 if (is_array($kirki_data) && !empty($kirki_data)) {
311 $this->exact_asset_url_from_blocks($kirki_data['data'], 'symbols');
312 $this->extract_asset_url_from_string(json_encode($kirki_data['styleBlocks']));
313 }
314 }
315 }
316 }
317
318
319
320 private function exact_asset_url_from_blocks($blocks, $context)
321 {
322 $upload_dir = wp_upload_dir();
323 $base_url = $upload_dir['baseurl'];
324
325 // $asset_urls = [];
326
327 foreach ($blocks as $key => $block) {
328 if ($block['name'] === 'image' && $block['properties']['attributes']['src']) {
329 $this->push_to_asset_urls_tracker($block['properties']['wp_attachment_id'], $block['properties']['attributes']['src']);
330 }
331
332 if ($block['name'] === 'video' && $block['properties']['attributes']['src']) {
333 $this->push_to_asset_urls_tracker(false, $block['properties']['attributes']['src']);
334 if ($block['properties']['thumbnail']['url']) {
335 $this->push_to_asset_urls_tracker($block['properties']['thumbnail']['wp_attachment_id'], $block['properties']['thumbnail']['url']);
336 }
337 }
338
339 if ($block['name'] === 'lottie' && $block['properties']['lottie']['src']) {
340 $this->push_to_asset_urls_tracker(false, $block['properties']['lottie']['src']);
341 }
342 if ($block['name'] === 'lightbox' && $block['properties']['lightbox']['thumbnail']['src']) {
343 $this->push_to_asset_urls_tracker(false, $block['properties']['lightbox']['thumbnail']['src']);
344
345 $lightbox_media = $block['properties']['lightbox']['media'];
346
347 foreach ($lightbox_media as $key => $media_item) {
348 if ($media_item['sources']['original']) {
349 $this->push_to_asset_urls_tracker(false, $media_item['sources']['original']);
350 }
351 }
352 }
353 }
354 }
355
356 private function count($array)
357 {
358 if (isset($array) && is_array($array)) {
359 return count($array);
360 }
361 return 0;
362 }
363
364 private function make_zip_and_get_url($zip_filename, $template_data)
365 {
366 try {
367 if (empty($wp_filesystem)) {
368 require_once ABSPATH . '/wp-admin/includes/file.php';
369 WP_Filesystem();
370 }
371
372 global $wp_filesystem;
373 $zip = new \ZipArchive();
374
375 $upload_dir = wp_upload_dir();
376
377 // Step 1: create zip file path and json file path
378 $zip_file_path = $upload_dir['basedir'] . "/$zip_filename.zip";
379
380 // site-settings.json
381 $site_settings_filename = 'site-settings.json';
382 $site_settings_file_path = $upload_dir['basedir'] . '/' . $site_settings_filename;
383 $site_settings_data = array();
384 $site_settings_data['exportId'] = $template_data['exportId'];
385 $site_settings_data['variable_prefix'] = $template_data['variable_prefix'];
386 $site_settings_data['batch_id'] = $template_data['batch_id'];
387 $site_settings_data['prefix'] = $template_data['prefix'];
388 $site_settings_data['siteInfo'] = $template_data['siteInfo'];
389 $site_settings_file_content = json_encode($site_settings_data, JSON_PRETTY_PRINT);
390 $site_settings_file_write = $wp_filesystem->put_contents(
391 $site_settings_file_path,
392 $site_settings_file_content,
393 FS_CHMOD_FILE // predefined mode settings for WP files
394 );
395
396 // assets.json
397 $assets_filename = 'assets.json';
398 $assets_file_path = $upload_dir['basedir'] . '/' . $assets_filename;
399 $assets_data = array();
400 $assets_data['assetUrls'] = $template_data['assetUrls'];
401 $assets_file_content = json_encode($assets_data, JSON_PRETTY_PRINT);
402 $assets_file_write = $wp_filesystem->put_contents(
403 $assets_file_path,
404 $assets_file_content,
405 FS_CHMOD_FILE // predefined mode settings for WP files
406 );
407
408 // pages.json
409 $pages_filename = 'pages.json';
410 $pages_file_path = $upload_dir['basedir'] . '/' . $pages_filename;
411 $pages_data = array();
412 $pages_data['pages'] = $template_data['pages'];
413 $pages_data['templates'] = $template_data['templates'];
414 $pages_data['utility_pages'] = $template_data['utility_pages'];
415 $pages_file_content = json_encode($pages_data, JSON_PRETTY_PRINT);
416 $pages_file_write = $wp_filesystem->put_contents(
417 $pages_file_path,
418 $pages_file_content,
419 FS_CHMOD_FILE // predefined mode settings for WP files
420 );
421
422 // popups.json
423 $popups_filename = 'popups.json';
424 $popups_file_path = $upload_dir['basedir'] . '/' . $popups_filename;
425 $popups_data = array();
426 $popups_data['popups'] = $template_data['popups'];
427 $popups_file_content = json_encode($popups_data, JSON_PRETTY_PRINT);
428 $popups_file_write = $wp_filesystem->put_contents(
429 $popups_file_path,
430 $popups_file_content,
431 FS_CHMOD_FILE // predefined mode settings for WP files
432 );
433
434 // content-manager.json
435 $content_manager_filename = 'content-manager.json';
436 $content_manager_file_path = $upload_dir['basedir'] . '/' . $content_manager_filename;
437 $content_manager_data = array();
438 $content_manager_data['contentManager'] = $template_data['contentManager'];
439 $content_manager_file_content = json_encode($content_manager_data, JSON_PRETTY_PRINT);
440 $content_manager_file_write = $wp_filesystem->put_contents(
441 $content_manager_file_path,
442 $content_manager_file_content,
443 FS_CHMOD_FILE // predefined mode settings for WP files
444 );
445
446 // content-manager-ref-fields.json
447 $content_manager_ref_fields_filename = 'content-manager-ref-fields.json';
448 $content_manager_ref_fields_file_path = $upload_dir['basedir'] . '/' . $content_manager_ref_fields_filename;
449 $content_manager_ref_fields_data = array();
450 $content_manager_ref_fields_data['contentManagerRefFields'] = $template_data['contentManagerRefFields'];
451 $content_manager_ref_fields_file_content = json_encode($content_manager_ref_fields_data, JSON_PRETTY_PRINT);
452 $content_manager_ref_fields_file_write = $wp_filesystem->put_contents(
453 $content_manager_ref_fields_file_path,
454 $content_manager_ref_fields_file_content,
455 FS_CHMOD_FILE // predefined mode settings for WP files
456 );
457
458 // symbols.json
459 $symbols_filename = 'symbols.json';
460 $symbols_file_path = $upload_dir['basedir'] . '/' . $symbols_filename;
461 $symbols_data = array();
462 $symbols_data['symbols'] = $template_data['symbols'];
463 $symbols_file_content = json_encode($symbols_data, JSON_PRETTY_PRINT);
464 $symbols_file_write = $wp_filesystem->put_contents(
465 $symbols_file_path,
466 $symbols_file_content,
467 FS_CHMOD_FILE // predefined mode settings for WP files
468 );
469
470 // styles.json
471 $styles_filename = 'styles.json';
472 $styles_file_path = $upload_dir['basedir'] . '/' . $styles_filename;
473 $styles_data = array();
474 $styles_data['viewPorts'] = $template_data['viewPorts'];
475 $styles_data['customFonts'] = $template_data['customFonts'];
476 $styles_data['globalStyleBlocks'] = $template_data['globalStyleBlocks'];
477 $styles_data['variables'] = $template_data['variables'];
478 $styles_file_content = json_encode($styles_data, JSON_PRETTY_PRINT);
479 $styles_file_write = $wp_filesystem->put_contents(
480 $styles_file_path,
481 $styles_file_content,
482 FS_CHMOD_FILE // predefined mode settings for WP files
483 );
484
485 // single.json
486 $single_filename = 'single.json';
487 $single_file_path = $upload_dir['basedir'] . '/' . $single_filename;
488 $single_data = array();
489 $single_data['exportId'] = $template_data['exportId'];
490 $single_data['batch_id'] = $template_data['batch_id'];
491
492 $template_info = array(
493 'symbols' => $this->count($template_data['symbols']),
494 'popups' => $this->count($template_data['popups']),
495 'pages' => $this->count($template_data['pages']),
496 'templates' => $this->count($template_data['templates']),
497 'utilityPages' => $this->count($template_data['utility_pages']),
498 'variables' => $this->count($template_data['variables']['data']),
499 'images' => $this->count($template_data['assetUrls']),
500 'fonts' => $this->count($template_data['customFonts']),
501 'contentManager' => $this->count($template_data['contentManager']),
502 'contentManager' => array_reduce(
503 $template_data['contentManager'],
504 function ($sum, $item) {
505 return $sum + (isset($item->children) ? count($item->children) : 0);
506 },
507 0
508 ),
509 'contentManagerRefFields' => $this->count($template_data['contentManagerRefFields']),
510 'contentManager_list' => array_map(
511 function ($item) {
512 return $item->post_title . ' (' . (isset($item->children) ? count($item->children) : 0) . ')';
513 },
514 $template_data['contentManager']
515 ),
516 );
517
518 $single_data['template_info'] = $template_info;
519
520 $single_data['pages'] = $this->extractPageData($template_data['pages']);
521 $single_data['templates'] = $this->extractPageData($template_data['templates']);
522 $single_data['utility_pages'] = $this->extractPageData($template_data['utility_pages']);
523 $single_file_content = json_encode($single_data, JSON_PRETTY_PRINT);
524 $single_file_write = $wp_filesystem->put_contents(
525 $single_file_path,
526 $single_file_content,
527 FS_CHMOD_FILE // predefined mode settings for WP files
528 );
529
530 if (!$site_settings_file_write || !$assets_file_write || !$pages_file_write || !$popups_file_write || !$content_manager_file_write || !$symbols_file_write || !$styles_file_write || !$single_file_write || !$content_manager_ref_fields_file_write) {
531 // throw new \Exception('Failed to write template file');
532 wp_send_json_error('Failed to write template file');
533 }
534
535 // Step 3: Open the zip file
536 if (true !== $zip->open($zip_file_path, \ZipArchive::CREATE)) {
537 // throw new \Exception('Failed to create zip file');
538 wp_send_json_error('Failed to write template file');
539 }
540
541 // Step 4: Add file to the zip file
542 foreach ($template_data['assetUrls'] as $key => $asset_item) {
543 $url = $asset_item['url'];
544 $file_name = basename($url); // Extract only the filename (e.g., "image.jpg")
545
546 $file_path = $upload_dir['basedir'] . str_replace($upload_dir['baseurl'], '', $url);
547 $zip_path = 'assets/' . $file_name; // Place all files inside "media/" without subdirectories
548
549 // Check if file exists
550 if (!file_exists($file_path)) {
551 // error_log("File not found: " . $file_path);
552 continue; // Skip to the next file
553 }
554
555 // Try adding the file to the ZIP
556 if (!$zip->addFile($file_path, $zip_path)) {
557 // error_log("Failed to add file to ZIP: " . $file_path);
558 }
559 }
560
561 if (false === $zip->addFile($site_settings_file_path, $site_settings_filename)) {
562 // throw new \Exception('Failed to add assets.json file to zip');
563 wp_send_json_error('Failed to write site-settings.json file');
564 }
565
566 if (isset($template_data['assetUrls']) && !empty($template_data['assetUrls'])) {
567 if (false === $zip->addFile($assets_file_path, $assets_filename)) {
568 // throw new \Exception('Failed to add assets.json file to zip');
569 wp_send_json_error('Failed to write assets.json file');
570 }
571 }
572
573 if (false === $zip->addFile($pages_file_path, $pages_filename)) {
574 // throw new \Exception('Failed to add pages.json file to zip');
575 wp_send_json_error('Failed to write pages.json file');
576 }
577
578 if (false === $zip->addFile($popups_file_path, $popups_filename)) {
579 // throw new \Exception('Failed to add popups.json file to zip');
580 wp_send_json_error('Failed to write popups.json file');
581 }
582
583 if (false === $zip->addFile($content_manager_file_path, $content_manager_filename)) {
584 // throw new \Exception('Failed to add content-manager.json file to zip');
585 wp_send_json_error('Failed to write content-manager.json file');
586 }
587
588 if (false === $zip->addFile($content_manager_ref_fields_file_path, $content_manager_ref_fields_filename)) {
589 // throw new \Exception('Failed to add content-manager-ref-fields.json file to zip');
590 wp_send_json_error('Failed to write content-manager-ref-fields.json file');
591 }
592
593 if (false === $zip->addFile($symbols_file_path, $symbols_filename)) {
594 // throw new \Exception('Failed to add symbols.json file to zip');
595 wp_send_json_error('Failed to write symbols.json file');
596
597 }
598 if (false === $zip->addFile($styles_file_path, $styles_filename)) {
599 // throw new \Exception('Failed to add styles.json file to zip');
600 wp_send_json_error('Failed to write styles.json file');
601 }
602 if (false === $zip->addFile($single_file_path, $single_filename)) {
603 // throw new \Exception('Failed to add styles.json file to zip');
604 wp_send_json_error('Failed to write styles.json file');
605 }
606
607 $zip->close();
608
609 // Step 6: remove the template file after zip file is created
610 wp_delete_file($site_settings_file_path);
611 wp_delete_file($assets_file_path);
612 wp_delete_file($pages_file_path);
613 wp_delete_file($popups_file_path);
614 wp_delete_file($content_manager_file_path);
615 wp_delete_file($content_manager_ref_fields_file_path);
616 wp_delete_file($symbols_file_path);
617 wp_delete_file($styles_file_path);
618 wp_delete_file($single_file_path);
619
620 // Step 7: Download the created zip file
621 return add_query_arg(
622 array(
623 'page-export' => 'true',
624 'file-name' => $zip_filename . '.zip',
625 'download_file_nonce' => wp_create_nonce('download_file_action'),
626 ),
627 home_url('/')
628 );
629 } catch (\Exception $e) {
630 // return false;
631 wp_send_json_error('Failed to write template file');
632 }
633 }
634
635 function extractPageData($pages)
636 {
637 $result = array();
638 foreach ($pages as $page) {
639 if (isset($page->post_title) && isset($page->post_name)) {
640 $result[] = array(
641 'post_title' => $page->post_title,
642 'post_name' => $page->post_name,
643 );
644 }
645 }
646 return $result; // Returns an array of page data
647 }
648
649
650 private function get_global_style_blocks()
651 {
652 return HelperFunctions::get_global_data_using_key(KIRKI_GLOBAL_STYLE_BLOCK_META_KEY);
653 }
654
655 private function get_all_pages()
656 {
657 return $this->get_posts_with_meta('page', array('publish'));
658 }
659
660 private function get_all_popups()
661 {
662 return $this->get_posts_with_meta('kirki_popup', array('publish', 'draft'));
663 }
664
665 private function get_all_templates()
666 {
667 return $this->get_posts_with_meta('kirki_template', array('publish'));
668 }
669
670 private function get_all_utility_pages()
671 {
672 return $this->get_posts_with_meta('kirki_utility', array('publish'));
673 }
674
675 private function get_all_symbols()
676 {
677 return $this->get_posts_with_meta('kirki_symbol', array('draft'));
678 }
679
680 private function get_all_content_manager_data()
681 {
682 $parent_posts = $this->get_posts_with_meta('kirki_cm', array('publish'));
683
684 foreach ($parent_posts as $key => $parent_post) {
685 $parent_posts[$key]->children = $this->get_posts_with_meta('kirki_cm_' . $parent_post->ID, array('publish'));
686 }
687
688 return $parent_posts;
689 }
690
691 private function get_view_ports()
692 {
693 $control = HelperFunctions::get_global_data_using_key(KIRKI_USER_CONTROLLER_META_KEY);
694
695 if (!$control) {
696 $control = array();
697 }
698 if (!isset($control['viewport'])) {
699 $control['viewport'] = HelperFunctions::get_initial_view_ports();
700 }
701 return $control['viewport'];
702 }
703
704 private function get_custom_fonts()
705 {
706 $custom_fonts = HelperFunctions::get_global_data_using_key(KIRKI_USER_CUSTOM_FONTS_META_KEY);
707 return $custom_fonts ? $custom_fonts : array();
708 }
709
710 private function get_posts_with_meta($post_type, $post_status = array('publish'))
711 {
712 $all_posts = get_posts(
713 array(
714 'post_type' => $post_type,
715 'posts_per_page' => -1,
716 'post_status' => $post_status,
717 )
718 );
719 foreach ($all_posts as $key => $post) {
720 $post_meta = get_post_meta($post->ID);
721 foreach ($this->discarded_meta as $discarded_meta_key) {
722 unset($post_meta[$discarded_meta_key]);
723 }
724 if ($this->only_published_pages) {
725 $stage_metas = Staging::get_all_stage_related_meta_names($post->ID);
726 foreach ($stage_metas as $discarded_meta_key) {
727 unset($post_meta[$discarded_meta_key]);
728 }
729 }
730 $post->meta = $post_meta;
731
732 // Get the post's content manager fields if available
733 // $cm_ref_fields = ContentManagerHelper::get_post_cm_ref_fields($post->ID);
734 // if (isset($cm_ref_fields) && !empty($cm_ref_fields)) {
735 // $post->cm_ref_fields = $cm_ref_fields;
736 // }
737
738 $all_posts[$key] = $post;
739 }
740
741 return $all_posts;
742 }
743
744 private function push_to_asset_urls_tracker($attachment_id, $url)
745 {
746 if (!$attachment_id) {
747 $attachment_id = attachment_url_to_postid($url);
748 }
749 if ($attachment_id) {
750 $this->asset_urls_tracker[$attachment_id] = array(
751 'attachment_id' => $attachment_id,
752 'url' => $url,
753 );
754 }
755 }
756
757 private function get_all_content_manager_ref_fields()
758 {
759 $cm_ref_fields = ContentManagerHelper::get_post_cm_ref_fields();
760
761 return $cm_ref_fields;
762 }
763 }
764