diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2023-05-21 12:34:05 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2023-05-21 12:34:05 +0200 |
commit | f824ab488c07eafeaf26b763dad83779a2a7581a (patch) | |
tree | 4385aecced7f4183a788cdcc5359e0b737831720 | |
parent | e3d29ee8cb3b314bc6b068bd67dd06a752beb2cb (diff) | |
download | migrate-f824ab488c07eafeaf26b763dad83779a2a7581a.tar.gz migrate-f824ab488c07eafeaf26b763dad83779a2a7581a.tar.bz2 migrate-f824ab488c07eafeaf26b763dad83779a2a7581a.zip |
Set post thumbnail from custom teaser if present.
-rw-r--r-- | import-posts.php | 88 |
1 files changed, 45 insertions, 43 deletions
diff --git a/import-posts.php b/import-posts.php index 70aa7a4..9c872c0 100644 --- a/import-posts.php +++ b/import-posts.php @@ -137,49 +137,51 @@ function migrate_images( array $meta ) : array { $thumb = null; - if ( isset( $meta['body'] ) ) { - $body = preg_replace_callback( - '/<img src="([^"]*)" title="([^"]*)" alt="([^"]*)"[^>]*>/', - function( array $matches ) use ( $post_date, &$thumb ) { - $remote_filename = get_real_image_name( $matches[1] ); - $filename = basename( $remote_filename ); - $title = $matches[2]; - $alt = $matches[3]; - - $uploads = wp_upload_dir( $post_date->format( 'Y/m' ) ); - $target_file = implode( '/', array( $uploads['path'], $filename ) ); - $target_url = implode( '/', array( $uploads['url'], $filename ) ); - - import_photo( $remote_filename, $target_file ); - - $mime = wp_get_image_mime( $target_file ); - if ( ! $mime ) { - die( "Error: Unknown mime type for {$target_file}" . PHP_EOL ); - } - - $attachment_id = wp_insert_attachment( - array( - 'post_mime_type' => $mime, - 'guid' => $target_url, - 'post_paren' => 0, - 'post_title' => $filename, - ), - $target_file - ); - - if ( 0 === $attachment_id ) { - die( "Error: Could not create attachment for {$target_file}" . PHP_EOL ); - } - - if ( null === $thumb ) { - $thumb = $attachment_id; - return '<!-- image moved to thumbnail -->'; - } else { - return "<img src=\"{$target_url}\" title=\"{$title}\" alt=\"{$alt}\">"; - } - }, - $meta['body'], - ); + foreach ( array( 'custom_teaser', 'body' ) as $section ) { + if ( isset( $meta[$section] ) ) { + $body = preg_replace_callback( + '/<img src="([^"]*)" title="([^"]*)" alt="([^"]*)"[^>]*>/', + function( array $matches ) use ( $post_date, &$thumb ) { + $remote_filename = get_real_image_name( $matches[1] ); + $filename = basename( $remote_filename ); + $title = $matches[2]; + $alt = $matches[3]; + + $uploads = wp_upload_dir( $post_date->format( 'Y/m' ) ); + $target_file = implode( '/', array( $uploads['path'], $filename ) ); + $target_url = implode( '/', array( $uploads['url'], $filename ) ); + + import_photo( $remote_filename, $target_file ); + + $mime = wp_get_image_mime( $target_file ); + if ( ! $mime ) { + die( "Error: Unknown mime type for {$target_file}" . PHP_EOL ); + } + + $attachment_id = wp_insert_attachment( + array( + 'post_mime_type' => $mime, + 'guid' => $target_url, + 'post_paren' => 0, + 'post_title' => $filename, + ), + $target_file + ); + + if ( 0 === $attachment_id ) { + die( "Error: Could not create attachment for {$target_file}" . PHP_EOL ); + } + + if ( null === $thumb ) { + $thumb = $attachment_id; + return '<!-- image moved to thumbnail -->'; + } else { + return "<img src=\"{$target_url}\" title=\"{$title}\" alt=\"{$alt}\">"; + } + }, + $meta[$section], + ); + } } return array( $body, $thumb ); |