WordPress.org

Make WordPress Core

Ticket #21270: 21270.4.diff

File 21270.4.diff, 7.3 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 ( empty( $id ) )
     330                $id = get_current_blog_id();
    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        if ( $id == get_current_blog_id() )
     333                return get_option( $option, $default );
    373334
    374         if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
    375                 $value = untrailingslashit( $value );
     335        switch_to_blog( $id );
     336        $option = get_option( $option, $default );
     337        restore_current_blog();
    376338
    377         return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
     339        return $option;
    378340}
    379341
    380342/**
    381  * Add an option for a particular blog.
     343 * Add a new option for a given blog id.
    382344 *
     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 *
    383354 * @since MU
    384355 *
    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.
     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.
    389360 */
    390 function add_blog_option( $id, $key, $value ) {
     361function add_blog_option( $id, $option, $value ) {
    391362        $id = (int) $id;
    392363
    393         switch_to_blog($id);
    394         $return = add_option( $key, $value );
     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 );
    395372        restore_current_blog();
    396         if ( $return )
    397                 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' );
     373
    398374        return $return;
    399375}
    400376
    401377/**
    402  * Delete an option for a particular blog.
     378 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
    403379 *
    404380 * @since MU
    405381 *
    406  * @param int $id The blog id
    407  * @param string $key The option key
    408  * @return bool True on success, false on failure.
     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.
    409385 */
    410 function delete_blog_option( $id, $key ) {
     386function delete_blog_option( $id, $option ) {
    411387        $id = (int) $id;
    412388
    413         switch_to_blog($id);
    414         $return = delete_option( $key );
     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 );
    415397        restore_current_blog();
    416         if ( $return )
    417                 wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' );
     398
    418399        return $return;
    419400}
    420401
     
    424405 * @since MU
    425406 *
    426407 * @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
    434415        if ( null !== $deprecated  )
    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}
    447429