From 13b52a310f54f09a3f9c5efabb38ede699410202 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 16 Nov 2021 01:41:23 +0000 Subject: [PATCH] Widgets: Use `isset()` in `WP_Widget:: display_callback()` to support `ArrayIterator` and `ArrayObject`. [33696] introduced support returning `ArrayIterator` and `ArrayObject` objects from `WP_Widget::get_settings()`. Per the PHP manual, `array_key_exists()` stopped supporting this in PHP 8.0.0 and deprecated in PHP 7.4.0. >For backward compatibility reasons, array_key_exists() will also return true if key is a property defined within an object given as array. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0. This commit uses `isset()` instead of `array_key_exists()` which is supported on all current versions of PHP. Includes unit tests. Ref: * https://www.php.net/manual/en/function.array-key-exists.php#refsect1-function.array-key-exists-notes Follow-up to [32602], [33696]. Props dlh, hellofromTonya, jrf, sergeybiryukov. Fixes #52728. git-svn-id: https://develop.svn.wordpress.org/trunk@52173 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-widget.php | 2 +- tests/phpunit/tests/widgets.php | 41 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-widget.php b/src/wp-includes/class-wp-widget.php index fa206e7d35..e91f5cd9f9 100644 --- a/src/wp-includes/class-wp-widget.php +++ b/src/wp-includes/class-wp-widget.php @@ -365,7 +365,7 @@ class WP_Widget { $this->_set( $widget_args['number'] ); $instances = $this->get_settings(); - if ( array_key_exists( $this->number, $instances ) ) { + if ( isset( $instances[ $this->number ] ) ) { $instance = $instances[ $this->number ]; /** diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index 679d2b7a59..36e9b684f6 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -590,6 +590,47 @@ class Tests_Widgets extends WP_UnitTestCase { // @todo Test WP_Widget::display_callback(). + /** + * @ticket 52728 + */ + function test_widget_display_callback_handles_arrayobject() { + $widget = new WP_Widget_Text(); + + register_widget( $widget ); + + add_filter( + "pre_option_{$widget->option_name}", + static function() { + return new ArrayObject( + array( + 2 => array( 'title' => 'Test Title' ), + '_multiwidget' => 1, + '__i__' => true, + ) + ); + } + ); + + // Effectively ignore the output until retrieving it later via `getActualOutput()`. + $this->expectOutputRegex( '`.`' ); + + $widget->display_callback( + array( + 'before_widget' => '
', + 'after_widget' => "
\n", + 'before_title' => '

', + 'after_title' => "

\n", + ), + 2 + ); + + $actual = $this->getActualOutput(); + + unregister_widget( $widget ); + + $this->assertStringContainsString( 'Test Title', $actual ); + } + /** * @see WP_Widget::is_preview() */