Make WordPress Core


Ignore:
Timestamp:
11/15/2017 02:26:56 AM (7 years ago)
Author:
westonruter
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets/class-wp-widget-text.php

    r42001 r42185  
    180180
    181181    /**
     182     * Filter gallery shortcode attributes.
     183     *
     184     * Prevents all of a site's attachments from being shown in a gallery displayed on a
     185     * non-singular template where a $post context is not available.
     186     *
     187     * @since 4.9.0
     188     *
     189     * @param array $attrs Attributes.
     190     * @return array Attributes.
     191     */
     192    public function _filter_gallery_shortcode_attrs( $attrs ) {
     193        if ( ! is_singular() && empty( $attrs['id'] ) && empty( $attrs['include'] ) ) {
     194            $attrs['id'] = -1;
     195        }
     196        return $attrs;
     197    }
     198
     199    /**
    182200     * Outputs the content for the current Text widget instance.
    183201     *
     
    222240        }
    223241
    224         // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context.
    225         $suspended_post = null;
    226         if ( isset( $post ) ) {
    227             $suspended_post = $post;
     242        // Override global $post so filters (and shortcodes) apply in a consistent context.
     243        $original_post = $post;
     244        if ( is_singular() ) {
     245            // Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).
     246            $post = get_queried_object();
     247        } else {
     248            // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.
    228249            $post = null;
    229250        }
     251
     252        // Prevent dumping out all attachments from the media library.
     253        add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
    230254
    231255        /**
     
    279303
    280304        // Restore post global.
    281         if ( isset( $suspended_post ) ) {
    282             $post = $suspended_post;
    283         }
     305        $post = $original_post;
     306        remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
    284307
    285308        // Undo suspension of legacy plugin-supplied shortcode handling.
Note: See TracChangeset for help on using the changeset viewer.