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.

Built from https://develop.svn.wordpress.org/branches/4.8@41133


git-svn-id: http://core.svn.wordpress.org/branches/4.8@40973 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2017-07-24 22:54:34 +00:00
parent 74314b2ddb
commit fd423f720e
5 changed files with 70 additions and 42 deletions

View File

@ -368,7 +368,7 @@ wp.textWidgets = ( function( $ ) {
}
// Bypass using TinyMCE when widget is in legacy mode.
if ( widgetForm.find( '.legacy' ).length > 0 ) {
if ( ! widgetForm.find( '.visual' ).val() ) {
return;
}
@ -429,7 +429,7 @@ wp.textWidgets = ( function( $ ) {
}
// Bypass using TinyMCE when widget is in legacy mode.
if ( widgetForm.find( '.legacy' ).length > 0 ) {
if ( ! widgetForm.find( '.visual' ).val() ) {
return;
}

File diff suppressed because one or more lines are too long

View File

@ -119,8 +119,16 @@ class WP_Widget_Custom_HTML extends WP_Widget {
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
// Prepare instance data that looks like a normal Text widget.
$simulated_text_widget_instance = array_merge( $instance, array(
'text' => isset( $instance['content'] ) ? $instance['content'] : '',
'filter' => false, // Because wpautop is not applied.
'visual' => false, // Because it wasn't created in TinyMCE.
) );
unset( $simulated_text_widget_instance['content'] ); // Was moved to 'text' prop.
/** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */
$content = apply_filters( 'widget_text', $instance['content'], $instance, $this );
$content = apply_filters( 'widget_text', $instance['content'], $simulated_text_widget_instance, $this );
/**
* Filters the content of the Custom HTML widget.

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.8.1-beta1-41130';
$wp_version = '4.8.1-beta1-41133';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -79,12 +79,12 @@ class WP_Widget_Text extends WP_Widget {
*/
public function is_legacy_instance( $instance ) {
// If the widget has been updated while in legacy mode, it stays in legacy mode.
if ( ! empty( $instance['legacy'] ) ) {
return true;
// Legacy mode when not in visual mode.
if ( isset( $instance['visual'] ) ) {
return ! $instance['visual'];
}
// If the widget has been added/updated in 4.8 then filter prop is 'content' and it is no longer legacy.
// Or, the widget has been added/updated in 4.8.0 then filter prop is 'content' and it is no longer legacy.
if ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ) {
return false;
}
@ -193,7 +193,16 @@ class WP_Widget_Text extends WP_Widget {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$text = ! empty( $instance['text'] ) ? $instance['text'] : '';
$is_visual_text_widget = ( ! empty( $instance['visual'] ) && ! empty( $instance['filter'] ) );
// In 4.8.0 only, visual Text widgets get filter=content, without visual prop; upgrade instance props just-in-time.
if ( ! $is_visual_text_widget ) {
$is_visual_text_widget = ( isset( $instance['filter'] ) && 'content' === $instance['filter'] );
}
if ( $is_visual_text_widget ) {
$instance['filter'] = true;
$instance['visual'] = true;
}
/*
* Just-in-time temporarily upgrade Visual Text widget shortcode handling
@ -221,8 +230,7 @@ class WP_Widget_Text extends WP_Widget {
*/
$text = apply_filters( 'widget_text', $text, $instance, $this );
if ( isset( $instance['filter'] ) ) {
if ( 'content' === $instance['filter'] ) {
if ( $is_visual_text_widget ) {
/**
* Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor.
@ -237,10 +245,9 @@ class WP_Widget_Text extends WP_Widget {
*/
$text = apply_filters( 'widget_text_content', $text, $instance, $this );
} elseif ( $instance['filter'] ) {
} elseif ( ! empty( $instance['filter'] ) ) {
$text = wpautop( $text ); // Back-compat for instances prior to 4.8.
}
}
// Undo temporary upgrade of the plugin-supplied shortcode handling.
if ( $should_upgrade_shortcode_handling ) {
@ -271,7 +278,15 @@ class WP_Widget_Text extends WP_Widget {
* @return array Settings to save or bool false to cancel saving.
*/
public function update( $new_instance, $old_instance ) {
$new_instance = wp_parse_args( $new_instance, array(
'title' => '',
'text' => '',
'filter' => false, // For back-compat.
'visual' => null, // Must be explicitly defined.
) );
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
@ -279,20 +294,23 @@ class WP_Widget_Text extends WP_Widget {
$instance['text'] = wp_kses_post( $new_instance['text'] );
}
/*
* If the Text widget is in legacy mode, then a hidden input will indicate this
* and the new content value for the filter prop will by bypassed. Otherwise,
* re-use legacy 'filter' (wpautop) property to now indicate content filters will always apply.
* Prior to 4.8, this is a boolean value used to indicate whether or not wpautop should be
* applied. By re-using this property, downgrading WordPress from 4.8 to 4.7 will ensure
* that the content for Text widgets created with TinyMCE will continue to get wpautop.
*/
if ( isset( $new_instance['legacy'] ) || isset( $old_instance['legacy'] ) || ( isset( $new_instance['filter'] ) && 'content' !== $new_instance['filter'] ) ) {
$instance['filter'] = ! empty( $new_instance['filter'] );
$instance['legacy'] = true;
} else {
$instance['filter'] = 'content';
unset( $instance['legacy'] );
// Upgrade 4.8.0 format.
if ( isset( $old_instance['filter'] ) && 'content' === $old_instance['filter'] ) {
$instance['visual'] = true;
}
if ( 'content' === $new_instance['filter'] ) {
$instance['visual'] = true;
}
if ( isset( $new_instance['visual'] ) ) {
$instance['visual'] = ! empty( $new_instance['visual'] );
}
// Filter is always true in visual mode.
if ( ! empty( $instance['visual'] ) ) {
$instance['filter'] = true;
}
return $instance;
@ -333,8 +351,10 @@ class WP_Widget_Text extends WP_Widget {
<?php if ( ! $this->is_legacy_instance( $instance ) ) : ?>
<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'] ); ?>">
<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'] ); ?>">
<input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" class="filter" type="hidden" value="on">
<input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value="on">
<?php else : ?>
<input name="<?php echo $this->get_field_name( 'legacy' ); ?>" type="hidden" class="legacy" value="true">
<input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value="">
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>"/>