Make WordPress Core


Ignore:
Timestamp:
09/20/2021 06:48:47 PM (3 years ago)
Author:
hellofromTonya
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-widgets-controller.php

    r51657 r51830  
    110110            'WP test widget',
    111111            static function () {
    112                 $settings = get_option( 'widget_testwidget' );
    113 
    114112                // check if anything's been sent.
    115113                if ( isset( $_POST['update_testwidget'] ) ) {
     114                    $settings = get_option( 'widget_testwidget', array() );
     115
    116116                    $settings['id']    = $_POST['test_id'];
    117117                    $settings['title'] = $_POST['test_title'];
     
    130130            static function () {
    131131                $settings = wp_parse_args(
    132                     get_option( 'widget_testwidget' ),
     132                    get_option( 'widget_testwidget', array() ),
    133133                    array(
    134134                        'id'    => 'Default id',
Note: See TracChangeset for help on using the changeset viewer.