Ticket #21270: 21270.4.diff
File 21270.4.diff, 7.3 KB (added by , 13 years ago) |
---|
-
wp-includes/ms-blogs.php
307 307 } 308 308 309 309 /** 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. 311 311 * 312 312 * If the option does not exist or does not have a value, then the return value 313 313 * will be false. This is useful to check whether you need to install an option 314 314 * and is commonly used during installation of plugin options and to test 315 315 * whether upgrading is required. 316 316 * 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. 321 318 * 322 319 * @since MU 323 * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.324 320 * 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 alreadybe 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. 328 324 * @return mixed Value set for the option. 329 325 */ 330 function get_blog_option( $ blog_id, $setting, $default = false ) {331 global $wpdb;326 function get_blog_option( $id, $option, $default = false ) { 327 $id = (int) $id; 332 328 333 if ( null === $blog_id)334 $ blog_id = $wpdb->blogid;329 if ( empty( $id ) ) 330 $id = get_current_blog_id(); 335 331 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 ); 373 334 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(); 376 338 377 return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );339 return $option; 378 340 } 379 341 380 342 /** 381 * Add a n option for a particular blog.343 * Add a new option for a given blog id. 382 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 * 383 354 * @since MU 384 355 * 385 * @param int $id The blog id386 * @param string $ key The option key387 * @param mixed $value The option value388 * @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. 389 360 */ 390 function add_blog_option( $id, $ key, $value ) {361 function add_blog_option( $id, $option, $value ) { 391 362 $id = (int) $id; 392 363 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 ); 395 372 restore_current_blog(); 396 if ( $return ) 397 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' ); 373 398 374 return $return; 399 375 } 400 376 401 377 /** 402 * Delete an option for a particular blog.378 * Removes option by name for a given blog id. Prevents removal of protected WordPress options. 403 379 * 404 380 * @since MU 405 381 * 406 * @param int $id The blog id407 * @param string $ key The option key408 * @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. 409 385 */ 410 function delete_blog_option( $id, $ key) {386 function delete_blog_option( $id, $option ) { 411 387 $id = (int) $id; 412 388 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 ); 415 397 restore_current_blog(); 416 if ( $return ) 417 wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' ); 398 418 399 return $return; 419 400 } 420 401 … … 424 405 * @since MU 425 406 * 426 407 * @param int $id The blog id 427 * @param string $ keyThe option key408 * @param string $option The option key 428 409 * @param mixed $value The option value 429 410 * @return bool True on success, false on failrue. 430 411 */ 431 function update_blog_option( $id, $ key, $value, $deprecated = null ) {412 function update_blog_option( $id, $option, $value, $deprecated = null ) { 432 413 $id = (int) $id; 433 414 434 415 if ( null !== $deprecated ) 435 416 _deprecated_argument( __FUNCTION__, '3.1' ); 436 417 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 ); 439 423 restore_current_blog(); 440 424 441 425 refresh_blog_details( $id ); 442 426 443 if ( $return )444 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options');445 427 return $return; 446 428 } 447 429