-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include attachments and featured images for posts being exported #5720
Conversation
The Trac ticket is focused on exporting for a "specific author" rather than all authors. The PR works for both. Is this intentional to include when no author is selected (i.e. to do all authors)? If yes it should also work for all authors, what might be the risks or impacts on sites with a large or massive amounts of content? |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
The Trac ticket mentions the bug happens when you filter by specific author, but it actually happens when you do any filter other than "All content" This patch fixes that by getting all the attachments for the posts being exported if
The patch takes this into consideration by splitting the work into chunks of 20 post IDs at a time. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
src/wp-admin/includes/export.php
Outdated
@@ -144,6 +144,40 @@ 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 ) ) { | |||
foreach ( array_chunk( $post_ids, 20 ) as $chunk ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already a chunking approach in the same file:
// Fetch 20 posts at a time rather than loading the entire table into memory.
while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
Could that approach also be used here for consistency?
Are there benefits to this approach over the other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach you mentioned actually is similar, but it modifies the array instead of creating new ones. I went ahead and used it for consistency but I also needed to create a copy of the $post_ids
so it wouldn't get modified.
Aha thanks @nate-allen for clarifying. Makes sense 👍 |
It's really close for commit. Left some comments to finalize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the work on this. This looks ready for me.
I've left one small nitpick, but happy to see it merge as is.
Thank you @nate-allen and everyone for the follow-ups to code review and suggestions. Reviewing today for commit. |
* @group attachments | ||
* @ticket 17379 | ||
*/ | ||
class Test_Export_Includes_Attachments extends WP_UnitTestCase { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test class covers the attachment part of ::export_wp
. But the test location, file name, and test class name need to change. Below, I'll share context, information, and handbook links to help explain the changes.
Note: I'll push a commit shortly to take care of these changes.
Sharing the longer details:
The test suite coding standards are located here in the handbook.
What is the function under test?
export_wp()
This means the test class is for export_wp()
coverage. Specifically for this bug, the coverage is for only the attachment fix within the export_wp()
.
Where should the file be located?
tests/phpunit/tests/admin/
directory as part of the admin
group of tests.
The export_wp()
function lives in the /admin/includes/export.php
file as part of the export group of admin functionality.
Notice the test files in the files and functions in /admin/includes/
are located in tests/admin/
.
What should the filename be?
exportWp.php
.
Notice in the Test Classes standard, the test filename is the function under test's name in camelCase.
What should the test class name be?
Tests_Admin_ExportWp
per the same standard, which defines the naming as
Tests_[TheGroup]_FunctionNameUnderTest
where the TheGroup
is the root or primary group for the set of tests, i.e. identified by subdirectory which is in this case is the admin
group of tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 6c9d28d ✅
Currently, attachmets are only exported if you choose to export with the 'all' option. This change will get the attachment IDs of all of the posts being exported when the 'all' option isn't used.
This is more consistent to how it is being done elsewhere in the file and is a bit more memory efficient as well.
To ensure there are no merge conflicts, a rebase is needed against the current WordPress Develop Update: Done ✅ |
Changes the test class to cover the export_wp() function, i.e. the function under test. 1. Relocates to tests/admin/. 2. Renames the test file to function's name in camelCase. 3. Renames the test class to follow Tests_[GroupName]_FunctionName. 4. Changes the groups to admin and export. 5. Renames tests to test_should_[what_it_should_do] format. 6. Moves the ticket annotation to the test methods. 7. Adds covers annotation. 8. Adds test failure reason when there are multiple assertions (i.e. to help identify which assertion failed). 9. Uses assertSame() as part of an effort in Core to reduce assertEquals() usage. 10. Switch to dataProvider. 11. Update docblocks.
1257009
to
6c9d28d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Confirmed tests fail without the changes in
export_wp()
✅ - Confirmed this PR resolves the issue for the featured images. See test report ✅
- Tests are per Core's test coding standards and practices ✅
- The changes in
export_wp()
are using the same code design as preexisting in theexport.php
file ✅
This patch is ready for commit ✅
Committed via https://core.trac.wordpress.org/changeset/57681. |
I closed my previous PR on this issue because it was stuck on a failed PHP 5.6 test that doesn't apply anymore.
This pull request fixes an issue where attachments are not included when exporting posts, unless you choose the "all" option.
A unit test is also included.
Trac ticket: 17379
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.