@@ -60,65 +60,62 @@ public function import( array $data ) {
6060
6161 switch_to_blog ( $ this ->import_coordinates ->dest_blog_id );
6262
63- if ( $ source_post_thumbnail_attachment instanceof \WP_Post ) {
64- // in some instances the folder sep. `/` might be duplicated, we de-duplicate it
65- array_walk (
66- $ source_upload_dir ,
67- function ( &$ entry ) {
68- $ entry = str_replace ( '// ' , '/ ' , $ entry );
69- }
70- );
71- $ source_uploads_dir = untrailingslashit (
72- str_replace (
73- $ source_upload_dir ['subdir ' ],
74- '' ,
75- $ source_upload_dir ['path ' ]
76- )
77- );
78- $ source_post_thumbnail_file = $ source_uploads_dir . '/ ' . $ source_post_thumbnail_meta ['_wp_attached_file ' ];
79-
80- // Check the type of file. We'll use this as the 'post_mime_type'.
81- $ filetype = wp_check_filetype ( basename ( $ source_post_thumbnail_file ), null );
82-
83- // Prepare an array of post data for the attachment.
84- $ attachment = array (
85- 'guid ' => $ source_post_thumbnail_attachment ->guid ,
86- 'post_mime_type ' => $ filetype ['type ' ],
87- 'post_title ' => preg_replace ( '/\.[^.]+$/ ' , '' , basename ( $ source_post_thumbnail_file ) ),
88- 'post_content ' => '' ,
89- 'post_status ' => 'inherit ' ,
90- );
63+ // in some instances, the folder sep. `/` might be duplicated, we de-duplicate it
64+ array_walk (
65+ $ source_upload_dir ,
66+ function ( &$ entry ) {
67+ $ entry = str_replace ( '// ' , '/ ' , $ entry );
68+ }
69+ );
70+ $ source_uploads_dir = untrailingslashit (
71+ str_replace (
72+ $ source_upload_dir ['subdir ' ],
73+ '' ,
74+ $ source_upload_dir ['path ' ]
75+ )
76+ );
77+ $ source_post_thumbnail_file = $ source_uploads_dir . '/ ' . $ source_post_thumbnail_meta ['_wp_attached_file ' ];
78+
79+ // Check the type of file. We'll use this as the 'post_mime_type'.
80+ $ filetype = wp_check_filetype ( basename ( $ source_post_thumbnail_file ), null );
81+
82+ // Prepare an array of post data for the attachment.
83+ $ attachment = array (
84+ 'guid ' => $ source_post_thumbnail_attachment ->guid ,
85+ 'post_mime_type ' => $ filetype ['type ' ],
86+ 'post_title ' => preg_replace ( '/\.[^.]+$/ ' , '' , basename ( $ source_post_thumbnail_file ) ),
87+ 'post_content ' => '' ,
88+ 'post_status ' => 'inherit ' ,
89+ );
9190
92- $ existing_criteria = array (
91+ $ found = get_posts (
92+ array (
9393 'post_type ' => 'attachment ' ,
9494 'title ' => $ attachment ['post_title ' ],
95+ )
96+ );
97+ if ( isset ( $ found [0 ]->ID ) ) {
98+ $ dest_post_thumbnail_id = $ found [0 ]->ID ;
99+ $ this ->logger ->log_success ( 'post-thumbnail/existing ' , $ dest_post_thumbnail_id );
100+ } else {
101+ // Insert the attachment.
102+ $ dest_post_thumbnail_id = wp_insert_attachment (
103+ $ attachment ,
104+ $ source_post_thumbnail_file ,
105+ $ dest_post_id
95106 );
96107
97- $ found = get_posts ( $ existing_criteria );
98-
99- if ( $ found && $ found [0 ] instanceof \WP_Post ) {
100- $ dest_post_thumbnail_id = $ found [0 ]->ID ;
101- $ this ->logger ->log_success ( 'post-thumbnail/existing ' , $ dest_post_thumbnail_id );
108+ if ( empty ( $ dest_post_thumbnail_id ) ) {
109+ $ this ->logger ->log_error ( 'post-thumbnail/created ' , $ dest_post_thumbnail_id );
102110 } else {
103- // Insert the attachment.
104- $ dest_post_thumbnail_id = wp_insert_attachment (
105- $ attachment ,
106- $ source_post_thumbnail_file ,
107- $ dest_post_id
108- );
109-
110- if ( empty ( $ dest_post_thumbnail_id ) ) {
111- $ this ->logger ->log_error ( 'post-thumbnail/created ' , $ dest_post_thumbnail_id );
112- } else {
113- $ this ->logger ->log_success ( 'post-thumbnail/created ' , $ dest_post_thumbnail_id );
114- }
115-
116- // the `_wp_attached_file` meta has been set before, so we skip it
117- unset( $ source_post_thumbnail_meta ['_wp_attached_file ' ] );
118-
119- foreach ( $ source_post_thumbnail_meta as $ key => $ value ) {
120- add_post_meta ( $ dest_post_thumbnail_id , $ key , $ value , true );
121- }
111+ $ this ->logger ->log_success ( 'post-thumbnail/created ' , $ dest_post_thumbnail_id );
112+ }
113+
114+ // the `_wp_attached_file` meta has been set before, so we skip it
115+ unset( $ source_post_thumbnail_meta ['_wp_attached_file ' ] );
116+
117+ foreach ( $ source_post_thumbnail_meta as $ key => $ value ) {
118+ add_post_meta ( $ dest_post_thumbnail_id , $ key , $ value , true );
122119 }
123120
124121 update_post_meta (
0 commit comments