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
This commit is contained in:
hellofromTonya 2024-02-21 18:15:14 +00:00
parent daf4121ae2
commit 6d1ed1715b
2 changed files with 47 additions and 1 deletions

View File

@ -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.

View File

@ -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.