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