diff --git a/src/wp-includes/class-wp-widget.php b/src/wp-includes/class-wp-widget.php index 8d1de7c375..b177c82053 100644 --- a/src/wp-includes/class-wp-widget.php +++ b/src/wp-includes/class-wp-widget.php @@ -160,7 +160,14 @@ class WP_Widget { * information on accepted arguments. Default empty array. */ public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) { - $this->id_base = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ) : strtolower( $id_base ); + if ( ! empty( $id_base ) ) { + $id_base = strtolower( $id_base ); + } else { + $id_base = preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ); + $id_base = str_replace( '\\', '-', $id_base ); + } + + $this->id_base = $id_base; $this->name = $name; $this->option_name = 'widget_' . $this->id_base; $this->widget_options = wp_parse_args( diff --git a/tests/phpunit/data/widgets/custom-widget-classes.php b/tests/phpunit/data/widgets/custom-widget-classes.php new file mode 100644 index 0000000000..8d079f3693 --- /dev/null +++ b/tests/phpunit/data/widgets/custom-widget-classes.php @@ -0,0 +1,14 @@ +assertArrayHasKey( $widget_class, $wp_widget_factory->widgets ); @@ -52,6 +54,7 @@ class Tests_Widgets extends WP_UnitTestCase { */ function test_register_and_unregister_widget_instance() { global $wp_widget_factory, $wp_registered_widgets; + $this->assertEmpty( $wp_widget_factory->widgets ); $this->assertEmpty( $wp_registered_widgets ); @@ -127,7 +130,6 @@ class Tests_Widgets extends WP_UnitTestCase { * @group sidebar */ function test_register_sidebars_single() { - global $wp_registered_sidebars; register_sidebars( 1, array( 'id' => 'wp-unit-test' ) ); @@ -140,7 +142,6 @@ class Tests_Widgets extends WP_UnitTestCase { * @group sidebar */ function test_register_sidebars_multiple() { - global $wp_registered_sidebars; $result = array(); @@ -199,7 +200,6 @@ class Tests_Widgets extends WP_UnitTestCase { * @group sidebar */ function test_register_sidebar_with_string_id() { - global $wp_registered_sidebars; $sidebar_id = 'wp-unit-test'; @@ -425,6 +425,50 @@ class Tests_Widgets extends WP_UnitTestCase { $this->assertSame( $control_options['id_base'], $bar_widget->control_options['id_base'] ); } + /** + * @ticket 44098 + * @see WP_Widget::__construct() + * @dataProvider data_wp_widget_id_base + */ + function test_wp_widget_id_base( $expected, $widget_class ) { + require_once DIR_TESTDATA . '/widgets/custom-widget-classes.php'; + + $widget = new $widget_class( '', 'Foo' ); + + $this->assertSame( $expected, $widget->id_base ); + } + + /** + * Data provider. + * + * Passes the expected `id_base` value and the class name. + * + * @since 5.8.0 + * + * @return array { + * @type array { + * @type string $expected The expected `id_base` value to be returned. + * @type string $widget_class The widget class name for creating an instance. + * } + * } + */ + function data_wp_widget_id_base() { + return array( + array( + 'search', + 'WP_Widget_Search', + ), + array( + 'test-sub-sub-namespaced_widget', + 'Test\Sub\Sub\Namespaced_Widget', + ), + array( + 'non_namespaced_widget', + 'Non_Namespaced_Widget', + ), + ); + } + /** * @see WP_Widget::get_field_name() * @dataProvider data_wp_widget_get_field_name @@ -450,7 +494,6 @@ class Tests_Widgets extends WP_UnitTestCase { * } */ function data_wp_widget_get_field_name() { - return array( array( 'widget-foo[2][title]',