PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.10
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.10
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 +174 -260 3.6.13.1.10 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
@@ -87,18 +83,8 @@
87 83 * WXR attachment ID
88 84 */
89 85 private $id;
90 86
91 - /**
92 - * @var mixed
93 - */
94 - private $json;
95 -
96 - /**
97 - * @var string
98 - */
99 - private $session_id;
100 -
101 87 // Information to import from WXR file.
102 88 private $version;
103 89 private $authors = [];
104 90 public $posts = [];
@@ -133,25 +119,8 @@
133 119 private $terms_meta = [];
134 120
135 121 public static $_replace_image_ids = [];
136 122
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 123 /**
155 124 * Parses filename from a Content-Disposition header value.
156 125 *
157 126 * As per RFC6266:
@@ -327,10 +296,9 @@
327 296
328 297 wp_defer_term_counting( true );
329 298 wp_defer_comment_counting( true );
330 299
331 - do_action( 'import_start' );
332 - do_action( 'templately_import_start', $this );
300 + do_action( 'import_start', $this );
333 301
334 302 return true;
335 303 }
336 304
@@ -384,8 +352,30 @@
384 352 }
385 353 }
386 354 }
387 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 +
388 378 /**
389 379 * Map old author logins to local user IDs based on decisions made
390 380 * in import options form. Can map to an existing user, create a new user
391 381 * or falls back to the current user in case of error with either of the previous
@@ -394,11 +384,17 @@
394 384 if ( ! isset( $this->args['imported_authors'] ) ) {
395 385 return;
396 386 }
397 387
388 + $attributes = [
389 + 'processed_authors',
390 + 'author_mapping',
391 + 'output',
392 + ];
398 393
399 - $processed_templates = $this->get_loop_result([], $this->import_data_key);
394 + $processed_templates = $this->origin->get_progress([], $this->import_data_key);
400 395 if (!empty($processed_templates)) {
396 + $this->retrieve_attributes($attributes);
401 397 return;
402 398 }
403 399
404 400 $create_users = apply_filters( 'import_allow_create_users', self::DEFAULT_ALLOW_CREATE_USERS );
@@ -457,9 +453,10 @@
457 453 $this->author_mapping[ $santized_old_login ] = (int) get_current_user_id();
458 454 }
459 455 }
460 456
461 - $this->set_loop_result( true, $this->import_data_key );
457 + $this->backup_attributes($attributes);
458 + $this->origin->update_progress( true, null, $this->import_data_key );
462 459 }
463 460
464 461 /**
465 462 * Create new terms based on import information
@@ -468,16 +465,23 @@
468 465 *
469 466 * @return array|array[] the ids of succeed/failed imported terms.
470 467 */
471 468 private function process_terms(): array {
472 - $result = [
469 + $params = $this->origin->get_request_params();
470 + $result = $params["imported_data"]["wp_import_terms_" . $this->import_data_key] ?? [
473 471 'succeed' => [],
474 472 'failed' => [],
475 473 ];
476 474
477 - $processed_templates = $this->get_loop_result([], "wp_import_terms_" . $this->import_data_key);
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);
478 482 if (!empty($processed_templates)) {
479 - $result = $this->get_loop_result([], "wp_import_terms_" . $this->import_data_key);
483 + $this->retrieve_attributes($attributes);
480 484 return $result;
481 485 }
482 486
483 487 $this->terms = apply_filters( 'wp_import_terms', $this->terms );
@@ -560,10 +564,11 @@
560 564 }
561 565
562 566 unset( $this->terms );
563 567
568 + $this->backup_attributes($attributes);
564 569 // 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);
570 + $this->origin->update_progress( true, [ "wp_import_terms_" . $this->import_data_key => $result ]);
566 571 return $result;
567 572 }
568 573
569 574 /**
@@ -633,44 +638,54 @@
633 638 *
634 639 * @return array the ids of succeed/failed imported posts.
635 640 */
636 641 private function process_posts(): array {
637 - $backup_key = "wp_import_post_" . $this->import_data_key;
642 + $backup_key = "wp_import_" . $this->import_data_key;
643 + $params = $this->origin->get_request_params();
644 + $result = $params["imported_data"][$backup_key] ?? [
645 + 'succeed' => [],
646 + 'failed' => [],
647 + ];
638 648
649 + $attributes = [
650 + 'posts',
651 + 'output',
652 + 'menu_item_orphans',
653 + 'processed_menu_items',
654 + 'post_orphans',
655 + 'processed_posts',
656 + 'featured_images',
657 + ];
658 + $this->retrieve_attributes($attributes);
659 +
639 660 $this->posts = apply_filters( 'wp_import_posts', $this->posts );
640 661
641 - $results = $this->loop( $this->posts, function($key, $post, $result ) {
662 + $processed_templates = $this->origin->get_progress([], $this->import_data_key);
642 663
643 - $result = !empty($result) ? $result : [
644 - 'succeed' => [],
645 - 'failed' => [],
646 - ];
664 + foreach ( $this->posts as $key => $post ) {
665 + if (in_array($key, $processed_templates)) {
666 + continue;
667 + }
647 668
648 - $original_post_id = $post['post_id'];
649 - $post = apply_filters( 'wp_import_post_data_raw', $post, $this );
669 + // Add the template to the processed templates and update the session data
670 + $processed_templates[] = $key;
671 + $this->origin->update_progress( $processed_templates, null, $this->import_data_key);
650 672
651 - if(empty($post)){
652 - return $result;
653 - }
673 + $post = apply_filters( 'wp_import_post_data_raw', $post );
654 674
655 - if(!is_array($post) && is_numeric($post)){
656 - $result['succeed'][ $original_post_id ] = $post;
657 - return $result;
658 - }
659 -
660 675 if ( ! post_type_exists( $post['post_type'] ) ) {
661 676 /* translators: 1: Post title, 2: Post type. */
662 677 $this->output['errors'][] = sprintf( esc_html__( 'Failed to import %1$s: Invalid post type %2$s', 'elementor' ), $post['post_title'], $post['post_type'] );
663 678 do_action( 'wp_import_post_exists', $post );
664 - return $result;
679 + continue;
665 680 }
666 681
667 682 if ( isset( $this->processed_posts[ $post['post_id'] ] ) && ! empty( $post['post_id'] ) ) {
668 - return $result;
683 + continue;
669 684 }
670 685
671 686 if ( 'auto-draft' === $post['status'] ) {
672 - return $result;
687 + continue;
673 688 }
674 689
675 690 if(!empty($post['post_content']) && !empty($post['post_id'])){
676 691 $post['post_content'] = Utils::import_and_replace_attachments($post['post_content'], $post['post_id']);
@@ -677,15 +692,15 @@
677 692 }
678 693
679 694 if ( 'nav_menu_item' === $post['post_type'] ) {
680 695 $result['succeed'] += $this->process_menu_item( $post );
681 - return $result;
696 + continue;
682 697 }
683 698
684 699 if ( 'wp_navigation' === $post['post_type'] ) {
685 700 $processed = $this->process_navigation( $post );
686 701 if ( ! $processed ) {
687 - return $result;
702 + continue;
688 703 }
689 704 }
690 705
691 706 $post_type_object = get_post_type_object( $post['post_type'] );
@@ -725,12 +740,9 @@
725 740 'post_type' => $post['post_type'],
726 741 'post_password' => $post['post_password'],
727 742 ];
728 743
729 - if(isset($post['original_attachment_url'])){
730 - $postdata['original_attachment_url'] = $post['original_attachment_url'];
731 - }
732 -
744 + $original_post_id = $post['post_id'];
733 745 $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
734 746
735 747 $postdata = wp_slash( $postdata );
736 748
@@ -735,9 +747,9 @@
735 747 $postdata = wp_slash( $postdata );
736 748
737 749 if ( 'attachment' === $postdata['post_type'] ) {
738 750 $remote_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid'];
739 - $attachment_sizes = [];
751 +
740 752 // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
741 753 // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
742 754 $postdata['upload_date'] = $post['post_date'];
743 755 if ( isset( $post['postmeta'] ) ) {
@@ -745,19 +757,14 @@
745 757 if ( '_wp_attached_file' === $meta['key'] ) {
746 758 if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) {
747 759 $postdata['upload_date'] = $matches[0];
748 760 }
749 - // break;
761 + break;
750 762 }
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 763 }
757 764 }
758 765
759 - $post_id = $this->process_attachment( $postdata, $remote_url, $attachment_sizes, $original_post_id );
766 + $post_id = $this->process_attachment( $postdata, $remote_url );
760 767 $comment_post_id = $post_id;
761 768 } else {
762 769 $post_id = wp_insert_post( $postdata, true );
763 770
@@ -778,13 +785,22 @@
778 785 $result['failed'][] = $original_post_id;
779 786
780 787 $this->output['errors'][] = $error;
781 788
782 - if ( 'attachment' === $postdata['post_type'] ) {
783 - do_action( 'templately_import.process_post', $post, $result, $this );
789 + // Add the template to the processed templates and update the session data
790 + $this->backup_attributes($attributes);
791 + $this->origin->update_progress( null, [ $backup_key => $result ], $this->import_data_key);
792 +
793 + // If it's not the last item, send the SSE message and exit
794 + if( end($this->posts) !== $post) {
795 + $this->origin->sse_message( [
796 + 'type' => 'continue',
797 + 'action' => 'continue',
798 + 'results' => __METHOD__ . '::' . __LINE__,
799 + ] );
800 + exit;
784 801 }
785 -
786 - return $result;
802 + continue;
787 803 }
788 804
789 805 $result['succeed'][ $original_post_id ] = $post_id;
790 806
@@ -939,14 +955,26 @@
939 955 }
940 956
941 957 do_action( 'templately_import.process_post', $post, $result, $this );
942 958
943 - return $result;
944 - }, $backup_key); //, true
959 + // Add the template to the processed templates and update the session data
960 + $this->backup_attributes($attributes);
961 + $this->origin->update_progress( null, [ $backup_key => $result ], $this->import_data_key);
945 962
963 + // If it's not the last item, send the SSE message and exit
964 + if( end($this->posts) !== $post) {
965 + $this->origin->sse_message( [
966 + 'type' => 'continue',
967 + 'action' => 'continue',
968 + 'results' => __METHOD__ . '::' . __LINE__,
969 + ] );
970 + exit;
971 + }
972 + }
973 +
946 974 unset( $this->posts );
947 975
948 - return $results;
976 + return $result;
949 977 }
950 978
951 979 /**
952 980 * Attempt to create a new menu item from import data
@@ -1127,9 +1155,9 @@
1127 1155 * @param string $url URL to fetch attachment from
1128 1156 *
1129 1157 * @return int|WP_Error Post ID on success, WP_Error otherwise
1130 1158 */
1131 - public function process_attachment( $post, $url, $sizes = [], $original_post_id = null ) {
1159 + public function process_attachment( $post, $url, $sizes = [] ) {
1132 1160 if ( ! $this->fetch_attachments ) {
1133 1161 return new WP_Error( 'attachment_processing_error', esc_html__( 'Fetching attachments is not enabled', 'elementor' ) );
1134 1162 }
1135 1163 if ( ! function_exists( 'wp_crop_image' ) ) {
@@ -1140,24 +1168,10 @@
1140 1168 $url = rtrim( $this->base_url, '/' ) . $url;
1141 1169 }
1142 1170
1143 1171 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 - }
1172 + $this->url_remap[ $url ] = wp_get_attachment_url( $saved_image );
1173 + $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension(wp_get_attachment_url( $saved_image ));
1160 1174 return $saved_image;
1161 1175 }
1162 1176
1163 1177 // Check if the URL is from the wp-includes/images directory
@@ -1179,24 +1193,10 @@
1179 1193 // Move the file to the uploads dir.
1180 1194 $file_name = basename( parse_url( $url, PHP_URL_PATH ) );
1181 1195 $file_name = wp_unique_filename( $upload_dir['path'], $file_name );
1182 1196 $dest_file = $upload_dir['path'] . "/$file_name";
1183 - $start = microtime(true);
1197 + $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir );
1184 1198
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 1199 if ( is_wp_error( $upload ) ) {
1200 1200 return $upload;
1201 1201 }
1202 1202
@@ -1206,19 +1206,14 @@
1206 1206 } else {
1207 1207 return new WP_Error( 'attachment_processing_error', esc_html__( 'Invalid file type', 'elementor' ) );
1208 1208 }
1209 1209
1210 - // $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1211 - // $this->set_url_map($post['guid'], $upload['url']);
1210 + $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1212 1211 $post['guid'] = $upload['url'];
1213 1212
1214 1213 // As per wp-admin/includes/upload.php.
1215 1214 $post_id = wp_insert_attachment( $post, $upload['file'] );
1216 1215
1217 - if(is_wp_error($post_id)){
1218 - return $post_id;
1219 - }
1220 -
1221 1216 $this->update_post_meta( $post_id );
1222 1217
1223 1218 // Generate attachment metadata
1224 1219 $metadata = wp_generate_attachment_metadata( $post_id, $upload['file'] );
@@ -1225,46 +1220,21 @@
1225 1220
1226 1221 // error_log('Metadata: ' . print_r($metadata, true));
1227 1222
1228 1223 // For gutenberg pages
1229 - $updated_metadata = $this->import_sizes($sizes, $metadata, $upload['file'], $post_id);
1224 + $metadata = $this->import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id);
1230 1225
1231 1226 // 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 - }
1227 + wp_update_attachment_metadata( $post_id, $metadata );
1237 1228
1238 1229 // @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 1230
1243 - if(!empty($post['original_attachment_url'])){
1244 - update_post_meta( $post_id, '_templately_demo_url', $post['original_attachment_url'] );
1245 - }
1246 - }
1247 -
1248 1231 update_post_meta( $post_id, '_elementor_source_image_hash', sha1( $url ) );
1249 1232 self::$_replace_image_ids[ sha1( $url ) ] = $post_id;
1250 1233
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 1234 // Remap resized image URLs, works by stripping the extension and remapping the URL stub.
1261 1235 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 - }
1236 + $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension($upload['url']);
1267 1237 }
1268 1238
1269 1239 return $post_id;
1270 1240 }
@@ -1275,17 +1245,8 @@
1275 1245
1276 1246 return $parts['dirname'] . '/' . $name;
1277 1247 }
1278 1248
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 1249 /**
1289 1250 * Attempt to download a remote file attachment
1290 1251 *
1291 1252 * @param string $url URL of item to fetch
@@ -1422,12 +1383,12 @@
1422 1383 'error' => false,
1423 1384 ];
1424 1385
1425 1386 // Keep track of the old and new urls so we can substitute them later.
1426 - $this->set_url_map($url, $upload['url']);
1387 + $this->url_remap[ $url ] = $upload['url'];
1427 1388 // Keep track of the destination if the remote url is redirected somewhere else.
1428 1389 if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] !== $url ) {
1429 - $this->set_url_map($headers['x-final-location'], $upload['url']);
1390 + $this->url_remap[ $headers['x-final-location'] ] = $upload['url'];
1430 1391 }
1431 1392
1432 1393 return $upload;
1433 1394 }
@@ -1441,9 +1402,9 @@
1441 1402 * @access private
1442 1403 *
1443 1404 * @param string $url The image URL.
1444 1405 *
1445 - * @return false|int New image ID or false.
1406 + * @return false|array New image ID or false.
1446 1407 */
1447 1408 private function get_saved_image( $url ) {
1448 1409 global $wpdb;
1449 1410
@@ -1464,9 +1425,9 @@
1464 1425 );
1465 1426
1466 1427 if ( $post_id ) {
1467 1428 self::$_replace_image_ids[ $hash ] = $post_id;
1468 - return (int) $post_id;
1429 + return $post_id;
1469 1430 }
1470 1431
1471 1432 return false;
1472 1433 }
@@ -1471,97 +1432,69 @@
1471 1432 return false;
1472 1433 }
1473 1434
1474 1435
1475 - public function import_sizes($sizes, $metadata, $full_size_file, $post_id) {
1476 - $metadata_modified = false;
1436 + public function extract_sizes($sizes) {
1437 + return array_reduce($sizes, function($carry, $url) {
1438 + if (preg_match('/-((\d+)x(\d+))\./', $url, $matches)) {
1439 + $carry[$matches[1]] = $url;
1440 + }
1441 + return $carry;
1442 + }, []);
1443 + }
1477 1444
1478 - if (!empty($sizes)) {
1479 - do_action('templately_import.finalize_gutenberg_attachment', $post_id);
1445 + public function size_exists_in_metadata($size_dimension, $metadata) {
1446 + foreach ($metadata['sizes'] as $size => $size_info) {
1447 + if (strpos($size_info['file'], $size_dimension) !== false) {
1448 + return true;
1449 + }
1450 + }
1451 + return false;
1452 + }
1480 1453
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 - }
1454 + public function create_unique_destination_file($upload_file, $size_dimension) {
1455 + $destination_file = preg_replace('/(\.[^.]+)$/', '-' . $size_dimension . '$1', $upload_file);
1456 + $destination_dir = pathinfo($destination_file, PATHINFO_DIRNAME);
1457 + $destination_filename = pathinfo($destination_file, PATHINFO_BASENAME);
1458 + $unique_filename = wp_unique_filename($destination_dir, $destination_filename);
1459 + return $destination_dir . '/' . $unique_filename;
1460 + }
1499 1461
1500 - // Only return metadata if new sizes were actually added
1501 - return $metadata_modified ? $metadata : false;
1502 - }
1462 + public function create_size_array($destination_file, $size_dimension) {
1463 + list($width, $height) = explode('x', $size_dimension);
1464 + return array(
1465 + 'file' => basename($destination_file),
1466 + 'width' => $width,
1467 + 'height' => $height,
1468 + 'mime-type' => wp_check_filetype($destination_file)['type'],
1469 + 'filesize' => filesize($destination_file),
1470 + 'resized' => false,
1471 + );
1472 + }
1503 1473
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 - }
1474 + public function import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id) {
1475 + if (!empty($sizes)) {
1476 + do_action( 'templately_import.finalize_gutenberg_attachment', $post_id );
1510 1477
1511 - $resized = $editor->resize($size['width'], $size['height'], true);
1512 - if (is_wp_error($resized)) {
1513 - return $resized;
1514 - }
1478 + $size_dimensions = $this->extract_sizes($sizes);
1479 + $size_dimensions = array_filter($size_dimensions);
1515 1480
1516 - $saved = $editor->save($destination_file);
1517 - if (is_wp_error($saved)) {
1518 - return $saved;
1519 - }
1481 + foreach ($size_dimensions as $size_dimension => $__url) {
1482 + if (!$this->size_exists_in_metadata($size_dimension, $metadata)) {
1483 + $unique_destination_file = $this->create_unique_destination_file($upload['file'], $size_dimension);
1484 + $missing_size = $this->fetch_remote_file($__url, $unique_destination_file, $upload_dir);
1520 1485
1521 - return $saved;
1522 - }
1486 + if($missing_size && !is_wp_error($missing_size)) {
1487 + $metadata['sizes'][$size_dimension] = $this->create_size_array($unique_destination_file, $size_dimension);
1488 + do_action( 'templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension );
1489 + }
1490 + }
1491 + }
1523 1492
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 - }
1493 + }
1494 + return $metadata;
1495 + }
1534 1496
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 1497 /**
1565 1498 * Attempt to associate posts and menu items with previously missing parents
1566 1499 *
1567 1500 * An imported post's parent may not have been imported when it was first created
@@ -1679,9 +1612,9 @@
1679 1612 */
1680 1613 private function is_valid_meta_key( $key ) {
1681 1614 // Skip attachment metadata since we'll regenerate it from scratch.
1682 1615 // 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' ] ) ) {
1616 + if ( in_array( $key, [ '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ] ) ) {
1684 1617 return false;
1685 1618 }
1686 1619
1687 1620 return $key;
@@ -1728,9 +1661,9 @@
1728 1661 * @param $post_id
1729 1662 *
1730 1663 * @return void
1731 1664 */
1732 - public function update_post_meta( $post_id ) {
1665 + private function update_post_meta( $post_id ) {
1733 1666 foreach ( $this->posts_meta as $meta_key => $meta_value ) {
1734 1667 update_post_meta( $post_id, $meta_key, $meta_value );
1735 1668 }
1736 1669 }
@@ -1741,23 +1674,8 @@
1741 1674 return $this->output;
1742 1675 }
1743 1676
1744 1677 /**
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 1678 * @param $file
1761 1679 * @param array $args
1762 1680 */
1763 1681 public function __construct( $file, array $args = [] ) {
@@ -1762,10 +1680,9 @@
1762 1680 */
1763 1681 public function __construct( $file, array $args = [] ) {
1764 1682 parent::__construct();
1765 1683
1766 - $this->args = $args;
1767 - $this->session_id = $args['session_id'];
1684 + $this->args = $args;
1768 1685
1769 1686 if ( ! empty( $args['json'] ) ) {
1770 1687 $this->json = $args['json'];
1771 1688 }
@@ -1801,9 +1718,6 @@
1801 1718
1802 1719 if ( ! empty( $this->args['terms_meta'] ) ) {
1803 1720 $this->terms_meta = $this->args['terms_meta'];
1804 1721 }
1805 -
1806 - // Register with Jetpack Sync for better analytics tracking.
1807 - add_filter( 'jetpack_sync_known_importers', [ $this, 'register_jetpack_importer' ] );
1808 1722 }
1809 1723 }