| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Templately\Core\Importer; |
| 4 | 4 | |
| 5 | 5 | use Templately\Core\Importer\Parsers\WXR_Parser; |
| 6 | +use Templately\Core\Importer\Runners\Loop; | |
| 6 | 7 | use Templately\Core\Importer\Utils\Utils; |
| 7 | 8 | use Templately\Utils\Helper; |
| 8 | 9 | use WP_Error; |
| 9 | 10 | use WP_Importer; |
| @@ -47,8 +48,11 @@ | ||
| 47 | 48 | } |
| 48 | 49 | } |
| 49 | 50 | |
| 50 | 51 | class WPImport extends WP_Importer { |
| 52 | + use LogHelper; | |
| 53 | + use Loop; | |
| 54 | + | |
| 51 | 55 | const DEFAULT_BUMP_REQUEST_TIMEOUT = 60; |
| 52 | 56 | const DEFAULT_ALLOW_CREATE_USERS = true; |
| 53 | 57 | const DEFAULT_IMPORT_ATTACHMENT_SIZE_LIMIT = 0; // 0 = unlimited. |
| 54 | 58 | |
| @@ -55,8 +59,12 @@ | ||
| 55 | 59 | /** |
| 56 | 60 | * @var string |
| 57 | 61 | */ |
| 58 | 62 | private $requested_file_path; |
| 63 | + /** | |
| 64 | + * @var string | |
| 65 | + */ | |
| 66 | + private $import_data_key; | |
| 59 | 67 | |
| 60 | 68 | /** |
| 61 | 69 | * @var array |
| 62 | 70 | */ |
| @@ -62,8 +70,13 @@ | ||
| 62 | 70 | */ |
| 63 | 71 | private $args; |
| 64 | 72 | |
| 65 | 73 | /** |
| 74 | + * @var FullSiteImport | |
| 75 | + */ | |
| 76 | + private $origin; | |
| 77 | + | |
| 78 | + /** | |
| 66 | 79 | * @var array |
| 67 | 80 | */ |
| 68 | 81 | private $output = [ |
| 69 | 82 | 'status' => 'failed', |
| @@ -110,8 +123,25 @@ | ||
| 110 | 123 | private $terms_meta = []; |
| 111 | 124 | |
| 112 | 125 | public static $_replace_image_ids = []; |
| 113 | 126 | |
| 127 | + | |
| 128 | + public $backup_attributes = [ | |
| 129 | + 'output', | |
| 130 | + 'url_remap', | |
| 131 | + '_replace_image_ids', | |
| 132 | + 'menu_item_orphans', | |
| 133 | + 'processed_menu_items', | |
| 134 | + 'post_orphans', | |
| 135 | + 'processed_posts', | |
| 136 | + 'featured_images', | |
| 137 | + 'mapped_terms_slug', | |
| 138 | + 'processed_terms', | |
| 139 | + 'processed_authors', | |
| 140 | + 'author_mapping', | |
| 141 | + ]; | |
| 142 | + | |
| 143 | + | |
| 114 | 144 | /** |
| 115 | 145 | * Parses filename from a Content-Disposition header value. |
| 116 | 146 | * |
| 117 | 147 | * As per RFC6266: |
| @@ -353,8 +383,14 @@ | ||
| 353 | 383 | if ( ! isset( $this->args['imported_authors'] ) ) { |
| 354 | 384 | return; |
| 355 | 385 | } |
| 356 | 386 | |
| 387 | + | |
| 388 | + $processed_templates = $this->get_progress([], $this->import_data_key); | |
| 389 | + if (!empty($processed_templates)) { | |
| 390 | + return; | |
| 391 | + } | |
| 392 | + | |
| 357 | 393 | $create_users = apply_filters( 'import_allow_create_users', self::DEFAULT_ALLOW_CREATE_USERS ); |
| 358 | 394 | |
| 359 | 395 | foreach ( (array) $this->args['imported_authors'] as $i => $old_login ) { |
| 360 | 396 | // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts. |
| @@ -409,8 +445,10 @@ | ||
| 409 | 445 | } |
| 410 | 446 | $this->author_mapping[ $santized_old_login ] = (int) get_current_user_id(); |
| 411 | 447 | } |
| 412 | 448 | } |
| 449 | + | |
| 450 | + $this->update_progress( true, null, $this->import_data_key ); | |
| 413 | 451 | } |
| 414 | 452 | |
| 415 | 453 | /** |
| 416 | 454 | * Create new terms based on import information |
| @@ -424,8 +462,14 @@ | ||
| 424 | 462 | 'succeed' => [], |
| 425 | 463 | 'failed' => [], |
| 426 | 464 | ]; |
| 427 | 465 | |
| 466 | + $processed_templates = $this->get_progress([], "wp_import_terms_" . $this->import_data_key); | |
| 467 | + if (!empty($processed_templates)) { | |
| 468 | + $result = $this->get_result([], "wp_import_terms_" . $this->import_data_key); | |
| 469 | + return $result; | |
| 470 | + } | |
| 471 | + | |
| 428 | 472 | $this->terms = apply_filters( 'wp_import_terms', $this->terms ); |
| 429 | 473 | if ( empty( $this->terms ) ) { |
| 430 | 474 | return $result; |
| 431 | 475 | } |
| @@ -505,8 +549,10 @@ | ||
| 505 | 549 | } |
| 506 | 550 | |
| 507 | 551 | unset( $this->terms ); |
| 508 | 552 | |
| 553 | + // Add the template to the processed templates and update the session data | |
| 554 | + $this->update_progress( true, $result, "wp_import_terms_" . $this->import_data_key); | |
| 509 | 555 | return $result; |
| 510 | 556 | } |
| 511 | 557 | |
| 512 | 558 | /** |
| @@ -576,16 +622,18 @@ | ||
| 576 | 622 | * |
| 577 | 623 | * @return array the ids of succeed/failed imported posts. |
| 578 | 624 | */ |
| 579 | 625 | private function process_posts(): array { |
| 580 | - $result = [ | |
| 581 | - 'succeed' => [], | |
| 582 | - 'failed' => [], | |
| 583 | - ]; | |
| 626 | + $backup_key = "wp_import_post_" . $this->import_data_key; | |
| 584 | 627 | |
| 585 | 628 | $this->posts = apply_filters( 'wp_import_posts', $this->posts ); |
| 586 | 629 | |
| 587 | - foreach ( $this->posts as $post ) { | |
| 630 | + $results = $this->loop( $this->posts, function($key, $post, $result ) { | |
| 631 | + | |
| 632 | + $result = !empty($result) ? $result : [ | |
| 633 | + 'succeed' => [], | |
| 634 | + 'failed' => [], | |
| 635 | + ]; | |
| 588 | 636 | $post = apply_filters( 'wp_import_post_data_raw', $post ); |
| 589 | 637 | |
| 590 | 638 | if ( ! post_type_exists( $post['post_type'] ) ) { |
| 591 | 639 | /* translators: 1: Post title, 2: Post type. */ |
| @@ -590,28 +638,32 @@ | ||
| 590 | 638 | if ( ! post_type_exists( $post['post_type'] ) ) { |
| 591 | 639 | /* translators: 1: Post title, 2: Post type. */ |
| 592 | 640 | $this->output['errors'][] = sprintf( esc_html__( 'Failed to import %1$s: Invalid post type %2$s', 'elementor' ), $post['post_title'], $post['post_type'] ); |
| 593 | 641 | do_action( 'wp_import_post_exists', $post ); |
| 594 | - continue; | |
| 642 | + return $result; | |
| 595 | 643 | } |
| 596 | 644 | |
| 597 | 645 | if ( isset( $this->processed_posts[ $post['post_id'] ] ) && ! empty( $post['post_id'] ) ) { |
| 598 | - continue; | |
| 646 | + return $result; | |
| 599 | 647 | } |
| 600 | 648 | |
| 601 | 649 | if ( 'auto-draft' === $post['status'] ) { |
| 602 | - continue; | |
| 650 | + return $result; | |
| 603 | 651 | } |
| 604 | 652 | |
| 653 | + if(!empty($post['post_content']) && !empty($post['post_id'])){ | |
| 654 | + $post['post_content'] = Utils::import_and_replace_attachments($post['post_content'], $post['post_id']); | |
| 655 | + } | |
| 656 | + | |
| 605 | 657 | if ( 'nav_menu_item' === $post['post_type'] ) { |
| 606 | 658 | $result['succeed'] += $this->process_menu_item( $post ); |
| 607 | - continue; | |
| 659 | + return $result; | |
| 608 | 660 | } |
| 609 | 661 | |
| 610 | 662 | if ( 'wp_navigation' === $post['post_type'] ) { |
| 611 | 663 | $processed = $this->process_navigation( $post ); |
| 612 | 664 | if ( ! $processed ) { |
| 613 | - continue; | |
| 665 | + return $result; | |
| 614 | 666 | } |
| 615 | 667 | } |
| 616 | 668 | |
| 617 | 669 | $post_type_object = get_post_type_object( $post['post_type'] ); |
| @@ -658,9 +710,9 @@ | ||
| 658 | 710 | $postdata = wp_slash( $postdata ); |
| 659 | 711 | |
| 660 | 712 | if ( 'attachment' === $postdata['post_type'] ) { |
| 661 | 713 | $remote_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid']; |
| 662 | - | |
| 714 | + $attachment_sizes = []; | |
| 663 | 715 | // try to use _wp_attached file for upload folder placement to ensure the same location as the export site |
| 664 | 716 | // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() |
| 665 | 717 | $postdata['upload_date'] = $post['post_date']; |
| 666 | 718 | if ( isset( $post['postmeta'] ) ) { |
| @@ -668,14 +720,19 @@ | ||
| 668 | 720 | if ( '_wp_attached_file' === $meta['key'] ) { |
| 669 | 721 | if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) { |
| 670 | 722 | $postdata['upload_date'] = $matches[0]; |
| 671 | 723 | } |
| 672 | - break; | |
| 724 | + // break; | |
| 673 | 725 | } |
| 726 | + else if ( '_wp_attachment_metadata' === $meta['key'] ) { | |
| 727 | + $attachment_metadata = maybe_unserialize( $meta['value'] ); | |
| 728 | + $attachment_sizes = $attachment_metadata['sizes'] ?? []; | |
| 729 | + // break; | |
| 730 | + } | |
| 674 | 731 | } |
| 675 | 732 | } |
| 676 | 733 | |
| 677 | - $post_id = $this->process_attachment( $postdata, $remote_url ); | |
| 734 | + $post_id = $this->process_attachment( $postdata, $remote_url, $attachment_sizes, $original_post_id ); | |
| 678 | 735 | $comment_post_id = $post_id; |
| 679 | 736 | } else { |
| 680 | 737 | $post_id = wp_insert_post( $postdata, true ); |
| 681 | 738 | |
| @@ -696,9 +753,13 @@ | ||
| 696 | 753 | $result['failed'][] = $original_post_id; |
| 697 | 754 | |
| 698 | 755 | $this->output['errors'][] = $error; |
| 699 | 756 | |
| 700 | - continue; | |
| 757 | + if ( 'attachment' === $postdata['post_type'] ) { | |
| 758 | + do_action( 'templately_import.process_post', $post, $result, $this ); | |
| 759 | + } | |
| 760 | + | |
| 761 | + return $result; | |
| 701 | 762 | } |
| 702 | 763 | |
| 703 | 764 | $result['succeed'][ $original_post_id ] = $post_id; |
| 704 | 765 | |
| @@ -852,13 +913,15 @@ | ||
| 852 | 913 | } |
| 853 | 914 | } |
| 854 | 915 | |
| 855 | 916 | do_action( 'templately_import.process_post', $post, $result, $this ); |
| 856 | - } | |
| 857 | 917 | |
| 918 | + return $result; | |
| 919 | + }, $backup_key); //, true | |
| 920 | + | |
| 858 | 921 | unset( $this->posts ); |
| 859 | 922 | |
| 860 | - return $result; | |
| 923 | + return $results; | |
| 861 | 924 | } |
| 862 | 925 | |
| 863 | 926 | /** |
| 864 | 927 | * Attempt to create a new menu item from import data |
| @@ -1039,9 +1102,9 @@ | ||
| 1039 | 1102 | * @param string $url URL to fetch attachment from |
| 1040 | 1103 | * |
| 1041 | 1104 | * @return int|WP_Error Post ID on success, WP_Error otherwise |
| 1042 | 1105 | */ |
| 1043 | - public function process_attachment( $post, $url, $sizes = [] ) { | |
| 1106 | + public function process_attachment( $post, $url, $sizes = [], $original_post_id = null ) { | |
| 1044 | 1107 | if ( ! $this->fetch_attachments ) { |
| 1045 | 1108 | return new WP_Error( 'attachment_processing_error', esc_html__( 'Fetching attachments is not enabled', 'elementor' ) ); |
| 1046 | 1109 | } |
| 1047 | 1110 | if ( ! function_exists( 'wp_crop_image' ) ) { |
| @@ -1054,8 +1117,13 @@ | ||
| 1054 | 1117 | |
| 1055 | 1118 | if($saved_image = $this->get_saved_image($url)){ |
| 1056 | 1119 | $this->url_remap[ $url ] = wp_get_attachment_url( $saved_image ); |
| 1057 | 1120 | $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension(wp_get_attachment_url( $saved_image )); |
| 1121 | + | |
| 1122 | + $full_size_path = get_attached_file($saved_image); | |
| 1123 | + $metadata = wp_get_attachment_metadata($saved_image); | |
| 1124 | + $metadata = $this->import_sizes($sizes, $metadata, $full_size_path, $saved_image); | |
| 1125 | + wp_update_attachment_metadata( $saved_image, $metadata ); | |
| 1058 | 1126 | return $saved_image; |
| 1059 | 1127 | } |
| 1060 | 1128 | |
| 1061 | 1129 | // Check if the URL is from the wp-includes/images directory |
| @@ -1077,10 +1145,19 @@ | ||
| 1077 | 1145 | // Move the file to the uploads dir. |
| 1078 | 1146 | $file_name = basename( parse_url( $url, PHP_URL_PATH ) ); |
| 1079 | 1147 | $file_name = wp_unique_filename( $upload_dir['path'], $file_name ); |
| 1080 | 1148 | $dest_file = $upload_dir['path'] . "/$file_name"; |
| 1081 | - $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir ); | |
| 1149 | + $start = microtime(true); | |
| 1082 | 1150 | |
| 1151 | + $upload = apply_filters( 'templately_import_copy_attachment', null, $original_post_id, $dest_file, $upload_dir ); | |
| 1152 | + if ( null === $upload ) { | |
| 1153 | + $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir ); | |
| 1154 | + } | |
| 1155 | + | |
| 1156 | + $end = microtime(true); | |
| 1157 | + $duration = $end - $start; | |
| 1158 | + error_log('Duration: ' . $duration); | |
| 1159 | + | |
| 1083 | 1160 | if ( is_wp_error( $upload ) ) { |
| 1084 | 1161 | return $upload; |
| 1085 | 1162 | } |
| 1086 | 1163 | |
| @@ -1096,8 +1173,12 @@ | ||
| 1096 | 1173 | |
| 1097 | 1174 | // As per wp-admin/includes/upload.php. |
| 1098 | 1175 | $post_id = wp_insert_attachment( $post, $upload['file'] ); |
| 1099 | 1176 | |
| 1177 | + if(is_wp_error($post_id)){ | |
| 1178 | + return $post_id; | |
| 1179 | + } | |
| 1180 | + | |
| 1100 | 1181 | $this->update_post_meta( $post_id ); |
| 1101 | 1182 | |
| 1102 | 1183 | // Generate attachment metadata |
| 1103 | 1184 | $metadata = wp_generate_attachment_metadata( $post_id, $upload['file'] ); |
| @@ -1104,9 +1185,9 @@ | ||
| 1104 | 1185 | |
| 1105 | 1186 | // error_log('Metadata: ' . print_r($metadata, true)); |
| 1106 | 1187 | |
| 1107 | 1188 | // For gutenberg pages |
| 1108 | - $metadata = $this->import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id); | |
| 1189 | + $metadata = $this->import_sizes($sizes, $metadata, $upload['file'], $post_id); | |
| 1109 | 1190 | |
| 1110 | 1191 | // error_log('Metadata: ' . print_r($metadata, true)); |
| 1111 | 1192 | wp_update_attachment_metadata( $post_id, $metadata ); |
| 1112 | 1193 | |
| @@ -1179,10 +1260,8 @@ | ||
| 1179 | 1260 | |
| 1180 | 1261 | // Make sure the fetch was successful. |
| 1181 | 1262 | if ( 200 !== $remote_response_code ) { |
| 1182 | 1263 | @unlink( $tmp_file_name ); |
| 1183 | - Helper::log($url); | |
| 1184 | - Helper::log($remote_response); | |
| 1185 | 1264 | return new WP_Error( 'import_file_error', sprintf( /* translators: 1: HTTP error message, 2: HTTP error code. */ esc_html__( 'Remote server returned the following unexpected result: %1$s (%2$s)', 'elementor' ), get_status_header_desc( $remote_response_code ), esc_html( $remote_response_code ) ) ); |
| 1186 | 1265 | } |
| 1187 | 1266 | |
| 1188 | 1267 | $headers = wp_remote_retrieve_headers( $remote_response ); |
| @@ -1311,9 +1390,9 @@ | ||
| 1311 | 1390 | ); |
| 1312 | 1391 | |
| 1313 | 1392 | if ( $post_id ) { |
| 1314 | 1393 | self::$_replace_image_ids[ $hash ] = $post_id; |
| 1315 | - return $post_id; | |
| 1394 | + return (int) $post_id; | |
| 1316 | 1395 | } |
| 1317 | 1396 | |
| 1318 | 1397 | return false; |
| 1319 | 1398 | } |
| @@ -1318,69 +1397,92 @@ | ||
| 1318 | 1397 | return false; |
| 1319 | 1398 | } |
| 1320 | 1399 | |
| 1321 | 1400 | |
| 1322 | - public function extract_sizes($sizes) { | |
| 1323 | - return array_reduce($sizes, function($carry, $url) { | |
| 1324 | - if (preg_match('/-((\d+)x(\d+))\./', $url, $matches)) { | |
| 1325 | - $carry[$matches[1]] = $url; | |
| 1326 | - } | |
| 1327 | - return $carry; | |
| 1328 | - }, []); | |
| 1329 | - } | |
| 1401 | + public function import_sizes($sizes, $metadata, $full_size_file, $post_id) { | |
| 1402 | + if (!empty($sizes)) { | |
| 1403 | + do_action('templately_import.finalize_gutenberg_attachment', $post_id); | |
| 1330 | 1404 | |
| 1331 | - public function size_exists_in_metadata($size_dimension, $metadata) { | |
| 1332 | - foreach ($metadata['sizes'] as $size => $size_info) { | |
| 1333 | - if (strpos($size_info['file'], $size_dimension) !== false) { | |
| 1334 | - return true; | |
| 1335 | - } | |
| 1336 | - } | |
| 1337 | - return false; | |
| 1338 | - } | |
| 1405 | + foreach ($sizes as $size_name => $size) { | |
| 1406 | + $size_dimension = $size['width'] . 'x' . $size['height']; | |
| 1407 | + $size_name = !is_string($size_name) ? $size_dimension : $size_name; | |
| 1408 | + $unique_destination_file = $this->create_unique_destination_file($full_size_file, $size); | |
| 1409 | + // check non cropped sizes. with dynamic height | |
| 1410 | + if (!$this->size_exists_in_metadata(basename($unique_destination_file), $metadata)) { | |
| 1411 | + if ($unique_destination_file) { | |
| 1412 | + $missing_size = $this->generate_missing_size_from_full($full_size_file, $unique_destination_file, $size); | |
| 1413 | + if ($missing_size && !is_wp_error($missing_size)) { | |
| 1414 | + unset($missing_size['path']); | |
| 1415 | + $metadata['sizes'][$size_name] = $missing_size; | |
| 1416 | + do_action('templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension); | |
| 1417 | + } | |
| 1418 | + } | |
| 1419 | + } | |
| 1420 | + } | |
| 1421 | + } | |
| 1422 | + return $metadata; | |
| 1423 | + } | |
| 1339 | 1424 | |
| 1340 | - public function create_unique_destination_file($upload_file, $size_dimension) { | |
| 1341 | - $destination_file = preg_replace('/(\.[^.]+)$/', '-' . $size_dimension . '$1', $upload_file); | |
| 1342 | - $destination_dir = pathinfo($destination_file, PATHINFO_DIRNAME); | |
| 1343 | - $destination_filename = pathinfo($destination_file, PATHINFO_BASENAME); | |
| 1344 | - $unique_filename = wp_unique_filename($destination_dir, $destination_filename); | |
| 1345 | - return $destination_dir . '/' . $unique_filename; | |
| 1346 | - } | |
| 1425 | + public function generate_missing_size_from_full($full_size_file, $destination_file, $size) { | |
| 1426 | + // Generate the missing size from the full-size image | |
| 1427 | + $editor = wp_get_image_editor($full_size_file); | |
| 1428 | + if (is_wp_error($editor)) { | |
| 1429 | + return $editor; | |
| 1430 | + } | |
| 1347 | 1431 | |
| 1348 | - public function create_size_array($destination_file, $size_dimension) { | |
| 1349 | - list($width, $height) = explode('x', $size_dimension); | |
| 1350 | - return array( | |
| 1351 | - 'file' => basename($destination_file), | |
| 1352 | - 'width' => $width, | |
| 1353 | - 'height' => $height, | |
| 1354 | - 'mime-type' => wp_check_filetype($destination_file)['type'], | |
| 1355 | - 'filesize' => filesize($destination_file), | |
| 1356 | - 'resized' => false, | |
| 1357 | - ); | |
| 1358 | - } | |
| 1432 | + $resized = $editor->resize($size['width'], $size['height'], true); | |
| 1433 | + if (is_wp_error($resized)) { | |
| 1434 | + return $resized; | |
| 1435 | + } | |
| 1359 | 1436 | |
| 1360 | - public function import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id) { | |
| 1361 | - if (!empty($sizes)) { | |
| 1362 | - do_action( 'templately_import.finalize_gutenberg_attachment', $post_id ); | |
| 1437 | + $saved = $editor->save($destination_file); | |
| 1438 | + if (is_wp_error($saved)) { | |
| 1439 | + return $saved; | |
| 1440 | + } | |
| 1363 | 1441 | |
| 1364 | - $size_dimensions = $this->extract_sizes($sizes); | |
| 1365 | - $size_dimensions = array_filter($size_dimensions); | |
| 1442 | + return $saved; | |
| 1443 | + } | |
| 1366 | 1444 | |
| 1367 | - foreach ($size_dimensions as $size_dimension => $__url) { | |
| 1368 | - if (!$this->size_exists_in_metadata($size_dimension, $metadata)) { | |
| 1369 | - $unique_destination_file = $this->create_unique_destination_file($upload['file'], $size_dimension); | |
| 1370 | - $missing_size = $this->fetch_remote_file($__url, $unique_destination_file, $upload_dir); | |
| 1445 | + public function size_exists_in_metadata($size_file, $metadata) { | |
| 1446 | + if (isset($metadata['sizes']) && is_array($metadata['sizes'])) { | |
| 1447 | + foreach ($metadata['sizes'] as $size => $size_info) { | |
| 1448 | + if ($size_info['file'] == $size_file) { | |
| 1449 | + return true; | |
| 1450 | + } | |
| 1451 | + } | |
| 1452 | + } | |
| 1453 | + return false; | |
| 1454 | + } | |
| 1371 | 1455 | |
| 1372 | - if($missing_size && !is_wp_error($missing_size)) { | |
| 1373 | - $metadata['sizes'][$size_dimension] = $this->create_size_array($unique_destination_file, $size_dimension); | |
| 1374 | - do_action( 'templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension ); | |
| 1375 | - } | |
| 1376 | - } | |
| 1377 | - } | |
| 1456 | + public function create_unique_destination_file($full_size_file, $size) { | |
| 1457 | + $pathinfo = pathinfo($full_size_file); | |
| 1458 | + $directory = $pathinfo['dirname']; | |
| 1459 | + $filename = $pathinfo['filename']; | |
| 1460 | + $extension = $pathinfo['extension']; | |
| 1378 | 1461 | |
| 1379 | - } | |
| 1380 | - return $metadata; | |
| 1381 | - } | |
| 1462 | + $destination_file = $directory . '/' . $filename . '-' . $size['width'] . 'x' . $size['height'] . '.' . $extension; | |
| 1382 | 1463 | |
| 1464 | + // Skip if the file already exists | |
| 1465 | + if (file_exists($destination_file)) { | |
| 1466 | + // return false; | |
| 1467 | + } | |
| 1468 | + | |
| 1469 | + return $destination_file; | |
| 1470 | + } | |
| 1471 | + | |
| 1472 | + public function create_size_array($destination_file, $size_dimension) { | |
| 1473 | + list($width, $height) = explode('x', $size_dimension); | |
| 1474 | + return array( | |
| 1475 | + 'file' => basename($destination_file), | |
| 1476 | + 'width' => $width, | |
| 1477 | + 'height' => $height, | |
| 1478 | + 'mime-type' => wp_check_filetype($destination_file)['type'], | |
| 1479 | + 'filesize' => filesize($destination_file), | |
| 1480 | + 'resized' => false, | |
| 1481 | + ); | |
| 1482 | + } | |
| 1483 | + | |
| 1484 | + | |
| 1383 | 1485 | /** |
| 1384 | 1486 | * Attempt to associate posts and menu items with previously missing parents |
| 1385 | 1487 | * |
| 1386 | 1488 | * An imported post's parent may not have been imported when it was first created |
| @@ -1498,9 +1600,9 @@ | ||
| 1498 | 1600 | */ |
| 1499 | 1601 | private function is_valid_meta_key( $key ) { |
| 1500 | 1602 | // Skip attachment metadata since we'll regenerate it from scratch. |
| 1501 | 1603 | // Skip _edit_lock as not relevant for import |
| 1502 | - if ( in_array( $key, [ '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ] ) ) { | |
| 1604 | + if ( in_array( $key, [ '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock', '_elementor_source_image_hash', 'sm_cloud' ] ) ) { | |
| 1503 | 1605 | return false; |
| 1504 | 1606 | } |
| 1505 | 1607 | |
| 1506 | 1608 | return $key; |
| @@ -1566,10 +1668,23 @@ | ||
| 1566 | 1668 | */ |
| 1567 | 1669 | public function __construct( $file, array $args = [] ) { |
| 1568 | 1670 | parent::__construct(); |
| 1569 | 1671 | |
| 1570 | - $this->requested_file_path = $file; | |
| 1571 | - $this->args = $args; | |
| 1672 | + $this->args = $args; | |
| 1673 | + $this->session_id = $args['session_id']; | |
| 1674 | + | |
| 1675 | + if ( ! empty( $args['json'] ) ) { | |
| 1676 | + $this->json = $args['json']; | |
| 1677 | + } | |
| 1678 | + | |
| 1679 | + if ( ! empty( $args['origin'] ) ) { | |
| 1680 | + $this->origin = $args['origin']; | |
| 1681 | + } | |
| 1682 | + | |
| 1683 | + if ( ! empty( $file ) ) { | |
| 1684 | + $this->requested_file_path = $file; | |
| 1685 | + $this->import_data_key = 'wp_importer_attributes_' . md5($this->requested_file_path); | |
| 1686 | + } | |
| 1572 | 1687 | |
| 1573 | 1688 | if ( ! empty( $this->args['fetch_attachments'] ) ) { |
| 1574 | 1689 | $this->fetch_attachments = true; |
| 1575 | 1690 | } |