Skip to content

Commit 0080f2a

Browse files
committed
[Data Liberation] Don't download assets in WP_Entity_Importer, use the same entity shape as the WP_Stream_Importer produces
Assets downloading is handled by WP_Stream_Importer. WP_Entity_Importer only goal is to insert/update the entities into the database.
1 parent e74077d commit 0080f2a

File tree

1 file changed

+25
-56
lines changed

1 file changed

+25
-56
lines changed

packages/playground/data-liberation/src/import/WP_Entity_Importer.php

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class=[\'"].*?\b(wp-image-\d+|attachment-[\w\-]+)\b
7777
* @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.)
7878
* @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.)
7979
* @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.)
8180
* @var int $default_author User ID to use if author is missing or invalid. (Default is null, which leaves posts unassigned.)
8281
* }
8382
*/
@@ -104,7 +103,6 @@ public function __construct( $options = array() ) {
104103
'prefill_existing_comments' => true,
105104
'prefill_existing_terms' => true,
106105
'update_attachment_guids' => false,
107-
'fetch_attachments' => false,
108106
'default_author' => null,
109107
)
110108
);
@@ -453,6 +451,8 @@ public function import_post( $data ) {
453451
return false;
454452
}
455453

454+
$meta = array();
455+
456456
$original_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : 0;
457457
$parent_id = isset( $data['post_parent'] ) ? (int) $data['post_parent'] : 0;
458458

@@ -464,7 +464,6 @@ public function import_post( $data ) {
464464

465465
$post_type = $data['post_type'] ?? 'post';
466466
$post_type_object = get_post_type_object( $post_type );
467-
468467
// Is this type even valid?
469468
if ( ! $post_type_object ) {
470469
$this->logger->warning(
@@ -542,6 +541,7 @@ public function import_post( $data ) {
542541
'menu_order' => true,
543542
'post_type' => true,
544543
'post_password' => true,
544+
'local_file_path' => true,
545545
);
546546
foreach ( $data as $key => $value ) {
547547
if ( ! isset( $allowed[ $key ] ) ) {
@@ -550,31 +550,17 @@ public function import_post( $data ) {
550550

551551
$postdata[ $key ] = $data[ $key ];
552552
}
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+
}
553559

554560
$postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $data );
555561

556562
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 );
578564
} else {
579565
$post_id = wp_insert_post( $postdata, true );
580566
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 ) {
757743
* @param string $url URL to fetch attachment from
758744
* @return int|WP_Error Post ID on success, WP_Error otherwise
759745
*/
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+
761751
// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
762752
// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
763753
$post['upload_date'] = $post['post_date'];
@@ -772,47 +762,26 @@ protected function process_attachment( $post, $meta, $remote_url ) {
772762
break;
773763
}
774764

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'] );
786766
if ( ! $info ) {
787767
return new WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'wordpress-importer' ) );
788768
}
789769

790770
$post['post_mime_type'] = $info['type'];
791771

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-
798772
// 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'] );
800774
if ( is_wp_error( $post_id ) ) {
801775
return $post_id;
802776
}
803777

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';
814780
}
815781

782+
$attachment_metadata = wp_generate_attachment_metadata( $post_id, $post['local_file_path'] );
783+
wp_update_attachment_metadata( $post_id, $attachment_metadata );
784+
816785
return $post_id;
817786
}
818787

@@ -865,7 +834,7 @@ public function import_attachment( $filepath, $post_id ) {
865834
* @return int|WP_Error Number of meta items imported on success, error otherwise.
866835
*/
867836
public function import_post_meta( $meta_item, $post_id ) {
868-
if ( empty( $meta ) ) {
837+
if ( empty( $meta_item ) ) {
869838
return true;
870839
}
871840

@@ -880,7 +849,7 @@ public function import_post_meta( $meta_item, $post_id ) {
880849
return false;
881850
}
882851

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 );
884853
$value = false;
885854

886855
if ( '_edit_last' === $key ) {
@@ -900,7 +869,7 @@ public function import_post_meta( $meta_item, $post_id ) {
900869
$value = maybe_unserialize( $meta_item['value'] );
901870
}
902871

903-
add_post_meta( $post_id, $key, $value );
872+
update_post_meta( $post_id, $key, $value );
904873
do_action( 'import_post_meta', $post_id, $key, $value );
905874

906875
// if the post has a featured image, take note of this in case of remap

0 commit comments

Comments
 (0)