WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/24/17 22:53:20 (3 months ago)
Author:
westonruter
Message:

Widgets: Rename Text widget's legacy mode to non-visual mode, restore boolean filter prop, and improve compatibility for widget_text filters applied in Custom HTML widget.

Merges [41132] onto 4.8 branch.
Amends [41050].
Props westonruter, obenland, timmydcrawford for testing.
See #35243, #40951, #40907.
Fixes #41394 for 4.8.1.

Location:
branches/4.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.8

  • branches/4.8/src/wp-includes/widgets/class-wp-widget-text.php

    r41087 r41133  
    8080    public function is_legacy_instance( $instance ) { 
    8181 
    82         // If the widget has been updated while in legacy mode, it stays in legacy mode. 
    83         if ( ! empty( $instance['legacy'] ) ) { 
    84             return true; 
    85         } 
    86  
    87         // If the widget has been added/updated in 4.8 then filter prop is 'content' and it is no longer legacy. 
     82        // Legacy mode when not in visual mode. 
     83        if ( isset( $instance['visual'] ) ) { 
     84            return ! $instance['visual']; 
     85        } 
     86 
     87        // Or, the widget has been added/updated in 4.8.0 then filter prop is 'content' and it is no longer legacy. 
    8888        if ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ) { 
    8989            return false; 
     
    194194 
    195195        $text = ! empty( $instance['text'] ) ? $instance['text'] : ''; 
    196         $is_visual_text_widget = ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ); 
     196        $is_visual_text_widget = ( ! empty( $instance['visual'] ) && ! empty( $instance['filter'] ) ); 
     197 
     198        // In 4.8.0 only, visual Text widgets get filter=content, without visual prop; upgrade instance props just-in-time. 
     199        if ( ! $is_visual_text_widget ) { 
     200            $is_visual_text_widget = ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ); 
     201        } 
     202        if ( $is_visual_text_widget ) { 
     203            $instance['filter'] = true; 
     204            $instance['visual'] = true; 
     205        } 
    197206 
    198207        /* 
     
    222231        $text = apply_filters( 'widget_text', $text, $instance, $this ); 
    223232 
    224         if ( isset( $instance['filter'] ) ) { 
    225             if ( 'content' === $instance['filter'] ) { 
    226  
    227                 /** 
    228                  * Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor. 
    229                  * 
    230                  * By default a subset of the_content filters are applied, including wpautop and wptexturize. 
    231                  * 
    232                  * @since 4.8.0 
    233                  * 
    234                  * @param string         $text     The widget content. 
    235                  * @param array          $instance Array of settings for the current widget. 
    236                  * @param WP_Widget_Text $this     Current Text widget instance. 
    237                  */ 
    238                 $text = apply_filters( 'widget_text_content', $text, $instance, $this ); 
    239  
    240             } elseif ( $instance['filter'] ) { 
    241                 $text = wpautop( $text ); // Back-compat for instances prior to 4.8. 
    242             } 
     233        if ( $is_visual_text_widget ) { 
     234 
     235            /** 
     236             * Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor. 
     237             * 
     238             * By default a subset of the_content filters are applied, including wpautop and wptexturize. 
     239             * 
     240             * @since 4.8.0 
     241             * 
     242             * @param string         $text     The widget content. 
     243             * @param array          $instance Array of settings for the current widget. 
     244             * @param WP_Widget_Text $this     Current Text widget instance. 
     245             */ 
     246            $text = apply_filters( 'widget_text_content', $text, $instance, $this ); 
     247 
     248        } elseif ( ! empty( $instance['filter'] ) ) { 
     249            $text = wpautop( $text ); // Back-compat for instances prior to 4.8. 
    243250        } 
    244251 
     
    272279     */ 
    273280    public function update( $new_instance, $old_instance ) { 
     281        $new_instance = wp_parse_args( $new_instance, array( 
     282            'title' => '', 
     283            'text' => '', 
     284            'filter' => false, // For back-compat. 
     285            'visual' => null, // Must be explicitly defined. 
     286        ) ); 
     287 
    274288        $instance = $old_instance; 
     289 
    275290        $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
    276291        if ( current_user_can( 'unfiltered_html' ) ) { 
     
    280295        } 
    281296 
    282         /* 
    283          * If the Text widget is in legacy mode, then a hidden input will indicate this 
    284          * and the new content value for the filter prop will by bypassed. Otherwise, 
    285          * re-use legacy 'filter' (wpautop) property to now indicate content filters will always apply. 
    286          * Prior to 4.8, this is a boolean value used to indicate whether or not wpautop should be 
    287          * applied. By re-using this property, downgrading WordPress from 4.8 to 4.7 will ensure 
    288          * that the content for Text widgets created with TinyMCE will continue to get wpautop. 
    289          */ 
    290         if ( isset( $new_instance['legacy'] ) || isset( $old_instance['legacy'] ) || ( isset( $new_instance['filter'] ) && 'content' !== $new_instance['filter'] ) ) { 
    291             $instance['filter'] = ! empty( $new_instance['filter'] ); 
    292             $instance['legacy'] = true; 
    293         } else { 
    294             $instance['filter'] = 'content'; 
    295             unset( $instance['legacy'] ); 
     297        $instance['filter'] = ! empty( $new_instance['filter'] ); 
     298 
     299        // Upgrade 4.8.0 format. 
     300        if ( isset( $old_instance['filter'] ) && 'content' === $old_instance['filter'] ) { 
     301            $instance['visual'] = true; 
     302        } 
     303        if ( 'content' === $new_instance['filter'] ) { 
     304            $instance['visual'] = true; 
     305        } 
     306 
     307        if ( isset( $new_instance['visual'] ) ) { 
     308            $instance['visual'] = ! empty( $new_instance['visual'] ); 
     309        } 
     310 
     311        // Filter is always true in visual mode. 
     312        if ( ! empty( $instance['visual'] ) ) { 
     313            $instance['filter'] = true; 
    296314        } 
    297315 
     
    334352            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="title" type="hidden" value="<?php echo esc_attr( $instance['title'] ); ?>"> 
    335353            <input id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" class="text" type="hidden" value="<?php echo esc_attr( $instance['text'] ); ?>"> 
     354            <input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" class="filter" type="hidden" value="on"> 
     355            <input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value="on"> 
    336356        <?php else : ?> 
    337             <input name="<?php echo $this->get_field_name( 'legacy' ); ?>" type="hidden" class="legacy" value="true"> 
     357            <input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value=""> 
    338358            <p> 
    339359                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
Note: See TracChangeset for help on using the changeset viewer.