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
This commit is contained in:
Tonya Mork 2021-09-20 18:48:47 +00:00
parent 506aa746b5
commit cece2cca5e

View File

@ -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',