Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import attachment posts from staging #93

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions env/import-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ 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, $time = null ) {
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, $time );
}

/**
* Import posts from a remote REST API to the local test site.
*
Expand Down Expand Up @@ -87,13 +114,28 @@ 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_parent' => $post->parent,
'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 ?? null,
'comment_status' => $post->comment_status,
'meta_input' => sanitize_meta_input( $post->meta ),
);

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, gmdate( 'Y/m', strtotime( $post->date ) ) );

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 );

if ( $existing_post ) {
Expand All @@ -117,6 +159,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 );
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion env/refresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
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'"