Make WordPress Core

Changeset 58811


Ignore:
Timestamp:
07/25/2024 11:02:47 PM (4 months ago)
Author:
peterwilsoncc
Message:

Options, Meta APIs: Prevent Single Site installs using network notoptions cache.

Modifies the caching of notoptions in delete_network_option() to ensure that the network cache is bypassed on single site installs.

On single site installs the incorrect caching was causing the notoptions cache to remain populated once a deleted option was subsequently added or updated.

Follow up to [58782].

Props bjorsch, pbearne.
Fixes #61730.
See #61484.

Location:
trunk
Files:
2 edited

Legend:

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

    r58782 r58811  
    22652265            )
    22662266        );
     2267
     2268        if ( $result ) {
     2269            $notoptions_key = "$network_id:notoptions";
     2270            $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
     2271
     2272            if ( ! is_array( $notoptions ) ) {
     2273                $notoptions = array();
     2274            }
     2275            $notoptions[ $option ] = true;
     2276            wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
     2277        }
    22672278    }
    22682279
     
    22932304         */
    22942305        do_action( 'delete_site_option', $option, $network_id );
    2295 
    2296         $notoptions_key = "$network_id:notoptions";
    2297         $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
    2298 
    2299         if ( ! is_array( $notoptions ) ) {
    2300             $notoptions = array();
    2301         }
    2302         $notoptions[ $option ] = true;
    2303         wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
    23042306
    23052307        return true;
  • trunk/tests/phpunit/tests/option/networkOption.php

    r58782 r58811  
    6161     *
    6262     * @ticket 61484
     63     * @ticket 61730
    6364     *
    6465     * @covers ::delete_network_option
     
    7374        $this->assertIsArray( $notoptions, 'The notoptions cache is expected to be an array.' );
    7475        $this->assertTrue( $notoptions['foo'], 'The deleted options is expected to be in notoptions.' );
     76
     77        if ( ! is_multisite() ) {
     78            $network_notoptions = wp_cache_get( '1:notoptions', 'site-options' );
     79            $this->assertTrue( empty( $network_notoptions['foo'] ), 'The deleted option is not expected to be in network notoptions on a non-multisite.' );
     80        }
    7581
    7682        $before = get_num_queries();
     
    303309        $this->assertArrayNotHasKey( $option_name, $updated_notoptions, 'The "foobar" option should not be in the notoptions cache after updating it.' );
    304310    }
     311
     312    /**
     313     * Test adding a previously known notoption returns the correct value.
     314     *
     315     * @ticket 61730
     316     *
     317     * @covers ::add_network_option
     318     * @covers ::delete_network_option
     319     */
     320    public function test_adding_previous_notoption_returns_correct_value() {
     321        $option_name = 'ticket_61730_option_to_be_created';
     322
     323        add_network_option( 1, $option_name, 'baz' );
     324        delete_network_option( 1, $option_name );
     325
     326        $this->assertFalse( get_network_option( 1, $option_name ), 'The option should not be found.' );
     327
     328        add_network_option( 1, $option_name, 'foo' );
     329        $this->assertSame( 'foo', get_network_option( 1, $option_name ), 'The option should return the newly set value.' );
     330    }
     331
     332    /**
     333     * Test `get_network_option()` does not use network notoptions cache for single sites.
     334     *
     335     * @ticket 61730
     336     *
     337     * @group ms-excluded
     338     *
     339     * @covers ::get_network_option
     340     */
     341    public function test_get_network_option_does_not_use_network_notoptions_cache_for_single_sites() {
     342        get_network_option( 1, 'ticket_61730_notoption' );
     343
     344        $network_notoptions_cache     = wp_cache_get( '1:notoptions', 'site-options' );
     345        $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' );
     346
     347        $this->assertEmpty( $network_notoptions_cache, 'Network notoptions cache should not be set for single site installs.' );
     348        $this->assertIsArray( $single_site_notoptions_cache, 'Single site notoptions cache should be set.' );
     349        $this->assertArrayHasKey( 'ticket_61730_notoption', $single_site_notoptions_cache, 'The option should be in the notoptions cache.' );
     350    }
     351
     352    /**
     353     * Test `delete_network_option()` does not use network notoptions cache for single sites.
     354     *
     355     * @ticket 61730
     356     * @ticket 61484
     357     *
     358     * @group ms-excluded
     359     *
     360     * @covers ::delete_network_option
     361     */
     362    public function test_delete_network_option_does_not_use_network_notoptions_cache_for_single_sites() {
     363        add_network_option( 1, 'ticket_61730_notoption', 'value' );
     364        delete_network_option( 1, 'ticket_61730_notoption' );
     365
     366        $network_notoptions_cache     = wp_cache_get( '1:notoptions', 'site-options' );
     367        $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' );
     368
     369        $this->assertEmpty( $network_notoptions_cache, 'Network notoptions cache should not be set for single site installs.' );
     370        $this->assertIsArray( $single_site_notoptions_cache, 'Single site notoptions cache should be set.' );
     371        $this->assertArrayHasKey( 'ticket_61730_notoption', $single_site_notoptions_cache, 'The option should be in the notoptions cache.' );
     372    }
     373
     374    /**
     375     * Test `get_network_option()` does not use single site notoptions cache for networks.
     376     *
     377     * @ticket 61730
     378     *
     379     * @group ms-required
     380     *
     381     * @covers ::get_network_option
     382     */
     383    public function test_get_network_option_does_not_use_single_site_notoptions_cache_for_networks() {
     384        get_network_option( 1, 'ticket_61730_notoption' );
     385
     386        $network_notoptions_cache     = wp_cache_get( '1:notoptions', 'site-options' );
     387        $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' );
     388
     389        $this->assertEmpty( $single_site_notoptions_cache, 'Single site notoptions cache should not be set for multisite installs.' );
     390        $this->assertIsArray( $network_notoptions_cache, 'Multisite notoptions cache should be set.' );
     391        $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache, 'The option should be in the notoptions cache.' );
     392    }
     393
     394    /**
     395     * Test `delete_network_option()` does not use single site notoptions cache for networks.
     396     *
     397     * @ticket 61730
     398     * @ticket 61484
     399     *
     400     * @group ms-required
     401     *
     402     * @covers ::delete_network_option
     403     */
     404    public function test_delete_network_option_does_not_use_single_site_notoptions_cache_for_networks() {
     405        add_network_option( 1, 'ticket_61730_notoption', 'value' );
     406        delete_network_option( 1, 'ticket_61730_notoption' );
     407
     408        $network_notoptions_cache     = wp_cache_get( '1:notoptions', 'site-options' );
     409        $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' );
     410
     411        $this->assertEmpty( $single_site_notoptions_cache, 'Single site notoptions cache should not be set for multisite installs.' );
     412        $this->assertIsArray( $network_notoptions_cache, 'Multisite notoptions cache should be set.' );
     413        $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache, 'The option should be in the notoptions cache.' );
     414    }
    305415}
Note: See TracChangeset for help on using the changeset viewer.