Make WordPress Core

Ticket #40854: 40854-widget-shortcodes.diff

File 40854-widget-shortcodes.diff, 3.1 KB (added by westonruter, 7 years ago)
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index a196f4ff19..1c7ae718f1 100644
    add_filter( 'widget_text_content', 'capital_P_dangit', 11 ); 
    169169add_filter( 'widget_text_content', 'wptexturize'          );
    170170add_filter( 'widget_text_content', 'convert_smilies',  20 );
    171171add_filter( 'widget_text_content', 'wpautop'              );
     172add_filter( 'widget_text_content', 'do_shortcode',     11 ); // Runs after wpautop() but note that $post global will be null when shortcodes run.
    172173
    173174add_filter( 'date_i18n', 'wp_maybe_decline_date' );
    174175
  • 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 a79e0daa6c..ed9d4e995c 100644
    class WP_Widget_Text extends WP_Widget { 
    183183         *
    184184         * @since 2.8.0
    185185         *
     186         * @global WP_Post $post
     187         *
    186188         * @param array $args     Display arguments including 'before_title', 'after_title',
    187189         *                        'before_widget', and 'after_widget'.
    188190         * @param array $instance Settings for the current Text widget instance.
    189191         */
    190192        public function widget( $args, $instance ) {
     193                global $post;
    191194
    192195                /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    193196                $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
    class WP_Widget_Text extends WP_Widget { 
    211214                 * added by the shortcode.
    212215                 */
    213216                $widget_text_do_shortcode_priority = has_filter( 'widget_text', 'do_shortcode' );
     217                $has_widget_text_content_shortcode_filter = has_filter( 'widget_text_content', 'do_shortcode' );
    214218                $should_upgrade_shortcode_handling = ( $is_visual_text_widget && false !== $widget_text_do_shortcode_priority );
    215219                if ( $should_upgrade_shortcode_handling ) {
    216220                        remove_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
    217                         add_filter( 'widget_text_content', 'do_shortcode', 11 );
     221                        if ( ! $has_widget_text_content_shortcode_filter ) {
     222                                add_filter( 'widget_text_content', 'do_shortcode', 11 );
     223                        }
     224                }
     225
     226                // Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context.
     227                $suspended_post = null;
     228                if ( isset( $post ) ) {
     229                        $suspended_post = $post;
     230                        $post = null;
    218231                }
    219232
    220233                /**
    class WP_Widget_Text extends WP_Widget { 
    249262                        $text = wpautop( $text ); // Back-compat for instances prior to 4.8.
    250263                }
    251264
     265                if ( isset( $suspended_post ) ) {
     266                        $post = $suspended_post;
     267                }
     268
    252269                // Undo temporary upgrade of the plugin-supplied shortcode handling.
    253270                if ( $should_upgrade_shortcode_handling ) {
    254271                        remove_filter( 'widget_text_content', 'do_shortcode', 11 );
    255                         add_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
     272
     273                        if ( ! $has_widget_text_content_shortcode_filter ) {
     274                                add_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
     275                        }
    256276                }
    257277
    258278                echo $args['before_widget'];