WordPress.org

Make WordPress Core

Changeset 21357


Ignore:
Timestamp:
07/27/12 14:57:32 (3 years ago)
Author:
ryan
Message:

Refactor *_blog_option() functions to use switch_to_blog(), restore_current_blog(), and the *_option() functions. Do not use site-options for blog option caching as this duplicated info and did not properly invalidate.

Props jeremyfelt
fixes #21270

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/ms-blogs.php

    r20461 r21357  
    308308 
    309309/** 
    310  * Retrieve option value based on setting name and blog_id. 
     310 * Retrieve option value for a given blog id based on name of option. 
    311311 * 
    312312 * If the option does not exist or does not have a value, then the return value 
     
    315315 * whether upgrading is required. 
    316316 * 
    317  * There is a filter called 'blog_option_$option' with the $option being 
    318  * replaced with the option name. The filter takes two parameters. $value and 
    319  * $blog_id. It returns $value. 
    320  * The 'option_$option' filter in get_option() is not called. 
    321  * 
    322  * @since MU 
    323  * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value. 
    324  * 
    325  * @param int $blog_id Optional. Blog ID, can be null to refer to the current blog. 
    326  * @param string $setting Name of option to retrieve. Should already be SQL-escaped. 
    327  * @param string $default (optional) Default value returned if option not found. 
     317 * If the option was serialized then it will be unserialized when it is returned. 
     318 * 
     319 * @since MU 
     320 * 
     321 * @param int $id A blog ID. Can be null to refer to the current blog. 
     322 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. 
     323 * @param mixed $default Optional. Default value to return if the option does not exist. 
    328324 * @return mixed Value set for the option. 
    329325 */ 
    330 function get_blog_option( $blog_id, $setting, $default = false ) { 
    331     global $wpdb; 
    332  
    333     if ( null === $blog_id ) 
    334         $blog_id = $wpdb->blogid; 
    335  
    336     $key = $blog_id . '-' . $setting . '-blog_option'; 
    337     $value = wp_cache_get( $key, 'site-options' ); 
    338     if ( $value == null ) { 
    339         if ( $blog_id == $wpdb->blogid ) { 
    340             $value = get_option( $setting, $default ); 
    341             $notoptions = wp_cache_get( 'notoptions', 'options' ); 
    342             if ( isset( $notoptions[$setting] ) ) { 
    343                 wp_cache_set( $key, 'noop', 'site-options' ); 
    344                 $value = $default; 
    345             } elseif ( $value == false ) { 
    346                 wp_cache_set( $key, 'falsevalue', 'site-options' ); 
    347             } else { 
    348                 wp_cache_set( $key, $value, 'site-options' ); 
    349             } 
    350             return apply_filters( 'blog_option_' . $setting, $value, $blog_id ); 
    351         } else { 
    352             $blog_prefix = $wpdb->get_blog_prefix( $blog_id ); 
    353             $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) ); 
    354             if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
    355                 $value = $row->option_value; 
    356                 if ( $value == false ) 
    357                     wp_cache_set( $key, 'falsevalue', 'site-options' ); 
    358                 else 
    359                     wp_cache_set( $key, $value, 'site-options' ); 
    360             } else { // option does not exist, so we must cache its non-existence 
    361                 wp_cache_set( $key, 'noop', 'site-options' ); 
    362                 $value = $default; 
    363             } 
    364         } 
    365     } elseif ( $value == 'noop' ) { 
    366         $value = $default; 
    367     } elseif ( $value == 'falsevalue' ) { 
    368         $value = false; 
    369     } 
    370     // If home is not set use siteurl. 
    371     if ( 'home' == $setting && '' == $value ) 
    372         return get_blog_option( $blog_id, 'siteurl' ); 
    373  
    374     if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting ) 
    375         $value = untrailingslashit( $value ); 
    376  
    377     return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id ); 
    378 } 
    379  
    380 /** 
    381  * Add an option for a particular blog. 
     326function get_blog_option( $id, $option, $default = false ) { 
     327    $id = (int) $id; 
     328 
     329    if ( empty( $id ) ) 
     330        $id = get_current_blog_id(); 
     331 
     332    if ( $id == get_current_blog_id() ) 
     333        return get_option( $option, $default ); 
     334 
     335    switch_to_blog( $id ); 
     336    $option = get_option( $option, $default ); 
     337    restore_current_blog(); 
     338 
     339    return $option; 
     340} 
     341 
     342/** 
     343 * Add a new option for a given blog id. 
     344 * 
     345 * You do not need to serialize values. If the value needs to be serialized, then 
     346 * it will be serialized before it is inserted into the database. Remember, 
     347 * resources can not be serialized or added as an option. 
     348 * 
     349 * You can create options without values and then update the values later. 
     350 * Existing options will not be updated and checks are performed to ensure that you 
     351 * aren't adding a protected WordPress option. Care should be taken to not name 
     352 * options the same as the ones which are protected. 
     353 * 
     354 * @since MU 
     355 * 
     356 * @param int $id A blog ID. Can be null to refer to the current blog. 
     357 * @param string $option Name of option to add. Expected to not be SQL-escaped. 
     358 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. 
     359 * @return bool False if option was not added and true if option was added. 
     360 */ 
     361function add_blog_option( $id, $option, $value ) { 
     362    $id = (int) $id; 
     363 
     364    if ( empty( $id ) ) 
     365        $id = get_current_blog_id(); 
     366 
     367    if ( $id == get_current_blog_id() ) 
     368        return add_option( $option, $value ); 
     369 
     370    switch_to_blog( $id ); 
     371    $return = add_option( $option, $value ); 
     372    restore_current_blog(); 
     373 
     374    return $return; 
     375} 
     376 
     377/** 
     378 * Removes option by name for a given blog id. Prevents removal of protected WordPress options. 
     379 * 
     380 * @since MU 
     381 * 
     382 * @param int $id A blog ID. Can be null to refer to the current blog. 
     383 * @param string $option Name of option to remove. Expected to not be SQL-escaped. 
     384 * @return bool True, if option is successfully deleted. False on failure. 
     385 */ 
     386function delete_blog_option( $id, $option ) { 
     387    $id = (int) $id; 
     388 
     389    if ( empty( $id ) ) 
     390        $id = get_current_blog_id(); 
     391 
     392    if ( $id == get_current_blog_id() ) 
     393        return delete_option( $option ); 
     394 
     395    switch_to_blog( $id ); 
     396    $return = delete_option( $option ); 
     397    restore_current_blog(); 
     398 
     399    return $return; 
     400} 
     401 
     402/** 
     403 * Update an option for a particular blog. 
    382404 * 
    383405 * @since MU 
    384406 * 
    385407 * @param int $id The blog id 
    386  * @param string $key The option key 
    387  * @param mixed $value The option value 
    388  * @return bool True on success, false on failure. 
    389  */ 
    390 function add_blog_option( $id, $key, $value ) { 
    391     $id = (int) $id; 
    392  
    393     switch_to_blog($id); 
    394     $return = add_option( $key, $value ); 
    395     restore_current_blog(); 
    396     if ( $return ) 
    397         wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' ); 
    398     return $return; 
    399 } 
    400  
    401 /** 
    402  * Delete an option for a particular blog. 
    403  * 
    404  * @since MU 
    405  * 
    406  * @param int $id The blog id 
    407  * @param string $key The option key 
    408  * @return bool True on success, false on failure. 
    409  */ 
    410 function delete_blog_option( $id, $key ) { 
    411     $id = (int) $id; 
    412  
    413     switch_to_blog($id); 
    414     $return = delete_option( $key ); 
    415     restore_current_blog(); 
    416     if ( $return ) 
    417         wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' ); 
    418     return $return; 
    419 } 
    420  
    421 /** 
    422  * Update an option for a particular blog. 
    423  * 
    424  * @since MU 
    425  * 
    426  * @param int $id The blog id 
    427  * @param string $key The option key 
     408 * @param string $option The option key 
    428409 * @param mixed $value The option value 
    429410 * @return bool True on success, false on failrue. 
    430411 */ 
    431 function update_blog_option( $id, $key, $value, $deprecated = null ) { 
     412function update_blog_option( $id, $option, $value, $deprecated = null ) { 
    432413    $id = (int) $id; 
    433414 
     
    435416        _deprecated_argument( __FUNCTION__, '3.1' ); 
    436417 
    437     switch_to_blog($id); 
    438     $return = update_option( $key, $value ); 
     418    if ( $id == get_current_blog_id() ) 
     419        return update_option( $option, $value ); 
     420 
     421    switch_to_blog( $id ); 
     422    $return = update_option( $option, $value ); 
    439423    restore_current_blog(); 
    440424 
    441425    refresh_blog_details( $id ); 
    442426 
    443     if ( $return ) 
    444         wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options'); 
    445427    return $return; 
    446428} 
Note: See TracChangeset for help on using the changeset viewer.