PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.3.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.3.2
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
← All changes | includes/Core/Importer/WPImport.php +221 -128 3.0.73.3.2 View file →
@@ -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
@@ -1068,9 +1136,28 @@
1068 1136
1069 1137 return $updated_url;
1070 1138 }
1071 1139
1072 - $upload = $this->fetch_remote_file( $url, $post );
1140 + $upload_dir = wp_upload_dir( $post['upload_date'] );
1141 + if ( ! ( $upload_dir && false === $upload_dir['error'] ) ) {
1142 + return new WP_Error( 'upload_dir_error', $upload_dir['error'] );
1143 + }
1144 +
1145 + // Move the file to the uploads dir.
1146 + $file_name = basename( parse_url( $url, PHP_URL_PATH ) );
1147 + $file_name = wp_unique_filename( $upload_dir['path'], $file_name );
1148 + $dest_file = $upload_dir['path'] . "/$file_name";
1149 + $start = microtime(true);
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 +
1073 1160 if ( is_wp_error( $upload ) ) {
1074 1161 return $upload;
1075 1162 }
1076 1163
@@ -1080,13 +1167,18 @@
1080 1167 } else {
1081 1168 return new WP_Error( 'attachment_processing_error', esc_html__( 'Invalid file type', 'elementor' ) );
1082 1169 }
1083 1170
1171 + $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1084 1172 $post['guid'] = $upload['url'];
1085 1173
1086 1174 // As per wp-admin/includes/upload.php.
1087 1175 $post_id = wp_insert_attachment( $post, $upload['file'] );
1088 1176
1177 + if(is_wp_error($post_id)){
1178 + return $post_id;
1179 + }
1180 +
1089 1181 $this->update_post_meta( $post_id );
1090 1182
1091 1183 // Generate attachment metadata
1092 1184 $metadata = wp_generate_attachment_metadata( $post_id, $upload['file'] );
@@ -1093,9 +1185,9 @@
1093 1185
1094 1186 // error_log('Metadata: ' . print_r($metadata, true));
1095 1187
1096 1188 // For gutenberg pages
1097 - $metadata = $this->import_sizes($sizes, $metadata, $upload, $post_id);
1189 + $metadata = $this->import_sizes($sizes, $metadata, $upload['file'], $post_id);
1098 1190
1099 1191 // error_log('Metadata: ' . print_r($metadata, true));
1100 1192 wp_update_attachment_metadata( $post_id, $metadata );
1101 1193
@@ -1118,35 +1210,8 @@
1118 1210
1119 1211 return $parts['dirname'] . '/' . $name;
1120 1212 }
1121 1213
1122 - private function import_size($remote_url, $destination_path) {
1123 - // Include the file for the download_url function
1124 - if(!function_exists('download_url')) {
1125 - require_once ABSPATH . 'wp-admin/includes/file.php';
1126 - }
1127 -
1128 - // Download file to temp dir
1129 - $temp_file = download_url($remote_url);
1130 -
1131 - // Check for download errors
1132 - if(is_wp_error($temp_file)) {
1133 - return $temp_file;
1134 - }
1135 -
1136 - // Move temp file to destination path
1137 - $result = copy($temp_file, $destination_path);
1138 -
1139 - // Remove temp file
1140 - @unlink($temp_file);
1141 -
1142 - if (!$result) {
1143 - return false;
1144 - }
1145 -
1146 - return true;
1147 - }
1148 -
1149 1214 /**
1150 1215 * Attempt to download a remote file attachment
1151 1216 *
1152 1217 * @param string $url URL of item to fetch
@@ -1153,16 +1218,12 @@
1153 1218 * @param array $post Attachment details
1154 1219 *
1155 1220 * @return array|WP_Error Local file location details on success, WP_Error otherwise
1156 1221 */
1157 - private function fetch_remote_file( $url, $post ) {
1158 - // Extract the file name from the URL.
1159 - $file_name = basename( parse_url( $url, PHP_URL_PATH ) );
1222 + private function fetch_remote_file( $url, $new_file, $uploads ) {
1223 + // Extract the file name from the new_file.
1224 + $file_name = basename( $new_file );
1160 1225
1161 - if ( ! $file_name ) {
1162 - $file_name = md5( $url );
1163 - }
1164 -
1165 1226 // Include the file for the download_url function
1166 1227 if(!function_exists('wp_tempnam')) {
1167 1228 require_once ABSPATH . 'wp-admin/includes/file.php';
1168 1229 }
@@ -1172,17 +1233,24 @@
1172 1233 return new WP_Error( 'import_no_file', esc_html__( 'Could not create temporary file.', 'elementor' ) );
1173 1234 }
1174 1235
1175 1236 // Fetch the remote URL and write it to the placeholder file.
1176 - $remote_response = wp_safe_remote_get( $url, [
1177 - 'timeout' => 300,
1178 - 'stream' => true,
1179 - 'filename' => $tmp_file_name,
1180 - 'headers' => [
1181 - 'Accept-Encoding' => 'identity',
1182 - ]
1183 - ] );
1237 + $attempt = 0;
1238 + $retry_count = 3;
1239 + $remote_response = null;
1240 + do {
1241 + $remote_response = wp_safe_remote_get( $url, [
1242 + 'timeout' => 300,
1243 + 'stream' => true,
1244 + 'filename' => $tmp_file_name,
1245 + 'headers' => [
1246 + 'Accept-Encoding' => 'identity',
1247 + ]
1248 + ] );
1249 + $attempt++;
1250 + } while (is_wp_error( $remote_response ) && $attempt < $retry_count);
1184 1251
1252 +
1185 1253 if ( is_wp_error( $remote_response ) ) {
1186 1254 @unlink( $tmp_file_name );
1187 1255
1188 1256 return new WP_Error( 'import_file_error', sprintf( /* translators: 1: WordPress error message, 2: WordPress error code. */ esc_html__( 'Request failed due to an error: %1$s (%2$s)', 'elementor' ), esc_html( $remote_response->get_error_message() ), esc_html( $remote_response->get_error_code() ) ) );
@@ -1192,10 +1260,8 @@
1192 1260
1193 1261 // Make sure the fetch was successful.
1194 1262 if ( 200 !== $remote_response_code ) {
1195 1263 @unlink( $tmp_file_name );
1196 - Helper::log($url);
1197 - Helper::log($remote_response);
1198 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 ) ) );
1199 1265 }
1200 1266
1201 1267 $headers = wp_remote_retrieve_headers( $remote_response );
@@ -1261,16 +1327,8 @@
1261 1327 if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
1262 1328 return new WP_Error( 'import_file_error', esc_html__( 'Sorry, this file type is not permitted for security reasons.', 'elementor' ) );
1263 1329 }
1264 1330
1265 - $uploads = wp_upload_dir( $post['upload_date'] );
1266 - if ( ! ( $uploads && false === $uploads['error'] ) ) {
1267 - return new WP_Error( 'upload_dir_error', $uploads['error'] );
1268 - }
1269 -
1270 - // Move the file to the uploads dir.
1271 - $file_name = wp_unique_filename( $uploads['path'], $file_name );
1272 - $new_file = $uploads['path'] . "/$file_name";
1273 1331 $move_new_file = copy( $tmp_file_name, $new_file );
1274 1332
1275 1333 if ( ! $move_new_file ) {
1276 1334 @unlink( $tmp_file_name );
@@ -1291,9 +1349,8 @@
1291 1349 ];
1292 1350
1293 1351 // Keep track of the old and new urls so we can substitute them later.
1294 1352 $this->url_remap[ $url ] = $upload['url'];
1295 - $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1296 1353 // Keep track of the destination if the remote url is redirected somewhere else.
1297 1354 if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] !== $url ) {
1298 1355 $this->url_remap[ $headers['x-final-location'] ] = $upload['url'];
1299 1356 }
@@ -1333,9 +1390,9 @@
1333 1390 );
1334 1391
1335 1392 if ( $post_id ) {
1336 1393 self::$_replace_image_ids[ $hash ] = $post_id;
1337 - return $post_id;
1394 + return (int) $post_id;
1338 1395 }
1339 1396
1340 1397 return false;
1341 1398 }
@@ -1340,69 +1397,92 @@
1340 1397 return false;
1341 1398 }
1342 1399
1343 1400
1344 - public function extract_sizes($sizes) {
1345 - return array_reduce($sizes, function($carry, $url) {
1346 - if (preg_match('/-((\d+)x(\d+))\./', $url, $matches)) {
1347 - $carry[$matches[1]] = $url;
1348 - }
1349 - return $carry;
1350 - }, []);
1351 - }
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);
1352 1404
1353 - public function size_exists_in_metadata($size_dimension, $metadata) {
1354 - foreach ($metadata['sizes'] as $size => $size_info) {
1355 - if (strpos($size_info['file'], $size_dimension) !== false) {
1356 - return true;
1357 - }
1358 - }
1359 - return false;
1360 - }
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 + }
1361 1424
1362 - public function create_unique_destination_file($upload_file, $size_dimension) {
1363 - $destination_file = preg_replace('/(\.[^.]+)$/', '-' . $size_dimension . '$1', $upload_file);
1364 - $destination_dir = pathinfo($destination_file, PATHINFO_DIRNAME);
1365 - $destination_filename = pathinfo($destination_file, PATHINFO_BASENAME);
1366 - $unique_filename = wp_unique_filename($destination_dir, $destination_filename);
1367 - return $destination_dir . '/' . $unique_filename;
1368 - }
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 + }
1369 1431
1370 - public function create_size_array($destination_file, $size_dimension) {
1371 - list($width, $height) = explode('x', $size_dimension);
1372 - return array(
1373 - 'file' => basename($destination_file),
1374 - 'width' => $width,
1375 - 'height' => $height,
1376 - 'mime-type' => wp_check_filetype($destination_file)['type'],
1377 - 'filesize' => filesize($destination_file),
1378 - 'resized' => false,
1379 - );
1380 - }
1432 + $resized = $editor->resize($size['width'], $size['height'], true);
1433 + if (is_wp_error($resized)) {
1434 + return $resized;
1435 + }
1381 1436
1382 - public function import_sizes($sizes, $metadata, $upload, $post_id) {
1383 - if (!empty($sizes)) {
1384 - 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 + }
1385 1441
1386 - $size_dimensions = $this->extract_sizes($sizes);
1387 - $size_dimensions = array_filter($size_dimensions);
1442 + return $saved;
1443 + }
1388 1444
1389 - foreach ($size_dimensions as $size_dimension => $__url) {
1390 - if (!$this->size_exists_in_metadata($size_dimension, $metadata)) {
1391 - $unique_destination_file = $this->create_unique_destination_file($upload['file'], $size_dimension);
1392 - $missing_size = $this->import_size($__url, $unique_destination_file);
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 + }
1393 1455
1394 - if($missing_size && !is_wp_error($missing_size)) {
1395 - $metadata['sizes'][$size_dimension] = $this->create_size_array($unique_destination_file, $size_dimension);
1396 - do_action( 'templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension );
1397 - }
1398 - }
1399 - }
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'];
1400 1461
1401 - }
1402 - return $metadata;
1403 - }
1462 + $destination_file = $directory . '/' . $filename . '-' . $size['width'] . 'x' . $size['height'] . '.' . $extension;
1404 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 +
1405 1485 /**
1406 1486 * Attempt to associate posts and menu items with previously missing parents
1407 1487 *
1408 1488 * An imported post's parent may not have been imported when it was first created
@@ -1520,9 +1600,9 @@
1520 1600 */
1521 1601 private function is_valid_meta_key( $key ) {
1522 1602 // Skip attachment metadata since we'll regenerate it from scratch.
1523 1603 // Skip _edit_lock as not relevant for import
1524 - 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' ] ) ) {
1525 1605 return false;
1526 1606 }
1527 1607
1528 1608 return $key;
@@ -1588,10 +1668,23 @@
1588 1668 */
1589 1669 public function __construct( $file, array $args = [] ) {
1590 1670 parent::__construct();
1591 1671
1592 - $this->requested_file_path = $file;
1593 - $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 + }
1594 1687
1595 1688 if ( ! empty( $this->args['fetch_attachments'] ) ) {
1596 1689 $this->fetch_attachments = true;
1597 1690 }