Opened 3 years ago
Closed 3 years ago
#55203 closed defect (bug) (fixed)
Bug with get_post_galleries
Reported by: | BinaryMoon | Owned by: | 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 )
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.
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
@
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
#3
@
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
- 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.'; } );
- Navigate to
Posts > Add New
. - Insert a paragraph with the text:
Paragraph before gallery
- Insert a gallery with any number of images.
- Publish the post.
- View the post.
- "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
Trac ticket: https://core.trac.wordpress.org/ticket/55203
#5
@
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
@
3 years ago
- Owner changed from costdev to audrasjb
- Status changed from accepted to assigned
Self assigning for review and commit consideration
#7
@
3 years ago
- Keywords commit added
The patch fixes the underlined issue. Thanks for the reproduction steps, it really helps reviewing the patch :)
3 years ago
#9
Committed in https://core.trac.wordpress.org/changeset/52797
#10
@
3 years ago
- Keywords fixed-major added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening for backport consideration.
#11
@
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
@
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
@
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
@
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.
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.