From 65c8adc252a98a8a9088fb671003e3183fcb6008 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 13 Oct 2015 01:48:41 +0000 Subject: [PATCH] Widgets: When using `the_widget()`, the `$before_widget` argument only receives the widget class if using the default sidebar arguments. Run `sprintf` after parsing the args to fix this. Adds unit test. Props coffee2code. Fixes #19450. git-svn-id: https://develop.svn.wordpress.org/trunk@35106 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/widget-functions.php | 11 ++++++++--- tests/phpunit/tests/widgets.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/widget-functions.php b/src/wp-includes/widget-functions.php index 8c9b568935..c9c8268a5e 100644 --- a/src/wp-includes/widget-functions.php +++ b/src/wp-includes/widget-functions.php @@ -949,10 +949,15 @@ function the_widget( $widget, $instance = array(), $args = array() ) { return; } - $before_widget = sprintf('
', $widget_obj->widget_options['classname'] ); - $default_args = array( 'before_widget' => $before_widget, 'after_widget' => "
", 'before_title' => '

', 'after_title' => '

' ); + $default_args = array( + 'before_widget' => '
', + 'after_widget' => "
", + 'before_title' => '

', + 'after_title' => '

', + ); + $args = wp_parse_args( $args, $default_args ); + $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); - $args = wp_parse_args($args, $default_args); $instance = wp_parse_args($instance); /** diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index a98942044c..a9c6b62278 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -607,4 +607,19 @@ class Tests_Widgets extends WP_UnitTestCase { $this->assertContains( $contained, $control ); } } + + function test_the_widget_custom_before_title_arg() { + + ob_start(); + the_widget( + 'WP_Widget_Text', + array( 'title' => 'Notes', 'text' => 'Sample text' ), + array( 'before_widget' => '', 'after_widget' => '' ) + ); + $actual = ob_get_clean(); + + $this->assertRegExp( '//', $actual ); + + } + }