From 0d0b0b4ac1098e8816f269368dbf96948a13dd27 Mon Sep 17 00:00:00 2001 From: tellyworth Date: Wed, 17 Aug 2022 18:11:38 +1000 Subject: [PATCH 1/4] Import attachment posts from staging --- env/import-content.php | 14 ++++++++++++-- env/refresh.sh | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/env/import-content.php b/env/import-content.php index d5948aa5..6a6a3fb4 100644 --- a/env/import-content.php +++ b/env/import-content.php @@ -87,13 +87,17 @@ function import_rest_to_posts( $rest_url ) { 'post_status' => $post->status, 'post_type' => $post->type, 'post_title' => $post->title->rendered, - 'post_content' => ( $post->content_raw ?? $post->content->rendered ), - 'post_excerpt' => wp_strip_all_tags( $post->excerpt->rendered ), + 'post_content' => ( $post->content_raw ?? $post->content->rendered ?? $post->description->rendered ), + 'post_excerpt' => ( isset( $post->excerpt ) ? wp_strip_all_tags( $post->excerpt->rendered ) : null ), 'post_parent' => $post->parent, 'comment_status' => $post->comment_status, 'meta_input' => sanitize_meta_input( $post->meta ), ); + if ( 'attachment' === $post->type && intval( $post->post ) > 0 ) { + $new_post[ 'post_parent' ] = $post->post; + } + $existing_post = get_post( $post->id, ARRAY_A ); if ( $existing_post ) { @@ -117,6 +121,12 @@ function import_rest_to_posts( $rest_url ) { } echo "Inserted $post->type $post->id as $new_post_id\n\n"; + + if ( !empty ( $post->media_details ) ) { + // This is probably not a perfect match of field names etc. + $media_details_array = json_decode( json_encode( $post->media_details ), true, 10 ); + wp_update_attachment_metadata( $new_post_id , $media_details_array ); + } } } diff --git a/env/refresh.sh b/env/refresh.sh index 18dbfd69..4ca51c58 100755 --- a/env/refresh.sh +++ b/env/refresh.sh @@ -3,4 +3,5 @@ # Refresh the content from the server npm run wp-env run cli "php env/import-content.php --url 'https://wordpress.org/main-test/wp-json/wp/v2/posts?context=wporg_export&per_page=50'" -npm run wp-env run cli "php env/import-content.php --url 'https://wordpress.org/main-test/wp-json/wp/v2/pages?context=wporg_export&per_page=50'" \ No newline at end of file +npm run wp-env run cli "php env/import-content.php --url 'https://wordpress.org/main-test/wp-json/wp/v2/pages?context=wporg_export&per_page=50'" +npm run wp-env run cli "php env/import-content.php --url 'https://wordpress.org/main-test/wp-json/wp/v2/media?per_page=50'" \ No newline at end of file From cdd5f5a4a44d3c3e79ab6ef763f3b3670648f44e Mon Sep 17 00:00:00 2001 From: tellyworth Date: Mon, 22 Aug 2022 17:04:23 +1000 Subject: [PATCH 2/4] Sideload image files --- env/import-content.php | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/env/import-content.php b/env/import-content.php index 6a6a3fb4..8a7b15f6 100644 --- a/env/import-content.php +++ b/env/import-content.php @@ -54,6 +54,29 @@ function filter_curl_options( $ch ) { curl_setopt( $ch, CURLOPT_CONNECT_TO, array( 'wordpress.org::w.org:' ) ); } +function sideload_from_url( $remote_url ) { + require_once( ABSPATH . 'wp-admin/includes/file.php' ); + + $temp_file = download_url( $remote_url ); + + if ( is_wp_error( $temp_file ) ) { + die( esc_html( $temp_file->get_error_message() ) ); + } + + $file_array = array( + 'name' => basename( $remote_url ), + 'tmp_name' => $temp_file, + 'error' => 0, + 'size' => filesize( $temp_file ), + ); + + $overrides = array( + 'test_form' => false, + ); + + return wp_handle_sideload( $file_array, $overrides ); +} + /** * Import posts from a remote REST API to the local test site. * @@ -89,13 +112,24 @@ function import_rest_to_posts( $rest_url ) { 'post_title' => $post->title->rendered, 'post_content' => ( $post->content_raw ?? $post->content->rendered ?? $post->description->rendered ), 'post_excerpt' => ( isset( $post->excerpt ) ? wp_strip_all_tags( $post->excerpt->rendered ) : null ), - 'post_parent' => $post->parent, + 'post_parent' => $post->parent ?? null, 'comment_status' => $post->comment_status, 'meta_input' => sanitize_meta_input( $post->meta ), ); - if ( 'attachment' === $post->type && intval( $post->post ) > 0 ) { - $new_post[ 'post_parent' ] = $post->post; + if ( 'attachment' === $post->type ) { + if ( intval( $post->post ) > 0 ) { + $new_post[ 'post_parent' ] = $post->post; + } + + // guid is the best source of the unscaled original image + $uploaded_file = sideload_from_url( $post->guid->rendered ); + + if ( is_wp_error( $uploaded_file ) ) { + die( esc_html( $uploaded_file->get_error_message() ) ); + } + + $new_post[ 'file' ] = $uploaded_file[ 'file' ]; } $existing_post = get_post( $post->id, ARRAY_A ); From 42fd6df28f6f309a7f6ccc85c96b29ed247f9702 Mon Sep 17 00:00:00 2001 From: tellyworth Date: Mon, 22 Aug 2022 17:06:19 +1000 Subject: [PATCH 3/4] Lint fixes --- env/import-content.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/env/import-content.php b/env/import-content.php index 8a7b15f6..95212f06 100644 --- a/env/import-content.php +++ b/env/import-content.php @@ -54,6 +54,10 @@ function filter_curl_options( $ch ) { curl_setopt( $ch, CURLOPT_CONNECT_TO, array( 'wordpress.org::w.org:' ) ); } +/** + * Download a remote URL and sideload it into the upload directory. + * Doesn't create an attachment. + */ function sideload_from_url( $remote_url ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); @@ -119,7 +123,7 @@ function import_rest_to_posts( $rest_url ) { if ( 'attachment' === $post->type ) { if ( intval( $post->post ) > 0 ) { - $new_post[ 'post_parent' ] = $post->post; + $new_post['post_parent'] = $post->post; } // guid is the best source of the unscaled original image @@ -129,7 +133,7 @@ function import_rest_to_posts( $rest_url ) { die( esc_html( $uploaded_file->get_error_message() ) ); } - $new_post[ 'file' ] = $uploaded_file[ 'file' ]; + $new_post['file'] = $uploaded_file['file']; } $existing_post = get_post( $post->id, ARRAY_A ); @@ -156,10 +160,10 @@ function import_rest_to_posts( $rest_url ) { echo "Inserted $post->type $post->id as $new_post_id\n\n"; - if ( !empty ( $post->media_details ) ) { + if ( ! empty( $post->media_details ) ) { // This is probably not a perfect match of field names etc. $media_details_array = json_decode( json_encode( $post->media_details ), true, 10 ); - wp_update_attachment_metadata( $new_post_id , $media_details_array ); + wp_update_attachment_metadata( $new_post_id, $media_details_array ); } } } From 46b2ddfcd914888a7030ebf47c8ead4b9dd3af01 Mon Sep 17 00:00:00 2001 From: tellyworth Date: Mon, 22 Aug 2022 17:11:41 +1000 Subject: [PATCH 4/4] Use the correct date for the upload dir --- env/import-content.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/env/import-content.php b/env/import-content.php index 95212f06..8b49436a 100644 --- a/env/import-content.php +++ b/env/import-content.php @@ -58,7 +58,7 @@ function filter_curl_options( $ch ) { * Download a remote URL and sideload it into the upload directory. * Doesn't create an attachment. */ -function sideload_from_url( $remote_url ) { +function sideload_from_url( $remote_url, $time = null ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); $temp_file = download_url( $remote_url ); @@ -78,7 +78,7 @@ function sideload_from_url( $remote_url ) { 'test_form' => false, ); - return wp_handle_sideload( $file_array, $overrides ); + return wp_handle_sideload( $file_array, $overrides, $time ); } /** @@ -127,7 +127,7 @@ function import_rest_to_posts( $rest_url ) { } // guid is the best source of the unscaled original image - $uploaded_file = sideload_from_url( $post->guid->rendered ); + $uploaded_file = sideload_from_url( $post->guid->rendered, gmdate( 'Y/m', strtotime( $post->date ) ) ); if ( is_wp_error( $uploaded_file ) ) { die( esc_html( $uploaded_file->get_error_message() ) );