@@ -77,7 +77,6 @@ class=[\'"].*?\b(wp-image-\d+|attachment-[\w\-]+)\b
77
77
* @var bool $prefill_existing_comments Should we prefill `comment_exists` calls? (True prefills and uses more memory, false checks once per imported comment and takes longer. Default is true.)
78
78
* @var bool $prefill_existing_terms Should we prefill `term_exists` calls? (True prefills and uses more memory, false checks once per imported term and takes longer. Default is true.)
79
79
* @var bool $update_attachment_guids Should attachment GUIDs be updated to the new URL? (True updates the GUID, which keeps compatibility with v1, false doesn't update, and allows deduplication and reimporting. Default is false.)
80
- * @var bool $fetch_attachments Fetch attachments from the remote server. (True fetches and creates attachment posts, false skips attachments. Default is false.)
81
80
* @var int $default_author User ID to use if author is missing or invalid. (Default is null, which leaves posts unassigned.)
82
81
* }
83
82
*/
@@ -104,7 +103,6 @@ public function __construct( $options = array() ) {
104
103
'prefill_existing_comments ' => true ,
105
104
'prefill_existing_terms ' => true ,
106
105
'update_attachment_guids ' => false ,
107
- 'fetch_attachments ' => false ,
108
106
'default_author ' => null ,
109
107
)
110
108
);
@@ -453,6 +451,8 @@ public function import_post( $data ) {
453
451
return false ;
454
452
}
455
453
454
+ $ meta = array ();
455
+
456
456
$ original_id = isset ( $ data ['post_id ' ] ) ? (int ) $ data ['post_id ' ] : 0 ;
457
457
$ parent_id = isset ( $ data ['post_parent ' ] ) ? (int ) $ data ['post_parent ' ] : 0 ;
458
458
@@ -464,7 +464,6 @@ public function import_post( $data ) {
464
464
465
465
$ post_type = $ data ['post_type ' ] ?? 'post ' ;
466
466
$ post_type_object = get_post_type_object ( $ post_type );
467
-
468
467
// Is this type even valid?
469
468
if ( ! $ post_type_object ) {
470
469
$ this ->logger ->warning (
@@ -542,6 +541,7 @@ public function import_post( $data ) {
542
541
'menu_order ' => true ,
543
542
'post_type ' => true ,
544
543
'post_password ' => true ,
544
+ 'local_file_path ' => true ,
545
545
);
546
546
foreach ( $ data as $ key => $ value ) {
547
547
if ( ! isset ( $ allowed [ $ key ] ) ) {
@@ -550,31 +550,17 @@ public function import_post( $data ) {
550
550
551
551
$ postdata [ $ key ] = $ data [ $ key ];
552
552
}
553
+ if ( ! isset ( $ postdata ['post_date ' ] ) ) {
554
+ $ postdata ['post_date ' ] = gmdate ( 'Y-m-d H:i:s ' );
555
+ }
556
+ if ( ! isset ( $ postdata ['post_date_gmt ' ] ) ) {
557
+ $ postdata ['post_date_gmt ' ] = gmdate ( 'Y-m-d H:i:s ' );
558
+ }
553
559
554
560
$ postdata = apply_filters ( 'wp_import_post_data_processed ' , $ postdata , $ data );
555
561
556
562
if ( isset ( $ postdata ['post_type ' ] ) && 'attachment ' === $ postdata ['post_type ' ] ) {
557
- // @TODO: Do not download any attachments here. We're just inserting the data
558
- // at this point. All the downloads have already been processed by now.
559
- if ( ! $ this ->options ['fetch_attachments ' ] ) {
560
- $ this ->logger ->notice (
561
- sprintf (
562
- /* translators: %s: post title */
563
- __ ( 'Skipping attachment "%s", fetching attachments disabled ' ),
564
- $ data ['post_title ' ]
565
- )
566
- );
567
- /**
568
- * Post processing skipped.
569
- *
570
- * @param array $data Raw data imported for the post.
571
- * @param array $meta Raw meta data, already processed by {@see process_post_meta}.
572
- */
573
- do_action ( 'wxr_importer_process_skipped_post ' , $ data );
574
- return false ;
575
- }
576
- $ remote_url = ! empty ( $ data ['attachment_url ' ] ) ? $ data ['attachment_url ' ] : $ data ['guid ' ];
577
- $ post_id = $ this ->process_attachment ( $ postdata , $ meta , $ remote_url );
563
+ $ post_id = $ this ->process_attachment ( $ postdata , $ meta );
578
564
} else {
579
565
$ post_id = wp_insert_post ( $ postdata , true );
580
566
do_action ( 'wp_import_insert_post ' , $ post_id , $ original_id , $ postdata , $ data );
@@ -757,7 +743,11 @@ protected function process_menu_item_meta( $post_id, $data, $meta ) {
757
743
* @param string $url URL to fetch attachment from
758
744
* @return int|WP_Error Post ID on success, WP_Error otherwise
759
745
*/
760
- protected function process_attachment ( $ post , $ meta , $ remote_url ) {
746
+ protected function process_attachment ( $ post , $ meta ) {
747
+ if ( ! isset ( $ post ['local_file_path ' ] ) || ! file_exists ( $ post ['local_file_path ' ] ) ) {
748
+ return new WP_Error ( 'attachment_processing_error ' , __ ( 'File does not exist ' , 'wordpress-importer ' ) );
749
+ }
750
+
761
751
// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
762
752
// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
763
753
$ post ['upload_date ' ] = $ post ['post_date ' ];
@@ -772,47 +762,26 @@ protected function process_attachment( $post, $meta, $remote_url ) {
772
762
break ;
773
763
}
774
764
775
- // if the URL is absolute, but does not contain address, then upload it assuming base_site_url
776
- if ( preg_match ( '|^/[\w\W]+$| ' , $ remote_url ) ) {
777
- $ remote_url = rtrim ( $ this ->base_url , '/ ' ) . $ remote_url ;
778
- }
779
-
780
- $ upload = $ this ->fetch_remote_file ( $ remote_url , $ post );
781
- if ( is_wp_error ( $ upload ) ) {
782
- return $ upload ;
783
- }
784
-
785
- $ info = wp_check_filetype ( $ upload ['file ' ] );
765
+ $ info = wp_check_filetype ( $ post ['local_file_path ' ] );
786
766
if ( ! $ info ) {
787
767
return new WP_Error ( 'attachment_processing_error ' , __ ( 'Invalid file type ' , 'wordpress-importer ' ) );
788
768
}
789
769
790
770
$ post ['post_mime_type ' ] = $ info ['type ' ];
791
771
792
- // WP really likes using the GUID for display. Allow updating it.
793
- // See https://core.trac.wordpress.org/ticket/33386
794
- if ( $ this ->options ['update_attachment_guids ' ] ) {
795
- $ post ['guid ' ] = $ upload ['url ' ];
796
- }
797
-
798
772
// as per wp-admin/includes/upload.php
799
- $ post_id = wp_insert_attachment ( $ post , $ upload [ ' file ' ] );
773
+ $ post_id = wp_insert_attachment ( $ post , $ post [ ' local_file_path ' ] );
800
774
if ( is_wp_error ( $ post_id ) ) {
801
775
return $ post_id ;
802
776
}
803
777
804
- $ attachment_metadata = wp_generate_attachment_metadata ( $ post_id , $ upload ['file ' ] );
805
- wp_update_attachment_metadata ( $ post_id , $ attachment_metadata );
806
-
807
- // Map this image URL later if we need to
808
- $ this ->url_remap [ $ remote_url ] = $ upload ['url ' ];
809
-
810
- // If we have a HTTPS URL, ensure the HTTP URL gets replaced too
811
- if ( substr ( $ remote_url , 0 , 8 ) === 'https:// ' ) {
812
- $ insecure_url = 'http ' . substr ( $ remote_url , 5 );
813
- $ this ->url_remap [ $ insecure_url ] = $ upload ['url ' ];
778
+ if ( ! function_exists ( 'wp_generate_attachment_metadata ' ) ) {
779
+ include ABSPATH . 'wp-admin/includes/image.php ' ;
814
780
}
815
781
782
+ $ attachment_metadata = wp_generate_attachment_metadata ( $ post_id , $ post ['local_file_path ' ] );
783
+ wp_update_attachment_metadata ( $ post_id , $ attachment_metadata );
784
+
816
785
return $ post_id ;
817
786
}
818
787
@@ -865,7 +834,7 @@ public function import_attachment( $filepath, $post_id ) {
865
834
* @return int|WP_Error Number of meta items imported on success, error otherwise.
866
835
*/
867
836
public function import_post_meta ( $ meta_item , $ post_id ) {
868
- if ( empty ( $ meta ) ) {
837
+ if ( empty ( $ meta_item ) ) {
869
838
return true ;
870
839
}
871
840
@@ -880,7 +849,7 @@ public function import_post_meta( $meta_item, $post_id ) {
880
849
return false ;
881
850
}
882
851
883
- $ key = apply_filters ( 'import_post_meta_key ' , $ meta_item ['key ' ], $ post_id, $ post );
852
+ $ key = apply_filters ( 'import_post_meta_key ' , $ meta_item ['key ' ], $ post_id );
884
853
$ value = false ;
885
854
886
855
if ( '_edit_last ' === $ key ) {
@@ -900,7 +869,7 @@ public function import_post_meta( $meta_item, $post_id ) {
900
869
$ value = maybe_unserialize ( $ meta_item ['value ' ] );
901
870
}
902
871
903
- add_post_meta ( $ post_id , $ key , $ value );
872
+ update_post_meta ( $ post_id , $ key , $ value );
904
873
do_action ( 'import_post_meta ' , $ post_id , $ key , $ value );
905
874
906
875
// if the post has a featured image, take note of this in case of remap
0 commit comments