Make WordPress Core


Ignore:
Timestamp:
09/06/2022 11:26:45 AM (19 months ago)
Author:
spacedmonkey
Message:

Networks and Sites: Use metadata api in *_network_options` functions.

Replace logic found in get_network_option, update_network_option and delete_network_option to use the metadata api. Using the metadata api has a number of benefits, such as consistency, default values and useful filters. This change also improves performance by priming the caches of all network options in a single database request.

Props spacedmonkey, swissspidy, sc0ttkclark, johnjamesjacoby, flixos90, jeremyfelt, pento, peterwilsoncc, mukesh27, desrosj.
Fixes #37181

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/option/networkOption.php

    r53865 r54080  
    138138
    139139    /**
    140      * @ticket 43506
     140     * @ticket 37181
     141     *
    141142     * @group ms-required
    142143     *
     
    145146     * @covers ::wp_cache_delete
    146147     */
    147     public function test_get_network_option_sets_notoptions_if_option_found() {
    148         $network_id     = get_current_network_id();
    149         $notoptions_key = "$network_id:notoptions";
    150 
    151         $original_cache = wp_cache_get( $notoptions_key, 'site-options' );
    152         if ( false !== $original_cache ) {
    153             wp_cache_delete( $notoptions_key, 'site-options' );
    154         }
    155 
    156         // Retrieve any existing option.
    157         get_network_option( $network_id, 'site_name' );
    158 
    159         $cache = wp_cache_get( $notoptions_key, 'site-options' );
    160         if ( false !== $original_cache ) {
    161             wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
    162         }
    163 
    164         $this->assertSame( array(), $cache );
    165     }
    166 
    167     /**
    168      * @ticket 43506
     148    public function test_meta_api_use_values_in_network_option() {
     149        $network_id = self::factory()->network->create();
     150        $option     = __FUNCTION__;
     151        $value      = __FUNCTION__;
     152
     153        add_metadata( 'site', $network_id, $option, $value, true );
     154        $this->assertEqualSets( get_metadata( 'site', $network_id, $option ), array( get_network_option( $network_id, $option, true ) ) );
     155    }
     156
     157    /**
     158     * @ticket 37181
     159     *
     160     * @group ms-required
     161     */
     162    function test_funky_network_meta() {
     163        $network_id      = self::factory()->network->create();
     164        $option          = __FUNCTION__;
     165        $classy          = new StdClass();
     166        $classy->ID      = 1;
     167        $classy->stringy = 'I love slashes\\\\';
     168        $funky_meta[]    = $classy;
     169
     170        $classy          = new StdClass();
     171        $classy->ID      = 2;
     172        $classy->stringy = 'I love slashes\\\\ more';
     173        $funky_meta[]    = $classy;
     174
     175        // Add a network meta item.
     176        $this->assertIsInt( add_metadata( 'site', $network_id, $option, $funky_meta, true ) );
     177
     178        // Check they exists.
     179        $this->assertEquals( $funky_meta, get_network_option( $network_id, $option ) );
     180    }
     181
     182    /**
     183     * @ticket 37181
     184     *
     185     * @group ms-required
     186     */
     187    public function test_meta_api_multiple_values_in_network_option() {
     188        $network_id = self::factory()->network->create();
     189        $option     = __FUNCTION__;
     190        add_metadata( 'site', $network_id, $option, 'monday', true );
     191        add_metadata( 'site', $network_id, $option, 'tuesday', true );
     192        add_metadata( 'site', $network_id, $option, 'wednesday', true );
     193        $this->assertEquals( 'monday', get_network_option( $network_id, $option, true ) );
     194    }
     195
     196    /**
     197     * @ticket 37181
     198     *
    169199     * @group ms-required
    170200     *
     
    172202     * @covers ::wp_cache_get
    173203     */
    174     public function test_get_network_option_sets_notoptions_if_option_not_found() {
    175         $network_id     = get_current_network_id();
    176         $notoptions_key = "$network_id:notoptions";
    177 
    178         $original_cache = wp_cache_get( $notoptions_key, 'site-options' );
    179         if ( false !== $original_cache ) {
    180             wp_cache_delete( $notoptions_key, 'site-options' );
    181         }
    182 
    183         // Retrieve any non-existing option.
    184         get_network_option( $network_id, 'this_does_not_exist' );
    185 
    186         $cache = wp_cache_get( $notoptions_key, 'site-options' );
    187         if ( false !== $original_cache ) {
    188             wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
    189         }
    190 
    191         $this->assertSame( array( 'this_does_not_exist' => true ), $cache );
     204    public function test_network_option_count_queries_on_non_existing() {
     205        $network_id = self::factory()->network->create();
     206        $option     = __FUNCTION__;
     207        add_network_option( $network_id, $option, 'monday' );
     208        get_network_option( $network_id, $option );
     209        $num_queries_pre_get = get_num_queries();
     210        get_network_option( $network_id, 'do_not_exist' );
     211        $num_queries_after_get = get_num_queries();
     212
     213        $this->assertSame( $num_queries_pre_get, $num_queries_after_get );
     214    }
     215
     216    /**
     217     * @ticket 37181
     218     *
     219     * @group ms-required
     220     */
     221    public function test_register_meta_network_option_single_false() {
     222        $network_id = self::factory()->network->create();
     223        $option     = __FUNCTION__;
     224        $value      = __FUNCTION__;
     225        register_meta(
     226            'site',
     227            $option,
     228            array(
     229                'type'    => 'string',
     230                'default' => $value,
     231                'single'  => false,
     232            )
     233        );
     234
     235        $this->assertSame( $value, get_network_option( $network_id, $option ) );
     236    }
     237
     238    /**
     239     * @ticket 37181
     240     *
     241     * @group ms-required
     242     */
     243    public function test_register_meta_network_option_single_true() {
     244        $network_id = self::factory()->network->create();
     245        $option     = __FUNCTION__;
     246        $value      = __FUNCTION__;
     247        register_meta(
     248            'site',
     249            $option,
     250            array(
     251                'type'    => 'string',
     252                'default' => $value,
     253                'single'  => true,
     254            )
     255        );
     256
     257        $this->assertSame( $value, get_network_option( $network_id, $option ) );
    192258    }
    193259
     
    197263     * @ticket 44956
    198264     *
     265     * @group ms-required
     266     *
    199267     * @covers ::update_network_option
    200268     */
    201269    public function test_update_network_option_array_with_object() {
     270        $network_id     = self::factory()->network->create();
     271        $option         = __FUNCTION__;
    202272        $array_w_object = array(
    203273            'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
     
    209279        );
    210280
    211         $array_w_object_2 = array(
    212             'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
    213             'meta_data' => (object) array(
    214                 'attachment_id' => 292,
    215                 'height'        => 708,
    216                 'width'         => 1260,
    217             ),
    218         );
    219 
    220         // Add the option, it did not exist before this.
    221         add_network_option( null, 'array_w_object', $array_w_object );
    222 
    223         $num_queries_pre_update = get_num_queries();
    224 
    225         // Update the option using the same array with an object for the value.
    226         $this->assertFalse( update_network_option( null, 'array_w_object', $array_w_object_2 ) );
    227 
    228         // Check that no new database queries were performed.
    229         $this->assertSame( $num_queries_pre_update, get_num_queries() );
     281        add_metadata( 'site', $network_id, $option, $array_w_object, true );
     282        $this->assertEquals( $array_w_object, get_network_option( $network_id, $option ) );
     283    }
     284
     285    /**
     286     * @ticket 37181
     287     *
     288     * @group ms-required
     289     *
     290     * @covers ::add_network_option
     291     *
     292     * @dataProvider data_types_options
     293     */
     294    public function test_type_add_network_option( $name, $value, $expected ) {
     295        $result = add_network_option( null, $name, $value );
     296        $this->assertTrue( $result, 'Network option was not added' );
     297
     298        $test_value = get_network_option( null, $name );
     299        $this->assertSame( $expected, $test_value, 'Values do not match' );
     300    }
     301
     302    /**
     303     * @ticket 37181
     304     *
     305     * @covers ::add_network_option
     306     *
     307     * @dataProvider data_slashed_options
     308     */
     309    public function test_slash_add_network_option( $name, $value ) {
     310        $result = add_network_option( null, $name, $value );
     311        $this->assertTrue( $result, 'Network option was not added' );
     312        $this->assertSame( $value, get_network_option( null, $name ), 'Values do not match' );
     313    }
     314
     315    /**
     316     * @ticket 37181
     317     *
     318     * @covers ::update_network_option
     319     *
     320     * @dataProvider data_slashed_options
     321     */
     322    public function test_slash_update_network_option( $name, $value ) {
     323        $result = update_network_option( null, $name, $value );
     324        $this->assertTrue( $result, 'Network option was not updated' );
     325        $this->assertSame( $value, get_network_option( null, $name ), 'Values do not match' );
     326    }
     327
     328    /**
     329     * @ticket 37181
     330     *
     331     * @covers ::delete_network_option()
     332     *
     333     * @dataProvider data_slashed_options
     334     */
     335    public function test_slash_delete_network_option( $name, $value ) {
     336        $result = add_network_option( null, $name, $value );
     337        $this->assertTrue( $result, 'Network option was not added' );
     338        $this->assertSame( $value, get_network_option( null, $name ) );
     339        $result = delete_network_option( null, $name );
     340        $this->assertTrue( $result, 'Network option was not deleted' );
     341        $this->assertFalse( get_network_option( null, $name ), 'Network option was not deleted' );
     342    }
     343
     344    public function data_slashed_options() {
     345        return array(
     346            'slashed option name'                   => array(
     347                'option' => 'String with 1 slash \\',
     348                'value'  => 'foo',
     349            ),
     350            'slashed in middle option name'         => array(
     351                'option' => 'String\\thing',
     352                'value'  => 'foo',
     353            ),
     354            'slashed option value'                  => array(
     355                'option' => 'bar',
     356                'value'  => 'String with 1 slash \\',
     357            ),
     358            'slashed option name and value'         => array(
     359                'option' => 'String with 1 slash \\',
     360                'value'  => 'String with 1 slash \\',
     361            ),
     362            'slashed 4 times option name and value' => array(
     363                'option' => 'String with 4 slashes \\\\\\\\',
     364                'value'  => 'String with 4 slashes \\\\\\\\',
     365            ),
     366            'slashed 7 times option name and value' => array(
     367                'option' => 'String with 7 slashes \\\\\\\\\\\\\\',
     368                'value'  => 'String with 7 slashes \\\\\\\\\\\\\\',
     369            ),
     370        );
     371    }
     372
     373    public function data_types_options() {
     374        return array(
     375            'array'       => array(
     376                'option'   => 'array',
     377                'value'    => array(),
     378                'expected' => array(),
     379            ),
     380            'array_keys'  => array(
     381                'option'   => 'array',
     382                'value'    => array( 'key' => 'value' ),
     383                'expected' => array( 'key' => 'value' ),
     384            ),
     385            'int'         => array(
     386                'option'   => 'int',
     387                'value'    => 33,
     388                'expected' => '33',
     389            ),
     390            'string'      => array(
     391                'option'   => 'string',
     392                'value'    => 'foo',
     393                'expected' => 'foo',
     394            ),
     395            'string_bool' => array(
     396                'option'   => 'string',
     397                'value'    => 'true',
     398                'expected' => 'true',
     399            ),
     400            'float'       => array(
     401                'option'   => 'float',
     402                'value'    => 33.5555,
     403                'expected' => '33.5555',
     404            ),
     405            'bool'        => array(
     406                'option'   => 'bool',
     407                'value'    => true,
     408                'expected' => '1',
     409            ),
     410            'null'        => array(
     411                'option'   => 'null',
     412                'value'    => null,
     413                'expected' => null,
     414            ),
     415        );
    230416    }
    231417}
Note: See TracChangeset for help on using the changeset viewer.