Ticket #10788: options-transients-apis-2.patch
| File options-transients-apis-2.patch, 31.0 KB (added by nacin, 3 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 329 328 // prevent non-existent options from triggering multiple queries 330 329 $notoptions = wp_cache_get( 'notoptions', 'options' ); 331 if ( isset( $notoptions[$ setting] ) )330 if ( isset( $notoptions[$option] ) ) 332 331 return $default; 333 332 334 333 $alloptions = wp_load_alloptions(); 335 334 336 if ( isset( $alloptions[$ setting] ) ) {337 $value = $alloptions[$ setting];335 if ( isset( $alloptions[$option] ) ) { 336 $value = $alloptions[$option]; 338 337 } else { 339 $value = wp_cache_get( $ setting, 'options' );338 $value = wp_cache_get( $option, 'options' ); 340 339 341 340 if ( false === $value ) { 342 341 if ( defined( 'WP_INSTALLING' ) ) 343 342 $suppress = $wpdb->suppress_errors(); 344 // expected_slashed ($ setting)345 $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$ setting' LIMIT 1" );343 // expected_slashed ($option) 344 $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$option' LIMIT 1" ); 346 345 if ( defined( 'WP_INSTALLING' ) ) 347 346 $wpdb->suppress_errors($suppress); 348 347 349 if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 348 // Has to be get_row instead of get_var because of funkiness with 0, false, null values 349 if ( is_object( $row ) ) { 350 350 $value = $row->option_value; 351 wp_cache_add( $ setting, $value, 'options' );351 wp_cache_add( $option, $value, 'options' ); 352 352 } else { // option does not exist, so we must cache its non-existence 353 $notoptions[$ setting] = true;353 $notoptions[$option] = true; 354 354 wp_cache_set( 'notoptions', $notoptions, 'options' ); 355 355 return $default; 356 356 } … … 358 358 } 359 359 360 360 // If home is not set use siteurl. 361 if ( 'home' == $ setting&& '' == $value )361 if ( 'home' == $option && '' == $value ) 362 362 return get_option( 'siteurl' ); 363 363 364 if ( in_array( $ setting, array('siteurl', 'home', 'category_base', 'tag_base') ) )364 if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) 365 365 $value = untrailingslashit( $value ); 366 366 367 return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) );367 return apply_filters( "option_{$option}", maybe_unserialize( $value ) ); 368 368 } 369 369 370 370 /** … … 404 404 * 405 405 * This is different from wp_load_alloptions() in that this function does not 406 406 * cache its results and will retrieve all options from the database every time 407 *408 407 * it is called. 409 408 * 410 409 * @since 1.0.0 … … 423 422 $wpdb->show_errors($show); 424 423 425 424 foreach ( (array) $options as $option ) { 426 // "When trying to design a foolproof system,427 // never underestimate the ingenuity of the fools :)" -- Dougal425 // A common mistake people make when trying to design something completely foolproof 426 // is to underestimate the ingenuity of complete fools. -- Douglas Adams 428 427 if ( in_array( $option->option_name, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) 429 428 $option->option_value = untrailingslashit( $option->option_value ); 430 429 $value = maybe_unserialize( $option->option_value ); … … 474 473 * value, but you will not be able to set whether it is autoloaded. If you want 475 474 * to set whether an option autoloaded, then you need to use the add_option(). 476 475 * 477 * Before the option is updated, then the filter named 478 * 'pre_update_option_$option_name', with the $option_name as the $option_name 479 * parameter value, will be called. The hook should accept two parameters, the 480 * first is the new value and the second is the old value. Whatever is 481 * returned will be used as the new value. 476 * Before the option is updated, then the filter named 'pre_update_option_$option' 477 * with the $option as the $option parameter value, will be called. The hook should 478 * accept two parameters, the first is the new value and the second is the old value. 479 * Whatever is returned will be used as the new value. 482 480 * 483 * After the value has been updated the action named 'update_option_$option_name' 484 * will be called. This action receives two parameters the first being the old 485 * value and the second the new value. 481 * After the value has been updated the actions named 'update_option_$option' and 482 * 'updated_option' will be called. The actions receive the old and new values. 486 483 * 487 484 * @since 1.0.0 488 485 * @package WordPress 489 486 * @subpackage Option 487 * @uses apply_filters() Calls 'pre_update_option_$option' to allow overwriting 488 * the option value in a plugin. 489 * @uses do_action() Calls 'update_option' before updating the option. 490 * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success. 490 491 * 491 * @param string $option _nameOption name. Expected to not be SQL-escaped492 * @param mixed $ newvalue Option value.492 * @param string $option Option name. Expected to not be SQL-escaped 493 * @param mixed $value Option value. 493 494 * @return bool False if value was not updated and true if value was updated. 494 495 */ 495 function update_option( $option _name, $newvalue ) {496 function update_option( $option, $value ) { 496 497 global $wpdb; 497 498 498 wp_protect_special_option( $option _name);499 wp_protect_special_option( $option ); 499 500 500 $safe_option_name = esc_sql( $option_name ); 501 $newvalue = sanitize_option( $option_name, $newvalue ); 501 $safe_option = esc_sql( $option ); 502 $new_value = sanitize_option( $option, $value ); 503 $old_value = get_option( $safe_option ); 504 $new_value = apply_filters( "pre_update_option_{$option}", $new_value, $old_value ); 502 505 503 $oldvalue = get_option( $safe_option_name );504 505 $newvalue = apply_filters( 'pre_update_option_' . $option_name, $newvalue, $oldvalue );506 507 506 // If the new and old values are the same, no need to update. 508 if ( $new value === $oldvalue )507 if ( $new_value === $old_value ) 509 508 return false; 510 509 511 if ( false === $oldvalue ) { 512 add_option( $option_name, $newvalue ); 513 return true; 514 } 510 if ( false === $old_value ) 511 return add_option( $option, $new_value ); 515 512 516 513 $notoptions = wp_cache_get( 'notoptions', 'options' ); 517 if ( is_array( $notoptions ) && isset( $notoptions[$option _name] ) ) {518 unset( $notoptions[$option _name] );514 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { 515 unset( $notoptions[$option] ); 519 516 wp_cache_set( 'notoptions', $notoptions, 'options' ); 520 517 } 521 518 522 $_new value = $newvalue;523 $new value = maybe_serialize( $newvalue );519 $_new_value = $new_value; 520 $new_value = maybe_serialize( $new_value ); 524 521 525 do_action( 'update_option', $option _name, $oldvalue, $newvalue );522 do_action( 'update_option', $option, $old_value, $new_value ); 526 523 $alloptions = wp_load_alloptions(); 527 if ( isset( $alloptions[$option _name] ) ) {528 $alloptions[$option _name] = $newvalue;524 if ( isset( $alloptions[$option] ) ) { 525 $alloptions[$option] = $new_value; 529 526 wp_cache_set( 'alloptions', $alloptions, 'options' ); 530 527 } else { 531 wp_cache_set( $option _name, $newvalue, 'options' );528 wp_cache_set( $option, $new_value, 'options' ); 532 529 } 533 530 534 $wpdb->update($wpdb->options, array('option_value' => $new value), array('option_name' => $option_name) );531 $wpdb->update($wpdb->options, array('option_value' => $new_value), array('option_name' => $option) ); 535 532 536 533 if ( $wpdb->rows_affected == 1 ) { 537 do_action( "update_option_{$option _name}", $oldvalue, $_newvalue );538 do_action( 'updated_option', $option _name, $oldvalue, $_newvalue );534 do_action( "update_option_{$option}", $old_value, $_new_value ); 535 do_action( 'updated_option', $option, $old_value, $_new_value ); 539 536 return true; 540 537 } 541 538 return false; … … 544 541 /** 545 542 * Add a new option. 546 543 * 547 * You do not need to serialize values , if the value needs to be serialize, then544 * You do not need to serialize values. If the value needs to be serialized, then 548 545 * it will be serialized before it is inserted into the database. Remember, 549 546 * resources can not be serialized or added as an option. 550 547 * … … 554 551 * options, the same as the ones which are protected and to not add options 555 552 * that were already added. 556 553 * 557 * The filter named 'add_option_$optionname', with the $optionname being558 * replaced with the option's name, will be called. The hook should accept two559 * parameters, the first is the option name, and the second is the value.560 *561 554 * @package WordPress 562 555 * @subpackage Option 563 556 * @since 1.0.0 564 557 * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton 558 * @uses apply_filters() Calls 'pre_add_option_$option' to allow overwriting 559 * the option value in a plugin. 560 * @uses do_action() Calls 'add_option' before adding the option. 561 * @uses do_action() Calls 'added_option_$option' and 'added_option' hooks on success. 565 562 * 566 * @param string $ name Option name to add. Expects to NOTbe SQL escaped.563 * @param string $option Name of option to add. Expects to not be SQL escaped. 567 564 * @param mixed $value Optional. Option value, can be anything. 568 565 * @param mixed $deprecated Optional. Description. Not used anymore. 569 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.570 * @return null returns when finished.566 * @param bool $autoload Optional. Whether to load the option when WordPress starts up. Default is enabled. 567 * @return bool False if option was not added and true if option was added. 571 568 */ 572 function add_option( $ name, $value = '', $deprecated = '', $autoload = 'yes') {569 function add_option( $option, $value = '', $deprecated = '', $autoload = true ) { 573 570 global $wpdb; 574 571 575 wp_protect_special_option( $name ); 576 $safe_name = esc_sql( $name ); 577 $value = sanitize_option( $name, $value ); 572 wp_protect_special_option( $option ); 573 $safe_name = esc_sql( $option ); 578 574 575 $value = sanitize_option( $option, $value ); 576 $value = apply_filters( "pre_add_option_{$option}", $value ); 577 579 578 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 580 579 $notoptions = wp_cache_get( 'notoptions', 'options' ); 581 if ( !is_array( $notoptions ) || !isset( $notoptions[$ name] ) )580 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) ) 582 581 if ( false !== get_option( $safe_name ) ) 583 return ;582 return false; 584 583 585 584 $value = maybe_serialize( $value ); 586 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';587 do_action( 'add_option', $ name, $value );588 if ( 'yes' ==$autoload ) {585 $autoload = ( $autoload && 'no' != $autoload ) ? true : false; 586 do_action( 'add_option', $option, $value ); 587 if ( $autoload ) { 589 588 $alloptions = wp_load_alloptions(); 590 $alloptions[$ name] = $value;589 $alloptions[$option] = $value; 591 590 wp_cache_set( 'alloptions', $alloptions, 'options' ); 592 591 } else { 593 wp_cache_set( $ name, $value, 'options' );592 wp_cache_set( $option, $value, 'options' ); 594 593 } 595 594 596 595 // This option exists now 597 596 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh 598 if ( is_array( $notoptions ) && isset( $notoptions[$ name] ) ) {599 unset( $notoptions[$ name] );597 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { 598 unset( $notoptions[$option] ); 600 599 wp_cache_set( 'notoptions', $notoptions, 'options' ); 601 600 } 602 601 603 $wpdb->insert($wpdb->options, array('option_name' => $ name, 'option_value' => $value, 'autoload' => $autoload) );602 $wpdb->insert($wpdb->options, array('option_name' => $option, 'option_value' => $value, 'autoload' => ( $autoload ) ? 'yes' : 'no' ) ); 604 603 605 do_action( "add_option_{$ name}", $name, $value );606 do_action( 'added_option', $ name, $value );607 608 return ;604 do_action( "add_option_{$option}", $option, $value ); 605 do_action( 'added_option', $option, $value ); 606 607 return true; 609 608 } 610 609 611 610 /** 612 * Removes option by name and prevents removal of protected WordPress options.611 * Removes option by name. Prevents removal of protected WordPress options. 613 612 * 614 613 * @package WordPress 615 614 * @subpackage Option 616 615 * @since 1.2.0 616 * @uses do_action() Calls 'delete_option' before deleting the option. 617 * @uses do_action() Calls 'delete_option_$option' and 'deleted_option' 618 * hooks on success. 617 619 * 618 * @param string $ name Option name to remove.619 * @return bool True, if succeed. False, iffailure.620 * @param string $option Name of option to remove. Expected to be SQL-escaped. 621 * @return bool True, on success. False, on failure. 620 622 */ 621 function delete_option( $ name) {623 function delete_option( $option ) { 622 624 global $wpdb; 623 625 624 wp_protect_special_option( $ name);626 wp_protect_special_option( $option ); 625 627 626 628 // Get the ID, if no ID then return 627 629 // expected_slashed ($name) 628 $option = $wpdb->get_row( "SELECT autoload FROM $wpdb->options WHERE option_name = '$ name'" );630 $option = $wpdb->get_row( "SELECT autoload FROM $wpdb->options WHERE option_name = '$option'" ); 629 631 if ( is_null($option) ) 630 632 return false; 631 do_action( 'delete_option', $ name);633 do_action( 'delete_option', $option ); 632 634 // expected_slashed ($name) 633 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$ name'" );635 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$option'" ); 634 636 if ( 'yes' == $option->autoload ) { 635 637 $alloptions = wp_load_alloptions(); 636 if ( isset( $alloptions[$ name] ) ) {637 unset( $alloptions[$ name] );638 if ( isset( $alloptions[$option] ) ) { 639 unset( $alloptions[$option] ); 638 640 wp_cache_set( 'alloptions', $alloptions, 'options' ); 639 641 } 640 642 } else { 641 wp_cache_delete( $ name, 'options' );643 wp_cache_delete( $option, 'options' ); 642 644 } 643 do_action( 'deleted_option', $name ); 645 do_action( "delete_option_{$option}", $option ); 646 do_action( 'deleted_option', $option ); 644 647 return true; 645 648 } 646 649 … … 650 653 * @since 2.8.0 651 654 * @package WordPress 652 655 * @subpackage Transient 656 * @uses do_action() Calls 'delete_transient' before deleting the transient. 657 * @uses do_action() Calls 'delete_transient_$transient' and 'deleted_transient' 658 * hooks on success. 653 659 * 654 660 * @param string $transient Transient name. Expected to not be SQL-escaped 655 661 * @return bool true if successful, false otherwise 656 662 */ 657 663 function delete_transient($transient) { 658 global $_wp_using_ext_object_cache , $wpdb;664 global $_wp_using_ext_object_cache; 659 665 666 $_transient = $transient; 667 do_action( 'delete_transient', $transient ); 660 668 if ( $_wp_using_ext_object_cache ) { 661 return wp_cache_delete($transient, 'transient');669 $result = wp_cache_delete( $transient, 'transient' ); 662 670 } else { 663 $transient = '_transient_' . esc_sql($transient); 664 return delete_option($transient); 671 $result = delete_option( '_transient_' . esc_sql( $transient ) ); 665 672 } 673 674 if ( $result ) { 675 do_action( "delete_transient_{$transient}", $transient ); 676 do_action( 'deleted_transient', $transient ); 677 } 678 return $result; 666 679 } 667 680 668 681 /** … … 674 687 * @since 2.8.0 675 688 * @package WordPress 676 689 * @subpackage Transient 690 * @uses apply_filters() Calls 'pre_transient_$transient' to allow overwriting 691 * the transient value in a plugin before checking the transient. 692 * @uses apply_filters() Calls 'transient_$transient' to allow overwriting 693 * the transient value in a plugin after checking the transient. 677 694 * 678 695 * @param string $transient Transient name. Expected to not be SQL-escaped 679 696 * @return mixed Value of transient 680 697 */ 681 698 function get_transient($transient) { 682 global $_wp_using_ext_object_cache , $wpdb;699 global $_wp_using_ext_object_cache; 683 700 684 $pre = apply_filters( 'pre_transient_' . $transient, false );701 $pre = apply_filters( "pre_transient_{$transient}", false ); 685 702 if ( false !== $pre ) 686 703 return $pre; 687 704 688 705 if ( $_wp_using_ext_object_cache ) { 689 706 $value = wp_cache_get($transient, 'transient'); 690 707 } else { 691 $transient_option = '_transient_' . esc_sql($transient); 708 $safe_transient = esc_sql( $transient ); 709 $transient_option = '_transient_' . $safe_transient; 692 710 // If option is not in alloptions, it is not autoloaded and thus has a timeout 693 711 $alloptions = wp_load_alloptions(); 694 712 if ( !isset( $alloptions[$transient_option] ) ) { 695 $transient_timeout = '_transient_timeout_' . esc_sql($transient);696 if ( get_option( $transient_timeout) < time() ) {697 delete_option( $transient_option);698 delete_option( $transient_timeout);713 $transient_timeout = '_transient_timeout_' . $safe_transient; 714 if ( get_option( $transient_timeout ) < time() ) { 715 delete_option( $transient_option ); 716 delete_option( $transient_timeout ); 699 717 return false; 700 718 } 701 719 } 702 720 703 $value = get_option( $transient_option);721 $value = get_option( $transient_option ); 704 722 } 705 723 706 return apply_filters( 'transient_' . $transient, $value);724 return apply_filters( "transient_{$transient}", $value ); 707 725 } 708 726 709 727 /** … … 715 733 * @since 2.8.0 716 734 * @package WordPress 717 735 * @subpackage Transient 736 * @uses apply_filters() Calls 'pre_set_transient_$transient' to allow overwriting 737 * the transient value in a plugin. 738 * @uses apply_filters() Calls 'pre_set_transient_expiration_$transient' to allow overwriting 739 * the transient expiration in a plugin. 740 * @uses do_action() Calls 'set_transient' before setting the transient. 741 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success. 718 742 * 719 743 * @param string $transient Transient name. Expected to not be SQL-escaped 720 744 * @param mixed $value Transient value. … … 722 746 * @return bool False if value was not set and true if value was set. 723 747 */ 724 748 function set_transient($transient, $value, $expiration = 0) { 725 global $_wp_using_ext_object_cache , $wpdb;749 global $_wp_using_ext_object_cache; 726 750 751 $value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration ); 752 $expiration = apply_filters( "pre_set_transient_expiration_{$transient}", $expiration, $value ); 753 754 do_action( 'set_transient', $transient, $value, $expiration ); 727 755 if ( $_wp_using_ext_object_cache ) { 728 returnwp_cache_set($transient, $value, 'transient', $expiration);756 $result = wp_cache_set($transient, $value, 'transient', $expiration); 729 757 } else { 730 758 $transient_timeout = '_transient_timeout_' . $transient; 731 759 $transient = '_transient_' . $transient; … … 734 762 $autoload = 'yes'; 735 763 if ( 0 != $expiration ) { 736 764 $autoload = 'no'; 737 add_option( $transient_timeout, time() + $expiration, '', 'no');765 add_option( $transient_timeout, time() + $expiration, '', 'no' ); 738 766 } 739 return add_option($transient, $value, '', $autoload);767 $result = add_option( $transient, $value, '', $autoload ); 740 768 } else { 741 769 if ( 0 != $expiration ) 742 update_option( $transient_timeout, time() + $expiration);743 return update_option($transient, $value);770 update_option( $transient_timeout, time() + $expiration ); 771 $result = update_option( $transient, $value ); 744 772 } 745 773 } 774 if ( $result ) { 775 do_action( "set_transient_{$transient}", $transient, $value, $expiration ); 776 do_action( 'setted_transient', $transient, $value, $expiration ); 777 } 778 return $result; 746 779 } 747 780 748 781 /** … … 3176 3209 return $current_suspend; 3177 3210 } 3178 3211 3179 function get_site_option( $key, $default = false, $use_cache = true ) { 3212 /** 3213 * Retrieve site option value based on option name. 3214 * 3215 * @see get_option() 3216 * @package WordPress 3217 * @subpackage Option 3218 * @since 2.8.0 3219 * @uses apply_filters() Calls 'pre_site_option_$option' to allow overwriting 3220 * the option value in a plugin before retrieving the value. 3221 * @uses apply_filters() Calls 'site_option_$option' to allow overwriting 3222 * the option value in a plugin after retrieving the value. 3223 * 3224 * @param string $option Name of option to retrieve. Should already be SQL-escaped 3225 * @param mixed $default Optional value to return if option doesn't exist 3226 * @param use_cache Used only in WordPress MU. 3227 * @return mixed Value set for the option. 3228 */ 3229 function get_site_option( $option, $default = false, $use_cache = true ) { 3180 3230 // Allow plugins to short-circuit site options. 3181 $pre = apply_filters( 'pre_site_option_' . $key, false );3231 $pre = apply_filters( "pre_site_option_{$option}", false ); 3182 3232 if ( false !== $pre ) 3183 3233 return $pre; 3184 3234 3185 $value = get_option( $key, $default);3235 $value = get_option( $option, $default ) ; 3186 3236 3187 return apply_filters( 'site_option_' . $key, $value );3237 return apply_filters( "site_option_{$option}", $value ); 3188 3238 } 3189 3239 3190 // expects $key, $value not to be SQL escaped 3191 function add_site_option( $key, $value ) { 3192 $value = apply_filters( 'pre_add_site_option_' . $key, $value ); 3193 $result = add_option($key, $value); 3194 do_action( "add_site_option_{$key}", $key, $value ); 3240 /** 3241 * Add a new site option. 3242 * 3243 * @see add_option() 3244 * @package WordPress 3245 * @subpackage Option 3246 * @since 2.8.0 3247 * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton 3248 * @uses apply_filters() Calls 'pre_add_site_option_$option' to allow overwriting 3249 * the option value in a plugin. 3250 * @uses do_action() Calls 'add_site_option' before adding the option. 3251 * @uses do_action() Calls 'added_site_option_$option' and 'added_site_option' hooks on success. 3252 * 3253 * @param string $option Name of option to add. Expects to not be SQL escaped. 3254 * @param mixed $value Optional. Option value, can be anything. 3255 * @return bool False if option was not added and true if option was added. 3256 */ 3257 function add_site_option( $option, $value ) { 3258 $value = apply_filters( "pre_add_site_option_{$option}", $value ); 3259 do_action( 'add_site_option', $option, $value ); 3260 $result = add_option( $option, $value ); 3261 if ( $result ) { 3262 do_action( "add_site_option_{$option}", $option, $value ); 3263 do_action( 'added_site_option', $option, $value ); 3264 } 3195 3265 return $result; 3196 3266 } 3197 3267 3198 function delete_site_option( $key ) { 3199 $result = delete_option($key); 3200 do_action( "delete_site_option_{$key}", $key ); 3268 /** 3269 * Removes site option by name. 3270 * 3271 * @see delete_option() 3272 * @package WordPress 3273 * @subpackage Option 3274 * @since 2.8.0 3275 * @uses do_action() Calls 'delete_site_option' before deleting the option. 3276 * @uses do_action() Calls 'delete_site_option_$option' and 'deleted_site_option' 3277 * hooks on success. 3278 * 3279 * @param string $option Name of option to remove. Expected to be SQL-escaped. 3280 * @return bool True, if succeed. False, if failure. 3281 */ 3282 function delete_site_option( $option ) { 3283 do_action( 'delete_site_option', $option ); 3284 $result = delete_option( $option ); 3285 if ( $result ) { 3286 do_action( "delete_site_option_{$option}", $option ); 3287 do_action( 'deleted_site_option', $option ); 3288 } 3201 3289 return $result; 3202 3290 } 3203 3291 3204 // expects $key, $value not to be SQL escaped 3205 function update_site_option( $key, $value ) { 3206 $oldvalue = get_site_option( $key ); 3207 $value = apply_filters( 'pre_update_site_option_' . $key, $value, $oldvalue ); 3208 $result = update_option($key, $value); 3209 do_action( "update_site_option_{$key}", $key, $value ); 3292 /** 3293 * Update the value of a site option that was already added. 3294 * 3295 * @see update_option() 3296 * @since 2.8.0 3297 * @package WordPress 3298 * @subpackage Option 3299 * @uses apply_filters() Calls 'pre_update_site_option_$option' to allow overwriting 3300 * the option value in a plugin. 3301 * @uses do_action() Calls 'update_site_option' before updating the option. 3302 * @uses do_action() Calls 'update_site_option_$option' and 'updated_site_option' hooks on success. 3303 * 3304 * @param string $option Option name. Expected to not be SQL-escaped 3305 * @param mixed $value Option value. 3306 * @return bool False if value was not updated and true if value was updated. 3307 */ 3308 function update_site_option( $option, $value ) { 3309 $old_value = get_site_option( $option ); 3310 $new_value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value ); 3311 3312 if ( $new_value === $old_value ) 3313 return false; 3314 3315 if ( false === $old_value ) 3316 return add_site_option( $option, $new_value ); 3317 3318 do_action( 'update_site_option', $option, $old_value, $new_value ); 3319 $result = update_option( $option, $new_value ); 3320 if ( $result ) { 3321 do_action( "update_site_option_{$option}", $option, $old_value, $new_value ); 3322 do_action( 'updated_site_option', $option, $old_value, $new_value ); 3323 } 3210 3324 return $result; 3211 3325 } 3212 3326 3213 3327 /** 3214 3328 * Delete a site transient 3215 3329 * 3216 * @since 2.890 3330 * @see delete_transient() 3331 * @since 2.9.0 3217 3332 * @package WordPress 3218 3333 * @subpackage Transient 3334 * @uses do_action() Calls 'delete_site_transient' before deleting the transient. 3335 * @uses do_action() Calls 'delete_site_transient_$transient' and 'deleted_site_transient' 3336 * hooks on success. 3219 3337 * 3220 3338 * @param string $transient Transient name. Expected to not be SQL-escaped 3221 3339 * @return bool true if successful, false otherwise 3222 3340 */ 3223 3341 function delete_site_transient($transient) { 3224 global $_wp_using_ext_object_cache , $wpdb;3342 global $_wp_using_ext_object_cache; 3225 3343 3344 do_action( 'delete_site_transient', $transient ); 3226 3345 if ( $_wp_using_ext_object_cache ) { 3227 return wp_cache_delete($transient, 'site-transient');3346 $result = wp_cache_delete( $transient, 'site-transient' ); 3228 3347 } else { 3229 $transient = '_site_transient_' . esc_sql($transient); 3230 return delete_site_option($transient); 3348 $result = delete_site_option( '_site_transient_' . esc_sql( $transient ) ); 3231 3349 } 3350 3351 if ( $result ) { 3352 do_action( "delete_site_transient_{$transient}", $transient ); 3353 do_action( 'deleted_site_transient', $transient ); 3354 } 3355 return $result; 3232 3356 } 3233 3357 3234 3358 /** … … 3236 3360 * 3237 3361 * If the transient does not exist or does not have a value, then the return value 3238 3362 * will be false. 3239 * 3363 * 3364 * @see get_transient() 3240 3365 * @since 2.9.0 3241 3366 * @package WordPress 3242 3367 * @subpackage Transient 3368 * @uses apply_filters() Calls 'pre_site_transient_$transient' to allow overwriting 3369 * the transient value in a plugin before checking the transient. 3370 * @uses apply_filters() Calls 'site_transient_$transient' to allow overwriting 3371 * the transient value in a plugin after checking the transient. 3243 3372 * 3244 3373 * @param string $transient Transient name. Expected to not be SQL-escaped 3245 3374 * @return mixed Value of transient 3246 3375 */ 3247 3376 function get_site_transient($transient) { 3248 global $_wp_using_ext_object_cache , $wpdb;3377 global $_wp_using_ext_object_cache; 3249 3378 3250 $pre = apply_filters( 'pre_site_transient_' . $transient, false );3379 $pre = apply_filters( "pre_site_transient_{$transient}", false ); 3251 3380 if ( false !== $pre ) 3252 3381 return $pre; 3253 3382 3254 3383 if ( $_wp_using_ext_object_cache ) { 3255 $value = wp_cache_get( $transient, 'site-transient');3384 $value = wp_cache_get( $transient, 'site-transient' ); 3256 3385 } else { 3257 $transient_option = '_site_transient_' . esc_sql($transient); 3258 $transient_timeout = '_site_transient_timeout_' . esc_sql($transient); 3259 if ( get_site_option($transient_timeout) < time() ) { 3260 delete_site_option($transient_option); 3261 delete_site_option($transient_timeout); 3386 $safe_transient = esc_sql( $transient ); 3387 $transient_option = '_site_transient_' . $safe_transient; 3388 $transient_timeout = '_site_transient_timeout_' . $safe_transient; 3389 if ( get_site_option( $transient_timeout ) < time() ) { 3390 delete_site_option( $transient_option ); 3391 delete_site_option( $transient_timeout ); 3262 3392 return false; 3263 3393 } 3264 3394 3265 $value = get_site_option( $transient_option);3395 $value = get_site_option( $transient_option ); 3266 3396 } 3267 3397 3268 return apply_filters( 'site_transient_' . $transient, $value);3398 return apply_filters( "site_transient_{$transient}", $value ); 3269 3399 } 3270 3400 3271 3401 /** … … 3274 3404 * You do not need to serialize values, if the value needs to be serialize, then 3275 3405 * it will be serialized before it is set. 3276 3406 * 3407 * @see set_transient() 3277 3408 * @since 2.9.0 3278 3409 * @package WordPress 3279 3410 * @subpackage Transient 3411 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' to allow overwriting 3412 * the transient value in a plugin. 3413 * @uses apply_filters() Calls 'pre_set_site_transient_expiration_$transient' to allow overwriting 3414 * the transient expiration in a plugin. 3415 * @uses do_action() Calls 'set_site_transient' before setting the transient. 3416 * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success. 3280 3417 * 3281 3418 * @param string $transient Transient name. Expected to not be SQL-escaped 3282 3419 * @param mixed $value Transient value. … … 3284 3421 * @return bool False if value was not set and true if value was set. 3285 3422 */ 3286 3423 function set_site_transient($transient, $value, $expiration = 0) { 3287 global $_wp_using_ext_object_cache , $wpdb;3424 global $_wp_using_ext_object_cache; 3288 3425 3426 $value = apply_filters( "pre_set_site_transient_{$transient}", $value, $expiration ); 3427 $expiration = apply_filters( "pre_set_site_transient_expiration_{$transient}", $expiration, $value ); 3428 3429 do_action( 'set_site_transient', $transient, $value, $expiration ); 3289 3430 if ( $_wp_using_ext_object_cache ) { 3290 returnwp_cache_set($transient, $value, 'site-transient', $expiration);3431 $result = wp_cache_set($transient, $value, 'site-transient', $expiration); 3291 3432 } else { 3292 3433 $transient_timeout = '_site_transient_timeout_' . $transient; 3293 3434 $transient = '_site_transient_' . $transient; 3294 3435 $safe_transient = esc_sql($transient); 3295 3436 if ( false === get_site_option( $safe_transient ) ) { 3296 3437 if ( 0 != $expiration ) 3297 add_site_option( $transient_timeout, time() + $expiration);3298 returnadd_site_option($transient, $value);3438 add_site_option( $transient_timeout, time() + $expiration ); 3439 $result = add_site_option($transient, $value); 3299 3440 } else { 3300 3441 if ( 0 != $expiration ) 3301 update_site_option( $transient_timeout, time() + $expiration);3302 return update_site_option($transient, $value);3442 update_site_option( $transient_timeout, time() + $expiration ); 3443 $result = update_site_option( $transient, $value ); 3303 3444 } 3304 3445 } 3446 if ( $result ) { 3447 do_action( "set_site_transient_{$transient}", $transient, $value, $expiration ); 3448 do_action( 'setted_site_transient', $transient, $value, $expiration ); 3449 } 3450 return $result; 3305 3451 } 3306 3452 3307 3453 /** … … 3593 3739 function _search_terms_tidy($t) { 3594 3740 return trim($t, "\"\'\n\r "); 3595 3741 } 3596 ?> 3742 ?> 3743 No newline at end of file
