WordPress.org

Make WordPress Core

Ticket #21270: 21270.3.diff

File 21270.3.diff, 7.1 KB (added by ryan, 6 years ago)
  • wp-includes/ms-blogs.php

     
    307307}
    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
    313313 * will be false. This is useful to check whether you need to install an option
    314314 * and is commonly used during installation of plugin options and to test
    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.
     317 * If the option was serialized then it will be unserialized when it is returned.
    321318 *
    322319 * @since MU
    323  * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
    324320 *
    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.
     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;
     326function get_blog_option( $id, $option, $default = false ) {
     327        $id = (int) $id;
    332328
    333         if ( null === $blog_id )
    334                 $blog_id = $wpdb->blogid;
     329        if ( $id == get_current_blog_id() )
     330                return get_option( $option, $default );
    335331
    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' );
     332        switch_to_blog( $id );
     333        $option = get_option( $option, $default );
     334        restore_current_blog();
    373335
    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 );
     336        return $option;
    378337}
    379338
    380339/**
    381  * Add an option for a particular blog.
     340 * Add a new option for a given blog id.
    382341 *
     342 * You do not need to serialize values. If the value needs to be serialized, then
     343 * it will be serialized before it is inserted into the database. Remember,
     344 * resources can not be serialized or added as an option.
     345 *
     346 * You can create options without values and then update the values later.
     347 * Existing options will not be updated and checks are performed to ensure that you
     348 * aren't adding a protected WordPress option. Care should be taken to not name
     349 * options the same as the ones which are protected.
     350 *
    383351 * @since MU
    384352 *
    385  * @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.
     353 * @param int $id A blog ID. Can be null to refer to the current blog.
     354 * @param string $option Name of option to add. Expected to not be SQL-escaped.
     355 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
     356 * @return bool False if option was not added and true if option was added.
    389357 */
    390 function add_blog_option( $id, $key, $value ) {
     358function add_blog_option( $id, $option, $value ) {
    391359        $id = (int) $id;
    392360
    393         switch_to_blog($id);
     361        if ( $id == get_current_blog_id() )
     362                return add_option( $option, $value );
     363
     364        switch_to_blog( $id );
    394365        $return = add_option( $key, $value );
    395366        restore_current_blog();
    396         if ( $return )
    397                 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' );
     367
    398368        return $return;
    399369}
    400370
    401371/**
    402  * Delete an option for a particular blog.
     372 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
    403373 *
    404374 * @since MU
    405375 *
    406  * @param int $id The blog id
    407  * @param string $key The option key
    408  * @return bool True on success, false on failure.
     376 * @param int $id A blog ID. Can be null to refer to the current blog.
     377 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
     378 * @return bool True, if option is successfully deleted. False on failure.
    409379 */
    410 function delete_blog_option( $id, $key ) {
     380function delete_blog_option( $id, $option ) {
    411381        $id = (int) $id;
    412382
    413         switch_to_blog($id);
    414         $return = delete_option( $key );
     383        if ( $id == get_current_blog_id() )
     384                return delete_option( $option );
     385
     386        switch_to_blog( $id );
     387        $return = delete_option( $option );
    415388        restore_current_blog();
    416         if ( $return )
    417                 wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' );
     389
    418390        return $return;
    419391}
    420392
     
    424396 * @since MU
    425397 *
    426398 * @param int $id The blog id
    427  * @param string $key The option key
     399 * @param string $option The option key
    428400 * @param mixed $value The option value
    429401 * @return bool True on success, false on failrue.
    430402 */
    431 function update_blog_option( $id, $key, $value, $deprecated = null ) {
     403function update_blog_option( $id, $option, $value, $deprecated = null ) {
    432404        $id = (int) $id;
    433405
    434406        if ( null !== $deprecated  )
    435407                _deprecated_argument( __FUNCTION__, '3.1' );
    436408
    437         switch_to_blog($id);
    438         $return = update_option( $key, $value );
     409        if ( $id == get_current_blog_id() )
     410                return update_option( $option, $value );
     411
     412        switch_to_blog( $id );
     413        $return = update_option( $option, $value );
    439414        restore_current_blog();
    440415
    441416        refresh_blog_details( $id );
    442417
    443         if ( $return )
    444                 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options');
    445418        return $return;
    446419}
    447420