PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.2
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 3.1.10 All 111 releases
← All changes | includes/Core/Importer/WPImport.php +89 -301 3.6.13.1.2 View file →
@@ -2,9 +2,8 @@
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;
7 6 use Templately\Core\Importer\Utils\Utils;
8 7 use Templately\Utils\Helper;
9 8 use WP_Error;
10 9 use WP_Importer;
@@ -48,11 +47,8 @@
48 47 }
49 48 }
50 49
51 50 class WPImport extends WP_Importer {
52 - use LogHelper;
53 - use Loop;
54 -
55 51 const DEFAULT_BUMP_REQUEST_TIMEOUT = 60;
56 52 const DEFAULT_ALLOW_CREATE_USERS = true;
57 53 const DEFAULT_IMPORT_ATTACHMENT_SIZE_LIMIT = 0; // 0 = unlimited.
58 54
@@ -59,12 +55,8 @@
59 55 /**
60 56 * @var string
61 57 */
62 58 private $requested_file_path;
63 - /**
64 - * @var string
65 - */
66 - private $import_data_key;
67 59
68 60 /**
69 61 * @var array
70 62 */
@@ -70,13 +62,8 @@
70 62 */
71 63 private $args;
72 64
73 65 /**
74 - * @var FullSiteImport
75 - */
76 - private $origin;
77 -
78 - /**
79 66 * @var array
80 67 */
81 68 private $output = [
82 69 'status' => 'failed',
@@ -87,18 +74,8 @@
87 74 * WXR attachment ID
88 75 */
89 76 private $id;
90 77
91 - /**
92 - * @var mixed
93 - */
94 - private $json;
95 -
96 - /**
97 - * @var string
98 - */
99 - private $session_id;
100 -
101 78 // Information to import from WXR file.
102 79 private $version;
103 80 private $authors = [];
104 81 public $posts = [];
@@ -133,25 +110,8 @@
133 110 private $terms_meta = [];
134 111
135 112 public static $_replace_image_ids = [];
136 113
137 -
138 - public $backup_attributes = [
139 - 'output',
140 - 'url_remap',
141 - '_replace_image_ids',
142 - 'menu_item_orphans',
143 - 'processed_menu_items',
144 - 'post_orphans',
145 - 'processed_posts',
146 - 'featured_images',
147 - 'mapped_terms_slug',
148 - 'processed_terms',
149 - 'processed_authors',
150 - 'author_mapping',
151 - ];
152 -
153 -
154 114 /**
155 115 * Parses filename from a Content-Disposition header value.
156 116 *
157 117 * As per RFC6266:
@@ -327,10 +287,9 @@
327 287
328 288 wp_defer_term_counting( true );
329 289 wp_defer_comment_counting( true );
330 290
331 - do_action( 'import_start' );
332 - do_action( 'templately_import_start', $this );
291 + do_action( 'import_start', $this );
333 292
334 293 return true;
335 294 }
336 295
@@ -394,14 +353,8 @@
394 353 if ( ! isset( $this->args['imported_authors'] ) ) {
395 354 return;
396 355 }
397 356
398 -
399 - $processed_templates = $this->get_loop_result([], $this->import_data_key);
400 - if (!empty($processed_templates)) {
401 - return;
402 - }
403 -
404 357 $create_users = apply_filters( 'import_allow_create_users', self::DEFAULT_ALLOW_CREATE_USERS );
405 358
406 359 foreach ( (array) $this->args['imported_authors'] as $i => $old_login ) {
407 360 // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
@@ -456,10 +409,8 @@
456 409 }
457 410 $this->author_mapping[ $santized_old_login ] = (int) get_current_user_id();
458 411 }
459 412 }
460 -
461 - $this->set_loop_result( true, $this->import_data_key );
462 413 }
463 414
464 415 /**
465 416 * Create new terms based on import information
@@ -473,14 +424,8 @@
473 424 'succeed' => [],
474 425 'failed' => [],
475 426 ];
476 427
477 - $processed_templates = $this->get_loop_result([], "wp_import_terms_" . $this->import_data_key);
478 - if (!empty($processed_templates)) {
479 - $result = $this->get_loop_result([], "wp_import_terms_" . $this->import_data_key);
480 - return $result;
481 - }
482 -
483 428 $this->terms = apply_filters( 'wp_import_terms', $this->terms );
484 429 if ( empty( $this->terms ) ) {
485 430 return $result;
486 431 }
@@ -560,10 +505,8 @@
560 505 }
561 506
562 507 unset( $this->terms );
563 508
564 - // Add the template to the processed templates and update the session data
565 - $this->set_loop_result( $result, "wp_import_terms_" . $this->import_data_key);
566 509 return $result;
567 510 }
568 511
569 512 /**
@@ -633,59 +576,42 @@
633 576 *
634 577 * @return array the ids of succeed/failed imported posts.
635 578 */
636 579 private function process_posts(): array {
637 - $backup_key = "wp_import_post_" . $this->import_data_key;
580 + $result = [
581 + 'succeed' => [],
582 + 'failed' => [],
583 + ];
638 584
639 585 $this->posts = apply_filters( 'wp_import_posts', $this->posts );
640 586
641 - $results = $this->loop( $this->posts, function($key, $post, $result ) {
587 + foreach ( $this->posts as $post ) {
588 + $post = apply_filters( 'wp_import_post_data_raw', $post );
642 589
643 - $result = !empty($result) ? $result : [
644 - 'succeed' => [],
645 - 'failed' => [],
646 - ];
647 -
648 - $original_post_id = $post['post_id'];
649 - $post = apply_filters( 'wp_import_post_data_raw', $post, $this );
650 -
651 - if(empty($post)){
652 - return $result;
653 - }
654 -
655 - if(!is_array($post) && is_numeric($post)){
656 - $result['succeed'][ $original_post_id ] = $post;
657 - return $result;
658 - }
659 -
660 590 if ( ! post_type_exists( $post['post_type'] ) ) {
661 591 /* translators: 1: Post title, 2: Post type. */
662 592 $this->output['errors'][] = sprintf( esc_html__( 'Failed to import %1$s: Invalid post type %2$s', 'elementor' ), $post['post_title'], $post['post_type'] );
663 593 do_action( 'wp_import_post_exists', $post );
664 - return $result;
594 + continue;
665 595 }
666 596
667 597 if ( isset( $this->processed_posts[ $post['post_id'] ] ) && ! empty( $post['post_id'] ) ) {
668 - return $result;
598 + continue;
669 599 }
670 600
671 601 if ( 'auto-draft' === $post['status'] ) {
672 - return $result;
602 + continue;
673 603 }
674 604
675 - if(!empty($post['post_content']) && !empty($post['post_id'])){
676 - $post['post_content'] = Utils::import_and_replace_attachments($post['post_content'], $post['post_id']);
677 - }
678 -
679 605 if ( 'nav_menu_item' === $post['post_type'] ) {
680 606 $result['succeed'] += $this->process_menu_item( $post );
681 - return $result;
607 + continue;
682 608 }
683 609
684 610 if ( 'wp_navigation' === $post['post_type'] ) {
685 611 $processed = $this->process_navigation( $post );
686 612 if ( ! $processed ) {
687 - return $result;
613 + continue;
688 614 }
689 615 }
690 616
691 617 $post_type_object = get_post_type_object( $post['post_type'] );
@@ -725,12 +651,9 @@
725 651 'post_type' => $post['post_type'],
726 652 'post_password' => $post['post_password'],
727 653 ];
728 654
729 - if(isset($post['original_attachment_url'])){
730 - $postdata['original_attachment_url'] = $post['original_attachment_url'];
731 - }
732 -
655 + $original_post_id = $post['post_id'];
733 656 $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
734 657
735 658 $postdata = wp_slash( $postdata );
736 659
@@ -735,9 +658,9 @@
735 658 $postdata = wp_slash( $postdata );
736 659
737 660 if ( 'attachment' === $postdata['post_type'] ) {
738 661 $remote_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid'];
739 - $attachment_sizes = [];
662 +
740 663 // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
741 664 // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
742 665 $postdata['upload_date'] = $post['post_date'];
743 666 if ( isset( $post['postmeta'] ) ) {
@@ -745,19 +668,14 @@
745 668 if ( '_wp_attached_file' === $meta['key'] ) {
746 669 if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) {
747 670 $postdata['upload_date'] = $matches[0];
748 671 }
749 - // break;
672 + break;
750 673 }
751 - else if ( '_wp_attachment_metadata' === $meta['key'] ) {
752 - $attachment_metadata = maybe_unserialize( $meta['value'] );
753 - $attachment_sizes = $attachment_metadata['sizes'] ?? [];
754 - // break;
755 - }
756 674 }
757 675 }
758 676
759 - $post_id = $this->process_attachment( $postdata, $remote_url, $attachment_sizes, $original_post_id );
677 + $post_id = $this->process_attachment( $postdata, $remote_url );
760 678 $comment_post_id = $post_id;
761 679 } else {
762 680 $post_id = wp_insert_post( $postdata, true );
763 681
@@ -778,13 +696,9 @@
778 696 $result['failed'][] = $original_post_id;
779 697
780 698 $this->output['errors'][] = $error;
781 699
782 - if ( 'attachment' === $postdata['post_type'] ) {
783 - do_action( 'templately_import.process_post', $post, $result, $this );
784 - }
785 -
786 - return $result;
700 + continue;
787 701 }
788 702
789 703 $result['succeed'][ $original_post_id ] = $post_id;
790 704
@@ -938,15 +852,13 @@
938 852 }
939 853 }
940 854
941 855 do_action( 'templately_import.process_post', $post, $result, $this );
856 + }
942 857
943 - return $result;
944 - }, $backup_key); //, true
945 -
946 858 unset( $this->posts );
947 859
948 - return $results;
860 + return $result;
949 861 }
950 862
951 863 /**
952 864 * Attempt to create a new menu item from import data
@@ -1127,9 +1039,9 @@
1127 1039 * @param string $url URL to fetch attachment from
1128 1040 *
1129 1041 * @return int|WP_Error Post ID on success, WP_Error otherwise
1130 1042 */
1131 - public function process_attachment( $post, $url, $sizes = [], $original_post_id = null ) {
1043 + public function process_attachment( $post, $url, $sizes = [] ) {
1132 1044 if ( ! $this->fetch_attachments ) {
1133 1045 return new WP_Error( 'attachment_processing_error', esc_html__( 'Fetching attachments is not enabled', 'elementor' ) );
1134 1046 }
1135 1047 if ( ! function_exists( 'wp_crop_image' ) ) {
@@ -1140,24 +1052,10 @@
1140 1052 $url = rtrim( $this->base_url, '/' ) . $url;
1141 1053 }
1142 1054
1143 1055 if($saved_image = $this->get_saved_image($url)){
1144 - // $this->url_remap[ $url ] = wp_get_attachment_url( $saved_image );
1145 - // $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension(wp_get_attachment_url( $saved_image ));
1146 - $upload_url = set_url_scheme( wp_get_attachment_url( $saved_image ) );
1147 - $this->set_url_map($url, $upload_url, true);
1148 -
1149 - if(!empty($post['original_attachment_url']) && !$this->get_saved_image($post['original_attachment_url'])){
1150 - add_post_meta( $saved_image, '_elementor_source_image_hash', sha1( $post['original_attachment_url'] ) );
1151 - self::$_replace_image_ids[ sha1( $post['original_attachment_url'] ) ] = $post_id;
1152 - }
1153 -
1154 - $full_size_path = get_attached_file($saved_image);
1155 - $metadata = wp_get_attachment_metadata($saved_image);
1156 - $updated_metadata = $this->import_sizes($sizes, $metadata, $full_size_path, $saved_image);
1157 - if ($updated_metadata !== false) {
1158 - wp_update_attachment_metadata( $saved_image, $updated_metadata );
1159 - }
1056 + $this->url_remap[ $url ] = wp_get_attachment_url( $saved_image );
1057 + $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension(wp_get_attachment_url( $saved_image ));
1160 1058 return $saved_image;
1161 1059 }
1162 1060
1163 1061 // Check if the URL is from the wp-includes/images directory
@@ -1179,24 +1077,10 @@
1179 1077 // Move the file to the uploads dir.
1180 1078 $file_name = basename( parse_url( $url, PHP_URL_PATH ) );
1181 1079 $file_name = wp_unique_filename( $upload_dir['path'], $file_name );
1182 1080 $dest_file = $upload_dir['path'] . "/$file_name";
1183 - $start = microtime(true);
1081 + $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir );
1184 1082
1185 - $upload = apply_filters( 'templately_import_copy_attachment', null, $original_post_id, $dest_file, $upload_dir );
1186 - if ( null === $upload ) {
1187 - $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir );
1188 - }
1189 -
1190 - // Normalize URL scheme to match the current site scheme (fixes HTTP URLs on HTTPS multisites).
1191 - if ( ! is_wp_error( $upload ) && ! empty( $upload['url'] ) ) {
1192 - $upload['url'] = set_url_scheme( $upload['url'] );
1193 - }
1194 -
1195 - $end = microtime(true);
1196 - $duration = $end - $start;
1197 - error_log('Duration: ' . $duration);
1198 -
1199 1083 if ( is_wp_error( $upload ) ) {
1200 1084 return $upload;
1201 1085 }
1202 1086
@@ -1206,19 +1090,14 @@
1206 1090 } else {
1207 1091 return new WP_Error( 'attachment_processing_error', esc_html__( 'Invalid file type', 'elementor' ) );
1208 1092 }
1209 1093
1210 - // $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1211 - // $this->set_url_map($post['guid'], $upload['url']);
1094 + $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1212 1095 $post['guid'] = $upload['url'];
1213 1096
1214 1097 // As per wp-admin/includes/upload.php.
1215 1098 $post_id = wp_insert_attachment( $post, $upload['file'] );
1216 1099
1217 - if(is_wp_error($post_id)){
1218 - return $post_id;
1219 - }
1220 -
1221 1100 $this->update_post_meta( $post_id );
1222 1101
1223 1102 // Generate attachment metadata
1224 1103 $metadata = wp_generate_attachment_metadata( $post_id, $upload['file'] );
@@ -1225,46 +1104,21 @@
1225 1104
1226 1105 // error_log('Metadata: ' . print_r($metadata, true));
1227 1106
1228 1107 // For gutenberg pages
1229 - $updated_metadata = $this->import_sizes($sizes, $metadata, $upload['file'], $post_id);
1108 + $metadata = $this->import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id);
1230 1109
1231 1110 // error_log('Metadata: ' . print_r($metadata, true));
1232 - if ($updated_metadata !== false) {
1233 - wp_update_attachment_metadata( $post_id, $updated_metadata );
1234 - } else {
1235 - wp_update_attachment_metadata( $post_id, $metadata );
1236 - }
1111 + wp_update_attachment_metadata( $post_id, $metadata );
1237 1112
1238 1113 // @todo: add missing image sizes
1239 - if(defined('TEMPLATELY_DEV') && TEMPLATELY_DEV){
1240 - update_post_meta( $post_id, '_templately_original_id', $original_post_id );
1241 - update_post_meta( $post_id, '_templately_original_url', $url );
1242 1114
1243 - if(!empty($post['original_attachment_url'])){
1244 - update_post_meta( $post_id, '_templately_demo_url', $post['original_attachment_url'] );
1245 - }
1246 - }
1247 -
1248 1115 update_post_meta( $post_id, '_elementor_source_image_hash', sha1( $url ) );
1249 1116 self::$_replace_image_ids[ sha1( $url ) ] = $post_id;
1250 1117
1251 - // add a second hash for original demo url
1252 - // if user is replacing image.
1253 - // so we can also match original demo url to new image
1254 - if(!empty($post['original_attachment_url'])){
1255 - $hash_meta_id = add_post_meta( $post_id, '_elementor_source_image_hash', sha1( $post['original_attachment_url'] ) );
1256 - add_post_meta( $post_id, '_templately_image_hash_meta_id', $hash_meta_id );
1257 - self::$_replace_image_ids[ sha1( $post['original_attachment_url'] ) ] = $post_id;
1258 - }
1259 -
1260 1118 // Remap resized image URLs, works by stripping the extension and remapping the URL stub.
1261 1119 if ( preg_match( '!^image/!', $info['type'] ) ) {
1262 - // $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension($upload['url']);
1263 - $this->set_url_map($url, $upload['url'], true);
1264 - if(!empty($post['original_attachment_url'])){
1265 - $this->set_url_map($post['original_attachment_url'], $upload['url'], true);
1266 - }
1120 + $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension($upload['url']);
1267 1121 }
1268 1122
1269 1123 return $post_id;
1270 1124 }
@@ -1275,17 +1129,8 @@
1275 1129
1276 1130 return $parts['dirname'] . '/' . $name;
1277 1131 }
1278 1132
1279 - public function set_url_map($original_url, $new_url, $remove_extension = false){
1280 - $this->url_remap[ $original_url ] = $new_url;
1281 - if($remove_extension){
1282 - $original_url = $this->remove_extension($original_url);
1283 - $new_url = $this->remove_extension($new_url);
1284 - $this->url_remap[ $original_url ] = $new_url;
1285 - }
1286 - }
1287 -
1288 1133 /**
1289 1134 * Attempt to download a remote file attachment
1290 1135 *
1291 1136 * @param string $url URL of item to fetch
@@ -1334,8 +1179,10 @@
1334 1179
1335 1180 // Make sure the fetch was successful.
1336 1181 if ( 200 !== $remote_response_code ) {
1337 1182 @unlink( $tmp_file_name );
1183 + Helper::log($url);
1184 + Helper::log($remote_response);
1338 1185 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 ) ) );
1339 1186 }
1340 1187
1341 1188 $headers = wp_remote_retrieve_headers( $remote_response );
@@ -1422,12 +1269,12 @@
1422 1269 'error' => false,
1423 1270 ];
1424 1271
1425 1272 // Keep track of the old and new urls so we can substitute them later.
1426 - $this->set_url_map($url, $upload['url']);
1273 + $this->url_remap[ $url ] = $upload['url'];
1427 1274 // Keep track of the destination if the remote url is redirected somewhere else.
1428 1275 if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] !== $url ) {
1429 - $this->set_url_map($headers['x-final-location'], $upload['url']);
1276 + $this->url_remap[ $headers['x-final-location'] ] = $upload['url'];
1430 1277 }
1431 1278
1432 1279 return $upload;
1433 1280 }
@@ -1441,9 +1288,9 @@
1441 1288 * @access private
1442 1289 *
1443 1290 * @param string $url The image URL.
1444 1291 *
1445 - * @return false|int New image ID or false.
1292 + * @return false|array New image ID or false.
1446 1293 */
1447 1294 private function get_saved_image( $url ) {
1448 1295 global $wpdb;
1449 1296
@@ -1464,9 +1311,9 @@
1464 1311 );
1465 1312
1466 1313 if ( $post_id ) {
1467 1314 self::$_replace_image_ids[ $hash ] = $post_id;
1468 - return (int) $post_id;
1315 + return $post_id;
1469 1316 }
1470 1317
1471 1318 return false;
1472 1319 }
@@ -1471,97 +1318,69 @@
1471 1318 return false;
1472 1319 }
1473 1320
1474 1321
1475 - public function import_sizes($sizes, $metadata, $full_size_file, $post_id) {
1476 - $metadata_modified = false;
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 + }
1477 1330
1478 - if (!empty($sizes)) {
1479 - do_action('templately_import.finalize_gutenberg_attachment', $post_id);
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 + }
1480 1339
1481 - foreach ($sizes as $size_name => $size) {
1482 - $size_dimension = $size['width'] . 'x' . $size['height'];
1483 - $size_name = !is_string($size_name) ? $size_dimension : $size_name;
1484 - $unique_destination_file = $this->create_unique_destination_file($full_size_file, $size);
1485 - // check non cropped sizes. with dynamic height
1486 - if (!$this->size_exists_in_metadata(basename($unique_destination_file), $metadata)) {
1487 - if ($unique_destination_file) {
1488 - $missing_size = $this->generate_missing_size_from_full($full_size_file, $unique_destination_file, $size);
1489 - if ($missing_size && !is_wp_error($missing_size)) {
1490 - unset($missing_size['path']);
1491 - $metadata['sizes'][$size_name] = $missing_size;
1492 - $metadata_modified = true;
1493 - do_action('templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension);
1494 - }
1495 - }
1496 - }
1497 - }
1498 - }
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 + }
1499 1347
1500 - // Only return metadata if new sizes were actually added
1501 - return $metadata_modified ? $metadata : false;
1502 - }
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 + }
1503 1359
1504 - public function generate_missing_size_from_full($full_size_file, $destination_file, $size) {
1505 - // Generate the missing size from the full-size image
1506 - $editor = wp_get_image_editor($full_size_file);
1507 - if (is_wp_error($editor)) {
1508 - return $editor;
1509 - }
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 );
1510 1363
1511 - $resized = $editor->resize($size['width'], $size['height'], true);
1512 - if (is_wp_error($resized)) {
1513 - return $resized;
1514 - }
1364 + $size_dimensions = $this->extract_sizes($sizes);
1365 + $size_dimensions = array_filter($size_dimensions);
1515 1366
1516 - $saved = $editor->save($destination_file);
1517 - if (is_wp_error($saved)) {
1518 - return $saved;
1519 - }
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);
1520 1371
1521 - return $saved;
1522 - }
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 + }
1523 1378
1524 - public function size_exists_in_metadata($size_file, $metadata) {
1525 - if (isset($metadata['sizes']) && is_array($metadata['sizes'])) {
1526 - foreach ($metadata['sizes'] as $size_info) {
1527 - if ($size_info['file'] == $size_file) {
1528 - return true;
1529 - }
1530 - }
1531 - }
1532 - return false;
1533 - }
1379 + }
1380 + return $metadata;
1381 + }
1534 1382
1535 - public function create_unique_destination_file($full_size_file, $size) {
1536 - $pathinfo = pathinfo($full_size_file);
1537 - $directory = $pathinfo['dirname'];
1538 - $filename = $pathinfo['filename'];
1539 - $extension = $pathinfo['extension'];
1540 -
1541 - $destination_file = $directory . '/' . $filename . '-' . $size['width'] . 'x' . $size['height'] . '.' . $extension;
1542 -
1543 - // Skip if the file already exists
1544 - if (file_exists($destination_file)) {
1545 - // return false;
1546 - }
1547 -
1548 - return $destination_file;
1549 - }
1550 -
1551 - public function create_size_array($destination_file, $size_dimension) {
1552 - list($width, $height) = explode('x', $size_dimension);
1553 - return array(
1554 - 'file' => basename($destination_file),
1555 - 'width' => $width,
1556 - 'height' => $height,
1557 - 'mime-type' => wp_check_filetype($destination_file)['type'],
1558 - 'filesize' => filesize($destination_file),
1559 - 'resized' => false,
1560 - );
1561 - }
1562 -
1563 -
1564 1383 /**
1565 1384 * Attempt to associate posts and menu items with previously missing parents
1566 1385 *
1567 1386 * An imported post's parent may not have been imported when it was first created
@@ -1679,9 +1498,9 @@
1679 1498 */
1680 1499 private function is_valid_meta_key( $key ) {
1681 1500 // Skip attachment metadata since we'll regenerate it from scratch.
1682 1501 // Skip _edit_lock as not relevant for import
1683 - if ( in_array( $key, [ '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock', '_elementor_source_image_hash', 'sm_cloud' ] ) ) {
1502 + if ( in_array( $key, [ '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ] ) ) {
1684 1503 return false;
1685 1504 }
1686 1505
1687 1506 return $key;
@@ -1728,9 +1547,9 @@
1728 1547 * @param $post_id
1729 1548 *
1730 1549 * @return void
1731 1550 */
1732 - public function update_post_meta( $post_id ) {
1551 + private function update_post_meta( $post_id ) {
1733 1552 foreach ( $this->posts_meta as $meta_key => $meta_value ) {
1734 1553 update_post_meta( $post_id, $meta_key, $meta_value );
1735 1554 }
1736 1555 }
@@ -1741,23 +1560,8 @@
1741 1560 return $this->output;
1742 1561 }
1743 1562
1744 1563 /**
1745 - * Register Templately with Jetpack Sync's known importers.
1746 - *
1747 - * This filter callback adds Templately to Jetpack's list of known importers,
1748 - * ensuring that 'templately' is sent to WordPress.com instead of the full class name.
1749 - * This provides cleaner analytics data and better tracking.
1750 - *
1751 - * @param array $known_importers Array of known importers with class names as keys and friendly names as values.
1752 - * @return array Modified array of known importers.
1753 - */
1754 - public function register_jetpack_importer( $known_importers ) {
1755 - $known_importers[ __CLASS__ ] = 'templately';
1756 - return $known_importers;
1757 - }
1758 -
1759 - /**
1760 1564 * @param $file
1761 1565 * @param array $args
1762 1566 */
1763 1567 public function __construct( $file, array $args = [] ) {
@@ -1762,24 +1566,11 @@
1762 1566 */
1763 1567 public function __construct( $file, array $args = [] ) {
1764 1568 parent::__construct();
1765 1569
1766 - $this->args = $args;
1767 - $this->session_id = $args['session_id'];
1570 + $this->requested_file_path = $file;
1571 + $this->args = $args;
1768 1572
1769 - if ( ! empty( $args['json'] ) ) {
1770 - $this->json = $args['json'];
1771 - }
1772 -
1773 - if ( ! empty( $args['origin'] ) ) {
1774 - $this->origin = $args['origin'];
1775 - }
1776 -
1777 - if ( ! empty( $file ) ) {
1778 - $this->requested_file_path = $file;
1779 - $this->import_data_key = 'wp_importer_attributes_' . md5($this->requested_file_path);
1780 - }
1781 -
1782 1573 if ( ! empty( $this->args['fetch_attachments'] ) ) {
1783 1574 $this->fetch_attachments = true;
1784 1575 }
1785 1576
@@ -1801,9 +1592,6 @@
1801 1592
1802 1593 if ( ! empty( $this->args['terms_meta'] ) ) {
1803 1594 $this->terms_meta = $this->args['terms_meta'];
1804 1595 }
1805 -
1806 - // Register with Jetpack Sync for better analytics tracking.
1807 - add_filter( 'jetpack_sync_known_importers', [ $this, 'register_jetpack_importer' ] );
1808 1596 }
1809 1597 }