Make WordPress Core

Ticket #59738: 59738.diff

File 59738.diff, 3.1 KB (added by peterwilsoncc, 2 years ago)
  • src/wp-includes/option.php

    diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php
    index 4e3886630d..9b93ad9237 100644
    a b function wp_load_options( $options ) { 
    275275                return;
    276276        }
    277277
     278        // Filter options that are known not to exist.
     279        $notoptions = wp_cache_get( 'notoptions', 'options' );
     280        if ( ! is_array( $notoptions ) ) {
     281                $notoptions = array();
     282        }
     283        $options_to_prime = array_diff( $options_to_prime, array_keys( $notoptions ) );
     284
     285        // Bail early if there are no options to be loaded.
     286        if ( empty( $options_to_prime ) ) {
     287                return;
     288        }
     289
    278290        global $wpdb;
    279291        $results = $wpdb->get_results(
    280292                $wpdb->prepare(
  • tests/phpunit/tests/option/wpLoadOptions.php

    diff --git a/tests/phpunit/tests/option/wpLoadOptions.php b/tests/phpunit/tests/option/wpLoadOptions.php
    index bf6a8d5895..0168017a58 100644
    a b class Tests_Option_PrimeOptions extends WP_UnitTestCase { 
    9696                foreach ( $options_to_load as $option ) {
    9797                        $this->assertArrayHasKey( $option, $new_notoptions, "$option was not added to the notoptions cache." );
    9898                }
     99
     100                $initial_num_queries = get_num_queries();
     101
     102                // Check getting the options does not result in a database query.
     103                foreach ( $options_to_load as $option ) {
     104                        get_option( $option );
     105                        $this->assertSame( 0, get_num_queries() - $initial_num_queries, "Loading notoption '{$option}' resulted in a database query." );
     106                }
     107
     108                // Check re-loading does not result in a database query.
     109                wp_load_options( $options_to_load );
     110                $this->assertSame( 0, get_num_queries() - $initial_num_queries, 'Reloading resulted in a database query.' );
    99111        }
    100112
    101113        /**
    class Tests_Option_PrimeOptions extends WP_UnitTestCase { 
    111123
    112124                $this->assertSame( $alloptions, wp_cache_get( 'alloptions', 'options' ), 'The alloptions cache was modified.' );
    113125                $this->assertSame( $notoptions, wp_cache_get( 'notoptions', 'options' ), 'The notoptions cache was modified.' );
     126
     127                $initial_num_queries = get_num_queries();
     128
     129                // Check re-loading does not result in a database query.
     130                wp_load_options( array() );
     131                $this->assertSame( 0, get_num_queries() - $initial_num_queries, 'Reloading resulted in a database query.' );
    114132        }
    115133
    116134        /**
    class Tests_Option_PrimeOptions extends WP_UnitTestCase { 
    126144                $notoptions = wp_cache_get( 'notoptions', 'options' );
    127145                $this->assertIsArray( $notoptions, 'The notoptions cache should be an array.' );
    128146                $this->assertArrayHasKey( 'nonexistent_option', $notoptions, 'nonexistent_option was not added to notoptions.' );
     147
     148                $initial_num_queries = get_num_queries();
     149
     150                // Check getting the options does not result in a database query.
     151                get_option( 'nonexistent_option' );
     152                $this->assertSame( 0, get_num_queries() - $initial_num_queries, "Loading notoption 'nonexistent_option' resulted in a database query." );
     153
     154                // Check re-loading does not result in a database query.
     155                wp_load_options( array( 'nonexistent_option' ) );
     156                $this->assertSame( 0, get_num_queries() - $initial_num_queries, 'Reloading resulted in a database query.' );
    129157        }
    130158}