Collaboration
1 week ago
Apps.php
1 month ago
Collection.php
1 month ago
Comments.php
3 months ago
DynamicContent.php
3 days ago
ExportImport.php
3 days ago
Form.php
1 month ago
Media.php
2 weeks ago
Page.php
1 week ago
PageSettings.php
1 month ago
RBAC.php
1 month ago
Symbol.php
1 month ago
Taxonomy.php
3 months ago
TemplateExportImport.php
3 months ago
UserData.php
1 month ago
Users.php
3 months ago
Walkthrough.php
1 week ago
WordpressData.php
3 months ago
WpAdmin.php
2 weeks ago
ExportImport.php
708 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 | ) |
| 284 | ); |
| 285 | |
| 286 | if (empty($wp_filesystem)) { |
| 287 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 288 | WP_Filesystem(); |
| 289 | } |
| 290 | |
| 291 | global $wp_filesystem; |
| 292 | |
| 293 | // Save the file to the destination |
| 294 | if ($wp_filesystem->put_contents($destination, $file_content, FS_CHMOD_FILE)) { |
| 295 | return true; |
| 296 | } else { |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | public static function template_import() |
| 302 | { |
| 303 | $upload_dir = wp_upload_dir(); |
| 304 | |
| 305 | $is_include_media = HelperFunctions::sanitize_text(isset($_POST['is_include_media']) ? $_POST['is_include_media'] : false); |
| 306 | |
| 307 | // get template file from url |
| 308 | $file_url = HelperFunctions::sanitize_text(isset($_POST['file_url']) ? $_POST['file_url'] : false); |
| 309 | |
| 310 | $destination_path = $upload_dir['basedir'] . '/kirki-template.zip'; |
| 311 | |
| 312 | if (self::download_and_save_zip_file($file_url, $destination_path)) { |
| 313 | $file = array( |
| 314 | 'name' => 'kirki-template.zip', |
| 315 | 'tmp_name' => $destination_path, |
| 316 | 'error' => 0, |
| 317 | ); |
| 318 | |
| 319 | self::handle_zip_file_upload($file, $is_include_media); |
| 320 | } else { |
| 321 | // Error occurred while downloading or saving the file |
| 322 | wp_send_json_error('Zip File upload Failed'); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | |
| 327 | public static function handle_zip_file_upload($file, $is_include_media) |
| 328 | { |
| 329 | $upload_dir = wp_upload_dir(); |
| 330 | |
| 331 | $file_name = $file['name']; |
| 332 | $file_tmp = $file['tmp_name']; |
| 333 | $file_error = $file['error']; |
| 334 | |
| 335 | $file_ext = explode('.', $file_name); // ['file', 'ext'] |
| 336 | $file_ext = strtolower(end($file_ext)); // 'ext' |
| 337 | |
| 338 | $allowed = array('zip'); |
| 339 | |
| 340 | if (!in_array($file_ext, $allowed, true)) { |
| 341 | wp_send_json_error('File type not allowed, please upload zip file'); |
| 342 | } |
| 343 | |
| 344 | if ($file_error !== 0) { |
| 345 | wp_send_json_error('File upload Failed'); |
| 346 | } |
| 347 | |
| 348 | $file_name_new = uniqid('', true) . '.' . $file_ext; // 'random.ext' |
| 349 | $file_destination = $upload_dir['basedir'] . '/' . $file_name_new; |
| 350 | |
| 351 | global $wp_filesystem; |
| 352 | if (empty($wp_filesystem)) { |
| 353 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 354 | WP_Filesystem(); |
| 355 | } |
| 356 | |
| 357 | if (!$wp_filesystem->move($file_tmp, $file_destination)) { |
| 358 | wp_send_json_error('Something went wrong, please try again'); |
| 359 | } |
| 360 | |
| 361 | $zip = new \ZipArchive(); |
| 362 | $res = $zip->open($file_destination); |
| 363 | |
| 364 | if ($res !== true) { |
| 365 | wp_send_json_error('Failed to extract zip file'); |
| 366 | } |
| 367 | |
| 368 | $filtered_zip_path = HelperFunctions::filterZipFile($zip, $file_destination); |
| 369 | |
| 370 | if (!$filtered_zip_path) { |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | $zip->close(); |
| 375 | wp_delete_file($file_destination); |
| 376 | |
| 377 | $temp_folder = 'kirki_temp'; |
| 378 | $temp_folder_path = HelperFunctions::get_temp_folder_path(); |
| 379 | |
| 380 | // Reopen the filtered ZIP file for extraction |
| 381 | $res = $zip->open($filtered_zip_path); |
| 382 | |
| 383 | $zip->extractTo($temp_folder_path); |
| 384 | $zip->close(); |
| 385 | |
| 386 | if (empty($wp_filesystem)) { |
| 387 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 388 | WP_Filesystem(); |
| 389 | } |
| 390 | |
| 391 | global $wp_filesystem; |
| 392 | |
| 393 | $kirki_json_file = $temp_folder_path . '/kirki-data.json'; |
| 394 | $kirki_json_data = $wp_filesystem->get_contents($kirki_json_file); |
| 395 | $kirki_json_data = json_decode($kirki_json_data, true); |
| 396 | |
| 397 | $blocks = $kirki_json_data['blocks']; |
| 398 | $symbols = $kirki_json_data['symbols']; |
| 399 | |
| 400 | /** |
| 401 | * @deprecated |
| 402 | * @see \Kirki\App\Supports\Template::process_template_from_json() |
| 403 | */ |
| 404 | if ($is_include_media === 'true') { |
| 405 | $asset_urls = $kirki_json_data['asset_urls']; |
| 406 | |
| 407 | $pivot_table = array(); |
| 408 | $assets_urls_map = array(); |
| 409 | |
| 410 | foreach ($asset_urls as $key => $asset_item) { |
| 411 | $url = $asset_item['url']; |
| 412 | |
| 413 | if (empty($pivot_table[$url]['url'])) { |
| 414 | $new_asset = self::upload_file($asset_item); |
| 415 | |
| 416 | if ($new_asset) { |
| 417 | $pivot_table[$url]['url'] = $new_asset['url']; |
| 418 | $pivot_table[$url]['attachment_id'] = $new_asset['attachment_id']; |
| 419 | |
| 420 | $asset_urls[$key]['url'] = $new_asset['url']; |
| 421 | $asset_urls[$key]['attachment_id'] = $new_asset['attachment_id']; |
| 422 | $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null; |
| 423 | $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false; |
| 424 | } |
| 425 | } else { |
| 426 | $asset_urls[$key]['url'] = $pivot_table[$url]['url']; |
| 427 | $asset_urls[$key]['attachment_id'] = $pivot_table[$url]['attachment_id']; |
| 428 | |
| 429 | $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null; |
| 430 | $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // update blocks with new asset urls |
| 435 | foreach ($asset_urls as $key => $new_asset) { |
| 436 | $assets_urls_map[$new_asset['id']] = $new_asset; |
| 437 | |
| 438 | if (isset($blocks[$new_asset['id']]['name'], $new_asset['name']) && $blocks[$new_asset['id']]['name'] === $new_asset['name']) { |
| 439 | if ($new_asset['name'] === 'image') { |
| 440 | $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url']; |
| 441 | $blocks[$new_asset['id']]['properties']['wp_attachment_id'] = $new_asset['attachment_id']; |
| 442 | } elseif ($new_asset['name'] === 'video') { |
| 443 | if ($new_asset['thumbnail']) { |
| 444 | $blocks[$new_asset['id']]['properties']['thumbnail']['url'] = $new_asset['url']; |
| 445 | } else { |
| 446 | $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url']; |
| 447 | } |
| 448 | } elseif ($new_asset['name'] === 'lottie') { |
| 449 | $blocks[$new_asset['id']]['properties']['lottie']['src'] = $new_asset['url']; |
| 450 | } elseif ($new_asset['name'] === 'lightbox') { |
| 451 | if ($new_asset['thumbnail']) { |
| 452 | $blocks[$new_asset['id']]['properties']['lightbox']['thumbnail']['src'] = $new_asset['url']; |
| 453 | } else { |
| 454 | $blocks[$new_asset['id']]['properties']['lightbox']['media'][$new_asset['index']]['sources']['original'] = $new_asset['url']; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // update symbols with new asset urls |
| 461 | foreach ($symbols as $sym_key => $symbol) { |
| 462 | foreach ($symbol['symbolData']['data'] as $key => $block) { |
| 463 | if (isset($assets_urls_map[$block['id']])) { |
| 464 | |
| 465 | if ($assets_urls_map[$block['id']]['name'] === 'image') { |
| 466 | $symbol['symbolData']['data'][$key]['attributes']['src'] = $assets_urls_map[$block['id']]['url']; |
| 467 | $symbol['symbolData']['data'][$key]['wp_attachment_id'] = $assets_urls_map[$block['id']]['attachment_id']; |
| 468 | } elseif ($assets_urls_map[$block['id']]['name'] === 'video') { |
| 469 | if ($assets_urls_map[$block['id']]['thumbnail']) { |
| 470 | $symbol['symbolData']['data'][$key]['thumbnail']['url'] = $assets_urls_map[$block['id']]['url']; |
| 471 | } else { |
| 472 | $symbol['symbolData']['data'][$key]['attributes']['src'] = $assets_urls_map[$block['id']]['url']; |
| 473 | } |
| 474 | } elseif ($assets_urls_map[$block['id']]['name'] === 'lottie') { |
| 475 | $symbol['symbolData']['data'][$key]['lottie']['src'] = $assets_urls_map[$block['id']]['url']; |
| 476 | } elseif ($assets_urls_map[$block['id']]['name'] === 'lightbox') { |
| 477 | if ($assets_urls_map[$block['id']]['thumbnail']) { |
| 478 | $symbol['symbolData']['data'][$key]['lightbox']['thumbnail']['src'] = $assets_urls_map[$block['id']]['url']; |
| 479 | } else { |
| 480 | $symbol['symbolData']['data'][$key]['lightbox']['media'][$assets_urls_map[$block['id']]['index']]['sources']['original'] = $assets_urls_map[$block['id']]['url']; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | $symbols[$sym_key]['symbolData']['data'] = $symbol['symbolData']['data']; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // Tracker for saved symbols |
| 491 | $saved_symbols_tracker = array(); |
| 492 | |
| 493 | if (is_array($symbols) && count($symbols) > 0) { |
| 494 | foreach ($symbols as $symbol) { |
| 495 | $symbol_id = $symbol['id']; |
| 496 | |
| 497 | // Check if the symbol is already saved |
| 498 | if (!isset($saved_symbols_tracker[$symbol_id])) { |
| 499 | // Save the symbol to the database |
| 500 | $saved_symbol = Symbol::save_to_db($symbol); |
| 501 | |
| 502 | // Add the saved symbol to the tracker |
| 503 | if ($saved_symbol) { |
| 504 | $saved_symbols_tracker[$symbol_id] = $saved_symbol; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // Update all relevant elements with the symbol's ID |
| 509 | if (isset($blocks[$symbol['elementId']]) && $blocks[$symbol['elementId']]['name'] === 'symbol') { |
| 510 | $blocks[$symbol['elementId']]['properties']['symbolId'] = $saved_symbols_tracker[$symbol_id]['id']; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | $kirki_json_data['blocks'] = $blocks; |
| 516 | $kirki_json_data['symbols'] = $symbols; |
| 517 | |
| 518 | // remove asset_urls |
| 519 | unset($kirki_json_data['asset_urls']); |
| 520 | |
| 521 | // delete zip file |
| 522 | $wp_filesystem->delete($file_destination); |
| 523 | |
| 524 | // delete temp folder |
| 525 | HelperFunctions::delete_directory($temp_folder_path); |
| 526 | |
| 527 | wp_send_json_success($kirki_json_data); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Process kirki template zip |
| 532 | * |
| 533 | * @param string $kirki_template_zip_path |
| 534 | * @param boolean $is_include_media |
| 535 | * @return boolean|string |
| 536 | * |
| 537 | * @deprecated |
| 538 | * @see \Kirki\App\Supports\Template::save_kirki_template_from_zip() |
| 539 | */ |
| 540 | public static function process_kirki_template_zip($kirki_template_zip_path, $is_include_media = false, $post_id = false) |
| 541 | { |
| 542 | $zip = new \ZipArchive(); |
| 543 | $res = $zip->open($kirki_template_zip_path); |
| 544 | |
| 545 | if ($res === true) { |
| 546 | $temp_folder_path = HelperFunctions::get_temp_folder_path(); |
| 547 | |
| 548 | $zip->extractTo($temp_folder_path); |
| 549 | $zip->close(); |
| 550 | |
| 551 | $kirki_json_file = $temp_folder_path . '/kirki-data.json'; |
| 552 | $kirki_json_data = file_get_contents($kirki_json_file); |
| 553 | $kirki_json_data = json_decode($kirki_json_data, true); |
| 554 | |
| 555 | if ($is_include_media === 'true') { |
| 556 | $asset_urls = $kirki_json_data['asset_urls']; |
| 557 | $blocks = $kirki_json_data['blocks']; |
| 558 | |
| 559 | $pivot_table = array(); |
| 560 | |
| 561 | foreach ($asset_urls as $key => $asset_item) { |
| 562 | $url = $asset_item['url']; |
| 563 | |
| 564 | if (empty($pivot_table[$url]['url'])) { |
| 565 | $new_asset = self::upload_file($asset_item); |
| 566 | |
| 567 | if ($new_asset) { |
| 568 | $pivot_table[$url]['url'] = $new_asset['url']; |
| 569 | $pivot_table[$url]['attachment_id'] = $new_asset['attachment_id']; |
| 570 | |
| 571 | $asset_urls[$key]['url'] = $new_asset['url']; |
| 572 | $asset_urls[$key]['attachment_id'] = $new_asset['attachment_id']; |
| 573 | $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null; |
| 574 | $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false; |
| 575 | } |
| 576 | } else { |
| 577 | $asset_urls[$key]['url'] = $pivot_table[$url]['url']; |
| 578 | $asset_urls[$key]['attachment_id'] = $pivot_table[$url]['attachment_id']; |
| 579 | |
| 580 | $asset_urls[$key]['index'] = isset($asset_item['index']) ? $asset_item['index'] : null; |
| 581 | $asset_urls[$key]['thumbnail'] = isset($asset_item['thumbnail']) ? $asset_item['thumbnail'] : false; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // update blocks with new asset urls |
| 586 | foreach ($asset_urls as $key => $new_asset) { |
| 587 | if (isset($blocks[$new_asset['id']]['name'], $new_asset['name']) && $blocks[$new_asset['id']]['name'] === $new_asset['name']) { |
| 588 | if ($new_asset['name'] === 'image') { |
| 589 | $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url']; |
| 590 | $blocks[$new_asset['id']]['properties']['wp_attachment_id'] = $new_asset['attachment_id']; |
| 591 | } elseif ($new_asset['name'] === 'video') { |
| 592 | if ($new_asset['thumbnail']) { |
| 593 | $blocks[$new_asset['id']]['properties']['thumbnail']['url'] = $new_asset['url']; |
| 594 | } else { |
| 595 | $blocks[$new_asset['id']]['properties']['attributes']['src'] = $new_asset['url']; |
| 596 | } |
| 597 | } elseif ($new_asset['name'] === 'lottie') { |
| 598 | $blocks[$new_asset['id']]['properties']['lottie']['src'] = $new_asset['url']; |
| 599 | } elseif ($new_asset['name'] === 'lightbox') { |
| 600 | if ($new_asset['thumbnail']) { |
| 601 | $blocks[$new_asset['id']]['properties']['lightbox']['thumbnail']['src'] = $new_asset['url']; |
| 602 | } else { |
| 603 | $blocks[$new_asset['id']]['properties']['lightbox']['media'][$new_asset['index']]['sources']['original'] = $new_asset['url']; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | $kirki_json_data['blocks'] = $blocks; |
| 610 | } |
| 611 | |
| 612 | // remove asset_urls |
| 613 | unset($kirki_json_data['asset_urls']); |
| 614 | |
| 615 | if ($post_id) { |
| 616 | $root = array( |
| 617 | 'accept' => '*', |
| 618 | 'children' => array('body'), |
| 619 | 'id' => 'root', |
| 620 | 'name' => 'root', |
| 621 | 'styleIds' => array(), |
| 622 | 'title' => 'Root', |
| 623 | ); |
| 624 | $kirki_json_data['blocks']['root'] = $root; |
| 625 | if ($post_id) { |
| 626 | foreach ($kirki_json_data['styles'] as $key => $style) { |
| 627 | $style['name'] = HelperFunctions::add_prefix_to_class_name('post-' . $post_id, $style['name']); |
| 628 | unset($style['isGlobal']); |
| 629 | unset($style['isDefault']); |
| 630 | $kirki_json_data['styles'][$key] = $style; |
| 631 | } |
| 632 | } |
| 633 | HelperFunctions::save_kirki_data_to_db($post_id, $kirki_json_data); |
| 634 | } |
| 635 | |
| 636 | // delete temp folder |
| 637 | HelperFunctions::delete_directory($temp_folder_path); |
| 638 | return $kirki_json_data; |
| 639 | } |
| 640 | |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * @deprecated |
| 646 | * @see \Kirki\App\Supports\Template::attach_assets_from_zip() |
| 647 | */ |
| 648 | private static function upload_file($asset_item) |
| 649 | { |
| 650 | $asset_name = basename($asset_item['url']); |
| 651 | $temp_folder_path = HelperFunctions::get_temp_folder_path(); |
| 652 | $source_file_path = $temp_folder_path . '/' . $asset_name; |
| 653 | if (file_exists($source_file_path)) { |
| 654 | $file_name = basename($source_file_path); |
| 655 | |
| 656 | // Upload the file |
| 657 | $file_array = array( |
| 658 | 'name' => $file_name, |
| 659 | 'tmp_name' => $source_file_path, |
| 660 | ); |
| 661 | |
| 662 | $_FILES['file'] = $file_array; |
| 663 | |
| 664 | $attachment_id = media_handle_upload( |
| 665 | 'file', |
| 666 | 0, |
| 667 | array(), |
| 668 | array( |
| 669 | 'test_form' => false, |
| 670 | 'action' => 'upload-attachment', |
| 671 | ) |
| 672 | ); |
| 673 | |
| 674 | // Check if the upload was successful |
| 675 | if (!is_wp_error($attachment_id)) { |
| 676 | $post = get_post($attachment_id); |
| 677 | |
| 678 | $new_asset = array( |
| 679 | 'id' => $asset_item['id'], |
| 680 | 'name' => $asset_item['name'], |
| 681 | 'url' => $post->guid, |
| 682 | 'attachment_id' => $attachment_id, |
| 683 | ); |
| 684 | |
| 685 | return $new_asset; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | return null; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | public static function delete_directory($dirname) |
| 694 | { |
| 695 | global $wp_filesystem; |
| 696 | if (empty($wp_filesystem)) { |
| 697 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 698 | WP_Filesystem(); |
| 699 | } |
| 700 | |
| 701 | if ($wp_filesystem->exists($dirname)) { |
| 702 | return $wp_filesystem->delete($dirname, true); |
| 703 | } |
| 704 | |
| 705 | return false; |
| 706 | } |
| 707 | } |
| 708 |