From cece2cca5e56f20080782edb82e94d87f2f30ef9 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 20 Sep 2021 18:48:47 +0000 Subject: [PATCH] REST API: Fix autovivification deprecation notice in `WP_Test_REST_Widgets_Controller::set_up()`. If the `'widget_testwidget'` option does not exist, `false` was returned from `get_option()`. The `set_up()` logic expects an `array()` and assigns values to keys without checking for an array. The automatic creation of an array (autovivification) triggers a `Deprecated: Automatic conversion of false to array is deprecated in` deprecation notice on PHP 8.1. This commit: - Fixes the deprecation notice by making the default value an empty array. - Moves getting the option within the conditional where it's needed. - Provides a micro-optimization by only getting the options when the conditions are correct for processing. - Makes the code consistent within the `set_up()` for both `get_option()` instances. Follow-up to [51029]. Props jrf, hellofromTonya, BinaryKitten. See #53635. git-svn-id: https://develop.svn.wordpress.org/trunk@51830 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/rest-api/rest-widgets-controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-widgets-controller.php b/tests/phpunit/tests/rest-api/rest-widgets-controller.php index a44512cf5a..be0d84e430 100644 --- a/tests/phpunit/tests/rest-api/rest-widgets-controller.php +++ b/tests/phpunit/tests/rest-api/rest-widgets-controller.php @@ -109,10 +109,10 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { 'testwidget', 'WP test widget', static function () { - $settings = get_option( 'widget_testwidget' ); - // check if anything's been sent. if ( isset( $_POST['update_testwidget'] ) ) { + $settings = get_option( 'widget_testwidget', array() ); + $settings['id'] = $_POST['test_id']; $settings['title'] = $_POST['test_title']; @@ -129,7 +129,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { 'WP test widget', static function () { $settings = wp_parse_args( - get_option( 'widget_testwidget' ), + get_option( 'widget_testwidget', array() ), array( 'id' => 'Default id', 'title' => 'Default text',