#39304 closed defect (bug) (fixed)
`get_post_galleries()` can return incorrect gallery items.
Reported by: | dd32 | Owned by: | dd32 |
---|---|---|---|
Milestone: | 4.7.3 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Media | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
get_post_galleries()
is a little hacky in the way it works and doesn't work as intended when a plain [gallery]
shortcode is within the content and the given post_id doesn't match the global $post
object.
For example, take the following scenario:
post_content = [gallery] $GLOBALS['post'] = null; $data = get_post_galleries( $id );
$data
now contains a gallery of all unattached media, as the shortcode handler doesn't have the global $post
context and instead queries for post_parent = 0
.
If the global $post
was set to another post, the gallery would be of that posts media.
Attachments (3)
Change History (16)
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
#4
@
8 years ago
- Keywords needs-unit-tests added
- Milestone changed from Awaiting Review to 4.8
I can't see a fault with the approach I've taken here, and although It feels a little hacky I can't see a cleaner way to achieve it.
This could definitely use a set of unit tests showing the incorrect behaviour and confirming it does work in various combinations of $id
param, $post
global and the id
shortcode attribute.
#6
@
8 years ago
- Keywords has-unit-tests added; needs-unit-tests removed
Hi @seanchayes, @dd32
I have tested 39304.1.diff patch with unit test. Please check the following result. It's working fine.
C:\wamp\www\wordpress-core>phpunit --group 22960 Installing... Running as single site... To run multisite, use -c tests/phpunit/multisite.xml Not running ajax tests. To execute these, use --group ajax. Not running ms-files tests. To execute these, use --group ms-files. Not running external-http tests. To execute these, use --group external-http. PHPUnit 5.7.5 by Sebastian Bergmann and contributors. ... 3 / 3 (100%) Time: 10.56 seconds, Memory: 104.00MB OK (3 tests, 3 assertions)
Thanks
This ticket was mentioned in Slack in #core by mayur. View the logs.
8 years ago
#9
@
8 years ago
I've split out a separate unit test for this case in 39304-tests.diff. The approach to force for the ID works ok, but is definitely a hack. Seems like a bad idea that gallery_shortcode()
always tries to get the post ID from global state when one is not explicitly passed as an attribute, but still maybe better to hack in an explicit post ID rather than trying to override global state at that point (which is another thing we could do).
As an aside, I can't think of a time where we would want gallery_shortcode()
to return unattached images whenever it's called in a context without a global post object set.
Hacky-ish approach, but also includes a fix for #39277