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