Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#39304 closed defect (bug) (fixed)

`get_post_galleries()` can return incorrect gallery items.

Reported by: dd32 Owned by: dd32
Priority: normal Milestone: 4.7.3
Component: Media Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

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)

39304.diff​ (1.9 KB ) - added by dd32 10 years ago.
Hacky-ish approach, but also includes a fix for #39277
39304.1.diff​ (1.7 KB ) - added by seanchayes 10 years ago.
Correcting variable name
39304-tests.diff​ (797 bytes ) - added by joemcgill 10 years ago.
Unit test only

Download all attachments as: .zip

Change History (16)

@dd32
10 years ago

Hacky-ish approach, but also includes a fix for #39277

#1 @dhanendran
10 years ago

  • Keywords has-patch added

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


10 years ago

#3 @pento
10 years ago

#34702 was marked as a duplicate.

#4 @dd32
10 years ago

  • Keywords needs-unit-tests added
  • Milestone Awaiting Review → 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.

#5 @seanchayes
10 years ago

Correcting variable name : shortcode_attrs - updated in patch 39304.1.diff

@seanchayes
10 years ago

Correcting variable name

#6 @mayurk
10 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.


10 years ago

#8 @matt
10 years ago

Would love to see this addressed.

#9 @joemcgill
10 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.

This ticket was mentioned in ​Slack in #core-media by joemcgill. ​View the logs.


10 years ago

#11 @dd32
10 years ago

  • Owner set to dd32
  • Resolution → fixed
  • Status new → closed

In 40070:

Media: Avoid PHP Warnings in get_post_galleries() when processing empty [gallery] shortcodes and avoid returning the incorrect results when the global $post does not match the provided post ID.

Props dd32, joemcgill, seanchayes.
Fixes #39277, #39304.

#12 @dd32
10 years ago

In 40071:

Media: Avoid PHP Warnings in get_post_galleries() when processing empty [gallery] shortcodes and avoid returning the incorrect results when the global $post does not match the provided post ID.

Props dd32, joemcgill, seanchayes.
Merges [40070] to the 4.7 branch.
Fixes #39277, #39304.

#13 @dd32
10 years ago

  • Milestone 4.8 → 4.7.3

It was easier to fix this along with #39277 - so it's gone into 4.7.3 as well.

Note: See TracTickets for help on using tickets.