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


git-svn-id: http://core.svn.wordpress.org/trunk@21414 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2012-08-03 17:51:42 +00:00
parent 7127ed1197
commit 5ca54e7d2f
10 changed files with 208 additions and 166 deletions

View File

@ -230,8 +230,11 @@ class WP_MS_Sites_List_Table extends WP_List_Table {
echo "<td class='column-$column_name $column_name'$style>"; ?>
<a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
<?php
if ( 'list' != $mode )
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>';
if ( 'list' != $mode ) {
switch_to_blog( $blog['blog_id'] );
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>';
restore_current_blog();
}
// Preordered.
$actions = array(

View File

@ -411,7 +411,10 @@ function fix_import_form_size( $size ) {
// Edit blog upload space setting on Edit Blog page
function upload_space_setting( $id ) {
$quota = get_blog_option( $id, 'blog_upload_space' );
switch_to_blog( $id );
$quota = get_option( 'blog_upload_space' );
restore_current_blog();
if ( !$quota )
$quota = '';

View File

@ -135,10 +135,15 @@ if ( ! empty( $messages ) ) {
<th scope="row"><?php _e( 'Path' ) ?></th>
<?php if ( $is_main_site ) { ?>
<td><code><?php echo esc_attr( $details->path ) ?></code></td>
<?php } else { ?>
<?php
} else {
switch_to_blog( $id );
?>
<td><input name="blog[path]" type="text" id="path" value="<?php echo esc_attr( $details->path ) ?>" size="40" style='margin-bottom:5px;' />
<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>
<?php } ?>
<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>
<?php
restore_current_blog();
} ?>
</tr>
<tr class="form-field">
<th scope="row"><?php _ex( 'Registered', 'site' ) ?></th>

View File

@ -45,33 +45,26 @@ if ( ! $id )
wp_die( __('Invalid site ID.') );
$details = get_blog_details( $id );
if ( !can_edit_network( $details->site_id ) )
if ( ! can_edit_network( $details->site_id ) )
wp_die( __( 'You do not have permission to access this page.' ) );
$is_main_site = is_main_site( $id );
// get blog prefix
$blog_prefix = $wpdb->get_blog_prefix( $id );
switch_to_blog( $id );
// @todo This is a hack. Eventually, add API to WP_Roles allowing retrieval of roles for a particular blog.
if ( ! empty($wp_roles->use_db) ) {
$editblog_roles = get_blog_option( $id, "{$blog_prefix}user_roles" );
} else {
// Roles are stored in memory, not the DB.
$editblog_roles = $wp_roles->roles;
}
$default_role = get_blog_option( $id, 'default_role' );
$editblog_roles = $wp_roles->roles;
$default_role = get_option( 'default_role' );
$action = $wp_list_table->current_action();
if ( $action ) {
switch_to_blog( $id );
switch ( $action ) {
case 'newuser':
check_admin_referer( 'add-user', '_wpnonce_add-new-user' );
$user = $_POST['user'];
if ( !is_array( $_POST['user'] ) || empty( $user['username'] ) || empty( $user['email'] ) ) {
if ( ! is_array( $_POST['user'] ) || empty( $user['username'] ) || empty( $user['email'] ) ) {
$update = 'err_new';
} else {
$password = wp_generate_password( 12, false);
@ -94,6 +87,7 @@ if ( $action ) {
$newuser = $_POST['newuser'];
$userid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->users . " WHERE user_login = %s", $newuser ) );
if ( $userid ) {
$blog_prefix = $wpdb->get_blog_prefix( $id );
$user = $wpdb->get_var( "SELECT user_id FROM " . $wpdb->usermeta . " WHERE user_id='$userid' AND meta_key='{$blog_prefix}capabilities'" );
if ( $user == false )
add_user_to_blog( $id, $userid, $_POST['new_role'] );
@ -108,7 +102,7 @@ if ( $action ) {
break;
case 'remove':
if ( !current_user_can('remove_users') )
if ( ! current_user_can( 'remove_users' ) )
die(__('You can&#8217;t remove users.'));
check_admin_referer( 'bulk-users' );
@ -152,11 +146,12 @@ if ( $action ) {
break;
}
restore_current_blog();
wp_safe_redirect( add_query_arg( 'update', $update, $referer ) );
exit();
}
restore_current_blog();
if ( isset( $_GET['action'] ) && 'update-site' == $_GET['action'] ) {
wp_safe_redirect( $referer );
exit();

View File

@ -60,9 +60,12 @@ switch ( $action ) {
}
echo "<ul>";
foreach ( (array) $blogs as $details ) {
$siteurl = get_blog_option( $details['blog_id'], 'siteurl' );
switch_to_blog( $details['blog_id'] );
$siteurl = site_url();
$upgrade_url = admin_url( 'upgrade.php?step=upgrade_db' );
restore_current_blog();
echo "<li>$siteurl</li>";
$response = wp_remote_get( trailingslashit( $siteurl ) . "wp-admin/upgrade.php?step=upgrade_db", array( 'timeout' => 120, 'httpversion' => '1.1' ) );
$response = wp_remote_get( $upgrade_url, array( 'timeout' => 120, 'httpversion' => '1.1' ) );
if ( is_wp_error( $response ) )
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() ) );
do_action( 'after_mu_upgrade', $response );

View File

@ -1109,18 +1109,24 @@ final class WP_Theme implements ArrayAccess {
$current = $blog_id == get_current_blog_id();
if ( $current )
if ( $current ) {
$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
else
$allowed_themes[ $blog_id ] = get_blog_option( $blog_id, 'allowedthemes' );
} else {
switch_to_blog( $blog_id );
$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
restore_current_blog();
}
// This is all super old MU back compat joy.
// 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
if ( false === $allowed_themes[ $blog_id ] ) {
if ( $current )
if ( $current ) {
$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
else
$allowed_themes[ $blog_id ] = get_blog_option( $blog_id, 'allowed_themes' );
} else {
switch_to_blog( $blog_id );
$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
restore_current_blog();
}
if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
$allowed_themes[ $blog_id ] = array();
@ -1139,8 +1145,10 @@ final class WP_Theme implements ArrayAccess {
update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
delete_option( 'allowed_themes' );
} else {
update_blog_option( $blog_id, 'allowedthemes', $allowed_themes[ $blog_id ] );
delete_blog_option( $blog_id, 'allowed_themes' );
switch_to_blog( $blog_id );
update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
delete_option( 'allowed_themes' );
restore_current_blog();
}
}
}

View File

@ -475,13 +475,15 @@ class wp_xmlrpc_server extends IXR_Server {
$blog_id = $blog->userblog_id;
$is_admin = current_user_can_for_blog( $blog_id, 'manage_options' );
switch_to_blog( $blog_id );
$struct[] = array(
'isAdmin' => $is_admin,
'url' => get_home_url( $blog_id, '/' ),
'url' => home_url( '/' ),
'blogid' => (string) $blog_id,
'blogName' => get_blog_option( $blog_id, 'blogname' ),
'xmlrpc' => get_site_url( $blog_id, 'xmlrpc.php' )
'blogName' => get_option( 'blogname' ),
'xmlrpc' => site_url( 'xmlrpc.php' )
);
restore_current_blog();
}
return $struct;

View File

@ -1896,10 +1896,13 @@ function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
$scheme = is_ssl() && !is_admin() ? 'https' : 'http';
if ( empty( $blog_id ) || !is_multisite() )
if ( empty( $blog_id ) || !is_multisite() ) {
$url = get_option( 'home' );
else
$url = get_blog_option( $blog_id, 'home' );
} else {
switch_to_blog( $blog_id );
$url = get_option( 'home' );
restore_current_blog();
}
if ( 'relative' == $scheme )
$url = preg_replace( '#^.+://[^/]*#', '', $url );
@ -1961,10 +1964,13 @@ function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
$scheme = ( is_ssl() ? 'https' : 'http' );
}
if ( empty( $blog_id ) || !is_multisite() )
if ( empty( $blog_id ) || !is_multisite() ) {
$url = get_option( 'siteurl' );
else
$url = get_blog_option( $blog_id, 'siteurl' );
} else {
switch_to_blog( $blog_id );
$url = get_option( 'siteurl' );
restore_current_blog();
}
if ( 'relative' == $scheme )
$url = preg_replace( '#^.+://[^/]*#', '', $url );

View File

@ -222,9 +222,11 @@ function get_blog_details( $fields, $get_all = true ) {
return $details;
}
$details->blogname = get_blog_option( $blog_id, 'blogname' );
$details->siteurl = get_blog_option( $blog_id, 'siteurl' );
$details->post_count = get_blog_option( $blog_id, 'post_count' );
switch_to_blog( $blog_id );
$details->blogname = get_option( 'blogname' );
$details->siteurl = get_option( 'siteurl' );
$details->post_count = get_option( 'post_count' );
restore_current_blog();
$details = apply_filters( 'blog_details', $details );
@ -298,135 +300,17 @@ function update_blog_details( $blog_id, $details = array() ) {
do_action( "make_ham_blog", $blog_id );
}
if ( isset($details[ 'public' ]) )
update_blog_option( $blog_id, 'blog_public', $details[ 'public' ] );
if ( isset($details[ 'public' ]) ) {
switch_to_blog( $blog_id );
update_option( 'blog_public', $details[ 'public' ] );
restore_current_blog();
}
refresh_blog_details($blog_id);
return true;
}
/**
* Retrieve option value for a given blog id based on name of option.
*
* If the option does not exist or does not have a value, then the return value
* will be false. This is useful to check whether you need to install an option
* and is commonly used during installation of plugin options and to test
* whether upgrading is required.
*
* If the option was serialized then it will be unserialized when it is returned.
*
* @since MU
*
* @param int $id A blog ID. Can be null to refer to the current blog.
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
* @param mixed $default Optional. Default value to return if the option does not exist.
* @return mixed Value set for the option.
*/
function get_blog_option( $id, $option, $default = false ) {
$id = (int) $id;
if ( empty( $id ) )
$id = get_current_blog_id();
if ( get_current_blog_id() == $id )
return get_option( $option, $default );
switch_to_blog( $id );
$option = get_option( $option, $default );
restore_current_blog();
return $option;
}
/**
* Add a new option for a given blog id.
*
* You do not need to serialize values. If the value needs to be serialized, then
* it will be serialized before it is inserted into the database. Remember,
* resources can not be serialized or added as an option.
*
* You can create options without values and then update the values later.
* Existing options will not be updated and checks are performed to ensure that you
* aren't adding a protected WordPress option. Care should be taken to not name
* options the same as the ones which are protected.
*
* @since MU
*
* @param int $id A blog ID. Can be null to refer to the current blog.
* @param string $option Name of option to add. Expected to not be SQL-escaped.
* @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
* @return bool False if option was not added and true if option was added.
*/
function add_blog_option( $id, $option, $value ) {
$id = (int) $id;
if ( empty( $id ) )
$id = get_current_blog_id();
if ( get_current_blog_id() == $id )
return add_option( $option, $value );
switch_to_blog( $id );
$return = add_option( $option, $value );
restore_current_blog();
return $return;
}
/**
* Removes option by name for a given blog id. Prevents removal of protected WordPress options.
*
* @since MU
*
* @param int $id A blog ID. Can be null to refer to the current blog.
* @param string $option Name of option to remove. Expected to not be SQL-escaped.
* @return bool True, if option is successfully deleted. False on failure.
*/
function delete_blog_option( $id, $option ) {
$id = (int) $id;
if ( empty( $id ) )
$id = get_current_blog_id();
if ( get_current_blog_id() == $id )
return delete_option( $option );
switch_to_blog( $id );
$return = delete_option( $option );
restore_current_blog();
return $return;
}
/**
* Update an option for a particular blog.
*
* @since MU
*
* @param int $id The blog id
* @param string $option The option key
* @param mixed $value The option value
* @return bool True on success, false on failrue.
*/
function update_blog_option( $id, $option, $value, $deprecated = null ) {
$id = (int) $id;
if ( null !== $deprecated )
_deprecated_argument( __FUNCTION__, '3.1' );
if ( get_current_blog_id() == $id )
return update_option( $option, $value );
switch_to_blog( $id );
$return = update_option( $option, $value );
restore_current_blog();
refresh_blog_details( $id );
return $return;
}
/**
* Switch the current blog.
*

View File

@ -270,3 +270,136 @@ function wpmu_admin_redirect_add_updated_param( $url = '' ) {
}
return $url;
}
/**
* Retrieve option value for a given blog id based on name of option.
*
* If the option does not exist or does not have a value, then the return value
* will be false. This is useful to check whether you need to install an option
* and is commonly used during installation of plugin options and to test
* whether upgrading is required.
*
* If the option was serialized then it will be unserialized when it is returned.
*
* @since MU
* @deprecated 3.5.0
*
* @param int $id A blog ID. Can be null to refer to the current blog.
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
* @param mixed $default Optional. Default value to return if the option does not exist.
* @return mixed Value set for the option.
*/
function get_blog_option( $id, $option, $default = false ) {
_deprecated_function( __FUNCTION__, '3.5' );
$id = (int) $id;
if ( empty( $id ) )
$id = get_current_blog_id();
if ( get_current_blog_id() == $id )
return get_option( $option, $default );
switch_to_blog( $id );
$option = get_option( $option, $default );
restore_current_blog();
return $option;
}
/**
* Add a new option for a given blog id.
*
* You do not need to serialize values. If the value needs to be serialized, then
* it will be serialized before it is inserted into the database. Remember,
* resources can not be serialized or added as an option.
*
* You can create options without values and then update the values later.
* Existing options will not be updated and checks are performed to ensure that you
* aren't adding a protected WordPress option. Care should be taken to not name
* options the same as the ones which are protected.
*
* @since MU
* @deprecated 3.5.0
*
* @param int $id A blog ID. Can be null to refer to the current blog.
* @param string $option Name of option to add. Expected to not be SQL-escaped.
* @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
* @return bool False if option was not added and true if option was added.
*/
function add_blog_option( $id, $option, $value ) {
_deprecated_function( __FUNCTION__, '3.5' );
$id = (int) $id;
if ( empty( $id ) )
$id = get_current_blog_id();
if ( get_current_blog_id() == $id )
return add_option( $option, $value );
switch_to_blog( $id );
$return = add_option( $option, $value );
restore_current_blog();
return $return;
}
/**
* Removes option by name for a given blog id. Prevents removal of protected WordPress options.
*
* @since MU
* @deprecated 3.5.0
*
* @param int $id A blog ID. Can be null to refer to the current blog.
* @param string $option Name of option to remove. Expected to not be SQL-escaped.
* @return bool True, if option is successfully deleted. False on failure.
*/
function delete_blog_option( $id, $option ) {
_deprecated_function( __FUNCTION__, '3.5' );
$id = (int) $id;
if ( empty( $id ) )
$id = get_current_blog_id();
if ( get_current_blog_id() == $id )
return delete_option( $option );
switch_to_blog( $id );
$return = delete_option( $option );
restore_current_blog();
return $return;
}
/**
* Update an option for a particular blog.
*
* @since MU
* @deprecated 3.5.0
*
* @param int $id The blog id
* @param string $option The option key
* @param mixed $value The option value
* @return bool True on success, false on failrue.
*/
function update_blog_option( $id, $option, $value, $deprecated = null ) {
_deprecated_function( __FUNCTION__, '3.5' );
$id = (int) $id;
if ( null !== $deprecated )
_deprecated_argument( __FUNCTION__, '3.1' );
if ( get_current_blog_id() == $id )
return update_option( $option, $value );
switch_to_blog( $id );
$return = update_option( $option, $value );
restore_current_blog();
refresh_blog_details( $id );
return $return;
}