Make WordPress Core

Changeset 58945


Ignore:
Timestamp:
08/28/2024 04:39:30 PM (5 months ago)
Author:
flixos90
Message:

Options, Meta APIs: Stop using 'yes' and 'no' for autoload parameter in favor of recommended boolean.

This changeset does not modify any behavior, it only updates the code to use the recommended type for the $autoload parameter as of WordPress 6.6. The old values 'yes' and 'no' are only maintained in certain tests that are explicitly about these backward compatibility values.

Props flixos90, joemcgill, mukesh27.
Fixes #61939.
See #61103, #61929.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/upgrade.php

    r58810 r58945  
    18451845            $uninstall_plugins = get_option( 'uninstall_plugins' );
    18461846            delete_option( 'uninstall_plugins' );
    1847             add_option( 'uninstall_plugins', $uninstall_plugins, null, 'no' );
     1847            add_option( 'uninstall_plugins', $uninstall_plugins, null, false );
    18481848        }
    18491849    }
     
    23412341            if ( false !== $can_compress_scripts ) {
    23422342                delete_option( 'can_compress_scripts' );
    2343                 add_option( 'can_compress_scripts', $can_compress_scripts, '', 'yes' );
     2343                add_option( 'can_compress_scripts', $can_compress_scripts, '', true );
    23442344            }
    23452345        }
     
    23942394        );
    23952395
    2396         $autoload = array_fill_keys( $theme_mods_options, 'no' );
     2396        $autoload = array_fill_keys( $theme_mods_options, false );
    23972397        wp_set_option_autoload_values( $autoload );
    23982398    }
  • trunk/tests/phpunit/tests/option/option.php

    r58782 r58945  
    144144     */
    145145    public function test_get_option_notoptions_do_not_load_cache() {
    146         add_option( 'foo', 'bar', '', 'no' );
     146        add_option( 'foo', 'bar', '', false );
    147147        wp_cache_delete( 'notoptions', 'options' );
    148148
     
    361361        return array(
    362362            // Supported values.
    363             array( 'autoload_yes', 'yes', 'on' ),
    364363            array( 'autoload_true', true, 'on' ),
    365             array( 'autoload_no', 'no', 'off' ),
    366364            array( 'autoload_false', false, 'off' ),
    367365            array( 'autoload_null', null, 'auto' ),
     366
     367            // Values supported for backward compatibility.
     368            array( 'autoload_yes', 'yes', 'on' ),
     369            array( 'autoload_no', 'no', 'off' ),
    368370
    369371            // Technically unsupported values.
     
    458460     */
    459461    public function test_update_option_with_autoload_change_no_to_yes() {
    460         add_option( 'foo', 'value1', '', 'no' );
    461         update_option( 'foo', 'value2', 'yes' );
     462        add_option( 'foo', 'value1', '', false );
     463        update_option( 'foo', 'value2', true );
    462464        delete_option( 'foo' );
    463465        $this->assertFalse( get_option( 'foo' ) );
     
    474476     */
    475477    public function test_update_option_with_autoload_change_yes_to_no() {
    476         add_option( 'foo', 'value1', '', 'yes' );
    477         update_option( 'foo', 'value2', 'no' );
     478        add_option( 'foo', 'value1', '', true );
     479        update_option( 'foo', 'value2', false );
    478480        delete_option( 'foo' );
    479481        $this->assertFalse( get_option( 'foo' ) );
  • trunk/tests/phpunit/tests/option/updateOption.php

    r55745 r58945  
    5353    public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_yes() {
    5454        $this->flush_cache();
    55         update_option( 'test_update_option_default', 'value', 'yes' );
     55        update_option( 'test_update_option_default', 'value', true );
    5656        $this->flush_cache();
    5757
     
    7676    public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_no() {
    7777        $this->flush_cache();
    78         update_option( 'test_update_option_default', 'value', 'no' );
     78        update_option( 'test_update_option_default', 'value', false );
    7979        $this->flush_cache();
    8080
     
    123123     */
    124124    public function test_autoload_should_be_updated_for_existing_option_when_value_is_changed() {
    125         add_option( 'foo', 'bar', '', 'no' );
     125        add_option( 'foo', 'bar', '', false );
    126126        $updated = update_option( 'foo', 'bar2', true );
    127127        $this->assertTrue( $updated );
     
    147147     */
    148148    public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_unchanged() {
    149         add_option( 'foo', 'bar', '', 'yes' );
     149        add_option( 'foo', 'bar', '', true );
    150150        $updated = update_option( 'foo', 'bar', false );
    151151        $this->assertFalse( $updated );
     
    172172     */
    173173    public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_changed_but_no_value_of_autoload_is_provided() {
    174         add_option( 'foo', 'bar', '', 'yes' );
     174        add_option( 'foo', 'bar', '', true );
    175175
    176176        // Don't pass a value for `$autoload`.
  • trunk/tests/phpunit/tests/option/wpLoadAlloptions.php

    r57920 r58945  
    2727    public function test_default_and_yes() {
    2828        add_option( 'foo', 'bar' );
    29         add_option( 'bar', 'foo', '', 'yes' );
     29        add_option( 'bar', 'foo', '', true );
    3030        $alloptions = wp_load_alloptions();
    3131        $this->assertArrayHasKey( 'foo', $alloptions );
     
    4040    public function test_default_and_no() {
    4141        add_option( 'foo', 'bar' );
    42         add_option( 'bar', 'foo', '', 'no' );
     42        add_option( 'bar', 'foo', '', false );
    4343        $alloptions = wp_load_alloptions();
    4444        $this->assertArrayHasKey( 'foo', $alloptions );
  • trunk/tests/phpunit/tests/option/wpSetOptionAutoload.php

    r57920 r58945  
    1111    /**
    1212     * Tests that setting an option's autoload value to 'yes' works as expected.
     13     *
     14     * The values 'yes' and 'no' are only supported for backward compatibility.
    1315     *
    1416     * @ticket 58964
     
    3032    /**
    3133     * Tests that setting an option's autoload value to 'no' works as expected.
     34     *
     35     * The values 'yes' and 'no' are only supported for backward compatibility.
    3236     *
    3337     * @ticket 58964
     
    5761        $value  = 'value';
    5862
    59         add_option( $option, $value, '', 'yes' );
     63        add_option( $option, $value, '', true );
    6064
    61         $this->assertFalse( wp_set_option_autoload( $option, 'yes' ), 'Function did unexpectedly succeed' );
     65        $this->assertFalse( wp_set_option_autoload( $option, true ), 'Function did unexpectedly succeed' );
    6266        $this->assertSame( 'on', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value unexpectedly updated in database' );
    6367    }
     
    7377        $option = 'test_option';
    7478
    75         $this->assertFalse( wp_set_option_autoload( $option, 'yes' ), 'Function did unexpectedly succeed' );
     79        $this->assertFalse( wp_set_option_autoload( $option, true ), 'Function did unexpectedly succeed' );
    7680        $this->assertNull( $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Missing option autoload value was set in database' );
    7781        $this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), 'Missing option found in alloptions cache' );
  • trunk/tests/phpunit/tests/option/wpSetOptionAutoloadValues.php

    r57920 r58945  
    1212     * Tests setting options' autoload to 'yes' where for some options this is already the case.
    1313     *
     14     * The values 'yes' and 'no' are only supported for backward compatibility.
     15     *
    1416     * @ticket 58964
    1517     */
     
    2123            'test_option2' => 'yes',
    2224        );
    23         add_option( 'test_option1', 'value1', '', 'yes' );
    24         add_option( 'test_option2', 'value2', '', 'no' );
     25        add_option( 'test_option1', 'value1', '', true );
     26        add_option( 'test_option2', 'value2', '', false );
    2527        $expected = array(
    2628            'test_option1' => false,
     
    4345     * In this case, the 'alloptions' cache should not be cleared, but only its options set to 'no' should be deleted.
    4446     *
     47     * The values 'yes' and 'no' are only supported for backward compatibility.
     48     *
    4549     * @ticket 58964
    4650     */
     
    5256            'test_option2' => 'no',
    5357        );
    54         add_option( 'test_option1', 'value1', '', 'yes' );
    55         add_option( 'test_option2', 'value2', '', 'no' );
     58        add_option( 'test_option1', 'value1', '', true );
     59        add_option( 'test_option2', 'value2', '', false );
    5660        $expected = array(
    5761            'test_option1' => true,
     
    7175     * Tests setting options' autoload to 'yes' where for all of them this is already the case.
    7276     *
     77     * The values 'yes' and 'no' are only supported for backward compatibility.
     78     *
    7379     * @ticket 58964
    7480     */
     
    8086            'test_option2' => 'yes',
    8187        );
    82         add_option( 'test_option1', 'value1', '', 'yes' );
    83         add_option( 'test_option2', 'value2', '', 'yes' );
     88        add_option( 'test_option1', 'value1', '', true );
     89        add_option( 'test_option2', 'value2', '', true );
    8490        $expected = array(
    8591            'test_option1' => false,
     
    97103
    98104    /**
    99      * Tests setting options' autoload to either 'yes' or 'no' where for some options this is already the case.
     105     * Tests setting options' autoload to either true or false where for some options this is already the case.
    100106     *
    101107     * The test also covers one option that is entirely missing.
     
    107113
    108114        $options = array(
    109             'test_option1' => 'yes',
    110             'test_option2' => 'no',
    111             'test_option3' => 'yes',
    112             'missing_opt'  => 'yes',
    113         );
    114         add_option( 'test_option1', 'value1', '', 'no' );
    115         add_option( 'test_option2', 'value2', '', 'yes' );
    116         add_option( 'test_option3', 'value3', '', 'yes' );
     115            'test_option1' => true,
     116            'test_option2' => false,
     117            'test_option3' => true,
     118            'missing_opt'  => true,
     119        );
     120        add_option( 'test_option1', 'value1', '', false );
     121        add_option( 'test_option2', 'value2', '', true );
     122        add_option( 'test_option3', 'value3', '', true );
    117123        $expected = array(
    118124            'test_option1' => true,
     
    133139
    134140    /**
    135      * Tests setting options' autoload to either 'yes' or 'no' while only the 'no' options actually need to be updated.
     141     * Tests setting options' autoload to either true or false while only the false options actually need to be updated.
    136142     *
    137143     * In this case, the 'alloptions' cache should not be cleared, but only its options set to 'no' should be deleted.
     
    143149
    144150        $options = array(
    145             'test_option1' => 'yes',
    146             'test_option2' => 'no',
    147             'test_option3' => 'yes',
    148         );
    149         add_option( 'test_option1', 'value1', '', 'yes' );
    150         add_option( 'test_option2', 'value2', '', 'yes' );
    151         add_option( 'test_option3', 'value3', '', 'yes' );
     151            'test_option1' => true,
     152            'test_option2' => false,
     153            'test_option3' => true,
     154        );
     155        add_option( 'test_option1', 'value1', '', true );
     156        add_option( 'test_option2', 'value2', '', true );
     157        add_option( 'test_option3', 'value3', '', true );
    152158        $expected = array(
    153159            'test_option1' => false,
     
    161167        $this->assertSameSets( array( 'on', 'off', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' );
    162168        foreach ( $options as $option => $autoload ) {
    163             if ( 'no' === $autoload ) {
     169            if ( false === $autoload ) {
    164170                $this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), sprintf( 'Option %s not deleted from alloptions cache', $option ) );
    165171            } else {
     
    178184
    179185        $options = array(
    180             'test_option1' => 'yes',
    181             'test_option2' => 'yes',
    182         );
    183         add_option( 'test_option1', 'value1', '', 'no' );
    184         add_option( 'test_option2', 'value2', '', 'no' );
     186            'test_option1' => true,
     187            'test_option2' => true,
     188        );
     189        add_option( 'test_option1', 'value1', '', false );
     190        add_option( 'test_option2', 'value2', '', false );
    185191
    186192        // Force UPDATE queries to fail, leading to no autoload values being updated.
     
    204210
    205211    /**
    206      * Tests setting options' autoload with boolean values.
     212     * Tests setting options' autoload with now encouraged boolean values.
    207213     *
    208214     * @ticket 58964
  • trunk/tests/phpunit/tests/option/wpSetOptionsAutoload.php

    r57920 r58945  
    1212     * Tests that setting options' autoload value to 'yes' works as expected.
    1313     *
     14     * The values 'yes' and 'no' are only supported for backward compatibility.
     15     *
    1416     * @ticket 58964
    1517     */
     
    2426        $expected = array();
    2527        foreach ( $options as $option => $value ) {
    26             add_option( $option, $value, '', 'no' );
     28            add_option( $option, $value, '', false );
    2729            $expected[ $option ] = true;
    2830        }
     
    4143     * Tests that setting options' autoload value to 'no' works as expected.
    4244     *
     45     * The values 'yes' and 'no' are only supported for backward compatibility.
     46     *
    4347     * @ticket 58964
    4448     */
     
    5357        $expected = array();
    5458        foreach ( $options as $option => $value ) {
    55             add_option( $option, $value, '', 'yes' );
     59            add_option( $option, $value, '', true );
    5660            $expected[ $option ] = true;
    5761        }
     
    8185        $expected = array();
    8286        foreach ( $options as $option => $value ) {
    83             add_option( $option, $value, '', 'yes' );
     87            add_option( $option, $value, '', true );
    8488            $expected[ $option ] = false;
    8589        }
    8690
    8791        $num_queries = get_num_queries();
    88         $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), 'yes' ), 'Function did unexpectedly succeed' );
     92        $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), true ), 'Function did unexpectedly succeed' );
    8993        $this->assertSame( $num_queries + 1, get_num_queries(), 'Function attempted to update options autoload value in database' );
    9094        $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Options autoload value unexpectedly updated in database' );
     
    109113        }
    110114
    111         $this->assertSame( $expected, wp_set_options_autoload( $options, 'yes' ), 'Function did unexpectedly succeed' );
     115        $this->assertSame( $expected, wp_set_options_autoload( $options, true ), 'Function did unexpectedly succeed' );
    112116        $this->assertSame( array(), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Missing options autoload value was set in database' );
    113117    }
     
    126130        );
    127131
    128         add_option( 'test_option1', $options['test_option1'], '', 'yes' );
    129         add_option( 'test_option2', $options['test_option2'], '', 'no' );
     132        add_option( 'test_option1', $options['test_option1'], '', true );
     133        add_option( 'test_option2', $options['test_option2'], '', false );
    130134        $expected = array(
    131135            'test_option1' => false,
     
    133137        );
    134138
    135         $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), 'yes' ), 'Function produced unexpected result' );
     139        $this->assertSame( $expected, wp_set_options_autoload( array_keys( $options ), true ), 'Function produced unexpected result' );
    136140        $this->assertSame( array( 'on', 'on' ), $wpdb->get_col( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name IN (" . implode( ',', array_fill( 0, count( $options ), '%s' ) ) . ')', ...array_keys( $options ) ) ), 'Option autoload values not updated in database' );
    137141        foreach ( $options as $option => $value ) {
Note: See TracChangeset for help on using the changeset viewer.