Make WordPress Core

Changeset 21414


Ignore:
Timestamp:
08/03/2012 05:51:42 PM (12 years ago)
Author:
ryan
Message:

Deprecate get_blog_option(), add_blog_option(), update_blog_option(), and delete_blog_option().

Use the regular option functions wrapped in switch_to_blog() and restore_current_blog() instead.

Group multiple operations within a single switch where possible.

fixes #21432

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-ms-sites-list-table.php

    r21323 r21414  
    231231                            <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
    232232                            <?php
    233                             if ( 'list' != $mode )
    234                                 echo '<p>' . sprintf( _x( '%1$s &#8211; <em>%2$s</em>', '%1$s: site name. %2$s: site tagline.' ), get_blog_option( $blog['blog_id'], 'blogname' ), get_blog_option( $blog['blog_id'], 'blogdescription ' ) ) . '</p>';
     233                            if ( 'list' != $mode ) {
     234                                switch_to_blog( $blog['blog_id'] );
     235                                echo '<p>' . sprintf( _x( '%1$s &#8211; <em>%2$s</em>', '%1$s: site name. %2$s: site tagline.' ), get_option( 'blogname' ), get_option( 'blogdescription ' ) ) . '</p>';
     236                                restore_current_blog();
     237                            }
    235238
    236239                            // Preordered.
  • trunk/wp-admin/includes/ms.php

    r21413 r21414  
    412412// Edit blog upload space setting on Edit Blog page
    413413function upload_space_setting( $id ) {
    414     $quota = get_blog_option( $id, 'blog_upload_space' );
     414    switch_to_blog( $id );
     415    $quota = get_option( 'blog_upload_space' );
     416    restore_current_blog();
     417
    415418    if ( !$quota )
    416419        $quota = '';
  • trunk/wp-admin/network/site-info.php

    r20713 r21414  
    136136            <?php if ( $is_main_site ) { ?>
    137137            <td><code><?php echo esc_attr( $details->path ) ?></code></td>
    138             <?php } else { ?>
     138            <?php
     139            } else {
     140                switch_to_blog( $id );
     141            ?>
    139142            <td><input name="blog[path]" type="text" id="path" value="<?php echo esc_attr( $details->path ) ?>" size="40" style='margin-bottom:5px;' />
    140             <br /><input type="checkbox" style="width:20px;" name="update_home_url" value="update" <?php if ( get_blog_option( $id, 'siteurl' ) == untrailingslashit( get_blogaddress_by_id ($id ) ) || get_blog_option( $id, 'home' ) == untrailingslashit( get_blogaddress_by_id( $id ) ) ) echo 'checked="checked"'; ?> /> <?php _e( 'Update <code>siteurl</code> and <code>home</code> as well.' ); ?></td>
    141             <?php } ?>
     143            <br /><input type="checkbox" style="width:20px;" name="update_home_url" value="update" <?php if ( get_option( 'siteurl' ) == untrailingslashit( get_blogaddress_by_id ($id ) ) || get_option( 'home' ) == untrailingslashit( get_blogaddress_by_id( $id ) ) ) echo 'checked="checked"'; ?> /> <?php _e( 'Update <code>siteurl</code> and <code>home</code> as well.' ); ?></td>
     144            <?php
     145                restore_current_blog();
     146            } ?>
    142147        </tr>
    143148        <tr class="form-field">
  • trunk/wp-admin/network/site-users.php

    r21413 r21414  
    4646
    4747$details = get_blog_details( $id );
    48 if ( !can_edit_network( $details->site_id ) )
     48if ( ! can_edit_network( $details->site_id ) )
    4949    wp_die( __( 'You do not have permission to access this page.' ) );
    5050
    5151$is_main_site = is_main_site( $id );
    5252
    53 // get blog prefix
    54 $blog_prefix = $wpdb->get_blog_prefix( $id );
    55 
    56 // @todo This is a hack. Eventually, add API to WP_Roles allowing retrieval of roles for a particular blog.
    57 if ( ! empty($wp_roles->use_db) ) {
    58     $editblog_roles = get_blog_option( $id, "{$blog_prefix}user_roles" );
    59 } else {
    60     // Roles are stored in memory, not the DB.
    61     $editblog_roles = $wp_roles->roles;
    62 }
    63 $default_role = get_blog_option( $id, 'default_role' );
     53switch_to_blog( $id );
     54
     55$editblog_roles = $wp_roles->roles;
     56
     57$default_role = get_option( 'default_role' );
    6458
    6559$action = $wp_list_table->current_action();
    6660
    6761if ( $action ) {
    68     switch_to_blog( $id );
    6962
    7063    switch ( $action ) {
     
    7265            check_admin_referer( 'add-user', '_wpnonce_add-new-user' );
    7366            $user = $_POST['user'];
    74             if ( !is_array( $_POST['user'] ) || empty( $user['username'] ) || empty( $user['email'] ) ) {
     67            if ( ! is_array( $_POST['user'] ) || empty( $user['username'] ) || empty( $user['email'] ) ) {
    7568                $update = 'err_new';
    7669            } else {
     
    9588                $userid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->users . " WHERE user_login = %s", $newuser ) );
    9689                if ( $userid ) {
     90                    $blog_prefix = $wpdb->get_blog_prefix( $id );
    9791                    $user = $wpdb->get_var( "SELECT user_id FROM " . $wpdb->usermeta . " WHERE user_id='$userid' AND meta_key='{$blog_prefix}capabilities'" );
    9892                    if ( $user == false )
     
    109103
    110104        case 'remove':
    111             if ( !current_user_can('remove_users')  )
     105            if ( ! current_user_can( 'remove_users' )  )
    112106                die(__('You can&#8217;t remove users.'));
    113107            check_admin_referer( 'bulk-users' );
     
    153147    }
    154148
    155     restore_current_blog();
    156149    wp_safe_redirect( add_query_arg( 'update', $update, $referer ) );
    157150    exit();
    158151}
     152
     153restore_current_blog();
    159154
    160155if ( isset( $_GET['action'] ) && 'update-site' == $_GET['action'] ) {
  • trunk/wp-admin/network/upgrade.php

    r19569 r21414  
    6161        echo "<ul>";
    6262        foreach ( (array) $blogs as $details ) {
    63             $siteurl = get_blog_option( $details['blog_id'], 'siteurl' );
     63            switch_to_blog( $details['blog_id'] );
     64            $siteurl = site_url();
     65            $upgrade_url = admin_url( 'upgrade.php?step=upgrade_db' );
     66            restore_current_blog();
    6467            echo "<li>$siteurl</li>";
    65             $response = wp_remote_get( trailingslashit( $siteurl ) . "wp-admin/upgrade.php?step=upgrade_db", array( 'timeout' => 120, 'httpversion' => '1.1' ) );
     68            $response = wp_remote_get( $upgrade_url, array( 'timeout' => 120, 'httpversion' => '1.1' ) );
    6669            if ( is_wp_error( $response ) )
    6770                wp_die( sprintf( __( 'Warning! Problem updating %1$s. Your server may not be able to connect to sites running on it. Error message: <em>%2$s</em>' ), $siteurl, $response->get_error_message() ) );
  • trunk/wp-includes/class-wp-theme.php

    r21117 r21414  
    11101110        $current = $blog_id == get_current_blog_id();
    11111111
    1112         if ( $current )
     1112        if ( $current ) {
    11131113            $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
    1114         else
    1115             $allowed_themes[ $blog_id ] = get_blog_option( $blog_id, 'allowedthemes' );
     1114        } else {
     1115            switch_to_blog( $blog_id );
     1116            $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
     1117            restore_current_blog();
     1118        }
    11161119
    11171120        // This is all super old MU back compat joy.
    11181121        // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
    11191122        if ( false === $allowed_themes[ $blog_id ] ) {
    1120             if ( $current )
     1123            if ( $current ) {
    11211124                $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
    1122             else
    1123                 $allowed_themes[ $blog_id ] = get_blog_option( $blog_id, 'allowed_themes' );
     1125            } else {
     1126                switch_to_blog( $blog_id );
     1127                $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
     1128                restore_current_blog();
     1129            }
    11241130
    11251131            if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
     
    11401146                    delete_option( 'allowed_themes' );
    11411147                } else {
    1142                     update_blog_option( $blog_id, 'allowedthemes', $allowed_themes[ $blog_id ] );
    1143                     delete_blog_option( $blog_id, 'allowed_themes' );
     1148                    switch_to_blog( $blog_id );
     1149                    update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
     1150                    delete_option( 'allowed_themes' );
     1151                    restore_current_blog();
    11441152                }
    11451153            }
  • trunk/wp-includes/class-wp-xmlrpc-server.php

    r21194 r21414  
    476476            $is_admin = current_user_can_for_blog( $blog_id, 'manage_options' );
    477477
     478            switch_to_blog( $blog_id );
    478479            $struct[] = array(
    479480                'isAdmin'       => $is_admin,
    480                 'url'           => get_home_url( $blog_id, '/' ),
     481                'url'           => home_url( '/' ),
    481482                'blogid'        => (string) $blog_id,
    482                 'blogName'      => get_blog_option( $blog_id, 'blogname' ),
    483                 'xmlrpc'        => get_site_url( $blog_id, 'xmlrpc.php' )
     483                'blogName'      => get_option( 'blogname' ),
     484                'xmlrpc'        => site_url( 'xmlrpc.php' )
    484485            );
     486            restore_current_blog();
    485487        }
    486488
  • trunk/wp-includes/link-template.php

    r21413 r21414  
    18971897        $scheme = is_ssl() && !is_admin() ? 'https' : 'http';
    18981898
    1899     if ( empty( $blog_id ) || !is_multisite() )
     1899    if ( empty( $blog_id ) || !is_multisite() ) {
    19001900        $url = get_option( 'home' );
    1901     else
    1902         $url = get_blog_option( $blog_id, 'home' );
     1901    } else {
     1902        switch_to_blog( $blog_id );
     1903        $url = get_option( 'home' );
     1904        restore_current_blog();
     1905    }
    19031906
    19041907    if ( 'relative' == $scheme )
     
    19621965    }
    19631966
    1964     if ( empty( $blog_id ) || !is_multisite() )
     1967    if ( empty( $blog_id ) || !is_multisite() ) {
    19651968        $url = get_option( 'siteurl' );
    1966     else
    1967         $url = get_blog_option( $blog_id, 'siteurl' );
     1969    } else {
     1970        switch_to_blog( $blog_id );
     1971        $url = get_option( 'siteurl' );
     1972        restore_current_blog();
     1973    }
    19681974
    19691975    if ( 'relative' == $scheme )
  • trunk/wp-includes/ms-blogs.php

    r21403 r21414  
    223223    }
    224224
    225     $details->blogname      = get_blog_option( $blog_id, 'blogname' );
    226     $details->siteurl       = get_blog_option( $blog_id, 'siteurl' );
    227     $details->post_count    = get_blog_option( $blog_id, 'post_count' );
     225    switch_to_blog( $blog_id );
     226    $details->blogname      = get_option( 'blogname' );
     227    $details->siteurl       = get_option( 'siteurl' );
     228    $details->post_count    = get_option( 'post_count' );
     229    restore_current_blog();
    228230
    229231    $details = apply_filters( 'blog_details', $details );
     
    299301    }
    300302
    301     if ( isset($details[ 'public' ]) )
    302         update_blog_option( $blog_id, 'blog_public', $details[ 'public' ] );
     303    if ( isset($details[ 'public' ]) ) {
     304        switch_to_blog( $blog_id );
     305        update_option( 'blog_public', $details[ 'public' ] );
     306        restore_current_blog();
     307    }
    303308
    304309    refresh_blog_details($blog_id);
    305310
    306311    return true;
    307 }
    308 
    309 /**
    310  * Retrieve option value for a given blog id based on name of option.
    311  *
    312  * If the option does not exist or does not have a value, then the return value
    313  * will be false. This is useful to check whether you need to install an option
    314  * and is commonly used during installation of plugin options and to test
    315  * whether upgrading is required.
    316  *
    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.
    324  * @return mixed Value set for the option.
    325  */
    326 function 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 ( get_current_blog_id() == $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  */
    361 function add_blog_option( $id, $option, $value ) {
    362     $id = (int) $id;
    363 
    364     if ( empty( $id ) )
    365         $id = get_current_blog_id();
    366 
    367     if ( get_current_blog_id() == $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  */
    386 function delete_blog_option( $id, $option ) {
    387     $id = (int) $id;
    388 
    389     if ( empty( $id ) )
    390         $id = get_current_blog_id();
    391 
    392     if ( get_current_blog_id() == $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.
    404  *
    405  * @since MU
    406  *
    407  * @param int $id The blog id
    408  * @param string $option The option key
    409  * @param mixed $value The option value
    410  * @return bool True on success, false on failrue.
    411  */
    412 function update_blog_option( $id, $option, $value, $deprecated = null ) {
    413     $id = (int) $id;
    414 
    415     if ( null !== $deprecated  )
    416         _deprecated_argument( __FUNCTION__, '3.1' );
    417 
    418     if ( get_current_blog_id() == $id )
    419         return update_option( $option, $value );
    420 
    421     switch_to_blog( $id );
    422     $return = update_option( $option, $value );
    423     restore_current_blog();
    424 
    425     refresh_blog_details( $id );
    426 
    427     return $return;
    428312}
    429313
  • trunk/wp-includes/ms-deprecated.php

    r20378 r21414  
    271271    return $url;
    272272}
     273
     274/**
     275 * Retrieve option value for a given blog id based on name of option.
     276 *
     277 * If the option does not exist or does not have a value, then the return value
     278 * will be false. This is useful to check whether you need to install an option
     279 * and is commonly used during installation of plugin options and to test
     280 * whether upgrading is required.
     281 *
     282 * If the option was serialized then it will be unserialized when it is returned.
     283 *
     284 * @since MU
     285 * @deprecated 3.5.0
     286 *
     287 * @param int $id A blog ID. Can be null to refer to the current blog.
     288 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
     289 * @param mixed $default Optional. Default value to return if the option does not exist.
     290 * @return mixed Value set for the option.
     291 */
     292function get_blog_option( $id, $option, $default = false ) {
     293    _deprecated_function( __FUNCTION__, '3.5' );
     294
     295    $id = (int) $id;
     296
     297    if ( empty( $id ) )
     298        $id = get_current_blog_id();
     299
     300    if ( get_current_blog_id() == $id )
     301        return get_option( $option, $default );
     302
     303    switch_to_blog( $id );
     304    $option = get_option( $option, $default );
     305    restore_current_blog();
     306
     307    return $option;
     308}
     309
     310/**
     311 * Add a new option for a given blog id.
     312 *
     313 * You do not need to serialize values. If the value needs to be serialized, then
     314 * it will be serialized before it is inserted into the database. Remember,
     315 * resources can not be serialized or added as an option.
     316 *
     317 * You can create options without values and then update the values later.
     318 * Existing options will not be updated and checks are performed to ensure that you
     319 * aren't adding a protected WordPress option. Care should be taken to not name
     320 * options the same as the ones which are protected.
     321 *
     322 * @since MU
     323 * @deprecated 3.5.0
     324 *
     325 * @param int $id A blog ID. Can be null to refer to the current blog.
     326 * @param string $option Name of option to add. Expected to not be SQL-escaped.
     327 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
     328 * @return bool False if option was not added and true if option was added.
     329 */
     330function add_blog_option( $id, $option, $value ) {
     331    _deprecated_function( __FUNCTION__, '3.5' );
     332
     333    $id = (int) $id;
     334
     335    if ( empty( $id ) )
     336        $id = get_current_blog_id();
     337
     338    if ( get_current_blog_id() == $id )
     339        return add_option( $option, $value );
     340
     341    switch_to_blog( $id );
     342    $return = add_option( $option, $value );
     343    restore_current_blog();
     344
     345    return $return;
     346}
     347
     348/**
     349 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
     350 *
     351 * @since MU
     352 * @deprecated 3.5.0
     353 *
     354 * @param int $id A blog ID. Can be null to refer to the current blog.
     355 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
     356 * @return bool True, if option is successfully deleted. False on failure.
     357 */
     358function delete_blog_option( $id, $option ) {
     359    _deprecated_function( __FUNCTION__, '3.5' );
     360
     361    $id = (int) $id;
     362
     363    if ( empty( $id ) )
     364        $id = get_current_blog_id();
     365
     366    if ( get_current_blog_id() == $id )
     367        return delete_option( $option );
     368
     369    switch_to_blog( $id );
     370    $return = delete_option( $option );
     371    restore_current_blog();
     372
     373    return $return;
     374}
     375
     376/**
     377 * Update an option for a particular blog.
     378 *
     379 * @since MU
     380 * @deprecated 3.5.0
     381 *
     382 * @param int $id The blog id
     383 * @param string $option The option key
     384 * @param mixed $value The option value
     385 * @return bool True on success, false on failrue.
     386 */
     387function update_blog_option( $id, $option, $value, $deprecated = null ) {
     388    _deprecated_function( __FUNCTION__, '3.5' );
     389
     390    $id = (int) $id;
     391
     392    if ( null !== $deprecated  )
     393        _deprecated_argument( __FUNCTION__, '3.1' );
     394
     395    if ( get_current_blog_id() == $id )
     396        return update_option( $option, $value );
     397
     398    switch_to_blog( $id );
     399    $return = update_option( $option, $value );
     400    restore_current_blog();
     401
     402    refresh_blog_details( $id );
     403
     404    return $return;
     405}
Note: See TracChangeset for help on using the changeset viewer.