Make WordPress Core

Ticket #42548: 42548.5.diff

File 42548.5.diff, 4.0 KB (added by westonruter, 7 years ago)

https://github.com/xwp/wordpress-develop/pull/302/commits/b9c921e

  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 1981f3ad9a..c91d2914a5 100644
    function gallery_shortcode( $attr ) { 
    16621662                return $output;
    16631663        }
    16641664
     1665        $id_supplied = isset( $attr['id'] );
     1666
    16651667        $html5 = current_theme_supports( 'html5', 'gallery' );
    16661668        $atts = shortcode_atts( array(
    16671669                'order'      => 'ASC',
    function gallery_shortcode( $attr ) { 
    16791681
    16801682        $id = intval( $atts['id'] );
    16811683
     1684        // Short-circuit if no attachments are selected and there is no post context explicitly provided (including 0).
     1685        if ( empty( $atts['include'] ) && empty( $id ) && ! $id_supplied ) {
     1686                return '';
     1687        }
     1688
    16821689        if ( ! empty( $atts['include'] ) ) {
    16831690                $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) );
    16841691
  • src/wp-includes/widgets/class-wp-widget-custom-html.php

    diff --git src/wp-includes/widgets/class-wp-widget-custom-html.php src/wp-includes/widgets/class-wp-widget-custom-html.php
    index 855c3c5895..758e08c3ba 100644
    class WP_Widget_Custom_HTML extends WP_Widget { 
    8585         *
    8686         * @since 4.8.1
    8787         *
     88         * @global WP_Post $post
    8889         * @param array $args     Display arguments including 'before_title', 'after_title',
    8990         *                        'before_widget', and 'after_widget'.
    9091         * @param array $instance Settings for the current Custom HTML widget instance.
    9192         */
    9293        public function widget( $args, $instance ) {
     94                global $post;
     95
     96                // Override global $post so filters (and shortcodes) apply in a consistent context.
     97                $original_post = $post;
     98                if ( is_singular() ) {
     99                        // Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).
     100                        $post = get_queried_object();
     101                } else {
     102                        // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.
     103                        $post = null;
     104                }
    93105
    94106                $instance = array_merge( $this->default_instance, $instance );
    95107
    class WP_Widget_Custom_HTML extends WP_Widget { 
    118130                 */
    119131                $content = apply_filters( 'widget_custom_html_content', $content, $instance, $this );
    120132
     133                // Restore post global.
     134                $post = $original_post;
     135
    121136                // Inject the Text widget's container class name alongside this widget's class name for theme styling compatibility.
    122137                $args['before_widget'] = preg_replace( '/(?<=\sclass=["\'])/', 'widget_text ', $args['before_widget'] );
    123138
  • src/wp-includes/widgets/class-wp-widget-text.php

    diff --git src/wp-includes/widgets/class-wp-widget-text.php src/wp-includes/widgets/class-wp-widget-text.php
    index ffcfe0cd09..a6a0b44c23 100644
    class WP_Widget_Text extends WP_Widget { 
    221221                        remove_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
    222222                }
    223223
    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;
     224                // Override global $post so filters (and shortcodes) apply in a consistent context.
     225                $original_post = $post;
     226                if ( is_singular() ) {
     227                        // Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).
     228                        $post = get_queried_object();
     229                } else {
     230                        // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.
    228231                        $post = null;
    229232                }
    230233
    class WP_Widget_Text extends WP_Widget { 
    278281                }
    279282
    280283                // Restore post global.
    281                 if ( isset( $suspended_post ) ) {
    282                         $post = $suspended_post;
    283                 }
     284                $post = $original_post;
    284285
    285286                // Undo suspension of legacy plugin-supplied shortcode handling.
    286287                if ( $should_suspend_legacy_shortcode_support ) {