Skip to content
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

Closed
wants to merge 12 commits into from

Conversation

nate-allen
Copy link

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.

@hellofromtonya
Copy link
Contributor

Request: Have Tools>Export "specific author" generate the same metadata as the "all content" export, so that Export "specific author" will also include metadata reference to the Featured Image.

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?

Copy link

github-actions bot commented Feb 13, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props nateallen, hellofromtonya, petitphp, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@nate-allen
Copy link
Author

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)?

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 $args['content'] isn't all. I'll update the patch to not do this if the posts being exported are attachment

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 patch takes this into consideration by splitting the work into chunks of 20 post IDs at a time.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@@ -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 ) {
Copy link
Contributor

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?

Copy link
Author

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.

@hellofromtonya
Copy link
Contributor

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"

Aha thanks @nate-allen for clarifying. Makes sense 👍

@hellofromtonya
Copy link
Contributor

It's really close for commit. Left some comments to finalize it.

src/wp-admin/includes/export.php Outdated Show resolved Hide resolved
src/wp-admin/includes/export.php Outdated Show resolved Hide resolved
Copy link

@petitphp petitphp left a 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.

src/wp-admin/includes/export.php Outdated Show resolved Hide resolved
@hellofromtonya
Copy link
Contributor

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 {
Copy link
Contributor

@hellofromtonya hellofromtonya Feb 20, 2024

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 6c9d28d

@hellofromtonya
Copy link
Contributor

hellofromtonya commented Feb 20, 2024

To ensure there are no merge conflicts, a rebase is needed against the current WordPress Develop trunk. Will force push shortly.

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.
Copy link
Contributor

@hellofromtonya hellofromtonya left a 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 the export.php file ✅

This patch is ready for commit ✅

@hellofromtonya
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants