From 09182448e7836c28a9d9afa68d4a57eb33f2178e Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Fri, 1 Sep 2017 08:49:50 +0000 Subject: [PATCH] Widgets: Add nudge for registered widgets Informs developers that widgets need to be registered before they can be displayed through `the_widget()`. Previously it would fail with an ambiguous undefined index notice. Props SeBsZ, mrasharirfan. Fixes #41743. git-svn-id: https://develop.svn.wordpress.org/trunk@41327 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/widgets.php | 5 +++++ tests/phpunit/tests/widgets.php | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index fbbdcf5f55..3b4944023c 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1036,6 +1036,11 @@ function wp_convert_widget_settings($base_name, $option_name, $settings) { function the_widget( $widget, $instance = array(), $args = array() ) { global $wp_widget_factory; + if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) { + _doing_it_wrong( __FUNCTION__, __( 'Widgets need to be registered before they can be displayed.' ), '4.9.0' ); + return; + } + $widget_obj = $wp_widget_factory->widgets[$widget]; if ( ! ( $widget_obj instanceof WP_Widget ) ) { return; diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index 233a7a8b02..83c1d3bcfa 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -678,6 +678,17 @@ class Tests_Widgets extends WP_UnitTestCase { } + /** + * Tests that no 'Undefined index' exception is thrown when trying to + * display an unregistered widget. + * + * @see \the_widget() + */ + function test_the_widget_with_unregistered_widget() { + $this->setExpectedIncorrectUsage( 'the_widget' ); + the_widget( 'Widget_Class' ); + } + /** * Register nav menu sidebars. *