Make WordPress Core

Changeset 57681


Ignore:
Timestamp:
02/21/2024 06:13:27 PM (5 months ago)
Author:
hellofromTonya
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/export.php

    r55942 r57681  
    144144    // Grab a snapshot of post IDs, just in case it changes during the export.
    145145    $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
     146
     147    // Get IDs for the attachments of each post, unless all content is already being exported.
     148    if ( ! in_array( $args['content'], array( 'all', 'attachment' ), true ) ) {
     149        // Array to hold all additional IDs (attachments and thumbnails).
     150        $additional_ids = array();
     151
     152        // Create a copy of the post IDs array to avoid modifying the original array.
     153        $processing_ids = $post_ids;
     154
     155        while ( $next_posts = array_splice( $processing_ids, 0, 20 ) ) {
     156            $posts_in     = array_map( 'absint', $next_posts );
     157            $placeholders = array_fill( 0, count( $posts_in ), '%d' );
     158
     159            // Create a string for the placeholders.
     160            $in_placeholder = implode( ',', $placeholders );
     161
     162            // Prepare the SQL statement for attachment ids.
     163            $attachment_ids = $wpdb->get_col(
     164                $wpdb->prepare(
     165                    "
     166                SELECT ID
     167                FROM $wpdb->posts
     168                WHERE post_parent IN ($in_placeholder) AND post_type = 'attachment'
     169                    ",
     170                    $posts_in
     171                )
     172            );
     173
     174            $thumbnails_ids = $wpdb->get_col(
     175                $wpdb->prepare(
     176                    "
     177                SELECT meta_value
     178                FROM $wpdb->postmeta
     179                WHERE $wpdb->postmeta.post_id IN ($in_placeholder)
     180                AND $wpdb->postmeta.meta_key = '_thumbnail_id'
     181                    ",
     182                    $posts_in
     183                )
     184            );
     185
     186            $additional_ids = array_merge( $additional_ids, $attachment_ids, $thumbnails_ids );
     187        }
     188
     189        // Merge the additional IDs back with the original post IDs after processing all posts
     190        $post_ids = array_unique( array_merge( $post_ids, $additional_ids ) );
     191    }
    146192
    147193    /*
Note: See TracChangeset for help on using the changeset viewer.