WordPress.org

Make WordPress Core

Opened 10 days ago

Closed 10 days ago

Last modified 10 days ago

#42548 closed defect (bug) (fixed)

Widgets: Populate global $post with queried object on singular queries instead of nullifying it

Reported by: westonruter Owned by: westonruter
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.9
Component: Widgets Keywords: has-patch dev-reviewed commit
Focuses: Cc:

Description (last modified by westonruter)

Until 4.9, if a plugin did add_filter( 'widget_text', 'do_shortcode' ) and they added a [gallery] to a widget that is displayed on a singular template, then the gallery would (usually) show the attachments from the current queried post. This behavior changed in #10457 for 4.9 whereby the global $post is nullified to ensure that the Text widget renders consistently across whatever template it may appear (singular, archive, 404, etc).

There are two problems with this:

  1. When $post is null, then the gallery shortcode will list out all attachments from the media library. This could be embarrassing or at least undesired.
  2. If a site previously did rely on being able to render a gallery of attached images for the current post in the sidebar, this is broken.

I think we can fix both of these issues simply by checking if is_singular() and if so, set the $post to the current queried object (get_queried_object()). This will ensure that the global $post isn't coming from an unpredictable source (such as a previous query that failed to call wp_reset_postdata()), which was a primary motivator for nullifying the global $post to begin with.

This same approach can be done for the Custom HTML widget, which currently doesn't nullify $post even though it does apply widget_text filters (though not for shortcodes in core). This would close #42547.

Attachments (9)

42548.0.diff (2.7 KB) - added by westonruter 10 days ago.
42548.1.diff (3.1 KB) - added by westonruter 10 days ago.
42548.2.diff (2.2 KB) - added by bobbingwide 10 days ago.
Change gallery_shortcode to not display unattached images when used in a Text or Custom HTML widget. Support id=0 when this is required.
42548.3.diff (5.3 KB) - added by westonruter 10 days ago.
42548.4.diff (4.2 KB) - added by westonruter 10 days ago.
42548.5.diff (4.0 KB) - added by westonruter 10 days ago.
https://github.com/xwp/wordpress-develop/pull/302/commits/b9c921e
42548.6.diff (3.1 KB) - added by westonruter 10 days ago.
https://github.com/xwp/wordpress-develop/pull/302/commits/5f164664063479ee35249633e49181f24aa0a00f
42548.7.diff (5.0 KB) - added by westonruter 10 days ago.
https://github.com/xwp/wordpress-develop/pull/302/commits/df9ed1d5b24341f1355714b7352d4def2d1621e6
42548.8.diff (4.9 KB) - added by westonruter 10 days ago.
https://github.com/xwp/wordpress-develop/pull/302/commits/824c3faafcdf9d94491ead42981cde284dd5ad4a

Download all attachments as: .zip

Change History (24)

#1 @westonruter
10 days ago

  • Description modified (diff)

@westonruter
10 days ago

#2 @westonruter
10 days ago

  • Keywords has-patch added

@westonruter
10 days ago

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


10 days ago

#4 @westonruter
10 days ago

  • Keywords needs-testing added

#5 @bobbingwide
10 days ago

Unless I've applied the patch incorrectly my tests indicate that gallery_shortcode needs changing to test if the $id is not empty.

Without this test, when viewing an archive, the [gallery] shortcode will still produce a display of all the unattached images. This applies to both Custom HTML widgets and Text widgets.

I've developed an additional patch that caters for this problem.
It also allows [gallery id=0] to display all the unattached images.

This patch is in addition to 42548.1.diff

Last edited 10 days ago by bobbingwide (previous) (diff)

@bobbingwide
10 days ago

Change gallery_shortcode to not display unattached images when used in a Text or Custom HTML widget. Support id=0 when this is required.

#6 @bobbingwide
10 days ago

Manually tested with posts with and without attached images. Widgets in the primary sidebar include a Custom HTML widget containing [gallery] and a Text widget containing the same shortcode.
The posts also contain the [gallery] shortcode.
One of the posts contains a [gallery id=0] shortcode.

When viewing the home page archive the widgets are empty.

When viewing a singular post the widgets show the attached images.

@westonruter
10 days ago

#7 @westonruter
10 days ago

  • Keywords 2nd-opinion added

I combined the two latest patches and cleaned up some formatting in 42548.3.diff.

I'll need a 2nd opinion from media team on the gallery changes.

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


10 days ago

@westonruter
10 days ago

#9 @westonruter
10 days ago

42548.4.diff ensures that the original $post->ID is supplied in the call to shortcode_atts so that it is available for shortcode_atts_gallery filters. But instead of passing 0 when there is no $post it instead passes null. If the null is then what gets returned, the gallery_shortcode() function will then short-circuit if no ids are supplied either.

#10 @westonruter
10 days ago

  • Keywords dev-feedback added; 2nd-opinion removed

42548.6.diff undoes the changes to gallery_shortcode(), since the changes weren't able to apply to plugins that filter post_gallery to override the default gallery behavior. In a future release we may want to revisit this to see if there is a better way to contextually render galleries when they are rendered in non-singular templates.

In ≤4.8, gallery shortcodes without explicit ids appearing in Text widgets (when a plugin adds support shortcode support) already have the behavior: when a theme resets the $post global or it is not set to begin with (such as on the 404 template) then the gallery widget currently will list out all attachments. In #10457 this behavior was applied to every widget in whatever context, even singulars. So 42548.6.diff makes sure that the $post is consistently the queried object (post) when is_singular(). Even before 4.9 if a user intended to have a widget appear in a sidebar and for it to rely on the post context, they should have already been using a sidebar that is dedicated for the singular template, or else use something like Jetpack Visibility to prevent the widget from appearing on anything aside from singular queries.

See full discussion with arguments for why gallery_shortcode() remains unchanged in 4.9: https://wordpress.slack.com/archives/C02RQBWTW/p1510703055000030

#11 @westonruter
10 days ago

  • Keywords needs-testing removed

#12 @westonruter
10 days ago

  • Keywords commit added

#13 @joemcgill
10 days ago

  • Keywords dev-reviewed added; dev-feedback removed

Based on my testing, and extensive conversation in Slack, I'm +1 on the approach in 42548.8.diff for 4.9.

#14 @westonruter
10 days ago

  • Owner set to westonruter
  • Resolution set to fixed
  • Status changed from new to closed

In 42185:

Widgets: Set global $post to current queried object instead of nullifying when is_singular() while applying filters (and shortcodes) in Text widget and (via plugin) Custom HTML widget.

Also prevent [gallery] shortcode from dumping out every attachment on the site when a containing Text widget is shown on an archive template.

Props westonruter, bobbingwide, joemcgill for testing.
See #10457.
Fixes #42548, #42547 for trunk.

#15 @westonruter
10 days ago

In 42186:

Widgets: Set global $post to current queried object instead of nullifying when is_singular() while applying filters (and shortcodes) in Text widget and (via plugin) Custom HTML widget.

Also prevent [gallery] shortcode from dumping out every attachment on the site when a containing Text widget is shown on an archive template.

Props westonruter, bobbingwide, joemcgill for testing.
See #10457.
Fixes #42548, #42547 for 4.9.

Note: See TracTickets for help on using tickets.