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 +96 -405 3.7.33.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 = [];
@@ -119,10 +96,8 @@
119 96 private $menu_item_orphans = [];
120 97 private $mapped_terms_slug = [];
121 98
122 99 private $fetch_attachments = false;
123 - private $attachment_timeout = 300;
124 - private $attachment_retry_count = 3;
125 100 private $featured_images = [];
126 101
127 102 /**
128 103 * @var array[] [meta_key => meta_value] Meta value that should be set for every imported post.
@@ -135,25 +110,8 @@
135 110 private $terms_meta = [];
136 111
137 112 public static $_replace_image_ids = [];
138 113
139 -
140 - public $backup_attributes = [
141 - 'output',
142 - 'url_remap',
143 - '_replace_image_ids',
144 - 'menu_item_orphans',
145 - 'processed_menu_items',
146 - 'post_orphans',
147 - 'processed_posts',
148 - 'featured_images',
149 - 'mapped_terms_slug',
150 - 'processed_terms',
151 - 'processed_authors',
152 - 'author_mapping',
153 - ];
154 -
155 -
156 114 /**
157 115 * Parses filename from a Content-Disposition header value.
158 116 *
159 117 * As per RFC6266:
@@ -329,10 +287,9 @@
329 287
330 288 wp_defer_term_counting( true );
331 289 wp_defer_comment_counting( true );
332 290
333 - do_action( 'import_start' );
334 - do_action( 'templately_import_start', $this );
291 + do_action( 'import_start', $this );
335 292
336 293 return true;
337 294 }
338 295
@@ -396,14 +353,8 @@
396 353 if ( ! isset( $this->args['imported_authors'] ) ) {
397 354 return;
398 355 }
399 356
400 -
401 - $processed_templates = $this->get_loop_result([], $this->import_data_key);
402 - if (!empty($processed_templates)) {
403 - return;
404 - }
405 -
406 357 $create_users = apply_filters( 'import_allow_create_users', self::DEFAULT_ALLOW_CREATE_USERS );
407 358
408 359 foreach ( (array) $this->args['imported_authors'] as $i => $old_login ) {
409 360 // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
@@ -458,10 +409,8 @@
458 409 }
459 410 $this->author_mapping[ $santized_old_login ] = (int) get_current_user_id();
460 411 }
461 412 }
462 -
463 - $this->set_loop_result( true, $this->import_data_key );
464 413 }
465 414
466 415 /**
467 416 * Create new terms based on import information
@@ -475,14 +424,8 @@
475 424 'succeed' => [],
476 425 'failed' => [],
477 426 ];
478 427
479 - $processed_templates = $this->get_loop_result([], "wp_import_terms_" . $this->import_data_key);
480 - if (!empty($processed_templates)) {
481 - $result = $this->get_loop_result([], "wp_import_terms_" . $this->import_data_key);
482 - return $result;
483 - }
484 -
485 428 $this->terms = apply_filters( 'wp_import_terms', $this->terms );
486 429 if ( empty( $this->terms ) ) {
487 430 return $result;
488 431 }
@@ -562,10 +505,8 @@
562 505 }
563 506
564 507 unset( $this->terms );
565 508
566 - // Add the template to the processed templates and update the session data
567 - $this->set_loop_result( $result, "wp_import_terms_" . $this->import_data_key);
568 509 return $result;
569 510 }
570 511
571 512 /**
@@ -635,59 +576,42 @@
635 576 *
636 577 * @return array the ids of succeed/failed imported posts.
637 578 */
638 579 private function process_posts(): array {
639 - $backup_key = "wp_import_post_" . $this->import_data_key;
580 + $result = [
581 + 'succeed' => [],
582 + 'failed' => [],
583 + ];
640 584
641 585 $this->posts = apply_filters( 'wp_import_posts', $this->posts );
642 586
643 - $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 );
644 589
645 - $result = !empty($result) ? $result : [
646 - 'succeed' => [],
647 - 'failed' => [],
648 - ];
649 -
650 - $original_post_id = $post['post_id'];
651 - $post = apply_filters( 'wp_import_post_data_raw', $post, $this );
652 -
653 - if(empty($post)){
654 - return $result;
655 - }
656 -
657 - if(!is_array($post) && is_numeric($post)){
658 - $result['succeed'][ $original_post_id ] = $post;
659 - return $result;
660 - }
661 -
662 590 if ( ! post_type_exists( $post['post_type'] ) ) {
663 591 /* translators: 1: Post title, 2: Post type. */
664 592 $this->output['errors'][] = sprintf( esc_html__( 'Failed to import %1$s: Invalid post type %2$s', 'elementor' ), $post['post_title'], $post['post_type'] );
665 593 do_action( 'wp_import_post_exists', $post );
666 - return $result;
594 + continue;
667 595 }
668 596
669 597 if ( isset( $this->processed_posts[ $post['post_id'] ] ) && ! empty( $post['post_id'] ) ) {
670 - return $result;
598 + continue;
671 599 }
672 600
673 601 if ( 'auto-draft' === $post['status'] ) {
674 - return $result;
602 + continue;
675 603 }
676 604
677 - if(!empty($post['post_content']) && !empty($post['post_id'])){
678 - $post['post_content'] = Utils::import_and_replace_attachments($post['post_content'], $post['post_id']);
679 - }
680 -
681 605 if ( 'nav_menu_item' === $post['post_type'] ) {
682 606 $result['succeed'] += $this->process_menu_item( $post );
683 - return $result;
607 + continue;
684 608 }
685 609
686 610 if ( 'wp_navigation' === $post['post_type'] ) {
687 611 $processed = $this->process_navigation( $post );
688 612 if ( ! $processed ) {
689 - return $result;
613 + continue;
690 614 }
691 615 }
692 616
693 617 $post_type_object = get_post_type_object( $post['post_type'] );
@@ -727,12 +651,9 @@
727 651 'post_type' => $post['post_type'],
728 652 'post_password' => $post['post_password'],
729 653 ];
730 654
731 - if(isset($post['original_attachment_url'])){
732 - $postdata['original_attachment_url'] = $post['original_attachment_url'];
733 - }
734 -
655 + $original_post_id = $post['post_id'];
735 656 $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
736 657
737 658 $postdata = wp_slash( $postdata );
738 659
@@ -737,9 +658,9 @@
737 658 $postdata = wp_slash( $postdata );
738 659
739 660 if ( 'attachment' === $postdata['post_type'] ) {
740 661 $remote_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid'];
741 - $attachment_sizes = [];
662 +
742 663 // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
743 664 // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
744 665 $postdata['upload_date'] = $post['post_date'];
745 666 if ( isset( $post['postmeta'] ) ) {
@@ -747,19 +668,14 @@
747 668 if ( '_wp_attached_file' === $meta['key'] ) {
748 669 if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) {
749 670 $postdata['upload_date'] = $matches[0];
750 671 }
751 - // break;
672 + break;
752 673 }
753 - else if ( '_wp_attachment_metadata' === $meta['key'] ) {
754 - $attachment_metadata = maybe_unserialize( $meta['value'] );
755 - $attachment_sizes = $attachment_metadata['sizes'] ?? [];
756 - // break;
757 - }
758 674 }
759 675 }
760 676
761 - $post_id = $this->process_attachment( $postdata, $remote_url, $attachment_sizes, $original_post_id );
677 + $post_id = $this->process_attachment( $postdata, $remote_url );
762 678 $comment_post_id = $post_id;
763 679 } else {
764 680 $post_id = wp_insert_post( $postdata, true );
765 681
@@ -780,13 +696,9 @@
780 696 $result['failed'][] = $original_post_id;
781 697
782 698 $this->output['errors'][] = $error;
783 699
784 - if ( 'attachment' === $postdata['post_type'] ) {
785 - do_action( 'templately_import.process_post', $post, $result, $this );
786 - }
787 -
788 - return $result;
700 + continue;
789 701 }
790 702
791 703 $result['succeed'][ $original_post_id ] = $post_id;
792 704
@@ -940,15 +852,13 @@
940 852 }
941 853 }
942 854
943 855 do_action( 'templately_import.process_post', $post, $result, $this );
856 + }
944 857
945 - return $result;
946 - }, $backup_key); //, true
947 -
948 858 unset( $this->posts );
949 859
950 - return $results;
860 + return $result;
951 861 }
952 862
953 863 /**
954 864 * Attempt to create a new menu item from import data
@@ -1129,9 +1039,9 @@
1129 1039 * @param string $url URL to fetch attachment from
1130 1040 *
1131 1041 * @return int|WP_Error Post ID on success, WP_Error otherwise
1132 1042 */
1133 - public function process_attachment( $post, $url, $sizes = [], $original_post_id = null ) {
1043 + public function process_attachment( $post, $url, $sizes = [] ) {
1134 1044 if ( ! $this->fetch_attachments ) {
1135 1045 return new WP_Error( 'attachment_processing_error', esc_html__( 'Fetching attachments is not enabled', 'elementor' ) );
1136 1046 }
1137 1047 if ( ! function_exists( 'wp_crop_image' ) ) {
@@ -1142,40 +1052,10 @@
1142 1052 $url = rtrim( $this->base_url, '/' ) . $url;
1143 1053 }
1144 1054
1145 1055 if($saved_image = $this->get_saved_image($url)){
1146 - // $this->url_remap[ $url ] = wp_get_attachment_url( $saved_image );
1147 - // $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension(wp_get_attachment_url( $saved_image ));
1148 - $upload_url = set_url_scheme( wp_get_attachment_url( $saved_image ) );
1149 - $this->set_url_map($url, $upload_url, true);
1150 -
1151 - // AI image replacement: the imported content still carries the ORIGINAL
1152 - // demo URL, so it must be remapped onto the replacement image here too.
1153 - // The fresh-upload branch below does this, but when the replacement
1154 - // image was already imported (a re-import, or the same stock photo
1155 - // reused) we land here instead — without the map the demo URL survives
1156 - // finalize and the pack's original images are silently kept.
1157 - if(!empty($post['original_attachment_url'])){
1158 - $this->set_url_map($post['original_attachment_url'], $upload_url, true);
1159 -
1160 - $original_hash = sha1( $post['original_attachment_url'] );
1161 -
1162 - // A stale hash from a previous import can still point the demo URL at
1163 - // the old, unreplaced attachment — re-point it at the replacement.
1164 - if( (int) $this->get_saved_image($post['original_attachment_url']) !== (int) $saved_image ){
1165 - $hash_meta_id = add_post_meta( $saved_image, '_elementor_source_image_hash', $original_hash );
1166 - add_post_meta( $saved_image, '_templately_image_hash_meta_id', $hash_meta_id );
1167 - }
1168 -
1169 - self::$_replace_image_ids[ $original_hash ] = (int) $saved_image;
1170 - }
1171 -
1172 - $full_size_path = get_attached_file($saved_image);
1173 - $metadata = wp_get_attachment_metadata($saved_image);
1174 - $updated_metadata = $this->import_sizes($sizes, $metadata, $full_size_path, $saved_image);
1175 - if ($updated_metadata !== false) {
1176 - wp_update_attachment_metadata( $saved_image, $updated_metadata );
1177 - }
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 ));
1178 1058 return $saved_image;
1179 1059 }
1180 1060
1181 1061 // Check if the URL is from the wp-includes/images directory
@@ -1194,40 +1074,13 @@
1194 1074 return new WP_Error( 'upload_dir_error', $upload_dir['error'] );
1195 1075 }
1196 1076
1197 1077 // Move the file to the uploads dir.
1198 - //
1199 - // The attachment url is attacker-reachable — a cloud template can point an
1200 - // image anywhere — so the url-derived name is sanitized, and an extension
1201 - // WordPress does not recognise as an uploadable type is dropped rather than
1202 - // carried into the destination path. fetch_remote_file() re-derives the
1203 - // extension from the response and validates it before writing, so dropping
1204 - // an unusable one here costs nothing.
1205 - $file_name = sanitize_file_name( basename( parse_url( $url, PHP_URL_PATH ) ) );
1206 - if ( pathinfo( $file_name, PATHINFO_EXTENSION ) ) {
1207 - $url_filetype = wp_check_filetype( $file_name );
1208 - if ( empty( $url_filetype['type'] ) ) {
1209 - $file_name = pathinfo( $file_name, PATHINFO_FILENAME );
1210 - }
1211 - }
1078 + $file_name = basename( parse_url( $url, PHP_URL_PATH ) );
1212 1079 $file_name = wp_unique_filename( $upload_dir['path'], $file_name );
1213 1080 $dest_file = $upload_dir['path'] . "/$file_name";
1214 - $start = microtime(true);
1081 + $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir );
1215 1082
1216 - $upload = apply_filters( 'templately_import_copy_attachment', null, $original_post_id, $dest_file, $upload_dir );
1217 - if ( null === $upload ) {
1218 - $upload = $this->fetch_remote_file( $url, $dest_file, $upload_dir );
1219 - }
1220 -
1221 - // Normalize URL scheme to match the current site scheme (fixes HTTP URLs on HTTPS multisites).
1222 - if ( ! is_wp_error( $upload ) && ! empty( $upload['url'] ) ) {
1223 - $upload['url'] = set_url_scheme( $upload['url'] );
1224 - }
1225 -
1226 - $end = microtime(true);
1227 - $duration = $end - $start;
1228 - error_log('Duration: ' . $duration);
1229 -
1230 1083 if ( is_wp_error( $upload ) ) {
1231 1084 return $upload;
1232 1085 }
1233 1086
@@ -1237,19 +1090,14 @@
1237 1090 } else {
1238 1091 return new WP_Error( 'attachment_processing_error', esc_html__( 'Invalid file type', 'elementor' ) );
1239 1092 }
1240 1093
1241 - // $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1242 - // $this->set_url_map($post['guid'], $upload['url']);
1094 + $this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
1243 1095 $post['guid'] = $upload['url'];
1244 1096
1245 1097 // As per wp-admin/includes/upload.php.
1246 1098 $post_id = wp_insert_attachment( $post, $upload['file'] );
1247 1099
1248 - if(is_wp_error($post_id)){
1249 - return $post_id;
1250 - }
1251 -
1252 1100 $this->update_post_meta( $post_id );
1253 1101
1254 1102 // Generate attachment metadata
1255 1103 $metadata = wp_generate_attachment_metadata( $post_id, $upload['file'] );
@@ -1256,46 +1104,21 @@
1256 1104
1257 1105 // error_log('Metadata: ' . print_r($metadata, true));
1258 1106
1259 1107 // For gutenberg pages
1260 - $updated_metadata = $this->import_sizes($sizes, $metadata, $upload['file'], $post_id);
1108 + $metadata = $this->import_sizes($sizes, $metadata, $upload, $upload_dir, $post_id);
1261 1109
1262 1110 // error_log('Metadata: ' . print_r($metadata, true));
1263 - if ($updated_metadata !== false) {
1264 - wp_update_attachment_metadata( $post_id, $updated_metadata );
1265 - } else {
1266 - wp_update_attachment_metadata( $post_id, $metadata );
1267 - }
1111 + wp_update_attachment_metadata( $post_id, $metadata );
1268 1112
1269 1113 // @todo: add missing image sizes
1270 - if(defined('TEMPLATELY_DEV') && TEMPLATELY_DEV){
1271 - update_post_meta( $post_id, '_templately_original_id', $original_post_id );
1272 - update_post_meta( $post_id, '_templately_original_url', $url );
1273 1114
1274 - if(!empty($post['original_attachment_url'])){
1275 - update_post_meta( $post_id, '_templately_demo_url', $post['original_attachment_url'] );
1276 - }
1277 - }
1278 -
1279 1115 update_post_meta( $post_id, '_elementor_source_image_hash', sha1( $url ) );
1280 1116 self::$_replace_image_ids[ sha1( $url ) ] = $post_id;
1281 1117
1282 - // add a second hash for original demo url
1283 - // if user is replacing image.
1284 - // so we can also match original demo url to new image
1285 - if(!empty($post['original_attachment_url'])){
1286 - $hash_meta_id = add_post_meta( $post_id, '_elementor_source_image_hash', sha1( $post['original_attachment_url'] ) );
1287 - add_post_meta( $post_id, '_templately_image_hash_meta_id', $hash_meta_id );
1288 - self::$_replace_image_ids[ sha1( $post['original_attachment_url'] ) ] = $post_id;
1289 - }
1290 -
1291 1118 // Remap resized image URLs, works by stripping the extension and remapping the URL stub.
1292 1119 if ( preg_match( '!^image/!', $info['type'] ) ) {
1293 - // $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension($upload['url']);
1294 - $this->set_url_map($url, $upload['url'], true);
1295 - if(!empty($post['original_attachment_url'])){
1296 - $this->set_url_map($post['original_attachment_url'], $upload['url'], true);
1297 - }
1120 + $this->url_remap[ $this->remove_extension($url) ] = $this->remove_extension($upload['url']);
1298 1121 }
1299 1122
1300 1123 return $post_id;
1301 1124 }
@@ -1301,29 +1124,13 @@
1301 1124 }
1302 1125
1303 1126 private function remove_extension($url) {
1304 1127 $parts = pathinfo($url);
1305 - // pathinfo() omits 'extension' for a dotless name, and 'dirname' for an
1306 - // empty one. Remote attachment urls are not obliged to carry an extension
1307 - // (`.../media/1234`), and the url of an attachment that has since been
1308 - // deleted resolves to ''.
1309 - $dirname = isset( $parts['dirname'] ) ? $parts['dirname'] : '';
1310 - $basename = isset( $parts['basename'] ) ? $parts['basename'] : '';
1311 - $extension = isset( $parts['extension'] ) ? $parts['extension'] : '';
1312 - $name = '' === $extension ? $basename : basename($basename, ".{$extension}"); // PATHINFO_FILENAME in PHP 5.2
1128 + $name = basename($parts['basename'], ".{$parts['extension']}"); // PATHINFO_FILENAME in PHP 5.2
1313 1129
1314 - return $dirname . '/' . $name;
1130 + return $parts['dirname'] . '/' . $name;
1315 1131 }
1316 1132
1317 - public function set_url_map($original_url, $new_url, $remove_extension = false){
1318 - $this->url_remap[ $original_url ] = $new_url;
1319 - if($remove_extension){
1320 - $original_url = $this->remove_extension($original_url);
1321 - $new_url = $this->remove_extension($new_url);
1322 - $this->url_remap[ $original_url ] = $new_url;
1323 - }
1324 - }
1325 -
1326 1133 /**
1327 1134 * Attempt to download a remote file attachment
1328 1135 *
1329 1136 * @param string $url URL of item to fetch
@@ -1346,13 +1153,13 @@
1346 1153 }
1347 1154
1348 1155 // Fetch the remote URL and write it to the placeholder file.
1349 1156 $attempt = 0;
1350 - $retry_count = $this->attachment_retry_count;
1157 + $retry_count = 3;
1351 1158 $remote_response = null;
1352 1159 do {
1353 1160 $remote_response = wp_safe_remote_get( $url, [
1354 - 'timeout' => $this->attachment_timeout,
1161 + 'timeout' => 300,
1355 1162 'stream' => true,
1356 1163 'filename' => $tmp_file_name,
1357 1164 'headers' => [
1358 1165 'Accept-Encoding' => 'identity',
@@ -1372,8 +1179,10 @@
1372 1179
1373 1180 // Make sure the fetch was successful.
1374 1181 if ( 200 !== $remote_response_code ) {
1375 1182 @unlink( $tmp_file_name );
1183 + Helper::log($url);
1184 + Helper::log($remote_response);
1376 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 ) ) );
1377 1186 }
1378 1187
1379 1188 $headers = wp_remote_retrieve_headers( $remote_response );
@@ -1407,18 +1216,12 @@
1407 1216
1408 1217 return new WP_Error( 'import_file_error', sprintf( esc_html__( 'Remote file is too large, limit is %s', 'elementor' ), size_format( $max_size ) ) );
1409 1218 }
1410 1219
1411 - // Override file name with Content-Disposition header value. The header is
1412 - // whatever the remote chose to send, so it is sanitized before it is allowed
1413 - // anywhere near a path: sanitize_file_name() strips directory separators and
1414 - // neutralizes the inner extension of a double-extension name.
1220 + // Override file name with Content-Disposition header value.
1415 1221 if ( ! empty( $headers['content-disposition'] ) ) {
1416 1222 $file_name_from_disposition = self::get_filename_from_disposition( (array) $headers['content-disposition'] );
1417 1223 if ( $file_name_from_disposition ) {
1418 - $file_name_from_disposition = sanitize_file_name( $file_name_from_disposition );
1419 - }
1420 - if ( $file_name_from_disposition ) {
1421 1224 $file_name = $file_name_from_disposition;
1422 1225 }
1423 1226 }
1424 1227
@@ -1442,29 +1245,11 @@
1442 1245 $file_name = $proper_filename;
1443 1246 }
1444 1247
1445 1248 if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
1446 - @unlink( $tmp_file_name );
1447 -
1448 1249 return new WP_Error( 'import_file_error', esc_html__( 'Sorry, this file type is not permitted for security reasons.', 'elementor' ) );
1449 1250 }
1450 1251
1451 - // The type check above ran against $file_name (Content-Disposition), but the
1452 - // write lands on $new_file, whose name came from the request url path. When
1453 - // those two extensions disagree the check guarantees nothing about what is
1454 - // actually written — an image/gif verdict on `ok.gif` would let the bytes go
1455 - // to a `.php` destination. Re-derive the destination from the validated name
1456 - // so the two can never diverge. (CVE-2026-18438)
1457 - if ( $ext && strtolower( pathinfo( $new_file, PATHINFO_EXTENSION ) ) !== strtolower( $ext ) ) {
1458 - $file_name = wp_unique_filename( $uploads['path'], $file_name );
1459 - $new_file = $uploads['path'] . "/$file_name";
1460 - }
1461 -
1462 - // Whatever the branch above decided, the reported name must describe the file
1463 - // that is actually on disk — the remapped url in the imported content is built
1464 - // from it.
1465 - $file_name = wp_basename( $new_file );
1466 -
1467 1252 $move_new_file = copy( $tmp_file_name, $new_file );
1468 1253
1469 1254 if ( ! $move_new_file ) {
1470 1255 @unlink( $tmp_file_name );
@@ -1484,12 +1269,12 @@
1484 1269 'error' => false,
1485 1270 ];
1486 1271
1487 1272 // Keep track of the old and new urls so we can substitute them later.
1488 - $this->set_url_map($url, $upload['url']);
1273 + $this->url_remap[ $url ] = $upload['url'];
1489 1274 // Keep track of the destination if the remote url is redirected somewhere else.
1490 1275 if ( isset( $headers['x-final-location'] ) && $headers['x-final-location'] !== $url ) {
1491 - $this->set_url_map($headers['x-final-location'], $upload['url']);
1276 + $this->url_remap[ $headers['x-final-location'] ] = $upload['url'];
1492 1277 }
1493 1278
1494 1279 return $upload;
1495 1280 }
@@ -1503,9 +1288,9 @@
1503 1288 * @access private
1504 1289 *
1505 1290 * @param string $url The image URL.
1506 1291 *
1507 - * @return false|int New image ID or false.
1292 + * @return false|array New image ID or false.
1508 1293 */
1509 1294 private function get_saved_image( $url ) {
1510 1295 global $wpdb;
1511 1296
@@ -1514,26 +1299,13 @@
1514 1299 if ( isset( self::$_replace_image_ids[ $hash ] ) ) {
1515 1300 return self::$_replace_image_ids[ $hash ];
1516 1301 }
1517 1302
1518 - // Newest row wins. A URL can be claimed by more than one attachment: an
1519 - // AI/customizer image replacement registers the ORIGINAL demo URL against
1520 - // the REPLACEMENT attachment (see process_attachment), while a previous
1521 - // plain import of the same pack already claimed that URL for the original
1522 - // image — and that older row is never cleaned up (clear_old_el_cache only
1523 - // tracks replacement hashes). Without the ordering the stale original wins
1524 - // and the replacement is silently ignored on every re-import.
1525 - //
1526 - // The static cache above short-circuits this, but the import is split
1527 - // across many requests (each SSE `continue` is a fresh PHP process), so
1528 - // the DB is the real source of truth for anything set in an earlier step.
1529 1303 $post_id = $wpdb->get_var(
1530 1304 $wpdb->prepare(
1531 1305 'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
1532 1306 WHERE `meta_key` = \'_elementor_source_image_hash\'
1533 1307 AND `meta_value` = %s
1534 - ORDER BY `meta_id` DESC
1535 - LIMIT 1
1536 1308 ;',
1537 1309 $hash
1538 1310 )
1539 1311 );
@@ -1539,9 +1311,9 @@
1539 1311 );
1540 1312
1541 1313 if ( $post_id ) {
1542 1314 self::$_replace_image_ids[ $hash ] = $post_id;
1543 - return (int) $post_id;
1315 + return $post_id;
1544 1316 }
1545 1317
1546 1318 return false;
1547 1319 }
@@ -1546,97 +1318,69 @@
1546 1318 return false;
1547 1319 }
1548 1320
1549 1321
1550 - public function import_sizes($sizes, $metadata, $full_size_file, $post_id) {
1551 - $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 + }
1552 1330
1553 - if (!empty($sizes)) {
1554 - 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 + }
1555 1339
1556 - foreach ($sizes as $size_name => $size) {
1557 - $size_dimension = $size['width'] . 'x' . $size['height'];
1558 - $size_name = !is_string($size_name) ? $size_dimension : $size_name;
1559 - $unique_destination_file = $this->create_unique_destination_file($full_size_file, $size);
1560 - // check non cropped sizes. with dynamic height
1561 - if (!$this->size_exists_in_metadata(basename($unique_destination_file), $metadata)) {
1562 - if ($unique_destination_file) {
1563 - $missing_size = $this->generate_missing_size_from_full($full_size_file, $unique_destination_file, $size);
1564 - if ($missing_size && !is_wp_error($missing_size)) {
1565 - unset($missing_size['path']);
1566 - $metadata['sizes'][$size_name] = $missing_size;
1567 - $metadata_modified = true;
1568 - do_action('templately_import.finalize_gutenberg_attachment', $post_id, $size_dimension);
1569 - }
1570 - }
1571 - }
1572 - }
1573 - }
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 + }
1574 1347
1575 - // Only return metadata if new sizes were actually added
1576 - return $metadata_modified ? $metadata : false;
1577 - }
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 + }
1578 1359
1579 - public function generate_missing_size_from_full($full_size_file, $destination_file, $size) {
1580 - // Generate the missing size from the full-size image
1581 - $editor = wp_get_image_editor($full_size_file);
1582 - if (is_wp_error($editor)) {
1583 - return $editor;
1584 - }
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 );
1585 1363
1586 - $resized = $editor->resize($size['width'], $size['height'], true);
1587 - if (is_wp_error($resized)) {
1588 - return $resized;
1589 - }
1364 + $size_dimensions = $this->extract_sizes($sizes);
1365 + $size_dimensions = array_filter($size_dimensions);
1590 1366
1591 - $saved = $editor->save($destination_file);
1592 - if (is_wp_error($saved)) {
1593 - return $saved;
1594 - }
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);
1595 1371
1596 - return $saved;
1597 - }
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 + }
1598 1378
1599 - public function size_exists_in_metadata($size_file, $metadata) {
1600 - if (isset($metadata['sizes']) && is_array($metadata['sizes'])) {
1601 - foreach ($metadata['sizes'] as $size_info) {
1602 - if ($size_info['file'] == $size_file) {
1603 - return true;
1604 - }
1605 - }
1606 - }
1607 - return false;
1608 - }
1379 + }
1380 + return $metadata;
1381 + }
1609 1382
1610 - public function create_unique_destination_file($full_size_file, $size) {
1611 - $pathinfo = pathinfo($full_size_file);
1612 - $directory = $pathinfo['dirname'];
1613 - $filename = $pathinfo['filename'];
1614 - $extension = $pathinfo['extension'];
1615 -
1616 - $destination_file = $directory . '/' . $filename . '-' . $size['width'] . 'x' . $size['height'] . '.' . $extension;
1617 -
1618 - // Skip if the file already exists
1619 - if (file_exists($destination_file)) {
1620 - // return false;
1621 - }
1622 -
1623 - return $destination_file;
1624 - }
1625 -
1626 - public function create_size_array($destination_file, $size_dimension) {
1627 - list($width, $height) = explode('x', $size_dimension);
1628 - return array(
1629 - 'file' => basename($destination_file),
1630 - 'width' => $width,
1631 - 'height' => $height,
1632 - 'mime-type' => wp_check_filetype($destination_file)['type'],
1633 - 'filesize' => filesize($destination_file),
1634 - 'resized' => false,
1635 - );
1636 - }
1637 -
1638 -
1639 1383 /**
1640 1384 * Attempt to associate posts and menu items with previously missing parents
1641 1385 *
1642 1386 * An imported post's parent may not have been imported when it was first created
@@ -1754,9 +1498,9 @@
1754 1498 */
1755 1499 private function is_valid_meta_key( $key ) {
1756 1500 // Skip attachment metadata since we'll regenerate it from scratch.
1757 1501 // Skip _edit_lock as not relevant for import
1758 - 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' ] ) ) {
1759 1503 return false;
1760 1504 }
1761 1505
1762 1506 return $key;
@@ -1803,9 +1547,9 @@
1803 1547 * @param $post_id
1804 1548 *
1805 1549 * @return void
1806 1550 */
1807 - public function update_post_meta( $post_id ) {
1551 + private function update_post_meta( $post_id ) {
1808 1552 foreach ( $this->posts_meta as $meta_key => $meta_value ) {
1809 1553 update_post_meta( $post_id, $meta_key, $meta_value );
1810 1554 }
1811 1555 }
@@ -1816,71 +1560,21 @@
1816 1560 return $this->output;
1817 1561 }
1818 1562
1819 1563 /**
1820 - * Register Templately with Jetpack Sync's known importers.
1821 - *
1822 - * This filter callback adds Templately to Jetpack's list of known importers,
1823 - * ensuring that 'templately' is sent to WordPress.com instead of the full class name.
1824 - * This provides cleaner analytics data and better tracking.
1825 - *
1826 - * @param array $known_importers Array of known importers with class names as keys and friendly names as values.
1827 - * @return array Modified array of known importers.
1828 - */
1829 - public function register_jetpack_importer( $known_importers ) {
1830 - $known_importers[ __CLASS__ ] = 'templately';
1831 - return $known_importers;
1832 - }
1833 -
1834 - /**
1835 - * There is deliberately NO `parent::__construct()` call here.
1836 - *
1837 - * `WP_Importer` declared an EMPTY constructor up to WordPress 7.0 and
1838 - * removed it in 7.1. Calling a parent constructor the parent does not
1839 - * declare throws `Error: Cannot call constructor`, so the call fataled every
1840 - * Full Site Import on 7.1 — and it had never done anything on the versions
1841 - * where it resolved, because the body was empty.
1842 - *
1843 - * Nothing contained it: `Error` is not an `Exception` in PHP 7+ (siblings
1844 - * under `Throwable`), and the outer import handlers catch `Exception`. The
1845 - * import streams over SSE, so the fatal landed mid-stream with no `error`
1846 - * event: the connection just stopped.
1847 - *
1848 - * Do not "restore" it for symmetry. `tests/unit/Core/Importer/test-WPImportConstruction.php`
1849 - * pins this, and only discriminates on a WP 7.1+ host.
1850 - *
1851 1564 * @param $file
1852 1565 * @param array $args
1853 1566 */
1854 1567 public function __construct( $file, array $args = [] ) {
1855 - $this->args = $args;
1856 - $this->session_id = $args['session_id'];
1568 + parent::__construct();
1857 1569
1858 - if ( ! empty( $args['json'] ) ) {
1859 - $this->json = $args['json'];
1860 - }
1570 + $this->requested_file_path = $file;
1571 + $this->args = $args;
1861 1572
1862 - if ( ! empty( $args['origin'] ) ) {
1863 - $this->origin = $args['origin'];
1864 - }
1865 -
1866 - if ( ! empty( $file ) ) {
1867 - $this->requested_file_path = $file;
1868 - $this->import_data_key = 'wp_importer_attributes_' . md5($this->requested_file_path);
1869 - }
1870 -
1871 1573 if ( ! empty( $this->args['fetch_attachments'] ) ) {
1872 1574 $this->fetch_attachments = true;
1873 1575 }
1874 1576
1875 - if ( isset( $this->args['attachment_timeout'] ) && is_numeric( $this->args['attachment_timeout'] ) ) {
1876 - $this->attachment_timeout = max( 1, (int) $this->args['attachment_timeout'] );
1877 - }
1878 -
1879 - if ( isset( $this->args['attachment_retries'] ) && is_numeric( $this->args['attachment_retries'] ) ) {
1880 - $this->attachment_retry_count = max( 1, (int) $this->args['attachment_retries'] + 1 );
1881 - }
1882 -
1883 1577 if ( isset( $this->args['posts'] ) && is_array( $this->args['posts'] ) ) {
1884 1578 $this->processed_posts = $this->args['posts'];
1885 1579 }
1886 1580
@@ -1898,9 +1592,6 @@
1898 1592
1899 1593 if ( ! empty( $this->args['terms_meta'] ) ) {
1900 1594 $this->terms_meta = $this->args['terms_meta'];
1901 1595 }
1902 -
1903 - // Register with Jetpack Sync for better analytics tracking.
1904 - add_filter( 'jetpack_sync_known_importers', [ $this, 'register_jetpack_importer' ] );
1905 1596 }
1906 1597 }