<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[57681] trunk: Export: Include featured image for posts or pages.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/57681">57681</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/57681","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-02-21 18:13:27 +0000 (Wed, 21 Feb 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/changeset/34326">[34326]</a>, <a href="https://core.trac.wordpress.org/changeset/14444">[14444]</a>, <a href="https://core.trac.wordpress.org/changeset/6375">[6375]</a>, <a href="https://core.trac.wordpress.org/changeset/6335">[6335]</a>.
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 <a href="https://core.trac.wordpress.org/ticket/17379">#17379</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesexportphp">trunk/src/wp-admin/includes/export.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsadminexportWpphp">trunk/tests/phpunit/tests/admin/exportWp.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesexportphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/export.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/export.php 2024-02-21 17:33:54 UTC (rev 57680)
+++ trunk/src/wp-admin/includes/export.php 2024-02-21 18:13:27 UTC (rev 57681)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -144,6 +144,52 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Grab a snapshot of post IDs, just in case it changes during the export.
</span><span class="cx" style="display: block; padding: 0 10px"> $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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 ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Get the requested terms ready, empty unless posts filtered by category
</span><span class="cx" style="display: block; padding: 0 10px"> * or all content.
</span></span></pre></div>
<a id="trunktestsphpunittestsadminexportWpphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/admin/exportWp.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/admin/exportWp.php (rev 0)
+++ trunk/tests/phpunit/tests/admin/exportWp.php 2024-02-21 18:13:27 UTC (rev 57681)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,293 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group admin
+ * @group export
+ *
+ * @covers ::export_wp
+ *
+ * Tests run in a separate process to prevent "headers already sent" error.
+ * @runTestsInSeparateProcesses
+ * @preserveGlobalState disabled
+ */
+class Tests_Admin_ExportWp extends WP_UnitTestCase {
+ /**
+ * Post IDs for posts, pages, and attachments.
+ *
+ * The structure is shown for understanding how to
+ * lookup / reference the information within it.
+ *
+ * IDs will be created in this order.
+ *
+ * @var array {
+ * @type array $data {
+ * Data for each post, page, or attachment.
+ *
+ * @type int $post_id The ID for the post, page, or attachment.
+ * @type int $post_author The author's ID.
+ * @type int $xml_item_index The XML item index for this post, page, or attachment.
+ * This number is based upon all of the posts, pages, and attachments
+ * in the self::$post_ids static property.
+ * }
+ * }
+ */
+ private static $post_ids = array(
+ 'post 1' => array(),
+ 'attachment for post 1' => array(),
+ 'post 2' => array(),
+ 'attachment for post 2' => array(),
+ 'page 1' => array(),
+ 'attachment for page 1' => array(),
+ 'page 2' => array(),
+ 'attachment for page 2' => array(),
+ );
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ require_once ABSPATH . 'wp-admin/includes/export.php';
+ $file = DIR_TESTDATA . '/images/test-image.jpg';
+
+ $dataset = array(
+ 'post 1' => array(
+ 'post_title' => 'Test Post 1',
+ 'post_type' => 'post',
+ ),
+ 'post 2' => array(
+ 'post_title' => 'Test Post 2',
+ 'post_type' => 'post',
+ ),
+ 'page 1' => array(
+ 'post_title' => 'Test Page 1',
+ 'post_type' => 'page',
+ ),
+ 'page 2' => array(
+ 'post_title' => 'Test Page 2',
+ 'post_type' => 'page',
+ ),
+ );
+
+ $xml_item_index = -1;
+
+ foreach ( $dataset as $post_key => $post_data ) {
+ $attachment_key = "attachment for $post_key";
+ $post_data['post_author'] = $factory->user->create( array( 'role' => 'editor' ) );
+
+ $post_id = $factory->post->create( $post_data );
+ $attachment_id = $factory->attachment->create_upload_object( $file, $post_id );
+ set_post_thumbnail( $post_id, $attachment_id );
+
+ self::$post_ids[ $post_key ] = array(
+ 'post_id' => $post_id,
+ 'post_author' => $post_data['post_author'],
+ 'xml_item_index' => ++$xml_item_index,
+ );
+ self::$post_ids[ $attachment_key ] = array(
+ 'post_id' => $attachment_id,
+ 'post_author' => $post_data['post_author'],
+ 'xml_item_index' => ++$xml_item_index,
+ );
+ }
+ }
+
+ /**
+ * @dataProvider data_should_include_attachments
+ *
+ * @ticket 17379
+ *
+ * @param array $args Arguments to pass to export_wp().
+ * @param array $expected {
+ * The expected data.
+ *
+ * @type array $items {
+ * The expected XML items count assertion arguments.
+ *
+ * @type int $number_of_items The expected number of XML items.
+ * @type string $message The assertion failure message.
+ * }
+ * @type array $ids A list of self::$post_ids keys.
+ */
+ public function test_should_include_attachments( array $args, array $expected ) {
+ $this->populate_args_post_authors( $args, $expected['ids'] );
+
+ $xml = $this->get_the_export( $args );
+
+ $expected_number_of_items = $expected['items']['number_of_items'];
+ $this->assertCount( $expected_number_of_items, $xml->channel->item, $expected['items']['message'] );
+
+ // Test each XML item's post ID to valid the post, page, and attachment (when appropriate) were exported.
+ foreach ( $expected['ids'] as $post_ids_key ) {
+ $xml_item = $this->get_xml_item( $xml, $post_ids_key, $expected_number_of_items );
+
+ $this->assertSame(
+ $this->get_expected_id( $post_ids_key ),
+ (int) $xml_item->post_id,
+ "In the XML, the {$post_ids_key}'s ID should match the expected content"
+ );
+ }
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array
+ */
+ public function data_should_include_attachments() {
+ return array(
+ 'for all content' => array(
+ 'args' => array(
+ 'content' => 'all',
+ ),
+ 'expected' => array(
+ 'items' => array(
+ 'number_of_items' => 8,
+ 'message' => 'The number of items should be 8 = 2 pages, 2 posts and 4 attachments',
+ ),
+ 'ids' => array(
+ 'post 1',
+ 'post 2',
+ 'page 1',
+ 'page 2',
+ 'attachment for page 1',
+ 'attachment for post 2',
+ 'attachment for page 1',
+ 'attachment for page 2',
+ ),
+ ),
+ ),
+ 'for all posts' => array(
+ 'args' => array(
+ 'content' => 'post',
+ ),
+ 'expected' => array(
+ 'items' => array(
+ 'number_of_items' => 4,
+ 'message' => 'The number of items should be 4 = 2 posts and 2 attachments',
+ ),
+ 'ids' => array(
+ 'post 1',
+ 'post 2',
+ 'attachment for post 1',
+ 'attachment for post 2',
+ ),
+ ),
+ ),
+ 'for all pages' => array(
+ 'args' => array(
+ 'content' => 'page',
+ ),
+ 'expected' => array(
+ 'items' => array(
+ 'number_of_items' => 4,
+ 'message' => 'The number of items should be 4 = 2 pages and 2 attachments',
+ ),
+ 'ids' => array(
+ 'page 1',
+ 'attachment for page 1',
+ 'page 2',
+ 'attachment for page 2',
+ ),
+ ),
+ ),
+ 'for specific author posts' => array(
+ 'args' => array(
+ 'content' => 'post',
+ 'author' => '', // The test will populate the author's ID.
+ ),
+ 'expected' => array(
+ 'items' => array(
+ 'number_of_items' => 2,
+ 'message' => 'The number of items should be 2 = 1 post and 1 attachment',
+ ),
+ 'ids' => array(
+ 'post 1',
+ 'attachment for post 1',
+ ),
+ ),
+ ),
+ 'for specific author pages' => array(
+ 'args' => array(
+ 'content' => 'page',
+ 'author' => '', // The test will populate the author's ID.
+ ),
+ 'expected' => array(
+ 'items' => array(
+ 'number_of_items' => 2,
+ 'message' => 'The number of items should be 2 = 1 page and 1 attachment',
+ ),
+ 'ids' => array(
+ 'page 2',
+ 'attachment for page 2',
+ ),
+ ),
+ ),
+ );
+ }
+
+ /**
+ * Gets the export results.
+ *
+ * @since 6.5.0
+ *
+ * @param array $args Arguments to pass to export_wp().
+ * @return SimpleXMLElement|false Returns the XML object on success, otherwise false is returned.
+ */
+ private function get_the_export( $args ) {
+ ob_start();
+ export_wp( $args );
+ $results = ob_get_clean();
+
+ return simplexml_load_string( $results );
+ }
+
+ /**
+ * Gets the expected ID.
+ *
+ * @since 6.5.0
+ *
+ * @param string $post_ids_key The key to lookup in the $post_ids static property.
+ * @return int Expected ID.
+ */
+ private function get_expected_id( $post_ids_key ) {
+ $post_info = self::$post_ids[ $post_ids_key ];
+
+ return $post_info['post_id'];
+ }
+
+ /**
+ * Gets the XML item for the given post or attachment in the self::$post_ids.
+ *
+ * @since 6.5.0
+ *
+ * @param SimpleXMLElement $xml XML object.
+ * @param string $post_ids_key The key to lookup in the $post_ids static property.
+ * @param int $number_of_items The number of expected XML items.
+ * @return SimpleXMLElement The XML item.
+ */
+ private function get_xml_item( $xml, $post_ids_key, $number_of_items ) {
+ $post_info = self::$post_ids[ $post_ids_key ];
+
+ if ( $post_info['xml_item_index'] < $number_of_items ) {
+ $xml_item_index = $post_info['xml_item_index'];
+ } elseif ( 2 === $number_of_items ) {
+ $xml_item_index = 0 === $post_info['xml_item_index'] % 2 ? 0 : 1;
+ } else {
+ $xml_item_index = $post_info['xml_item_index'] - $number_of_items;
+ }
+
+ return $xml->channel->item[ $xml_item_index ]->children( 'wp', true );
+ }
+
+ /**
+ * Populates the post author in the given args.
+ *
+ * @since 6.5.0
+ *
+ * @param array $args Passed by reference. export_wp() arguments to process.
+ */
+ private function populate_args_post_authors( array &$args, $expected_ids ) {
+ if ( ! isset( $args['author'] ) ) {
+ return;
+ }
+ $post_ids_key = $expected_ids[0];
+ $args['author'] = self::$post_ids[ $post_ids_key ]['post_author'];
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/admin/exportWp.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>