PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.2.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.2.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 +273 -117 3.0.3 → 3.2.2 View file →
@@ -55,8 +55,12 @@
55 55 /**
56 56 * @var string
57 57 */
58 58 private $requested_file_path;
59 + /**
60 + * @var string
61 + */
62 + private $import_data_key;
59 63
60 64 /**
61 65 * @var array
62 66 */
@@ -62,8 +66,13 @@
62 66 */
63 67 private $args;
64 68
65 69 /**
70 + * @var FullSiteImport
71 + */
72 + private $origin;
73 +
74 + /**
66 75 * @var array
67 76 */
68 77 private $output = [
69 78 'status' => 'failed',
@@ -343,8 +352,30 @@
343 352 }
344 353 }
345 354 }
346 355
356 + private function retrieve_attributes($attributes){
357 + $params = $this->origin->get_request_params();
358 + $attr_values = $params["imported_data"][$this->import_data_key] ?? [];
359 + foreach ($attributes as $attribute) {
360 + if(isset($attr_values[$attribute])){
361 + $this->$attribute = $attr_values[$attribute];
362 + }
363 + }
364 + }
365 +
366 + private function backup_attributes($attributes){
367 + $params = $this->origin->get_request_params();
368 + $attr_values = $params["imported_data"][$this->import_data_key] ?? [];
369 +
370 + foreach ($attributes as $attribute) {
371 + if(isset($this->$attribute)){
372 + $attr_values[$attribute] = $this->$attribute;
373 + }
374 + }
375 + $this->origin->update_progress( null, [$this->import_data_key => $attr_values]);
376 + }
377 +
347 378 /**
348 379 * Map old author logins to local user IDs based on decisions made
349 380 * in import options form. Can map to an existing user, create a new user
350 381 * or falls back to the current user in case of error with either of the previous
@@ -353,8 +384,20 @@
353 384 if ( ! isset( $this->args['imported_authors'] ) ) {
354 385 return;
355 386 }
356 387
388 + $attributes = [
389 + 'processed_authors',
390 + 'author_mapping',
391 + 'output',
392 + ];
393 +
394 + $processed_templates = $this->origin->get_progress([], $this->import_data_key);
395 + if (!empty($processed_templates)) {
396 + $this->retrieve_attributes($attributes);
397 + return;
398 + }
399 +
357 400 $create_users = apply_filters( 'import_allow_create_users', self::DEFAULT_ALLOW_CREATE_USERS );
358 401
359 402 foreach ( (array) $this->args['imported_authors'] as $i => $old_login ) {
360 403 // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
@@ -409,8 +452,11 @@
409 452 }
410 453 $this->author_mapping[ $santized_old_login ] = (int) get_current_user_id();
411 454 }
412 455 }
456 +
457 + $this->backup_attributes($attributes);
458 + $this->origin->update_progress( true, null, $this->import_data_key );
413 459 }
414 460
415 461 /**
416 462 * Create new terms based on import information
@@ -419,13 +465,26 @@
419 465 *
420 466 * @return array|array[] the ids of succeed/failed imported terms.
421 467 */
422 468 private function process_terms(): array {
423 - $result = [
469 + $params = $this->origin->get_request_params();
470 + $result = $params["imported_data"]["wp_import_terms_" . $this->import_data_key] ?? [
424 471 'succeed' => [],
425 472 'failed' => [],
426 473 ];
427 474
475 + $attributes = [
476 + 'terms',
477 + 'output',
478 + 'mapped_terms_slug',
479 + 'processed_terms',
480 + ];
481 + $processed_templates = $this->origin->get_progress([], $this->import_data_key);
482 + if (!empty($processed_templates)) {
483 + $this->retrieve_attributes($attributes);
484 + return $result;
485 + }
486 +
428 487 $this->terms = apply_filters( 'wp_import_terms', $this->terms );
429 488 if ( empty( $this->terms ) ) {
430 489 return $result;
431 490 }
@@ -505,8 +564,11 @@
505 564 }
506 565
507 566 unset( $this->terms );
508 567
568 + $this->backup_attributes($attributes);
569 + // Add the template to the processed templates and update the session data
570 + $this->origin->update_progress( true, [ "wp_import_terms_" . $this->import_data_key => $result ]);
509 571 return $result;
510 572 }
511 573
512 574 /**
@@ -576,16 +638,38 @@
576 638 *
577 639 * @return array the ids of succeed/failed imported posts.
578 640 */
579 641 private function process_posts(): array {
580 - $result = [
642 + $backup_key = "wp_import_" . $this->import_data_key;
643 + $params = $this->origin->get_request_params();
644 + $result = $params["imported_data"][$backup_key] ?? [
581 645 'succeed' => [],
582 646 'failed' => [],
583 647 ];
584 648
649 + $attributes = [
650 + 'output',
651 + 'menu_item_orphans',
652 + 'processed_menu_items',
653 + 'post_orphans',
654 + 'processed_posts',
655 + 'featured_images',
656 + ];
657 + $this->retrieve_attributes($attributes);
658 +
585 659 $this->posts = apply_filters( 'wp_import_posts', $this->posts );
586 660
587 - foreach ( $this->posts as $post ) {
661 + $processed_templates = $this->origin->get_progress([], $this->import_data_key);
662 +
663 + foreach ( $this->posts as $key => $post ) {
664 + if (in_array($key, $processed_templates)) {
665 + continue;
666 + }
667 +
668 + // Add the template to the processed templates and update the session data
669 + $processed_templates[] = $key;
670 + $this->origin->update_progress( $processed_templates, null, $this->import_data_key);
671 +
588 672 $post = apply_filters( 'wp_import_post_data_raw', $post );
589 673
590 674 if ( ! post_type_exists( $post['post_type'] ) ) {
591 675 /* translators: 1: Post title, 2: Post type. */
@@ -601,8 +685,12 @@
601 685 if ( 'auto-draft' === $post['status'] ) {
602 686 continue;
603 687 }
604 688
689 + if(!empty($post['post_content']) && !empty($post['post_id'])){
690 + $post['post_content'] = Utils::import_and_replace_attachments($post['post_content'], $post['post_id']);
691 + }
692 +
605 693 if ( 'nav_menu_item' === $post['post_type'] ) {
606 694 $result['succeed'] += $this->process_menu_item( $post );
607 695 continue;
608 696 }
@@ -658,9 +746,9 @@
658 746 $postdata = wp_slash( $postdata );
659 747
660 748 if ( 'attachment' === $postdata['post_type'] ) {
661 749 $remote_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid'];
662 -
750 + $attachment_sizes = [];
663 751 // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
664 752 // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
665 753 $postdata['upload_date'] = $post['post_date'];
666 754 if ( isset( $post['postmeta'] ) ) {
@@ -668,14 +756,19 @@
668 756 if ( '_wp_attached_file' === $meta['key'] ) {
669 757 if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) {
670 758 $postdata['upload_date'] = $matches[0];
671 759 }
672 - break;
760 + // break;
673 761 }
762 + else if ( '_wp_attachment_metadata' === $meta['key'] ) {
763 + $attachment_metadata = maybe_unserialize( $meta['value'] );
764 + $attachment_sizes = $attachment_metadata['sizes'] ?? [];
765 + // break;
766 + }
674 767 }
675 768 }
676 769
677 - $post_id = $this->process_attachment( $postdata, $remote_url );
770 + $post_id = $this->process_attachment( $postdata, $remote_url, $attachment_sizes, $original_post_id );
678 771 $comment_post_id = $post_id;
679 772 } else {
680 773 $post_id = wp_insert_post( $postdata, true );
681 774
@@ -696,8 +789,26 @@
696 789 $result['failed'][] = $original_post_id;
697 790
698 791 $this->output['errors'][] = $error;
699 792
793 + if ( 'attachment' === $postdata['post_type'] ) {
794 + do_action( 'templately_import.process_post', $post, $result, $this );
795 + }
796 +
797 + // Add the template to the processed templates and update the session data
798 + $this->backup_attributes($attributes);
799 + $this->origin->update_progress( null, [ $backup_key => $result ], $this->import_data_key);
800 +
801 + // If it's not the last item, send the SSE message and exit
802 + if(Helper::fsi_should_exit() && 'attachment' === $postdata['post_type'] && end($this->posts) !== $post) {
803 + $this->origin->sse_message( [
804 + 'type' => 'continue',
805 + 'action' => 'continue',
806 + 'results' => __METHOD__ . '::' . __LINE__,
807 + 'data' => Helper::fsi_should_exit(),
808 + ] );
809 + exit;
810 + }
700 811 continue;
701 812 }
702 813
703 814 $result['succeed'][ $original_post_id ] = $post_id;
@@ -852,8 +963,24 @@
852 963 }
853 964 }
854 965
855 966 do_action( 'templately_import.process_post', $post, $result, $this );
967 +
968 + // Add the template to the processed templates and update the session data
969 + $this->backup_attributes($attributes);
970 + $this->origin->update_progress( null, [ $backup_key => $result ], $this->import_data_key);
971 +
972 +
973 + // If it's not the last item, send the SSE message and exit
974 + if(Helper::fsi_should_exit() && 'attachment' === $postdata['post_type'] && end($this->posts) !== $post) {
975 + $this->origin->sse_message( [
976 + 'type' => 'continue',
977 + 'action' => 'continue',
978 + 'results' => __METHOD__ . '::' . __LINE__,
979 + 'data' => Helper::fsi_should_exit(),
980 + ] );
981 + exit;
982 + }
856 983 }
857 984
858 985 unset( $this->posts );
859 986
@@ -1039,9 +1166,9 @@
1039 1166 * @param string $url URL to fetch attachment from
1040 1167 *
1041 1168 * @return int|WP_Error Post ID on success, WP_Error otherwise
1042 1169 */
1043 - public function process_attachment( $post, $url, $sizes = [] ) {
1170 + public function process_attachment( $post, $url, $sizes = [], $original_post_id = null ) {
1044 1171 if ( ! $this->fetch_attachments ) {
1045 1172 return new WP_Error( 'attachment_processing_error', esc_html__( 'Fetching attachments is not enabled', 'elementor' ) );
1046 1173 }
1047 1174 if ( ! function_exists( 'wp_crop_image' ) ) {
@@ -1054,8 +1181,13 @@
1054 1181
1055 1182 if($saved_image = $this->get_saved_image($url)){
1056 1183 $this->url_remap[ $url ] = wp_get_attachment_url( $saved_image );
1057 1184 $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension(wp_get_attachment_url( $saved_image ));
1185 +
1186 + $full_size_path = get_attached_file($saved_image);
1187 + $metadata = wp_get_attachment_metadata($saved_image);
1188 + $metadata = $this->import_sizes($sizes, $metadata, $full_size_path, $saved_image);
1189 + wp_update_attachment_metadata( $saved_image, $metadata );
1058 1190 return $saved_image;
1059 1191 }
1060 1192
1061 1193 // Check if the URL is from the wp-includes/images directory
@@ -1068,9 +1200,28 @@
1068 1200
1069 1201 return $updated_url;
1070 1202 }
1071 1203
1072 - $upload = $this->fetch_remote_file( $url, $post );
1204 + $upload_dir = wp_upload_dir( $post['upload_date'] );
1205 + if ( ! ( $upload_dir && false === $upload_dir['error'] ) ) {
1206 + return new WP_Error( 'upload_dir_error', $upload_dir['error'] );
1207 + }
1208 +
1209 + // Move the file to the uploads dir.
1210 + $file_name = basename( parse_url( $url, PHP_URL_PATH ) );
1211 + $file_name = wp_unique_filename( $upload_dir['path'], $file_name );
1212 + $dest_file = $upload_dir['path'] . "/$file_name";
1213 + $start = microtime(true);
1214 +
1215 + $upload = apply_filters( 'templately_import_copy_attachment', null, $original_post_id, $dest_file, $upload_dir );
1216 + if ( null === $upload ) {
1217 + $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir );
1218 + }
1219 +
1220 + $end = microtime(true);
1221 + $duration = $end - $start;
1222 + error_log('Duration: ' . $duration);
1223 +
1073 1224 if ( is_wp_error( $upload ) ) {
1074 1225 return $upload;
1075 1226 }
1076 1227
@@ -1080,13 +1231,18 @@
1080 1231 } else {
1081 1232 return new WP_Error( 'attachment_processing_error', esc_html__( 'Invalid file type', 'elementor' ) );
1082 1233 }
1083 1234
1235 + $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1084 1236 $post['guid'] = $upload['url'];
1085 1237
1086 1238 // As per wp-admin/includes/upload.php.
1087 1239 $post_id = wp_insert_attachment( $post, $upload['file'] );
1088 1240
1241 + if(is_wp_error($post_id)){
1242 + return $post_id;
1243 + }
1244 +
1089 1245 $this->update_post_meta( $post_id );
1090 1246
1091 1247 // Generate attachment metadata
1092 1248 $metadata = wp_generate_attachment_metadata( $post_id, $upload['file'] );
@@ -1093,9 +1249,9 @@
1093 1249
1094 1250 // error_log('Metadata: ' . print_r($metadata, true));
1095 1251
1096 1252 // For gutenberg pages
1097 - $metadata = $this->import_sizes($sizes, $metadata, $upload, $post_id);
1253 + $metadata = $this->import_sizes($sizes, $metadata, $upload['file'], $post_id);
1098 1254
1099 1255 // error_log('Metadata: ' . print_r($metadata, true));
1100 1256 wp_update_attachment_metadata( $post_id, $metadata );
1101 1257
@@ -1118,35 +1274,8 @@
1118 1274
1119 1275 return $parts['dirname'] . '/' . $name;
1120 1276 }
1121 1277
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 1278 /**
1150 1279 * Attempt to download a remote file attachment
1151 1280 *
1152 1281 * @param string $url URL of item to fetch
@@ -1153,16 +1282,12 @@
1153 1282 * @param array $post Attachment details
1154 1283 *
1155 1284 * @return array|WP_Error Local file location details on success, WP_Error otherwise
1156 1285 */
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 ) );
1286 + private function fetch_remote_file( $url, $new_file, $uploads ) {
1287 + // Extract the file name from the new_file.
1288 + $file_name = basename( $new_file );
1160 1289
1161 - if ( ! $file_name ) {
1162 - $file_name = md5( $url );
1163 - }
1164 -
1165 1290 // Include the file for the download_url function
1166 1291 if(!function_exists('wp_tempnam')) {
1167 1292 require_once ABSPATH . 'wp-admin/includes/file.php';
1168 1293 }
@@ -1172,17 +1297,24 @@
1172 1297 return new WP_Error( 'import_no_file', esc_html__( 'Could not create temporary file.', 'elementor' ) );
1173 1298 }
1174 1299
1175 1300 // 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 - ] );
1301 + $attempt = 0;
1302 + $retry_count = 3;
1303 + $remote_response = null;
1304 + do {
1305 + $remote_response = wp_safe_remote_get( $url, [
1306 + 'timeout' => 300,
1307 + 'stream' => true,
1308 + 'filename' => $tmp_file_name,
1309 + 'headers' => [
1310 + 'Accept-Encoding' => 'identity',
1311 + ]
1312 + ] );
1313 + $attempt++;
1314 + } while (is_wp_error( $remote_response ) && $attempt < $retry_count);
1184 1315
1316 +
1185 1317 if ( is_wp_error( $remote_response ) ) {
1186 1318 @unlink( $tmp_file_name );
1187 1319
1188 1320 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 +1324,8 @@
1192 1324
1193 1325 // Make sure the fetch was successful.
1194 1326 if ( 200 !== $remote_response_code ) {
1195 1327 @unlink( $tmp_file_name );
1196 - Helper::log($url);
1197 - Helper::log($remote_response);
1198 1328 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 1329 }
1200 1330
1201 1331 $headers = wp_remote_retrieve_headers( $remote_response );
@@ -1261,16 +1391,8 @@
1261 1391 if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
1262 1392 return new WP_Error( 'import_file_error', esc_html__( 'Sorry, this file type is not permitted for security reasons.', 'elementor' ) );
1263 1393 }
1264 1394
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 1395 $move_new_file = copy( $tmp_file_name, $new_file );
1274 1396
1275 1397 if ( ! $move_new_file ) {
1276 1398 @unlink( $tmp_file_name );
@@ -1291,9 +1413,8 @@
1291 1413 ];
1292 1414
1293 1415 // Keep track of the old and new urls so we can substitute them later.
1294 1416 $this->url_remap[ $url ] = $upload['url'];
1295 - $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1296 1417 // Keep track of the destination if the remote url is redirected somewhere else.
1297 1418 if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] !== $url ) {
1298 1419 $this->url_remap[ $headers['x-final-location'] ] = $upload['url'];
1299 1420 }
@@ -1333,9 +1454,9 @@
1333 1454 );
1334 1455
1335 1456 if ( $post_id ) {
1336 1457 self::$_replace_image_ids[ $hash ] = $post_id;
1337 - return $post_id;
1458 + return (int) $post_id;
1338 1459 }
1339 1460
1340 1461 return false;
1341 1462 }
@@ -1340,69 +1461,92 @@
1340 1461 return false;
1341 1462 }
1342 1463
1343 1464
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 - }
1465 + public function import_sizes($sizes, $metadata, $full_size_file, $post_id) {
1466 + if (!empty($sizes)) {
1467 + do_action('templately_import.finalize_gutenberg_attachment', $post_id);
1352 1468
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 - }
1469 + foreach ($sizes as $size_name => $size) {
1470 + $size_dimension = $size['width'] . 'x' . $size['height'];
1471 + $size_name = !is_string($size_name) ? $size_dimension : $size_name;
1472 + $unique_destination_file = $this->create_unique_destination_file($full_size_file, $size);
1473 + // check non cropped sizes. with dynamic height
1474 + if (!$this->size_exists_in_metadata(basename($unique_destination_file), $metadata)) {
1475 + if ($unique_destination_file) {
1476 + $missing_size = $this->generate_missing_size_from_full($full_size_file, $unique_destination_file, $size);
1477 + if ($missing_size && !is_wp_error($missing_size)) {
1478 + unset($missing_size['path']);
1479 + $metadata['sizes'][$size_name] = $missing_size;
1480 + do_action('templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension);
1481 + }
1482 + }
1483 + }
1484 + }
1485 + }
1486 + return $metadata;
1487 + }
1361 1488
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 - }
1489 + public function generate_missing_size_from_full($full_size_file, $destination_file, $size) {
1490 + // Generate the missing size from the full-size image
1491 + $editor = wp_get_image_editor($full_size_file);
1492 + if (is_wp_error($editor)) {
1493 + return $editor;
1494 + }
1369 1495
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 - }
1496 + $resized = $editor->resize($size['width'], $size['height'], true);
1497 + if (is_wp_error($resized)) {
1498 + return $resized;
1499 + }
1381 1500
1382 - public function import_sizes($sizes, $metadata, $upload, $post_id) {
1383 - if (!empty($sizes)) {
1384 - do_action( 'templately_import.finalize_gutenberg_attachment', $post_id );
1501 + $saved = $editor->save($destination_file);
1502 + if (is_wp_error($saved)) {
1503 + return $saved;
1504 + }
1385 1505
1386 - $size_dimensions = $this->extract_sizes($sizes);
1387 - $size_dimensions = array_filter($size_dimensions);
1506 + return $saved;
1507 + }
1388 1508
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);
1509 + public function size_exists_in_metadata($size_file, $metadata) {
1510 + if (isset($metadata['sizes']) && is_array($metadata['sizes'])) {
1511 + foreach ($metadata['sizes'] as $size => $size_info) {
1512 + if ($size_info['file'] == $size_file) {
1513 + return true;
1514 + }
1515 + }
1516 + }
1517 + return false;
1518 + }
1393 1519
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 - }
1520 + public function create_unique_destination_file($full_size_file, $size) {
1521 + $pathinfo = pathinfo($full_size_file);
1522 + $directory = $pathinfo['dirname'];
1523 + $filename = $pathinfo['filename'];
1524 + $extension = $pathinfo['extension'];
1400 1525
1401 - }
1402 - return $metadata;
1403 - }
1526 + $destination_file = $directory . '/' . $filename . '-' . $size['width'] . 'x' . $size['height'] . '.' . $extension;
1404 1527
1528 + // Skip if the file already exists
1529 + if (file_exists($destination_file)) {
1530 + // return false;
1531 + }
1532 +
1533 + return $destination_file;
1534 + }
1535 +
1536 + public function create_size_array($destination_file, $size_dimension) {
1537 + list($width, $height) = explode('x', $size_dimension);
1538 + return array(
1539 + 'file' => basename($destination_file),
1540 + 'width' => $width,
1541 + 'height' => $height,
1542 + 'mime-type' => wp_check_filetype($destination_file)['type'],
1543 + 'filesize' => filesize($destination_file),
1544 + 'resized' => false,
1545 + );
1546 + }
1547 +
1548 +
1405 1549 /**
1406 1550 * Attempt to associate posts and menu items with previously missing parents
1407 1551 *
1408 1552 * An imported post's parent may not have been imported when it was first created
@@ -1520,9 +1664,9 @@
1520 1664 */
1521 1665 private function is_valid_meta_key( $key ) {
1522 1666 // Skip attachment metadata since we'll regenerate it from scratch.
1523 1667 // Skip _edit_lock as not relevant for import
1524 - if ( in_array( $key, [ '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ] ) ) {
1668 + if ( in_array( $key, [ '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock', '_elementor_source_image_hash', 'sm_cloud' ] ) ) {
1525 1669 return false;
1526 1670 }
1527 1671
1528 1672 return $key;
@@ -1588,10 +1732,22 @@
1588 1732 */
1589 1733 public function __construct( $file, array $args = [] ) {
1590 1734 parent::__construct();
1591 1735
1592 - $this->requested_file_path = $file;
1593 1736 $this->args = $args;
1737 +
1738 + if ( ! empty( $args['json'] ) ) {
1739 + $this->json = $args['json'];
1740 + }
1741 +
1742 + if ( ! empty( $args['origin'] ) ) {
1743 + $this->origin = $args['origin'];
1744 + }
1745 +
1746 + if ( ! empty( $file ) ) {
1747 + $this->requested_file_path = $file;
1748 + $this->import_data_key = 'wp_importer_attributes_' . md5($this->requested_file_path);
1749 + }
1594 1750
1595 1751 if ( ! empty( $this->args['fetch_attachments'] ) ) {
1596 1752 $this->fetch_attachments = true;
1597 1753 }