From 6d1ed1715b3bfc5953076306792bef4ac5f00f03 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Wed, 21 Feb 2024 18:15:14 +0000 Subject: [PATCH] Export: Include featured image for posts or pages. This bugfix resolves an issue in `export_wp()` with featured images. When using Tools > Export and selecting either Posts or Pages (with or without a specific author), the resulting XML file now includes a XML item for each post|page's featured image attachment and its metadata. Uses same chunking (for performance) and code patterns from existing code in the same file. Adds a new test class for `export_wp()` with code coverage specific to this bugfix. Follow-up to [34326], [14444], [6375], [6335]. Props billseymour, nateallen, petitphp, hellofromTonya, duck_, jane, rcain, jghazally, jghazally, smub, batmoo, axwax, creativeslice, dlocc, nacin, wonderboymusic, ganon, SergeyBiryukov, hlashbrooke, chriscct7, fischfood, hifidesign, ankit-k-gupta, 5um17, shailu25, huzaifaalmesbah, mukesh27. Fixes #17379. Built from https://develop.svn.wordpress.org/trunk@57681 git-svn-id: http://core.svn.wordpress.org/trunk@57182 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/export.php | 46 ++++++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/wp-admin/includes/export.php b/wp-admin/includes/export.php index 9610ac8c9f..d05f98f734 100644 --- a/wp-admin/includes/export.php +++ b/wp-admin/includes/export.php @@ -144,6 +144,52 @@ function export_wp( $args = array() ) { // Grab a snapshot of post IDs, just in case it changes during the export. $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" ); + // Get IDs for the attachments of each post, unless all content is already being exported. + if ( ! in_array( $args['content'], array( 'all', 'attachment' ), true ) ) { + // Array to hold all additional IDs (attachments and thumbnails). + $additional_ids = array(); + + // Create a copy of the post IDs array to avoid modifying the original array. + $processing_ids = $post_ids; + + while ( $next_posts = array_splice( $processing_ids, 0, 20 ) ) { + $posts_in = array_map( 'absint', $next_posts ); + $placeholders = array_fill( 0, count( $posts_in ), '%d' ); + + // Create a string for the placeholders. + $in_placeholder = implode( ',', $placeholders ); + + // Prepare the SQL statement for attachment ids. + $attachment_ids = $wpdb->get_col( + $wpdb->prepare( + " + SELECT ID + FROM $wpdb->posts + WHERE post_parent IN ($in_placeholder) AND post_type = 'attachment' + ", + $posts_in + ) + ); + + $thumbnails_ids = $wpdb->get_col( + $wpdb->prepare( + " + SELECT meta_value + FROM $wpdb->postmeta + WHERE $wpdb->postmeta.post_id IN ($in_placeholder) + AND $wpdb->postmeta.meta_key = '_thumbnail_id' + ", + $posts_in + ) + ); + + $additional_ids = array_merge( $additional_ids, $attachment_ids, $thumbnails_ids ); + } + + // Merge the additional IDs back with the original post IDs after processing all posts + $post_ids = array_unique( array_merge( $post_ids, $additional_ids ) ); + } + /* * Get the requested terms ready, empty unless posts filtered by category * or all content. diff --git a/wp-includes/version.php b/wp-includes/version.php index 022c7b4964..3050757503 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-beta2-57680'; +$wp_version = '6.5-beta2-57681'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.