Ticket #41394: 41394.2.diff
File 41394.2.diff, 27.0 KB (added by , 6 years ago) |
---|
-
src/wp-admin/js/widgets/text-widgets.js
diff --git src/wp-admin/js/widgets/text-widgets.js src/wp-admin/js/widgets/text-widgets.js index 282090d585..692a0642c9 100644
wp.textWidgets = ( function( $ ) { 368 368 } 369 369 370 370 // Bypass using TinyMCE when widget is in legacy mode. 371 if ( widgetForm.find( '.legacy' ).length > 0) {371 if ( ! widgetForm.find( '.visual' ).val() ) { 372 372 return; 373 373 } 374 374 … … wp.textWidgets = ( function( $ ) { 429 429 } 430 430 431 431 // Bypass using TinyMCE when widget is in legacy mode. 432 if ( widgetForm.find( '.legacy' ).length > 0) {432 if ( ! widgetForm.find( '.visual' ).val() ) { 433 433 return; 434 434 } 435 435 -
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 7fcce78f19..25d221ba46 100644
class WP_Widget_Custom_HTML extends WP_Widget { 61 61 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 62 62 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); 63 63 64 // Prepare instance data that looks like a normal Text widget. 65 $simulated_text_widget_instance = array_merge( $instance, array( 66 'text' => isset( $instance['content'] ) ? $instance['content'] : '', 67 'filter' => false, // Because wpautop is not applied. 68 'visual' => false, // Because it wasn't created in TinyMCE. 69 ) ); 70 unset( $simulated_text_widget_instance['content'] ); // Was moved to 'text' prop. 71 64 72 /** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */ 65 $content = apply_filters( 'widget_text', $instance['content'], $ instance, $this );73 $content = apply_filters( 'widget_text', $instance['content'], $simulated_text_widget_instance, $this ); 66 74 67 75 /** 68 76 * Filters the content of the Custom HTML widget. -
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 2297d2bee2..0335a3e489 100644
class WP_Widget_Text extends WP_Widget { 79 79 */ 80 80 public function is_legacy_instance( $instance ) { 81 81 82 // If the widget has been updated while in legacy mode, it stays in legacymode.83 if ( ! empty( $instance['legacy'] ) ) {84 return true;82 // Legacy mode when not in visual mode. 83 if ( isset( $instance['visual'] ) ) { 84 return ! $instance['visual']; 85 85 } 86 86 87 // If the widget has been added/updated in 4.8then filter prop is 'content' and it is no longer legacy.87 // Or, the widget has been added/updated in 4.8.0 then filter prop is 'content' and it is no longer legacy. 88 88 if ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ) { 89 89 return false; 90 90 } … … class WP_Widget_Text extends WP_Widget { 193 193 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); 194 194 195 195 $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 } 197 206 198 207 /* 199 208 * Just-in-time temporarily upgrade Visual Text widget shortcode handling … … class WP_Widget_Text extends WP_Widget { 221 230 */ 222 231 $text = apply_filters( 'widget_text', $text, $instance, $this ); 223 232 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. 243 250 } 244 251 245 252 // Undo temporary upgrade of the plugin-supplied shortcode handling. … … class WP_Widget_Text extends WP_Widget { 271 278 * @return array Settings to save or bool false to cancel saving. 272 279 */ 273 280 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 274 288 $instance = $old_instance; 289 275 290 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 276 291 if ( current_user_can( 'unfiltered_html' ) ) { 277 292 $instance['text'] = $new_instance['text']; … … class WP_Widget_Text extends WP_Widget { 279 294 $instance['text'] = wp_kses_post( $new_instance['text'] ); 280 295 } 281 296 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; 296 314 } 297 315 298 316 return $instance; … … class WP_Widget_Text extends WP_Widget { 333 351 <?php if ( ! $this->is_legacy_instance( $instance ) ) : ?> 334 352 <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'] ); ?>"> 335 353 <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="1"> 355 <input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value="1"> 336 356 <?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=""> 338 358 <p> 339 359 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 340 360 <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'] ); ?>"/> -
tests/phpunit/tests/widgets/custom-html-widget.php
diff --git tests/phpunit/tests/widgets/custom-html-widget.php tests/phpunit/tests/widgets/custom-html-widget.php index dcd03d2007..35dac8462a 100644
class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { 59 59 'content' => $content, 60 60 ); 61 61 62 // Convert Custom HTML widget instance into Text widget instance data. 63 $text_widget_instance = array_merge( $instance, array( 64 'text' => $instance['content'], 65 'filter' => false, 66 'visual' => false, 67 ) ); 68 unset( $text_widget_instance['content'] ); 69 62 70 update_option( 'use_balanceTags', 0 ); 63 71 add_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5, 3 ); 64 72 add_filter( 'widget_text', array( $this, 'filter_widget_text' ), 10, 3 ); … … class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { 75 83 $this->assertNotContains( '<p>', $output ); 76 84 $this->assertNotContains( '<br>', $output ); 77 85 $this->assertNotContains( '</u>', $output ); 78 $this->assertEquals( $ instance, $this->widget_text_args[1] );86 $this->assertEquals( $text_widget_instance, $this->widget_text_args[1] ); 79 87 $this->assertEquals( $instance, $this->widget_custom_html_content_args[1] ); 80 88 $this->assertSame( $widget, $this->widget_text_args[2] ); 81 89 $this->assertSame( $widget, $this->widget_custom_html_content_args[2] ); 82 remove_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5 , 3);90 remove_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5 ); 83 91 remove_filter( 'widget_text', array( $this, 'filter_widget_text' ), 10 ); 84 92 85 93 update_option( 'use_balanceTags', 1 ); -
tests/phpunit/tests/widgets/text-widget.php
diff --git tests/phpunit/tests/widgets/text-widget.php tests/phpunit/tests/widgets/text-widget.php index 764b4eaeac..c65e2e0f5c 100644
class Test_WP_Widget_Text extends WP_UnitTestCase { 82 82 'before_widget' => '<section>', 83 83 'after_widget' => "</section>\n", 84 84 ); 85 $instance = array(86 'title' => 'Foo',87 'text' => $text,88 'filter' => false,89 );90 85 91 86 add_filter( 'widget_text_content', array( $this, 'filter_widget_text_content' ), 5, 3 ); 92 87 add_filter( 'widget_text', array( $this, 'filter_widget_text' ), 5, 3 ); 93 88 94 // Test with filter=false. 89 // Test with filter=false, implicit legacy mode. 90 $this->widget_text_content_args = null; 95 91 ob_start(); 92 $instance = array( 93 'title' => 'Foo', 94 'text' => $text, 95 'filter' => false, 96 ); 96 97 $widget->widget( $args, $instance ); 97 98 $output = ob_get_clean(); 98 99 $this->assertNotContains( '<p>', $output ); … … class Test_WP_Widget_Text extends WP_UnitTestCase { 102 103 $this->assertContains( '[filter:widget_text]', $output ); 103 104 $this->assertNotContains( '[filter:widget_text_content]', $output ); 104 105 105 // Test with filter=true. 106 $instance['filter'] = true; 106 // Test with filter=true, implicit legacy mode. 107 $this->widget_text_content_args = null; 108 $instance = array( 109 'title' => 'Foo', 110 'text' => $text, 111 'filter' => true, 112 ); 107 113 ob_start(); 108 114 $widget->widget( $args, $instance ); 109 115 $output = ob_get_clean(); … … class Test_WP_Widget_Text extends WP_UnitTestCase { 117 123 $this->assertContains( '[filter:widget_text]', $output ); 118 124 $this->assertNotContains( '[filter:widget_text_content]', $output ); 119 125 120 // Test with filter=content, the upgraded widget. 121 $instance['filter'] = 'content'; 126 // Test with filter=content, the upgraded widget, in 4.8.0 only. 127 $this->widget_text_content_args = null; 128 $instance = array( 129 'title' => 'Foo', 130 'text' => $text, 131 'filter' => 'content', 132 ); 133 $expected_instance = array_merge( $instance, array( 134 'filter' => true, 135 'visual' => true, 136 ) ); 137 ob_start(); 138 $widget->widget( $args, $instance ); 139 $output = ob_get_clean(); 140 $this->assertContains( '<p>', $output ); 141 $this->assertContains( '<br />', $output ); 142 $this->assertCount( 3, $this->widget_text_args ); 143 $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] ); 144 $this->assertEquals( $expected_instance, $this->widget_text_args[1] ); 145 $this->assertEquals( $widget, $this->widget_text_args[2] ); 146 $this->assertCount( 3, $this->widget_text_content_args ); 147 $this->assertEquals( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] ); 148 $this->assertEquals( $expected_instance, $this->widget_text_content_args[1] ); 149 $this->assertEquals( $widget, $this->widget_text_content_args[2] ); 150 $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output ); 151 152 // Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above. 153 $this->widget_text_content_args = null; 154 $instance = array( 155 'title' => 'Foo', 156 'text' => $text, 157 'filter' => true, 158 'visual' => true, 159 ); 160 $expected_instance = $instance; 122 161 ob_start(); 123 162 $widget->widget( $args, $instance ); 124 163 $output = ob_get_clean(); 125 164 $this->assertContains( '<p>', $output ); 126 165 $this->assertContains( '<br />', $output ); 127 166 $this->assertCount( 3, $this->widget_text_args ); 128 $this->assertEquals( $ instance['text'], $this->widget_text_args[0] );129 $this->assertEquals( $ instance, $this->widget_text_args[1] );167 $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] ); 168 $this->assertEquals( $expected_instance, $this->widget_text_args[1] ); 130 169 $this->assertEquals( $widget, $this->widget_text_args[2] ); 131 170 $this->assertCount( 3, $this->widget_text_content_args ); 132 $this->assertEquals( $ instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );133 $this->assertEquals( $ instance, $this->widget_text_content_args[1] );171 $this->assertEquals( $expected_instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] ); 172 $this->assertEquals( $expected_instance, $this->widget_text_content_args[1] ); 134 173 $this->assertEquals( $widget, $this->widget_text_content_args[2] ); 135 $this->assertContains( wpautop( $instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output ); 174 $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output ); 175 176 // Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above. 177 $this->widget_text_content_args = null; 178 $instance = array( 179 'title' => 'Foo', 180 'text' => $text, 181 'filter' => true, 182 'visual' => false, 183 ); 184 $expected_instance = $instance; 185 ob_start(); 186 $widget->widget( $args, $instance ); 187 $output = ob_get_clean(); 188 $this->assertContains( '<p>', $output ); 189 $this->assertContains( '<br />', $output ); 190 $this->assertCount( 3, $this->widget_text_args ); 191 $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] ); 192 $this->assertEquals( $expected_instance, $this->widget_text_args[1] ); 193 $this->assertEquals( $widget, $this->widget_text_args[2] ); 194 $this->assertNull( $this->widget_text_content_args ); 195 $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text]' ), $output ); 196 197 // Test with filter=false&visual=false, the upgraded widget, in 4.8.1 and above. 198 $this->widget_text_content_args = null; 199 $instance = array( 200 'title' => 'Foo', 201 'text' => $text, 202 'filter' => false, 203 'visual' => false, 204 ); 205 $expected_instance = $instance; 206 ob_start(); 207 $widget->widget( $args, $instance ); 208 $output = ob_get_clean(); 209 $this->assertNotContains( '<p>', $output ); 210 $this->assertNotContains( '<br />', $output ); 211 $this->assertCount( 3, $this->widget_text_args ); 212 $this->assertEquals( $expected_instance['text'], $this->widget_text_args[0] ); 213 $this->assertEquals( $expected_instance, $this->widget_text_args[1] ); 214 $this->assertEquals( $widget, $this->widget_text_args[2] ); 215 $this->assertNull( $this->widget_text_content_args ); 216 $this->assertContains( $expected_instance['text'] . '[filter:widget_text]', $output ); 136 217 } 137 218 138 219 /** … … class Test_WP_Widget_Text extends WP_UnitTestCase { 250 331 ); 251 332 252 333 $instance = array_merge( $base_instance, array( 253 ' legacy' => true,334 'visual' => false, 254 335 ) ); 255 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when legacy prop is present.' ); 336 $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when visual=false prop is present.' ); 337 338 $instance = array_merge( $base_instance, array( 339 'visual' => true, 340 ) ); 341 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when visual=true prop is present.' ); 256 342 257 343 $instance = array_merge( $base_instance, array( 258 344 'filter' => 'content', 259 345 ) ); 260 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when filter is explicitly content .' );346 $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when filter is explicitly content (in WP 4.8.0 only).' ); 261 347 262 348 $instance = array_merge( $base_instance, array( 263 349 'text' => '', … … class Test_WP_Widget_Text extends WP_UnitTestCase { 364 450 'title' => 'Title', 365 451 'text' => 'Text', 366 452 'filter' => false, 367 ' legacy' => true,453 'visual' => false, 368 454 ); 369 455 $this->assertTrue( $widget->is_legacy_instance( $instance ) ); 370 456 ob_start(); 371 457 $widget->form( $instance ); 372 458 $form = ob_get_clean(); 373 $this->assertContains( 'class="legacy"', $form ); 459 $this->assertContains( 'class="visual" type="hidden" value=""', $form ); 460 $this->assertNotContains( 'class="visual" type="hidden" value="1"', $form ); 374 461 375 462 $instance = array( 376 463 'title' => 'Title', … … class Test_WP_Widget_Text extends WP_UnitTestCase { 381 468 ob_start(); 382 469 $widget->form( $instance ); 383 470 $form = ob_get_clean(); 384 $this->assertNotContains( 'class="legacy"', $form ); 471 $this->assertContains( 'class="visual" type="hidden" value="1"', $form ); 472 $this->assertNotContains( 'class="visual" type="hidden" value=""', $form ); 473 474 $instance = array( 475 'title' => 'Title', 476 'text' => 'Text', 477 'filter' => true, 478 ); 479 $this->assertFalse( $widget->is_legacy_instance( $instance ) ); 480 ob_start(); 481 $widget->form( $instance ); 482 $form = ob_get_clean(); 483 $this->assertContains( 'class="visual" type="hidden" value="1"', $form ); 484 $this->assertNotContains( 'class="visual" type="hidden" value=""', $form ); 485 486 $instance = array( 487 'title' => 'Title', 488 'text' => 'Text', 489 'filter' => true, 490 'visual' => true, 491 ); 492 $this->assertFalse( $widget->is_legacy_instance( $instance ) ); 493 ob_start(); 494 $widget->form( $instance ); 495 $form = ob_get_clean(); 496 $this->assertContains( 'class="visual" type="hidden" value="1"', $form ); 497 $this->assertNotContains( 'class="visual" type="hidden" value=""', $form ); 385 498 } 386 499 387 500 /** … … class Test_WP_Widget_Text extends WP_UnitTestCase { 394 507 $instance = array( 395 508 'title' => "The\nTitle", 396 509 'text' => "The\n\nText", 397 'filter' => 'content', 510 'filter' => true, 511 'visual' => true, 398 512 ); 399 513 400 514 wp_set_current_user( $this->factory()->user->create( array( 401 515 'role' => 'administrator', 402 516 ) ) ); 403 517 404 // Should return valid instance in legacy mode since filter=false and there are line breaks.405 518 $expected = array( 406 519 'title' => sanitize_text_field( $instance['title'] ), 407 520 'text' => $instance['text'], 408 'filter' => 'content', 521 'filter' => true, 522 'visual' => true, 409 523 ); 410 524 $result = $widget->update( $instance, array() ); 411 525 $this->assertEquals( $expected, $result ); 412 526 $this->assertTrue( ! empty( $expected['filter'] ), 'Expected filter prop to be truthy, to handle case where 4.8 is downgraded to 4.7.' ); 413 527 414 // Make sure KSES is applying as expected.415 528 add_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ), 10, 2 ); 416 529 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 417 530 $instance['text'] = '<script>alert( "Howdy!" );</script>'; 418 531 $expected['text'] = $instance['text']; 419 532 $result = $widget->update( $instance, array() ); 420 $this->assertEquals( $expected, $result );533 $this->assertEquals( $expected, $result, 'KSES should apply as expected.' ); 421 534 remove_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ) ); 422 535 423 536 add_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10, 2 ); … … class Test_WP_Widget_Text extends WP_UnitTestCase { 425 538 $instance['text'] = '<script>alert( "Howdy!" );</script>'; 426 539 $expected['text'] = wp_kses_post( $instance['text'] ); 427 540 $result = $widget->update( $instance, array() ); 428 $this->assertEquals( $expected, $result );541 $this->assertEquals( $expected, $result, 'KSES should not apply since user can unfiltered_html.' ); 429 542 remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10 ); 430 543 } 431 544 … … class Test_WP_Widget_Text extends WP_UnitTestCase { 437 550 function test_update_legacy() { 438 551 $widget = new WP_Widget_Text(); 439 552 440 // Updating a widget with explicit filter=true persists with legacy mode. 553 // -- 554 $instance = array( 555 'title' => 'Legacy', 556 'text' => 'Text', 557 'filter' => false, 558 ); 559 $result = $widget->update( $instance, array() ); 560 $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=false leaves visual prop absent' ); 561 562 // -- 441 563 $instance = array( 442 564 'title' => 'Legacy', 443 565 'text' => 'Text', 444 566 'filter' => true, 445 567 ); 446 568 $result = $widget->update( $instance, array() ); 569 $this->assertEquals( $instance, $result, 'Updating a widget without visual prop and explicit filter=true leaves legacy prop absent.' ); 570 571 // -- 572 $instance = array( 573 'title' => 'Legacy', 574 'text' => 'Text', 575 'visual' => true, 576 ); 577 $old_instance = array_merge( $instance, array( 578 'filter' => false, 579 ) ); 447 580 $expected = array_merge( $instance, array( 448 'legacy' => true,449 581 'filter' => true, 450 582 ) ); 451 $this->assertEquals( $expected, $result ); 583 $result = $widget->update( $instance, $old_instance ); 584 $this->assertEquals( $expected, $result, 'Updating a pre-existing widget with visual mode forces filter to be true.' ); 452 585 453 // Updating a widget with explicit filter=false persists with legacy mode. 454 $instance['filter'] = false; 586 // -- 587 $instance = array( 588 'title' => 'Legacy', 589 'text' => 'Text', 590 'filter' => true, 591 ); 592 $old_instance = array_merge( $instance, array( 593 'visual' => true, 594 ) ); 595 $result = $widget->update( $instance, $old_instance ); 596 $expected = array_merge( $instance, array( 597 'visual' => true, 598 ) ); 599 $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual mode when updated.' ); 600 601 // -- 602 $instance = array( 603 'title' => 'Legacy', 604 'text' => 'Text', 605 ); 606 $old_instance = array_merge( $instance, array( 607 'visual' => true, 608 ) ); 609 $result = $widget->update( $instance, $old_instance ); 610 $expected = array_merge( $instance, array( 611 'visual' => true, 612 'filter' => true, 613 ) ); 614 $this->assertEquals( $expected, $result, 'Updating a pre-existing visual widget retains visual=true and supplies missing filter=true.' ); 615 616 // -- 617 $instance = array( 618 'title' => 'Legacy', 619 'text' => 'Text', 620 'visual' => true, 621 ); 622 $expected = array_merge( $instance, array( 623 'filter' => true, 624 ) ); 625 $result = $widget->update( $instance, array() ); 626 $this->assertEquals( $expected, $result, 'Updating a widget with explicit visual=true and absent filter prop causes filter to be set to true.' ); 627 628 // -- 629 $instance = array( 630 'title' => 'Legacy', 631 'text' => 'Text', 632 'visual' => false, 633 ); 455 634 $result = $widget->update( $instance, array() ); 456 635 $expected = array_merge( $instance, array( 457 'legacy' => true,458 636 'filter' => false, 459 637 ) ); 460 $this->assertEquals( $expected, $result ); 638 $this->assertEquals( $expected, $result, 'Updating a widget in legacy mode results in filter=false as if checkbox not checked.' ); 639 640 // -- 641 $instance = array( 642 'title' => 'Title', 643 'text' => 'Text', 644 'filter' => false, 645 ); 646 $old_instance = array_merge( $instance, array( 647 'visual' => false, 648 'filter' => true, 649 ) ); 650 $result = $widget->update( $instance, $old_instance ); 651 $expected = array_merge( $instance, array( 652 'visual' => false, 653 'filter' => false, 654 ) ); 655 $this->assertEquals( $expected, $result, 'Updating a widget that previously had legacy form results in filter allowed to be false.' ); 461 656 462 // Updating a widget in legacy form results in filter=false when checkbox not checked. 463 $instance['filter'] = true; 657 // -- 658 $instance = array( 659 'title' => 'Title', 660 'text' => 'Text', 661 'filter' => 'content', 662 ); 464 663 $result = $widget->update( $instance, array() ); 465 664 $expected = array_merge( $instance, array( 466 'legacy' => true,467 665 'filter' => true, 666 'visual' => true, 468 667 ) ); 469 $this->assertEquals( $expected, $result );668 $this->assertEquals( $expected, $result, 'Updating a widget that had \'content\' as its filter value persists non-legacy mode. This only existed in WP 4.8.0.' ); 470 669 471 // Updating a widget that previously had legacy form results in filter persisting. 472 unset( $instance['legacy'] ); 473 $instance['filter'] = true; 474 $result = $widget->update( $instance, array( 475 'legacy' => true, 670 // -- 671 $instance = array( 672 'title' => 'Title', 673 'text' => 'Text', 674 ); 675 $old_instance = array_merge( $instance, array( 676 'filter' => 'content', 476 677 ) ); 678 $result = $widget->update( $instance, $old_instance ); 477 679 $expected = array_merge( $instance, array( 478 ' legacy' => true,680 'visual' => true, 479 681 'filter' => true, 480 682 ) ); 481 $this->assertEquals( $expected, $result ); 683 $this->assertEquals( $expected, $result, 'Updating a pre-existing widget with the filter=content prop in WP 4.8.0 upgrades to filter=true&visual=true.' ); 684 685 // -- 686 $instance = array( 687 'title' => 'Title', 688 'text' => 'Text', 689 'filter' => 'content', 690 ); 691 $result = $widget->update( $instance, array() ); 692 $expected = array_merge( $instance, array( 693 'filter' => true, 694 'visual' => true, 695 ) ); 696 $this->assertEquals( $expected, $result, 'Updating a widget with filter=content (from WP 4.8.0) upgrades to filter=true&visual=true.' ); 482 697 } 483 698 484 699 /**