Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#55203 closed defect (bug) (fixed)

Bug with get_post_galleries

Reported by: binarymoon's profile BinaryMoon Owned by: audrasjb's profile audrasjb
Milestone: 5.9.3 Priority: normal
Severity: normal Version: 5.9
Component: Media Keywords: has-patch has-unit-tests commit fixed-major
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Hi - I really like the changes made to get_post_galleries in 5.9 but there is a bug with it.

The change I am referring to can be seen here: #43826

In this section
https://core.trac.wordpress.org/browser/trunk/src/wp-includes/media.php?rev=52190#L4770

There is no check that you are actually dealing with galleries, and so non-gallery blocks can be returned. I spotted this in a problem when using get_post_gallery and a paragraph was being returned.

This happens when the gallery block is not the first block in the post content. If you have a paragraph, and then a gallery, and use get_post_gallery the paragraph will be returned instead of the gallery.

The process as I see it is:

  • check for block name
  • check has innerBlocks and not gallery (for nested blocks)
  • check if has inner blocks
  • grab ids and use those

Currently there is no check that the block being looped through is actually a gallery.

I think it needs to check if the block is a gallery or not before doing any processing. I would add this check after the check for innerblocks and not gallery.

The following fixed the issue for me.

https://cldup.com/OgtyNxvnsz.png

Currently get_post_gallery will only work when the gallery is the first item in the post. If there's a different block in the post then it's possible that will be used instead.

Change History (16)

#1 @costdev
3 years ago

  • Keywords needs-testing added
  • Milestone changed from Awaiting Review to 5.9.2
  • Owner set to costdev
  • Status changed from new to accepted

Hi @BinaryMoon, thanks for opening this ticket!

I'll take ownership of this ticket and I'll post an issue reproduction report tomorrow.

Once the issue is verified, I'll submit a patch with the fix and updated unit tests.

As this issue will have been introduced in 5.9, it should be fixed in a minor release. 5.9.1 is in RC and planned for release early next week. I'd like to make sure we have enough time to review and thoroughly test the patch, so I'm milestoning it for 5.9.2 for awareness.

#2 @SergeyBiryukov
3 years ago

  • Description modified (diff)

#3 @costdev
3 years ago

  • Keywords needs-testing removed

Issue Reproduction Report

Env

  • Web Server: Apache
  • WordPress: 6.0-alpha-52448-src
  • Browser: Chrome
  • OS: Linux
  • Theme: Twenty Twenty-One
  • Plugins: None activated

Steps to test

  1. Add the following to functions.php:
    add_action(
            'the_post',
            static function( $post ) {
                    echo str_contains(
                            implode( '', get_post_galleries( $post ) ),
                            'Paragraph before gallery'
                    ) ? 'Reproduced.' : 'Not reproduced.';
            }
    );
    
  2. Navigate to Posts > Add New.
  3. Insert a paragraph with the text: Paragraph before gallery
  4. Insert a gallery with any number of images.
  5. Publish the post.
  6. View the post.
  7. "Issue reproduced." should appear on the screen. ✅

Results

  • Issue reproduced.

This ticket was mentioned in PR #2336 on WordPress/wordpress-develop by costdev.


3 years ago
#4

  • Keywords has-patch has-unit-tests added

#5 @BinaryMoon
3 years ago

@costdev Thanks a lot for looking into this and addressing it so quickly. Hopefully it will be accepted for 5.9.2

#6 @audrasjb
3 years ago

  • Owner changed from costdev to audrasjb
  • Status changed from accepted to assigned

Self assigning for review and commit consideration

#7 @audrasjb
3 years ago

  • Keywords commit added

The patch fixes the underlined issue. Thanks for the reproduction steps, it really helps reviewing the patch :)

#8 @audrasjb
3 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 52797:

Media: Make get_post_galleries() only return galleries.

This change makes sure only gallery content is returned by get_post_galleries(). It fixes an issue where non gallery block content was also returned by the function.

Props BinaryMoon, costdev, glendaviesnz.
Fixes #55203.

#10 @audrasjb
3 years ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for backport consideration.

#11 @BinaryMoon
3 years ago

I'm not sure this needs backporting. The bug was introduced in 5.9 because of a change in the get_post_galleries function to make it support blocks. To be backported beyond 5.9 the entire change would need to be moved, not just the fix.

#12 @costdev
3 years ago

@BinaryMoon

52797 was a commit to trunk.

This is followed by consideration for backport to the 5.9 branch in order to be released in the next minor release, 5.9.2.

#13 @BinaryMoon
3 years ago

@costdev Thanks for explaining. I assumed 5.9.2 was the next release but I now understand that trunk is currently working towards 6.0 and so, even though 5.9.2 will be released next, it's not actually the version that's officially in progress.

#14 @audrasjb
3 years ago

  • Milestone changed from 5.9.2 to 5.9.3

Moving to milestone 5.9.3 since we're about to release 5.9.2.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


3 years ago

#16 @audrasjb
3 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 53006:

Media: Make get_post_galleries() only return galleries.

This change makes sure only gallery content is returned by get_post_galleries(). It fixes an issue where non gallery block content was also returned by the function.

Props BinaryMoon, costdev, glendaviesnz.
Merges [52797] to the 5.9 branch.
Fixes #55203.

Note: See TracTickets for help on using tickets.