From c1c93f1e9565f8d647080e103699f6d216c2564d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 24 Sep 2015 00:29:54 +0000 Subject: [PATCH] Widgets: when passing a string arg value to `dynamic_sidebar()`, don't reset `$index` when the arg's sanitized value matches the sanitized name of a sidebar. Adds unit test. Props tyxla, fjarrett. Fixes #23423. git-svn-id: https://develop.svn.wordpress.org/trunk@34465 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/widget-functions.php | 8 ++++---- tests/phpunit/tests/widgets.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/widget-functions.php b/src/wp-includes/widget-functions.php index c7d46158c1..eac11f03a6 100644 --- a/src/wp-includes/widget-functions.php +++ b/src/wp-includes/widget-functions.php @@ -501,15 +501,15 @@ function wp_unregister_widget_control($id) { * @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar. * @return bool True, if widget sidebar was found and called. False if not found or not called. */ -function dynamic_sidebar($index = 1) { +function dynamic_sidebar( $index = 1 ) { global $wp_registered_sidebars, $wp_registered_widgets; - if ( is_int($index) ) { + if ( is_int( $index ) ) { $index = "sidebar-$index"; } else { - $index = sanitize_title($index); + $sanitized_index = sanitize_title( $index ); foreach ( (array) $wp_registered_sidebars as $key => $value ) { - if ( sanitize_title($value['name']) == $index ) { + if ( sanitize_title( $value['name'] ) == $sanitized_index ) { $index = $key; break; } diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index 443d3f3d7c..b7cdb14d32 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -293,4 +293,21 @@ class Tests_Widgets extends WP_UnitTestCase { $this->assertArrayNotHasKey( 2, $option_value ); } + /** + * @ticket 23423 + */ + function test_dynamic_sidebar_id_special_characters() { + wp_widgets_init(); + register_sidebar( array( + 'name' => 'Sidebar 2', + 'id' => 'sidebar-2', + ) ); + + ob_start(); + $result = dynamic_sidebar( 'Sidebar 1' ); + ob_end_clean(); + + $this->assertFalse( $result ); + } + }