PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.4
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.4
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 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 / Ajax / ExportImport.php
kirki / includes / Ajax Last commit date
Collaboration 1 month ago Apps.php 1 month ago Collection.php 2 months ago Comments.php 4 months ago DynamicContent.php 3 weeks ago ExportImport.php 1 week ago Form.php 1 month ago Media.php 2 weeks ago Page.php 1 month ago PageSettings.php 1 month ago RBAC.php 1 month ago Symbol.php 1 month ago Taxonomy.php 4 months ago TemplateExportImport.php 1 week ago UserData.php 1 month ago Users.php 2 weeks ago Walkthrough.php 1 month ago WordpressData.php 4 months ago WpAdmin.php 1 month ago
ExportImport.php
713 lines
1 <?php
2
3 /**
4 * Manage dynamic form data api calls
5 *
6 * @package kirki
7 */
8
9 namespace Kirki\Ajax;
10
11 use Kirki\HelperFunctions;
12 use Kirki\Ajax\Symbol;
13
14 if (!defined('ABSPATH')) {
15 exit; // Exit if accessed directly.
16 }
17
18 class ExportImport
19 {
20
21
22 public static function export()
23 {
24 $filename = HelperFunctions::sanitize_text(isset($_POST['filename']) ? $_POST['filename'] : '');
25 $filename = basename($filename);
26 $data = isset($_POST['data']) ? $_POST['data'] : '{}';
27 $data = json_decode(stripslashes($data), true);
28 $blocks = $data['blocks'];
29 $upload_dir = wp_upload_dir();
30 $base_url = $upload_dir['baseurl'];
31
32 $asset_urls = array();
33 $symbol_ids = array();
34 $symbols = array();
35
36 foreach ($blocks as $key => $block) {
37 if (isset($block['properties']['symbolId'])) {
38 $symbol_id = array(
39 'id' => $block['properties']['symbolId'],
40 'elementId' => $block['id'],
41 );
42
43 array_push($symbol_ids, $symbol_id);
44 }
45 self::add_asset($asset_urls, $key, $block);
46 }
47
48 // filter asset_urls using base_url if base_url not included remove item
49 $upload_base_path = wp_parse_url($base_url, PHP_URL_PATH);
50 $upload_host = wp_parse_url($base_url, PHP_URL_HOST);
51
52 $asset_urls = array_filter(
53 $asset_urls,
54 function ($asset_item) use ($upload_base_path, $upload_host) {
55 $parsed = wp_parse_url($asset_item['url']);
56
57 if (empty($parsed['host']) || $parsed['host'] !== $upload_host) {
58 return false;
59 }
60
61 if (!isset($parsed['scheme']) || !in_array(strtolower($parsed['scheme']), array('http', 'https'), true)) {
62 return false;
63 }
64
65 $path = wp_normalize_path(rawurldecode(isset($parsed['path']) ? $parsed['path'] : ''));
66
67 // Must start with the uploads base path and contain no traversal.
68 return $upload_base_path
69 && strpos($path, $upload_base_path) === 0
70 && strpos($path, '..') === false;
71 }
72 );
73
74 // get symbols data
75 foreach ($symbol_ids as $symbol_id) {
76 $symbol = Symbol::get_single_symbol($symbol_id['id'], true);
77
78 if ($symbol) {
79 $symbol['elementId'] = $symbol_id['elementId'];
80 array_push($symbols, $symbol);
81 }
82 }
83 // iterate through symbols and add assets
84 foreach ($symbols as $symbol) {
85 foreach ($symbol['symbolData']['data'] as $key => $block) {
86 self::add_asset($asset_urls, $key, $block);
87 }
88 }
89
90 $data['asset_urls'] = $asset_urls;
91 $data['symbols'] = $symbols;
92
93 self::get_assets_make_zip($filename, $asset_urls, $data);
94 }
95
96 public static function add_asset(&$asset_urls, $key, $block)
97 {
98 if ($block['name'] === 'image' && $block['properties']['attributes']['src']) {
99 $image_item = array(
100 'id' => $key,
101 'name' => $block['name'],
102 'url' => $block['properties']['attributes']['src'],
103 );
104 array_push($asset_urls, $image_item);
105 }
106
107 if ($block['name'] === 'video' && $block['properties']['attributes']['src']) {
108 $video_item = array(
109 'id' => $key,
110 'name' => $block['name'],
111 'url' => $block['properties']['attributes']['src'],
112 );
113 array_push($asset_urls, $video_item);
114
115 if ($block['properties']['thumbnail']['url']) {
116 $video_thumbnail = array(
117 'id' => $key,
118 'name' => $block['name'],
119 'url' => $block['properties']['thumbnail']['url'],
120 'thumbnail' => true,
121 );
122 array_push($asset_urls, $video_thumbnail);
123 }
124 }
125
126 if ($block['name'] === 'lottie' && $block['properties']['lottie']['src']) {
127 $lottie = array(
128 'id' => $key,
129 'name' => $block['name'],
130 'url' => $block['properties']['lottie']['src'],
131 );
132 array_push($asset_urls, $lottie);
133 }
134 if ($block['name'] === 'lightbox' && $block['properties']['lightbox']['thumbnail']['src']) {
135 $lightbox_thumbnail = array(
136 'id' => $key,
137 'name' => $block['name'],
138 'url' => $block['properties']['lightbox']['thumbnail']['src'],
139 'thumbnail' => true,
140 );
141 array_push($asset_urls, $lightbox_thumbnail);
142
143 $lightbox_media = $block['properties']['lightbox']['media'];
144
145 foreach ($lightbox_media as $key => $media_item) {
146 if ($media_item['sources']['original']) {
147 $lightbox_media_item = array(
148 'id' => $key,
149 'name' => $block['name'],
150 'url' => $media_item['sources']['original'],
151 'index' => $key,
152 );
153 array_push($asset_urls, $lightbox_media_item);
154 }
155 }
156 }
157 }
158
159 public static function get_assets_make_zip($filename, $asset_urls, $data)
160 {
161 try {
162 if (empty($wp_filesystem)) {
163 require_once ABSPATH . '/wp-admin/includes/file.php';
164 WP_Filesystem();
165 }
166
167 global $wp_filesystem;
168
169 $zip = new \ZipArchive();
170 $upload_dir = wp_upload_dir();
171
172 $data['asset_urls'] = $asset_urls;
173
174 // Step 1: Name of the zip file to be created
175 $zipFileName = $upload_dir['basedir'] . "/$filename.zip";
176 $kirki_json_file = $upload_dir['basedir'] . '/kirki-data.json';
177
178 // Step 2: Convert the array to JSON
179 $json_data = json_encode($data, JSON_PRETTY_PRINT);
180
181 $is_file_written = $wp_filesystem->put_contents(
182 $kirki_json_file,
183 $json_data,
184 FS_CHMOD_FILE // predefined mode settings for WP files
185 );
186
187 if (false === $is_file_written) {
188 throw new \Exception('Failed to write kirki-data.json file');
189 }
190
191 if (true !== $zip->open($zipFileName, \ZipArchive::CREATE)) {
192 throw new \Exception('Failed to create zip file');
193 }
194
195 // Step 3: Add file to the zip file
196 $uploads_real = realpath($upload_dir['basedir']);
197 $uploads_base_path = wp_normalize_path(wp_parse_url($upload_dir['baseurl'], PHP_URL_PATH));
198 $allowed_exts = array('jpg', 'jpeg', 'png', 'webp', 'gif', 'avif', 'mp4', 'mov', 'webm', 'mp3', 'json', 'lottie');
199
200 foreach ($asset_urls as $key => $asset_item) {
201 $url = $asset_item['url'];
202 $parsed = wp_parse_url($url);
203
204 if (empty($parsed['scheme']) || !in_array(strtolower($parsed['scheme']), array('http', 'https'), true)) {
205 continue;
206 }
207
208 if (empty($parsed['host']) || $parsed['host'] !== wp_parse_url($upload_dir['baseurl'], PHP_URL_HOST)) {
209 continue;
210 }
211
212 $path = wp_normalize_path(rawurldecode(isset($parsed['path']) ? $parsed['path'] : ''));
213 if (!$uploads_base_path || strpos($path, $uploads_base_path) !== 0) {
214 continue; // not under /uploads
215 }
216
217 $relative = ltrim(substr($path, strlen($uploads_base_path)), '/');
218 if ('' === $relative || false !== strpos($relative, '..')) {
219 continue; // empty or traversal
220 }
221
222 $file_path = $uploads_real . DIRECTORY_SEPARATOR . $relative;
223 $real_path = realpath($file_path);
224
225 // Resolved file must exist, be a regular file, and stay inside uploads.
226 if (false === $real_path || !is_file($real_path)) {
227 continue;
228 }
229
230 if ($uploads_real && strpos($real_path, $uploads_real . DIRECTORY_SEPARATOR) !== 0) {
231 continue;
232 }
233
234 $ext = strtolower(pathinfo($real_path, PATHINFO_EXTENSION));
235 if (!in_array($ext, $allowed_exts, true)) {
236 continue; // only media/config files may be exported
237 }
238
239 $zip->addFile($real_path, basename($real_path));
240 }
241
242 if (false === $zip->addFile($kirki_json_file, 'kirki-data.json')) {
243 throw new \Exception('Failed to add kirki-data.json file to zip');
244 }
245
246 $zip->close();
247
248 // remove kirki-data.json
249 wp_delete_file($kirki_json_file);
250
251 // Step 4: Download the created zip file
252 $file_url = add_query_arg(
253 array(
254 'page-export' => 'true',
255 'file-name' => $filename . '.zip',
256 'download_file_nonce' => wp_create_nonce('download_file_action'),
257 ),
258 home_url('/')
259 );
260 wp_send_json($file_url);
261 } catch (\Exception $e) {
262 wp_send_json_error($e->getMessage(), 401);
263 }
264 }
265
266 public static function import()
267 {
268 set_time_limit(300);
269
270 $is_include_media = HelperFunctions::sanitize_text(isset($_POST['is_include_media']) ? $_POST['is_include_media'] : false);
271 $file = $_FILES['file']; // zip file
272
273 self::handle_zip_file_upload($file, $is_include_media);
274 }
275
276 public static function download_and_save_zip_file($url, $destination)
277 {
278 // Execute the cURL session
279 $file_content = HelperFunctions::http_get(
280 $url,
281 array(
282 'timeout' => 300, // Seconds
283 'redirection' => 0,
284 )
285 );
286
287 if (empty($wp_filesystem)) {
288 require_once ABSPATH . '/wp-admin/includes/file.php';
289 WP_Filesystem();
290 }
291
292 global $wp_filesystem;
293
294 // Save the file to the destination
295 if ($wp_filesystem->put_contents($destination, $file_content, FS_CHMOD_FILE)) {
296 return true;
297 } else {
298 return false;
299 }
300 }
301
302 public static function template_import()
303 {
304 $upload_dir = wp_upload_dir();
305
306 $is_include_media = HelperFunctions::sanitize_text(isset($_POST['is_include_media']) ? $_POST['is_include_media'] : false);
307
308 // get template file from url
309 $file_url = HelperFunctions::sanitize_text(isset($_POST['file_url']) ? $_POST['file_url'] : false);
310
311 if (!$file_url || !HelperFunctions::is_safe_url($file_url)) {
312 wp_send_json_error('Invalid file URL');
313 }
314
315 $destination_path = $upload_dir['basedir'] . '/kirki-template.zip';
316
317 if (self::download_and_save_zip_file($file_url, $destination_path)) {
318 $file = array(
319 'name' => 'kirki-template.zip',
320 'tmp_name' => $destination_path,
321 'error' => 0,
322 );
323
324 self::handle_zip_file_upload($file, $is_include_media);
325 } else {
326 // Error occurred while downloading or saving the file
327 wp_send_json_error('Zip File upload Failed');
328 }
329 }
330
331
332 public static function handle_zip_file_upload($file, $is_include_media)
333 {
334 $upload_dir = wp_upload_dir();
335
336 $file_name = $file['name'];
337 $file_tmp = $file['tmp_name'];
338 $file_error = $file['error'];
339
340 $file_ext = explode('.', $file_name); // ['file', 'ext']
341 $file_ext = strtolower(end($file_ext)); // 'ext'
342
343 $allowed = array('zip');
344
345 if (!in_array($file_ext, $allowed, true)) {
346 wp_send_json_error('File type not allowed, please upload zip file');
347 }
348
349 if ($file_error !== 0) {
350 wp_send_json_error('File upload Failed');
351 }
352
353 $file_name_new = uniqid('', true) . '.' . $file_ext; // 'random.ext'
354 $file_destination = $upload_dir['basedir'] . '/' . $file_name_new;
355
356 global $wp_filesystem;
357 if (empty($wp_filesystem)) {
358 require_once ABSPATH . 'wp-admin/includes/file.php';
359 WP_Filesystem();
360 }
361
362 if (!$wp_filesystem->move($file_tmp, $file_destination)) {
363 wp_send_json_error('Something went wrong, please try again');
364 }
365
366 $zip = new \ZipArchive();
367 $res = $zip->open($file_destination);
368
369 if ($res !== true) {
370 wp_send_json_error('Failed to extract zip file');
371 }
372
373 $filtered_zip_path = HelperFunctions::filterZipFile($zip, $file_destination);
374
375 if (!$filtered_zip_path) {
376 return false;
377 }
378
379 $zip->close();
380 wp_delete_file($file_destination);
381
382 $temp_folder = 'kirki_temp';
383 $temp_folder_path = HelperFunctions::get_temp_folder_path();
384
385 // Reopen the filtered ZIP file for extraction
386 $res = $zip->open($filtered_zip_path);
387
388 $zip->extractTo($temp_folder_path);
389 $zip->close();
390
391 if (empty($wp_filesystem)) {
392 require_once ABSPATH . '/wp-admin/includes/file.php';
393 WP_Filesystem();
394 }
395
396 global $wp_filesystem;
397
398 $kirki_json_file = $temp_folder_path . '/kirki-data.json';
399 $kirki_json_data = $wp_filesystem->get_contents($kirki_json_file);
400 $kirki_json_data = json_decode($kirki_json_data, true);
401
402 $blocks = $kirki_json_data['blocks'];
403 $symbols = $kirki_json_data['symbols'];
404
405 /**
406 * @deprecated
407 * @see \Kirki\App\Supports\Template::process_template_from_json()
408 */
409 if ($is_include_media === 'true') {
410 $asset_urls = $kirki_json_data['asset_urls'];
411
412 $pivot_table = array();
413 $assets_urls_map = array();
414
415 foreach ($asset_urls as $key => $asset_item) {
416 $url = $asset_item['url'];
417
418 if (empty($pivot_table[$url]['url'])) {
419 $new_asset = self::upload_file($asset_item);
420
421 if ($new_asset) {
422 $pivot_table[$url]['url'] = $new_asset['url'];
423 $pivot_table[$url]['attachment_id'] = $new_asset['attachment_id'];
424
425 $asset_urls[$key]['url'] = $new_asset['url'];
426 $asset_urls[$key]['attachment_id'] = $new_asset['attachment_id'];
427 $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null;
428 $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false;
429 }
430 } else {
431 $asset_urls[$key]['url'] = $pivot_table[$url]['url'];
432 $asset_urls[$key]['attachment_id'] = $pivot_table[$url]['attachment_id'];
433
434 $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null;
435 $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false;
436 }
437 }
438
439 // update blocks with new asset urls
440 foreach ($asset_urls as $key => $new_asset) {
441 $assets_urls_map[$new_asset['id']] = $new_asset;
442
443 if (isset($blocks[$new_asset['id']]['name'], $new_asset['name']) && $blocks[$new_asset['id']]['name'] === $new_asset['name']) {
444 if ($new_asset['name'] === 'image') {
445 $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url'];
446 $blocks[$new_asset['id']]['properties']['wp_attachment_id'] = $new_asset['attachment_id'];
447 } elseif ($new_asset['name'] === 'video') {
448 if ($new_asset['thumbnail']) {
449 $blocks[$new_asset['id']]['properties']['thumbnail']['url'] = $new_asset['url'];
450 } else {
451 $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url'];
452 }
453 } elseif ($new_asset['name'] === 'lottie') {
454 $blocks[$new_asset['id']]['properties']['lottie']['src'] = $new_asset['url'];
455 } elseif ($new_asset['name'] === 'lightbox') {
456 if ($new_asset['thumbnail']) {
457 $blocks[$new_asset['id']]['properties']['lightbox']['thumbnail']['src'] = $new_asset['url'];
458 } else {
459 $blocks[$new_asset['id']]['properties']['lightbox']['media'][$new_asset['index']]['sources']['original'] = $new_asset['url'];
460 }
461 }
462 }
463 }
464
465 // update symbols with new asset urls
466 foreach ($symbols as $sym_key => $symbol) {
467 foreach ($symbol['symbolData']['data'] as $key => $block) {
468 if (isset($assets_urls_map[$block['id']])) {
469
470 if ($assets_urls_map[$block['id']]['name'] === 'image') {
471 $symbol['symbolData']['data'][$key]['attributes']['src'] = $assets_urls_map[$block['id']]['url'];
472 $symbol['symbolData']['data'][$key]['wp_attachment_id'] = $assets_urls_map[$block['id']]['attachment_id'];
473 } elseif ($assets_urls_map[$block['id']]['name'] === 'video') {
474 if ($assets_urls_map[$block['id']]['thumbnail']) {
475 $symbol['symbolData']['data'][$key]['thumbnail']['url'] = $assets_urls_map[$block['id']]['url'];
476 } else {
477 $symbol['symbolData']['data'][$key]['attributes']['src'] = $assets_urls_map[$block['id']]['url'];
478 }
479 } elseif ($assets_urls_map[$block['id']]['name'] === 'lottie') {
480 $symbol['symbolData']['data'][$key]['lottie']['src'] = $assets_urls_map[$block['id']]['url'];
481 } elseif ($assets_urls_map[$block['id']]['name'] === 'lightbox') {
482 if ($assets_urls_map[$block['id']]['thumbnail']) {
483 $symbol['symbolData']['data'][$key]['lightbox']['thumbnail']['src'] = $assets_urls_map[$block['id']]['url'];
484 } else {
485 $symbol['symbolData']['data'][$key]['lightbox']['media'][$assets_urls_map[$block['id']]['index']]['sources']['original'] = $assets_urls_map[$block['id']]['url'];
486 }
487 }
488 }
489 }
490
491 $symbols[$sym_key]['symbolData']['data'] = $symbol['symbolData']['data'];
492 }
493 }
494
495 // Tracker for saved symbols
496 $saved_symbols_tracker = array();
497
498 if (is_array($symbols) && count($symbols) > 0) {
499 foreach ($symbols as $symbol) {
500 $symbol_id = $symbol['id'];
501
502 // Check if the symbol is already saved
503 if (!isset($saved_symbols_tracker[$symbol_id])) {
504 // Save the symbol to the database
505 $saved_symbol = Symbol::save_to_db($symbol);
506
507 // Add the saved symbol to the tracker
508 if ($saved_symbol) {
509 $saved_symbols_tracker[$symbol_id] = $saved_symbol;
510 }
511 }
512
513 // Update all relevant elements with the symbol's ID
514 if (isset($blocks[$symbol['elementId']]) && $blocks[$symbol['elementId']]['name'] === 'symbol') {
515 $blocks[$symbol['elementId']]['properties']['symbolId'] = $saved_symbols_tracker[$symbol_id]['id'];
516 }
517 }
518 }
519
520 $kirki_json_data['blocks'] = $blocks;
521 $kirki_json_data['symbols'] = $symbols;
522
523 // remove asset_urls
524 unset($kirki_json_data['asset_urls']);
525
526 // delete zip file
527 $wp_filesystem->delete($file_destination);
528
529 // delete temp folder
530 HelperFunctions::delete_directory($temp_folder_path);
531
532 wp_send_json_success($kirki_json_data);
533 }
534
535 /**
536 * Process kirki template zip
537 *
538 * @param string $kirki_template_zip_path
539 * @param boolean $is_include_media
540 * @return boolean|string
541 *
542 * @deprecated
543 * @see \Kirki\App\Supports\Template::save_kirki_template_from_zip()
544 */
545 public static function process_kirki_template_zip($kirki_template_zip_path, $is_include_media = false, $post_id = false)
546 {
547 $zip = new \ZipArchive();
548 $res = $zip->open($kirki_template_zip_path);
549
550 if ($res === true) {
551 $temp_folder_path = HelperFunctions::get_temp_folder_path();
552
553 $zip->extractTo($temp_folder_path);
554 $zip->close();
555
556 $kirki_json_file = $temp_folder_path . '/kirki-data.json';
557 $kirki_json_data = file_get_contents($kirki_json_file);
558 $kirki_json_data = json_decode($kirki_json_data, true);
559
560 if ($is_include_media === 'true') {
561 $asset_urls = $kirki_json_data['asset_urls'];
562 $blocks = $kirki_json_data['blocks'];
563
564 $pivot_table = array();
565
566 foreach ($asset_urls as $key => $asset_item) {
567 $url = $asset_item['url'];
568
569 if (empty($pivot_table[$url]['url'])) {
570 $new_asset = self::upload_file($asset_item);
571
572 if ($new_asset) {
573 $pivot_table[$url]['url'] = $new_asset['url'];
574 $pivot_table[$url]['attachment_id'] = $new_asset['attachment_id'];
575
576 $asset_urls[$key]['url'] = $new_asset['url'];
577 $asset_urls[$key]['attachment_id'] = $new_asset['attachment_id'];
578 $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null;
579 $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false;
580 }
581 } else {
582 $asset_urls[$key]['url'] = $pivot_table[$url]['url'];
583 $asset_urls[$key]['attachment_id'] = $pivot_table[$url]['attachment_id'];
584
585 $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null;
586 $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false;
587 }
588 }
589
590 // update blocks with new asset urls
591 foreach ($asset_urls as $key => $new_asset) {
592 if (isset($blocks[$new_asset['id']]['name'], $new_asset['name']) && $blocks[$new_asset['id']]['name'] === $new_asset['name']) {
593 if ($new_asset['name'] === 'image') {
594 $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url'];
595 $blocks[$new_asset['id']]['properties']['wp_attachment_id'] = $new_asset['attachment_id'];
596 } elseif ($new_asset['name'] === 'video') {
597 if ($new_asset['thumbnail']) {
598 $blocks[$new_asset['id']]['properties']['thumbnail']['url'] = $new_asset['url'];
599 } else {
600 $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url'];
601 }
602 } elseif ($new_asset['name'] === 'lottie') {
603 $blocks[$new_asset['id']]['properties']['lottie']['src'] = $new_asset['url'];
604 } elseif ($new_asset['name'] === 'lightbox') {
605 if ($new_asset['thumbnail']) {
606 $blocks[$new_asset['id']]['properties']['lightbox']['thumbnail']['src'] = $new_asset['url'];
607 } else {
608 $blocks[$new_asset['id']]['properties']['lightbox']['media'][$new_asset['index']]['sources']['original'] = $new_asset['url'];
609 }
610 }
611 }
612 }
613
614 $kirki_json_data['blocks'] = $blocks;
615 }
616
617 // remove asset_urls
618 unset($kirki_json_data['asset_urls']);
619
620 if ($post_id) {
621 $root = array(
622 'accept' => '*',
623 'children' => array('body'),
624 'id' => 'root',
625 'name' => 'root',
626 'styleIds' => array(),
627 'title' => 'Root',
628 );
629 $kirki_json_data['blocks']['root'] = $root;
630 if ($post_id) {
631 foreach ($kirki_json_data['styles'] as $key => $style) {
632 $style['name'] = HelperFunctions::add_prefix_to_class_name('post-' . $post_id, $style['name']);
633 unset($style['isGlobal']);
634 unset($style['isDefault']);
635 $kirki_json_data['styles'][$key] = $style;
636 }
637 }
638 HelperFunctions::save_kirki_data_to_db($post_id, $kirki_json_data);
639 }
640
641 // delete temp folder
642 HelperFunctions::delete_directory($temp_folder_path);
643 return $kirki_json_data;
644 }
645
646 return false;
647 }
648
649 /**
650 * @deprecated
651 * @see \Kirki\App\Supports\Template::attach_assets_from_zip()
652 */
653 private static function upload_file($asset_item)
654 {
655 $asset_name = basename($asset_item['url']);
656 $temp_folder_path = HelperFunctions::get_temp_folder_path();
657 $source_file_path = $temp_folder_path . '/' . $asset_name;
658 if (file_exists($source_file_path)) {
659 $file_name = basename($source_file_path);
660
661 // Upload the file
662 $file_array = array(
663 'name' => $file_name,
664 'tmp_name' => $source_file_path,
665 );
666
667 $_FILES['file'] = $file_array;
668
669 $attachment_id = media_handle_upload(
670 'file',
671 0,
672 array(),
673 array(
674 'test_form' => false,
675 'action' => 'upload-attachment',
676 )
677 );
678
679 // Check if the upload was successful
680 if (!is_wp_error($attachment_id)) {
681 $post = get_post($attachment_id);
682
683 $new_asset = array(
684 'id' => $asset_item['id'],
685 'name' => $asset_item['name'],
686 'url' => $post->guid,
687 'attachment_id' => $attachment_id,
688 );
689
690 return $new_asset;
691 }
692 }
693
694 return null;
695 }
696
697
698 public static function delete_directory($dirname)
699 {
700 global $wp_filesystem;
701 if (empty($wp_filesystem)) {
702 require_once ABSPATH . '/wp-admin/includes/file.php';
703 WP_Filesystem();
704 }
705
706 if ($wp_filesystem->exists($dirname)) {
707 return $wp_filesystem->delete($dirname, true);
708 }
709
710 return false;
711 }
712 }
713