Ticket #10788: 10788.2.diff
| File 10788.2.diff, 36.1 KB (added by , 17 years ago) |
|---|
-
wp-includes/functions.php
289 289 } 290 290 291 291 /** 292 * Retrieve option value based on settingname.292 * Retrieve option value based on option name. 293 293 * 294 294 * If the option does not exist or does not have a value, then the return value 295 295 * will be false. This is useful to check whether you need to install an option … … 302 302 * name. You should not try to override special options, but you will not be 303 303 * prevented from doing so. 304 304 * 305 * There is a second filter called 'option_$option' with the $option being306 * replaced with the option name. This gives the value as the only parameter.307 *308 305 * If the option was serialized, when the option was added and, or updated, then 309 306 * it will be unserialized, when it is returned. 310 307 * 311 308 * @since 1.5.0 312 309 * @package WordPress 313 310 * @subpackage Option 314 * @uses apply_filters() Calls 'pre_option_$optionname' false to allow 315 * overwriting the option value in a plugin. 316 * @uses apply_filters() Calls 'option_$optionname' with the option name value. 311 * @uses apply_filters() Calls 'pre_option_$option' to allow overwriting 312 * the option value in a plugin before the option value is retrieved. 313 * @uses apply_filters() Calls 'option_$option' to allow overwriting the option value 314 * in a plugin after the option value is retrieved. 317 315 * 318 * @param string $setting Name of option to retrieve. Should already be SQL-escaped 316 * @param string $option Name of option to retrieve. Should already be SQL-escaped 317 * @param mixed $default Optional value to return if option doesn't exist 319 318 * @return mixed Value set for the option. 320 319 */ 321 function get_option( $ setting, $default = false ) {320 function get_option( $option, $default = false ) { 322 321 global $wpdb; 323 322 324 323 // Allow plugins to short-circuit options. 325 $pre = apply_filters( 'pre_option_' . $setting, false );324 $pre = apply_filters( "pre_option_{$option}", false ); 326 325 if ( false !== $pre ) 327 326 return $pre; 328 327 … … 331 330 $notoptions = array(); 332 331 } else { 333 332 $notoptions = wp_cache_get( 'notoptions', 'options' ); 334 if ( isset( $notoptions[ $setting] ) )333 if ( isset( $notoptions[ $option ] ) ) 335 334 return $default; 336 335 } 337 336 338 337 $alloptions = wp_load_alloptions(); 339 338 340 if ( isset( $alloptions[ $setting] ) ) {341 $value = $alloptions[ $setting];339 if ( isset( $alloptions[ $option ] ) ) { 340 $value = $alloptions[ $option ]; 342 341 } else { 343 $value = wp_cache_get( $ setting, 'options' );342 $value = wp_cache_get( $option, 'options' ); 344 343 345 344 if ( false === $value ) { 346 345 if ( defined( 'WP_INSTALLING' ) ) 347 346 $suppress = $wpdb->suppress_errors(); 348 // expected_slashed ($ setting)349 $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$ setting' LIMIT 1" );347 // expected_slashed ($option) 348 $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$option' LIMIT 1" ); 350 349 if ( defined( 'WP_INSTALLING' ) ) 351 $wpdb->suppress_errors( $suppress);350 $wpdb->suppress_errors( $suppress ); 352 351 353 if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 352 // Has to be get_row instead of get_var because of funkiness with 0, false, null values 353 if ( is_object( $row ) ) { 354 354 $value = $row->option_value; 355 wp_cache_add( $ setting, $value, 'options' );355 wp_cache_add( $option, $value, 'options' ); 356 356 } else { // option does not exist, so we must cache its non-existence 357 $notoptions[$ setting] = true;357 $notoptions[$option] = true; 358 358 wp_cache_set( 'notoptions', $notoptions, 'options' ); 359 359 return $default; 360 360 } … … 362 362 } 363 363 364 364 // If home is not set use siteurl. 365 if ( 'home' == $ setting&& '' == $value )365 if ( 'home' == $option && '' == $value ) 366 366 return get_option( 'siteurl' ); 367 367 368 if ( in_array( $ setting, array('siteurl', 'home', 'category_base', 'tag_base') ) )368 if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) 369 369 $value = untrailingslashit( $value ); 370 370 371 return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) );371 return apply_filters( "option_{$option}", maybe_unserialize( $value ) ); 372 372 } 373 373 374 374 /** … … 446 446 * value, but you will not be able to set whether it is autoloaded. If you want 447 447 * to set whether an option autoloaded, then you need to use the add_option(). 448 448 * 449 * Before the option is updated, then the filter named 450 * 'pre_update_option_$option_name', with the $option_name as the $option_name 451 * parameter value, will be called. The hook should accept two parameters, the 452 * first is the new value and the second is the old value. Whatever is 453 * returned will be used as the new value. 449 * Before the option is updated, then the filter named 'pre_update_option_$option' 450 * with the $option as the $option parameter value, will be called. The hook should 451 * accept two parameters, the first is the new value and the second is the old value. 452 * Whatever is returned will be used as the new value. 454 453 * 455 * After the value has been updated the action named 'update_option_$option_name' 456 * will be called. This action receives two parameters the first being the old 457 * value and the second the new value. 454 * After the value has been updated the actions named 'update_option_$option' and 455 * 'updated_option' will be called. The actions receive the old and new values. 458 456 * 459 457 * @since 1.0.0 460 458 * @package WordPress 461 459 * @subpackage Option 460 * @uses apply_filters() Calls 'pre_update_option_$option' to allow overwriting 461 * the option value in a plugin. 462 * @uses do_action() Calls 'update_option' before updating the option. 463 * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success. 462 464 * 463 * @param string $option _nameOption name. Expected to not be SQL-escaped464 * @param mixed $new value Option value.465 * @param string $option Option name. Expected to not be SQL-escaped 466 * @param mixed $new_value New option value. 465 467 * @return bool False if value was not updated and true if value was updated. 466 468 */ 467 function update_option( $option _name, $newvalue ) {469 function update_option( $option, $new_value ) { 468 470 global $wpdb; 469 471 470 wp_protect_special_option( $option _name);472 wp_protect_special_option( $option ); 471 473 472 $safe_option_name = esc_sql( $option_name ); 473 $newvalue = sanitize_option( $option_name, $newvalue ); 474 $safe_option = esc_sql( $option ); 475 $new_value = sanitize_option( $option, $new_value ); 476 $old_value = get_option( $safe_option ); 477 $new_value = apply_filters( "pre_update_option_{$option}", $new_value, $old_value ); 474 478 475 $oldvalue = get_option( $safe_option_name );476 477 $newvalue = apply_filters( 'pre_update_option_' . $option_name, $newvalue, $oldvalue );478 479 479 // If the new and old values are the same, no need to update. 480 if ( $new value === $oldvalue )480 if ( $new_value === $old_value ) 481 481 return false; 482 482 483 if ( false === $oldvalue ) { 484 add_option( $option_name, $newvalue ); 485 return true; 486 } 483 if ( false === $old_value ) 484 return add_option( $option, $new_value ); 487 485 488 486 $notoptions = wp_cache_get( 'notoptions', 'options' ); 489 if ( is_array( $notoptions ) && isset( $notoptions[ $option_name] ) ) {490 unset( $notoptions[ $option_name] );487 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 488 unset( $notoptions[ $option ] ); 491 489 wp_cache_set( 'notoptions', $notoptions, 'options' ); 492 490 } 493 491 494 $_new value = $newvalue;495 $new value = maybe_serialize( $newvalue );492 $_new_value = $new_value; 493 $new_value = maybe_serialize( $new_value ); 496 494 497 do_action( 'update_option', $option _name, $oldvalue, $newvalue );495 do_action( 'update_option', $option, $old_value, $new_value ); 498 496 $alloptions = wp_load_alloptions(); 499 if ( isset( $alloptions[ $option_name] ) ) {500 $alloptions[ $option_name] = $newvalue;497 if ( isset( $alloptions[ $option ] ) ) { 498 $alloptions[ $option ] = $new_value; 501 499 wp_cache_set( 'alloptions', $alloptions, 'options' ); 502 500 } else { 503 wp_cache_set( $option _name, $newvalue, 'options' );501 wp_cache_set( $option, $new_value, 'options' ); 504 502 } 505 503 506 $ wpdb->update($wpdb->options, array('option_value' => $newvalue), array('option_name' => $option_name) );504 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $new_value ), array( 'option_name' => $option ) ); 507 505 508 if ( $wpdb->rows_affected == 1 ) { 509 do_action( "update_option_{$option_name}", $oldvalue, $_newvalue ); 510 do_action( 'updated_option', $option_name, $oldvalue, $_newvalue ); 511 return true; 506 if ( $result ) { 507 do_action( "update_option_{$option}", $old_value, $_new_value ); 508 do_action( 'updated_option', $option, $old_value, $_new_value ); 512 509 } 513 return false;510 return $result; 514 511 } 515 512 516 513 /** 517 514 * Add a new option. 518 515 * 519 * You do not need to serialize values , if the value needs to be serialize, then516 * You do not need to serialize values. If the value needs to be serialized, then 520 517 * it will be serialized before it is inserted into the database. Remember, 521 518 * resources can not be serialized or added as an option. 522 519 * … … 526 523 * options, the same as the ones which are protected and to not add options 527 524 * that were already added. 528 525 * 529 * The filter named 'add_option_$optionname', with the $optionname being530 * replaced with the option's name, will be called. The hook should accept two531 * parameters, the first is the option name, and the second is the value.532 *533 526 * @package WordPress 534 527 * @subpackage Option 535 528 * @since 1.0.0 536 529 * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton 530 * @uses apply_filters() Calls 'pre_add_option_$option' to allow overwriting 531 * the option value in a plugin. 532 * @uses do_action() Calls 'add_option' before adding the option. 533 * @uses do_action() Calls 'added_option_$option' and 'added_option' hooks on success. 537 534 * 538 * @param string $ name Option name to add. Expects to NOTbe SQL escaped.535 * @param string $option Name of option to add. Expects to not be SQL escaped. 539 536 * @param mixed $value Optional. Option value, can be anything. 540 537 * @param mixed $deprecated Optional. Description. Not used anymore. 541 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.542 * @return null returns when finished.538 * @param bool $autoload Optional. Whether to load the option when WordPress starts up. Default is enabled. 539 * @return bool False if option was not added and true if option was added. 543 540 */ 544 function add_option( $ name, $value = '', $deprecated = '', $autoload = 'yes') {541 function add_option( $option, $value = '', $deprecated = '', $autoload = true ) { 545 542 if ( !empty( $deprecated ) ) 546 543 _deprecated_argument( __FUNCTION__, '2.3' ); 547 544 548 545 global $wpdb; 549 546 550 wp_protect_special_option( $name ); 551 $safe_name = esc_sql( $name ); 552 $value = sanitize_option( $name, $value ); 547 wp_protect_special_option( $option ); 548 $safe_name = esc_sql( $option ); 553 549 550 $value = sanitize_option( $option, $value ); 551 $value = apply_filters( "pre_add_option_{$option}", $value ); 552 554 553 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 555 554 $notoptions = wp_cache_get( 'notoptions', 'options' ); 556 if ( !is_array( $notoptions ) || !isset( $notoptions[ $name] ) )555 if ( !is_array( $notoptions ) || !isset( $notoptions[ $option ] ) ) 557 556 if ( false !== get_option( $safe_name ) ) 558 return ;557 return false; 559 558 560 559 $value = maybe_serialize( $value ); 561 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';562 do_action( 'add_option', $ name, $value );563 if ( 'yes' ==$autoload ) {560 $autoload = ( $autoload && 'no' != $autoload ) ? true : false; 561 do_action( 'add_option', $option, $value ); 562 if ( $autoload ) { 564 563 $alloptions = wp_load_alloptions(); 565 $alloptions[ $name] = $value;564 $alloptions[ $option ] = $value; 566 565 wp_cache_set( 'alloptions', $alloptions, 'options' ); 567 566 } else { 568 wp_cache_set( $ name, $value, 'options' );567 wp_cache_set( $option, $value, 'options' ); 569 568 } 570 569 571 570 // This option exists now 572 571 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh 573 if ( is_array( $notoptions ) && isset( $notoptions[ $name] ) ) {574 unset( $notoptions[ $name] );572 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 573 unset( $notoptions[ $option ] ); 575 574 wp_cache_set( 'notoptions', $notoptions, 'options' ); 576 575 } 577 576 578 $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $ name, $value, $autoload ) );577 $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $value, $autoload ) ); 579 578 580 do_action( "add_option_{$ name}", $name, $value );581 do_action( 'added_option', $ name, $value );579 do_action( "add_option_{$option}", $option, $value ); 580 do_action( 'added_option', $option, $value ); 582 581 583 return ;582 return true; 584 583 } 585 584 586 585 /** 587 * Removes option by name and prevents removal of protected WordPress options.586 * Removes option by name. Prevents removal of protected WordPress options. 588 587 * 589 588 * @package WordPress 590 589 * @subpackage Option 591 590 * @since 1.2.0 591 * @uses do_action() Calls 'delete_option' before deleting the option. 592 * @uses do_action() Calls 'delete_option_$option' and 'deleted_option' 593 * hooks on success. 592 594 * 593 * @param string $ name Option name to remove.594 * @return bool True, if succeed. False, iffailure.595 * @param string $option Name of option to remove. Expected to be SQL-escaped. 596 * @return bool True, on success. False, on failure. 595 597 */ 596 function delete_option( $ name) {598 function delete_option( $option ) { 597 599 global $wpdb; 598 600 599 wp_protect_special_option( $ name);601 wp_protect_special_option( $option ); 600 602 601 603 // Get the ID, if no ID then return 602 // expected_slashed ($ name)603 $ option = $wpdb->get_row( "SELECT autoload FROM $wpdb->options WHERE option_name = '$name'" );604 if ( is_null( $option) )604 // expected_slashed ($option) 605 $row = $wpdb->get_row( "SELECT autoload FROM $wpdb->options WHERE option_name = '$option'" ); 606 if ( is_null( $row ) ) 605 607 return false; 606 do_action( 'delete_option', $ name);607 // expected_slashed ($ name)608 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$ name'" );609 if ( 'yes' == $ option->autoload ) {608 do_action( 'delete_option', $option ); 609 // expected_slashed ($option) 610 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$option'" ); 611 if ( 'yes' == $row->autoload ) { 610 612 $alloptions = wp_load_alloptions(); 611 if ( isset( $alloptions[ $name] ) ) {612 unset( $alloptions[ $name] );613 if ( isset( $alloptions[ $option ] ) ) { 614 unset( $alloptions[ $option ] ); 613 615 wp_cache_set( 'alloptions', $alloptions, 'options' ); 614 616 } 615 617 } else { 616 wp_cache_delete( $ name, 'options' );618 wp_cache_delete( $option, 'options' ); 617 619 } 618 do_action( 'deleted_option', $name ); 620 do_action( "delete_option_{$option}", $option ); 621 do_action( 'deleted_option', $option ); 619 622 return true; 620 623 } 621 624 … … 625 628 * @since 2.8.0 626 629 * @package WordPress 627 630 * @subpackage Transient 631 * @uses do_action() Calls 'delete_transient' before deleting the transient. 632 * @uses do_action() Calls 'delete_transient_$transient' and 'deleted_transient' 633 * hooks on success. 628 634 * 629 635 * @param string $transient Transient name. Expected to not be SQL-escaped 630 636 * @return bool true if successful, false otherwise 631 637 */ 632 function delete_transient( $transient) {633 global $_wp_using_ext_object_cache , $wpdb;638 function delete_transient( $transient ) { 639 global $_wp_using_ext_object_cache; 634 640 635 641 do_action( 'delete_transient_' . $transient ); 636 642 637 if ( $_wp_using_ext_object_cache ) { 638 return wp_cache_delete($transient, 'transient'); 639 } else { 640 $transient = '_transient_' . esc_sql($transient); 641 return delete_option($transient); 643 if ( $_wp_using_ext_object_cache ) 644 $result = wp_cache_delete( $transient, 'transient' ); 645 else 646 $result = delete_option( '_transient_' . esc_sql( $transient ) ); 647 648 if ( $result ) { 649 do_action( "delete_transient_{$transient}", $transient ); 650 do_action( 'deleted_transient', $transient ); 642 651 } 652 return $result; 643 653 } 644 654 645 655 /** … … 651 661 * @since 2.8.0 652 662 * @package WordPress 653 663 * @subpackage Transient 664 * @uses apply_filters() Calls 'pre_transient_$transient' to allow overwriting 665 * the transient value in a plugin before checking the transient. 666 * @uses apply_filters() Calls 'transient_$transient' to allow overwriting 667 * the transient value in a plugin after checking the transient. 654 668 * 655 669 * @param string $transient Transient name. Expected to not be SQL-escaped 656 670 * @return mixed Value of transient 657 671 */ 658 function get_transient( $transient) {659 global $_wp_using_ext_object_cache , $wpdb;672 function get_transient( $transient ) { 673 global $_wp_using_ext_object_cache; 660 674 661 $pre = apply_filters( 'pre_transient_' . $transient, false );675 $pre = apply_filters( "pre_transient_{$transient}", false ); 662 676 if ( false !== $pre ) 663 677 return $pre; 664 678 665 679 if ( $_wp_using_ext_object_cache ) { 666 $value = wp_cache_get( $transient, 'transient');680 $value = wp_cache_get( $transient, 'transient' ); 667 681 } else { 668 $transient_option = '_transient_' . esc_sql($transient); 682 $safe_transient = esc_sql( $transient ); 683 $transient_option = '_transient_' . $safe_transient; 669 684 // If option is not in alloptions, it is not autoloaded and thus has a timeout 670 685 $alloptions = wp_load_alloptions(); 671 if ( !isset( $alloptions[ $transient_option] ) ) {672 $transient_timeout = '_transient_timeout_' . esc_sql($transient);673 if ( get_option( $transient_timeout) < time() ) {674 delete_option( $transient_option);675 delete_option( $transient_timeout);686 if ( !isset( $alloptions[ $transient_option ] ) ) { 687 $transient_timeout = '_transient_timeout_' . $safe_transient; 688 if ( get_option( $transient_timeout ) < time() ) { 689 delete_option( $transient_option ); 690 delete_option( $transient_timeout ); 676 691 return false; 677 692 } 678 693 } 679 694 680 $value = get_option( $transient_option);695 $value = get_option( $transient_option ); 681 696 } 682 697 683 return apply_filters( 'transient_' . $transient, $value);698 return apply_filters( "transient_{$transient}", $value ); 684 699 } 685 700 686 701 /** 687 702 * Set/update the value of a transient 688 703 * 689 * You do not need to serialize values, if the value needs to be serialize , then704 * You do not need to serialize values, if the value needs to be serialized, then 690 705 * it will be serialized before it is set. 691 706 * 692 707 * @since 2.8.0 693 708 * @package WordPress 694 709 * @subpackage Transient 710 * @uses apply_filters() Calls 'pre_set_transient_$transient' to allow overwriting 711 * the transient value in a plugin. 712 * @uses apply_filters() Calls 'pre_set_transient_expiration_$transient' to allow overwriting 713 * the transient expiration in a plugin. 714 * @uses do_action() Calls 'set_transient' before setting the transient. 715 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success. 695 716 * 696 717 * @param string $transient Transient name. Expected to not be SQL-escaped 697 718 * @param mixed $value Transient value. 698 719 * @param int $expiration Time until expiration in seconds, default 0 699 720 * @return bool False if value was not set and true if value was set. 700 721 */ 701 function set_transient( $transient, $value, $expiration = 0) {702 global $_wp_using_ext_object_cache , $wpdb;722 function set_transient( $transient, $value, $expiration = 0 ) { 723 global $_wp_using_ext_object_cache; 703 724 704 $value = apply_filters( 'pre_set_transient_' . $transient, $value ); 725 $value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration ); 726 $expiration = apply_filters( "pre_set_transient_expiration_{$transient}", $expiration, $value ); 705 727 728 do_action( 'set_transient', $transient, $value, $expiration ); 706 729 if ( $_wp_using_ext_object_cache ) { 707 return wp_cache_set($transient, $value, 'transient', $expiration);730 $result = wp_cache_set( $transient, $value, 'transient', $expiration ); 708 731 } else { 709 732 $transient_timeout = '_transient_timeout_' . $transient; 710 733 $transient = '_transient_' . $transient; 711 $safe_transient = esc_sql( $transient);734 $safe_transient = esc_sql( $transient ); 712 735 if ( false === get_option( $safe_transient ) ) { 713 736 $autoload = 'yes'; 714 737 if ( 0 != $expiration ) { 715 738 $autoload = 'no'; 716 add_option( $transient_timeout, time() + $expiration, '', 'no');739 add_option( $transient_timeout, time() + $expiration, '', 'no' ); 717 740 } 718 return add_option($transient, $value, '', $autoload);741 $result = add_option( $transient, $value, '', $autoload ); 719 742 } else { 720 743 if ( 0 != $expiration ) 721 update_option( $transient_timeout, time() + $expiration);722 return update_option($transient, $value);744 update_option( $transient_timeout, time() + $expiration ); 745 $result = update_option( $transient, $value ); 723 746 } 724 747 } 748 if ( $result ) { 749 do_action( "set_transient_{$transient}", $transient, $value, $expiration ); 750 do_action( 'setted_transient', $transient, $value, $expiration ); 751 } 752 return $result; 725 753 } 726 754 727 755 /** … … 1145 1173 $type = $headers['content-type']; 1146 1174 $allowed_types = array( 'video', 'audio' ); 1147 1175 1148 // Check to see if we can figure out the mime type from 1149 // the extension 1176 // Check to see if we can figure out the mime type from the extension 1150 1177 $url_parts = parse_url( $url ); 1151 1178 $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION ); 1152 1179 if ( !empty( $extension ) ) { … … 3295 3322 return $current_suspend; 3296 3323 } 3297 3324 3298 function get_site_option( $key, $default = false, $use_cache = true ) { 3325 /** 3326 * Retrieve site option value based on option name. 3327 * 3328 * @see get_option() 3329 * @package WordPress 3330 * @subpackage Option 3331 * @since 2.8.0 3332 * @uses apply_filters() Calls 'pre_site_option_$option' to allow overwriting 3333 * the option value in a plugin before retrieving the value. 3334 * @uses apply_filters() Calls 'site_option_$option' to allow overwriting 3335 * the option value in a plugin after retrieving the value. 3336 * 3337 * @param string $option Name of option to retrieve. Should already be SQL-escaped 3338 * @param mixed $default Optional value to return if option doesn't exist 3339 * @param bool $use_cache Whether to use cache. Multisite only. Default true. 3340 * @return mixed Value set for the option. 3341 */ 3342 function get_site_option( $option, $default = false, $use_cache = true ) { 3299 3343 global $wpdb; 3300 3344 3301 3345 // Allow plugins to short-circuit site options. 3302 $pre = apply_filters( 'pre_site_option_' . $key, false );3346 $pre = apply_filters( "pre_site_option_{$option}", false ); 3303 3347 if ( false !== $pre ) 3304 3348 return $pre; 3305 3349 3306 3350 if ( !is_multisite() ) { 3307 $value = get_option( $key, $default);3351 $value = get_option( $option, $default ); 3308 3352 } else { 3309 $cache_key = "{$wpdb->siteid}:$ key";3353 $cache_key = "{$wpdb->siteid}:$option"; 3310 3354 3311 if ( $use_cache == true && $value = wp_cache_get($cache_key, 'site-options') )3312 return $value;3355 if ( $use_cache && $value = wp_cache_get( $cache_key, 'site-options' ) ) 3356 return apply_filters( "site_option_{$option}", $value ); 3313 3357 3314 $value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $ key, $wpdb->siteid) );3358 $value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid) ); 3315 3359 3316 3360 if ( is_null($value) ) 3317 3361 $value = $default; … … 3321 3365 wp_cache_set( $cache_key, $value, 'site-options' ); 3322 3366 } 3323 3367 3324 return apply_filters( 'site_option_' . $key, $value );3368 return apply_filters( "site_option_{$option}", $value ); 3325 3369 } 3326 3370 3327 // expects $key, $value not to be SQL escaped 3328 function add_site_option( $key, $value ) { 3371 /** 3372 * Add a new site option. 3373 * 3374 * @see add_option() 3375 * @package WordPress 3376 * @subpackage Option 3377 * @since 2.8.0 3378 * @uses apply_filters() Calls 'pre_add_site_option_$option' to allow overwriting 3379 * the option value in a plugin. 3380 * @uses do_action() Calls 'add_site_option' before adding the option. 3381 * @uses do_action() Calls 'added_site_option_$option' and 'added_site_option' hooks on success. 3382 * 3383 * @param string $option Name of option to add. Expects to not be SQL escaped. 3384 * @param mixed $value Optional. Option value, can be anything. 3385 * @return bool False if option was not added and true if option was added. 3386 */ 3387 function add_site_option( $option, $value ) { 3329 3388 global $wpdb; 3330 3389 3331 $value = apply_filters( 'pre_add_site_option_' . $key, $value );3390 $value = apply_filters( "pre_add_site_option_{$option}", $value ); 3332 3391 3333 3392 if ( !is_multisite() ) { 3334 $result = add_option($key, $value);3393 $result = add_option( $option, $value ); 3335 3394 } else { 3336 $cache_key = "{$wpdb->siteid}:$ key";3395 $cache_key = "{$wpdb->siteid}:$option"; 3337 3396 3338 if ( $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $ key, $wpdb->siteid ) ) )3339 return update_site_option( $ key, $value );3397 if ( $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ) ) 3398 return update_site_option( $option, $value ); 3340 3399 3341 $value = sanitize_option( $ key, $value );3342 wp_cache_set( $cache_key, $value, 'site-options' );3400 $value = sanitize_option( $option, $value ); 3401 wp_cache_set( $cache_key, $value, 'site-options' ); 3343 3402 3344 $value = maybe_serialize($value); 3403 $_value = maybe_serialize( $value ); 3404 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $_value ) ); 3405 } 3345 3406 3346 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $key, 'meta_value' => $value) ); 3407 if ( $result ) { 3408 do_action( "add_site_option_{$option}", $option, $value ); 3409 do_action( "add_site_option", $option, $value ); 3347 3410 } 3348 3349 do_action( "add_site_option_{$key}", $key, $value );3350 do_action( "add_site_option", $key, $value );3351 3352 3411 return $result; 3353 3412 } 3354 3413 3355 function delete_site_option( $key ) { 3414 /** 3415 * Removes site option by name. 3416 * 3417 * @see delete_option() 3418 * @package WordPress 3419 * @subpackage Option 3420 * @since 2.8.0 3421 * 3422 * @uses do_action() Calls 'pre_delete_site_option' and 'pre_delete_site_option_$option' 3423 * before deleting the option. Note differences with actions in delete_option(). 3424 * @uses do_action() Calls 'delete_site_option_$option' and 'delete_site_option' 3425 * hooks on success. Note differences with actions in delete_option(). 3426 * 3427 * @param string $option Name of option to remove. Expected to be SQL-escaped. 3428 * @return bool True, if succeed. False, if failure. 3429 */ 3430 function delete_site_option( $option ) { 3356 3431 global $wpdb; 3357 3432 3358 //wpmu_protect_special_option( $ key); @todo3433 //wpmu_protect_special_option( $option ); @todo 3359 3434 3360 do_action( 'pre_delete_site_option_' . $key ); 3435 do_action( 'pre_delete_site_option', $option ); 3436 do_action( "pre_delete_site_option_{$option}" ); 3361 3437 3362 3438 if ( !is_multisite() ) { 3363 $result = delete_option( $key);3439 $result = delete_option( $option ); 3364 3440 } else { 3365 $ option = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid ) );3366 if ( is_null( $ option ) || !$option->meta_id )3441 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ); 3442 if ( is_null( $row ) || !$row->meta_id ) 3367 3443 return false; 3368 $cache_key = "{$wpdb->siteid}:$ key";3444 $cache_key = "{$wpdb->siteid}:$option"; 3369 3445 wp_cache_delete( $cache_key, 'site-options' ); 3370 3446 3371 $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $ key, $wpdb->siteid ) );3447 $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ); 3372 3448 } 3373 3449 3374 do_action( "delete_site_option_{$key}", $key ); 3375 do_action( "delete_site_option", $key ); 3450 if ( $result ) { 3451 do_action( "delete_site_option_{$option}", $option ); 3452 do_action( "delete_site_option", $option ); 3453 } 3376 3454 return $result; 3377 3455 } 3378 3456 3379 // expects $key, $value not to be SQL escaped 3380 function update_site_option( $key, $value ) { 3457 /** 3458 * Update the value of a site option that was already added. 3459 * 3460 * @see update_option() 3461 * @since 2.8.0 3462 * @package WordPress 3463 * @subpackage Option 3464 * @uses apply_filters() Calls 'pre_update_site_option_$option' to allow overwriting 3465 * the option value in a plugin. 3466 * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success. Note differences with actions in update_option(). 3467 * 3468 * @param string $option Option name. Expected to not be SQL-escaped 3469 * @param mixed $value Option value. 3470 * @return bool False if value was not updated and true if value was updated. 3471 */ 3472 function update_site_option( $option, $new_value ) { 3381 3473 global $wpdb; 3382 3474 3383 $old value = get_site_option( $key);3384 $ value = apply_filters( 'pre_update_site_option_' . $key, $value, $oldvalue );3475 $old_value = get_site_option( $option ); 3476 $new_value = apply_filters( 'pre_update_site_option_' . $option, $new_value, $old_value ); 3385 3477 3386 if ( $ value == $oldvalue )3478 if ( $new_value === $old_value ) 3387 3479 return false; 3388 3480 3389 3481 if ( !is_multisite() ) { 3390 $result = update_option( $key, $value);3482 $result = update_option( $option, $new_value ); 3391 3483 } else { 3392 $cache_key = "{$wpdb->siteid}:$ key";3484 $cache_key = "{$wpdb->siteid}:$option"; 3393 3485 3394 if ( $value && !$wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) ) ) 3395 return add_site_option( $key, $value ); 3396 $value = sanitize_option( $key, $value ); 3397 wp_cache_set( $cache_key, $value, 'site-options' ); 3486 if ( $new_value && !$wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ) ) 3487 return add_site_option( $option, $new_value ); 3398 3488 3399 $value = maybe_serialize($value); 3400 $result = $wpdb->update( $wpdb->sitemeta, array('meta_value' => $value), array('site_id' => $wpdb->siteid, 'meta_key' => $key) ); 3489 $value = sanitize_option( $option, $new_value ); 3490 wp_cache_set( $cache_key, $new_value, 'site-options' ); 3491 3492 $_new_value = maybe_serialize( $new_value ); 3493 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $_new_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) ); 3401 3494 } 3402 3495 3403 do_action( "update_site_option_{$key}", $key, $value ); 3404 do_action( "update_site_option", $key, $value ); 3496 if ( $result ) { 3497 do_action( "update_site_option_{$option}", $option, $new_value ); 3498 do_action( "update_site_option", $option, $new_value ); 3499 } 3405 3500 return $result; 3406 3501 } 3407 3502 3408 3503 /** 3409 3504 * Delete a site transient 3410 3505 * 3411 * @since 2.890 3506 * @see delete_transient() 3507 * @since 2.9.0 3412 3508 * @package WordPress 3413 3509 * @subpackage Transient 3510 * @uses do_action() Calls 'delete_site_transient' before deleting the transient. 3511 * @uses do_action() Calls 'delete_site_transient_$transient' and 'deleted_site_transient' 3512 * hooks on success. 3414 3513 * 3415 3514 * @param string $transient Transient name. Expected to not be SQL-escaped 3416 * @return bool true if successful, false otherwise3515 * @return bool True if successful, false otherwise 3417 3516 */ 3418 function delete_site_transient( $transient) {3419 global $_wp_using_ext_object_cache , $wpdb;3517 function delete_site_transient( $transient ) { 3518 global $_wp_using_ext_object_cache; 3420 3519 3421 if ( $_wp_using_ext_object_cache ) { 3422 return wp_cache_delete($transient, 'site-transient'); 3423 } else { 3424 $transient = '_site_transient_' . esc_sql($transient); 3425 return delete_site_option($transient); 3520 do_action( 'delete_site_transient', $transient ); 3521 if ( $_wp_using_ext_object_cache ) 3522 $result = wp_cache_delete( $transient, 'site-transient' ); 3523 else 3524 $result = delete_site_option( '_site_transient_' . esc_sql( $transient ) ); 3525 3526 if ( $result ) { 3527 do_action( "delete_site_transient_{$transient}", $transient ); 3528 do_action( 'deleted_site_transient', $transient ); 3426 3529 } 3530 return $result; 3427 3531 } 3428 3532 3429 3533 /** … … 3432 3536 * If the transient does not exist or does not have a value, then the return value 3433 3537 * will be false. 3434 3538 * 3539 * @see get_transient() 3435 3540 * @since 2.9.0 3436 3541 * @package WordPress 3437 3542 * @subpackage Transient 3543 * @uses apply_filters() Calls 'pre_site_transient_$transient' to allow overwriting 3544 * the transient value in a plugin before checking the transient. 3545 * @uses apply_filters() Calls 'site_transient_$transient' to allow overwriting 3546 * the transient value in a plugin after checking the transient. 3438 3547 * 3439 3548 * @param string $transient Transient name. Expected to not be SQL-escaped 3440 3549 * @return mixed Value of transient 3441 3550 */ 3442 3551 function get_site_transient($transient) { 3443 global $_wp_using_ext_object_cache , $wpdb;3552 global $_wp_using_ext_object_cache; 3444 3553 3445 $pre = apply_filters( 'pre_site_transient_' . $transient, false );3554 $pre = apply_filters( "pre_site_transient_{$transient}", false ); 3446 3555 if ( false !== $pre ) 3447 3556 return $pre; 3448 3557 3449 3558 if ( $_wp_using_ext_object_cache ) { 3450 $value = wp_cache_get( $transient, 'site-transient');3559 $value = wp_cache_get( $transient, 'site-transient' ); 3451 3560 } else { 3452 $transient_option = '_site_transient_' . esc_sql($transient); 3453 $transient_timeout = '_site_transient_timeout_' . esc_sql($transient); 3454 $timeout = get_site_option($transient_timeout); 3561 $safe_transient = esc_sql( $transient ); 3562 $transient_option = '_site_transient_' . $safe_transient; 3563 $transient_timeout = '_site_transient_timeout_' . $safe_transient; 3564 $timeout = get_site_option( $transient_timeout ); 3455 3565 if ( false !== $timeout && $timeout < time() ) { 3456 delete_site_option( $transient_option);3457 delete_site_option( $transient_timeout);3566 delete_site_option( $transient_option ); 3567 delete_site_option( $transient_timeout ); 3458 3568 return false; 3459 3569 } 3460 3570 3461 $value = get_site_option( $transient_option);3571 $value = get_site_option( $transient_option ); 3462 3572 } 3463 3573 3464 return apply_filters( 'site_transient_' . $transient, $value);3574 return apply_filters( "site_transient_{$transient}", $value ); 3465 3575 } 3466 3576 3467 3577 /** … … 3470 3580 * You do not need to serialize values, if the value needs to be serialize, then 3471 3581 * it will be serialized before it is set. 3472 3582 * 3583 * @see set_transient() 3473 3584 * @since 2.9.0 3474 3585 * @package WordPress 3475 3586 * @subpackage Transient 3587 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' to allow overwriting 3588 * the transient value in a plugin. 3589 * @uses apply_filters() Calls 'pre_set_site_transient_expiration_$transient' to allow overwriting 3590 * the transient expiration in a plugin. 3591 * @uses do_action() Calls 'set_site_transient' before setting the transient. 3592 * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success. 3476 3593 * 3477 3594 * @param string $transient Transient name. Expected to not be SQL-escaped 3478 3595 * @param mixed $value Transient value. … … 3480 3597 * @return bool False if value was not set and true if value was set. 3481 3598 */ 3482 3599 function set_site_transient($transient, $value, $expiration = 0) { 3483 global $_wp_using_ext_object_cache , $wpdb;3600 global $_wp_using_ext_object_cache; 3484 3601 3602 $value = apply_filters( "pre_set_site_transient_{$transient}", $value, $expiration ); 3603 $expiration = apply_filters( "pre_set_site_transient_expiration_{$transient}", $expiration, $value ); 3604 3605 do_action( 'set_site_transient', $transient, $value, $expiration ); 3485 3606 if ( $_wp_using_ext_object_cache ) { 3486 returnwp_cache_set($transient, $value, 'site-transient', $expiration);3607 $result = wp_cache_set($transient, $value, 'site-transient', $expiration); 3487 3608 } else { 3488 3609 $transient_timeout = '_site_transient_timeout_' . $transient; 3489 3610 $transient = '_site_transient_' . $transient; 3490 3611 $safe_transient = esc_sql($transient); 3491 3612 if ( false === get_site_option( $safe_transient ) ) { 3492 3613 if ( 0 != $expiration ) 3493 add_site_option( $transient_timeout, time() + $expiration);3494 returnadd_site_option($transient, $value);3614 add_site_option( $transient_timeout, time() + $expiration ); 3615 $result = add_site_option($transient, $value); 3495 3616 } else { 3496 3617 if ( 0 != $expiration ) 3497 update_site_option( $transient_timeout, time() + $expiration);3498 return update_site_option($transient, $value);3618 update_site_option( $transient_timeout, time() + $expiration ); 3619 $result = update_site_option( $transient, $value ); 3499 3620 } 3500 3621 } 3622 if ( $result ) { 3623 do_action( "set_site_transient_{$transient}", $transient, $value, $expiration ); 3624 do_action( 'setted_site_transient', $transient, $value, $expiration ); 3625 } 3626 return $result; 3501 3627 } 3502 3628 3503 3629 /**
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)