WordPress.org

Make WordPress Core

Ticket #10788: 10788.diff

File 10788.diff, 36.1 KB (added by nacin, 8 years ago)

Against [12933]

  • wp-includes/functions.php

     
    289289}
    290290
    291291/**
    292  * Retrieve option value based on setting name.
     292 * Retrieve option value based on option name.
    293293 *
    294294 * If the option does not exist or does not have a value, then the return value
    295295 * will be false. This is useful to check whether you need to install an option
     
    302302 * name. You should not try to override special options, but you will not be
    303303 * prevented from doing so.
    304304 *
    305  * There is a second filter called 'option_$option' with the $option being
    306  * replaced with the option name. This gives the value as the only parameter.
    307  *
    308305 * If the option was serialized, when the option was added and, or updated, then
    309306 * it will be unserialized, when it is returned.
    310307 *
    311308 * @since 1.5.0
    312309 * @package WordPress
    313310 * @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.
    317315 *
    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
    319318 * @return mixed Value set for the option.
    320319 */
    321 function get_option( $setting, $default = false ) {
     320function get_option( $option, $default = false ) {
    322321        global $wpdb;
    323322
    324323        // Allow plugins to short-circuit options.
    325         $pre = apply_filters( 'pre_option_' . $setting, false );
     324        $pre = apply_filters( "pre_option_{$option}", false );
    326325        if ( false !== $pre )
    327326                return $pre;
    328327
     
    331330                $notoptions = array();
    332331        } else {
    333332                $notoptions = wp_cache_get( 'notoptions', 'options' );
    334                 if ( isset( $notoptions[$setting] ) )
     333                if ( isset( $notoptions[ $option ] ) )
    335334                        return $default;
    336335        }
    337336
    338337        $alloptions = wp_load_alloptions();
    339338
    340         if ( isset( $alloptions[$setting] ) ) {
    341                 $value = $alloptions[$setting];
     339        if ( isset( $alloptions[ $option ] ) ) {
     340                $value = $alloptions[ $option ];
    342341        } else {
    343                 $value = wp_cache_get( $setting, 'options' );
     342                $value = wp_cache_get( $option, 'options' );
    344343
    345344                if ( false === $value ) {
    346345                        if ( defined( 'WP_INSTALLING' ) )
    347346                                $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" );
    350349                        if ( defined( 'WP_INSTALLING' ) )
    351                                 $wpdb->suppress_errors($suppress);
     350                                $wpdb->suppress_errors( $suppress );
    352351
    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 ) ) {
    354354                                $value = $row->option_value;
    355                                 wp_cache_add( $setting, $value, 'options' );
     355                                wp_cache_add( $option, $value, 'options' );
    356356                        } else { // option does not exist, so we must cache its non-existence
    357                                 $notoptions[$setting] = true;
     357                                $notoptions[$option] = true;
    358358                                wp_cache_set( 'notoptions', $notoptions, 'options' );
    359359                                return $default;
    360360                        }
     
    362362        }
    363363
    364364        // If home is not set use siteurl.
    365         if ( 'home' == $setting && '' == $value )
     365        if ( 'home' == $option && '' == $value )
    366366                return get_option( 'siteurl' );
    367367
    368         if ( in_array( $setting, array('siteurl', 'home', 'category_base', 'tag_base') ) )
     368        if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) )
    369369                $value = untrailingslashit( $value );
    370370
    371         return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) );
     371        return apply_filters( "option_{$option}", maybe_unserialize( $value ) );
    372372}
    373373
    374374/**
     
    446446 * value, but you will not be able to set whether it is autoloaded. If you want
    447447 * to set whether an option autoloaded, then you need to use the add_option().
    448448 *
    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.
    454453 *
    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.
    458456 *
    459457 * @since 1.0.0
    460458 * @package WordPress
    461459 * @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.
    462464 *
    463  * @param string $option_name Option name. Expected to not be SQL-escaped
    464  * @param mixed $newvalue Option value.
     465 * @param string $option Option name. Expected to not be SQL-escaped
     466 * @param mixed $new_value New option value.
    465467 * @return bool False if value was not updated and true if value was updated.
    466468 */
    467 function update_option( $option_name, $newvalue ) {
     469function update_option( $option, $new_value ) {
    468470        global $wpdb;
    469471
    470         wp_protect_special_option( $option_name );
     472        wp_protect_special_option( $option );
    471473
    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 );
    474478
    475         $oldvalue = get_option( $safe_option_name );
    476 
    477         $newvalue = apply_filters( 'pre_update_option_' . $option_name, $newvalue, $oldvalue );
    478 
    479479        // If the new and old values are the same, no need to update.
    480         if ( $newvalue === $oldvalue )
     480        if ( $new_value === $old_value )
    481481                return false;
    482482
    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 );
    487485
    488486        $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 ] );
    491489                wp_cache_set( 'notoptions', $notoptions, 'options' );
    492490        }
    493491
    494         $_newvalue = $newvalue;
    495         $newvalue = maybe_serialize( $newvalue );
     492        $_new_value = $new_value;
     493        $new_value  = maybe_serialize( $new_value );
    496494
    497         do_action( 'update_option', $option_name, $oldvalue, $newvalue );
     495        do_action( 'update_option', $option, $old_value, $new_value );
    498496        $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;
    501499                wp_cache_set( 'alloptions', $alloptions, 'options' );
    502500        } else {
    503                 wp_cache_set( $option_name, $newvalue, 'options' );
     501                wp_cache_set( $option, $new_value, 'options' );
    504502        }
    505503
    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 ) );
    507505
    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 );
    512509        }
    513         return false;
     510        return $result;
    514511}
    515512
    516513/**
    517514 * Add a new option.
    518515 *
    519  * You do not need to serialize values, if the value needs to be serialize, then
     516 * You do not need to serialize values. If the value needs to be serialized, then
    520517 * it will be serialized before it is inserted into the database. Remember,
    521518 * resources can not be serialized or added as an option.
    522519 *
     
    526523 * options, the same as the ones which are protected and to not add options
    527524 * that were already added.
    528525 *
    529  * The filter named 'add_option_$optionname', with the $optionname being
    530  * replaced with the option's name, will be called. The hook should accept two
    531  * parameters, the first is the option name, and the second is the value.
    532  *
    533526 * @package WordPress
    534527 * @subpackage Option
    535528 * @since 1.0.0
    536529 * @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.
    537534 *
    538  * @param string $name Option name to add. Expects to NOT be SQL escaped.
     535 * @param string $option Name of option to add. Expects to not be SQL escaped.
    539536 * @param mixed $value Optional. Option value, can be anything.
    540537 * @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.
    543540 */
    544 function add_option( $name, $value = '', $deprecated = '', $autoload = 'yes' ) {
     541function add_option( $option, $value = '', $deprecated = '', $autoload = true ) {
    545542        if ( !empty( $deprecated ) )
    546543                _deprecated_argument( __FUNCTION__, '2.3' );
    547544
    548545        global $wpdb;
    549546
    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 );
    553549
     550        $value = sanitize_option( $option, $value );
     551        $value = apply_filters( "pre_add_option_{$option}", $value );
     552
    554553        // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
    555554        $notoptions = wp_cache_get( 'notoptions', 'options' );
    556         if ( !is_array( $notoptions ) || !isset( $notoptions[$name] ) )
     555        if ( !is_array( $notoptions ) || !isset( $notoptions[ $option ] ) )
    557556                if ( false !== get_option( $safe_name ) )
    558                         return;
     557                        return false;
    559558
    560559        $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 ) {
    564563                $alloptions = wp_load_alloptions();
    565                 $alloptions[$name] = $value;
     564                $alloptions[ $option ] = $value;
    566565                wp_cache_set( 'alloptions', $alloptions, 'options' );
    567566        } else {
    568                 wp_cache_set( $name, $value, 'options' );
     567                wp_cache_set( $option, $value, 'options' );
    569568        }
    570569
    571570        // This option exists now
    572571        $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 ] );
    575574                wp_cache_set( 'notoptions', $notoptions, 'options' );
    576575        }
    577576
    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 ) );
    579578
    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 );
    582581
    583         return;
     582        return true;
    584583}
    585584
    586585/**
    587  * Removes option by name and prevents removal of protected WordPress options.
     586 * Removes option by name. Prevents removal of protected WordPress options.
    588587 *
    589588 * @package WordPress
    590589 * @subpackage Option
    591590 * @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.
    592594 *
    593  * @param string $name Option name to remove.
    594  * @return bool True, if succeed. False, if failure.
     595 * @param string $option Name of option to remove. Expected to be SQL-escaped.
     596 * @return bool True, on success. False, on failure.
    595597 */
    596 function delete_option( $name ) {
     598function delete_option( $option ) {
    597599        global $wpdb;
    598600
    599         wp_protect_special_option( $name );
     601        wp_protect_special_option( $option );
    600602
    601603        // 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 ) )
    605607                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 ) {
    610612                $alloptions = wp_load_alloptions();
    611                 if ( isset( $alloptions[$name] ) ) {
    612                         unset( $alloptions[$name] );
     613                if ( isset( $alloptions[ $option ] ) ) {
     614                        unset( $alloptions[ $option ] );
    613615                        wp_cache_set( 'alloptions', $alloptions, 'options' );
    614616                }
    615617        } else {
    616                 wp_cache_delete( $name, 'options' );
     618                wp_cache_delete( $option, 'options' );
    617619        }
    618         do_action( 'deleted_option', $name );
     620        do_action( "delete_option_{$option}", $option );
     621        do_action( 'deleted_option', $option );
    619622        return true;
    620623}
    621624
     
    625628 * @since 2.8.0
    626629 * @package WordPress
    627630 * @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.
    628634 *
    629635 * @param string $transient Transient name. Expected to not be SQL-escaped
    630636 * @return bool true if successful, false otherwise
    631637 */
    632 function delete_transient($transient) {
    633         global $_wp_using_ext_object_cache, $wpdb;
     638function delete_transient( $transient ) {
     639        global $_wp_using_ext_object_cache;
    634640
    635641    do_action( 'delete_transient_' . $transient );
    636642
    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 );
    642651        }
     652        return $result;
    643653}
    644654
    645655/**
     
    651661 * @since 2.8.0
    652662 * @package WordPress
    653663 * @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.
    654668 *
    655669 * @param string $transient Transient name. Expected to not be SQL-escaped
    656670 * @return mixed Value of transient
    657671 */
    658 function get_transient($transient) {
    659         global $_wp_using_ext_object_cache, $wpdb;
     672function get_transient( $transient ) {
     673        global $_wp_using_ext_object_cache;
    660674
    661         $pre = apply_filters( 'pre_transient_' . $transient, false );
     675        $pre = apply_filters( "pre_transient_{$transient}", false );
    662676        if ( false !== $pre )
    663677                return $pre;
    664678
    665679        if ( $_wp_using_ext_object_cache ) {
    666                 $value = wp_cache_get($transient, 'transient');
     680                $value = wp_cache_get( $transient, 'transient' );
    667681        } else {
    668                 $transient_option = '_transient_' . esc_sql($transient);
     682                $safe_transient = esc_sql( $transient );
     683                $transient_option = '_transient_' . $safe_transient;
    669684                // If option is not in alloptions, it is not autoloaded and thus has a timeout
    670685                $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 );
    676691                                return false;
    677692                        }
    678693                }
    679694
    680                 $value = get_option($transient_option);
     695                $value = get_option( $transient_option );
    681696        }
    682697
    683         return apply_filters('transient_' . $transient, $value);
     698        return apply_filters( "transient_{$transient}", $value );
    684699}
    685700
    686701/**
    687702 * Set/update the value of a transient
    688703 *
    689  * You do not need to serialize values, if the value needs to be serialize, then
     704 * You do not need to serialize values, if the value needs to be serialized, then
    690705 * it will be serialized before it is set.
    691706 *
    692707 * @since 2.8.0
    693708 * @package WordPress
    694709 * @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.
    695716 *
    696717 * @param string $transient Transient name. Expected to not be SQL-escaped
    697718 * @param mixed $value Transient value.
    698719 * @param int $expiration Time until expiration in seconds, default 0
    699720 * @return bool False if value was not set and true if value was set.
    700721 */
    701 function set_transient($transient, $value, $expiration = 0) {
    702         global $_wp_using_ext_object_cache, $wpdb;
     722function set_transient( $transient, $value, $expiration = 0 ) {
     723        global $_wp_using_ext_object_cache;
    703724
    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 );
    705727
     728        do_action( 'set_transient', $transient, $value, $expiration );
    706729        if ( $_wp_using_ext_object_cache ) {
    707                 return wp_cache_set($transient, $value, 'transient', $expiration);
     730                $result = wp_cache_set( $transient, $value, 'transient', $expiration );
    708731        } else {
    709732                $transient_timeout = '_transient_timeout_' . $transient;
    710733                $transient = '_transient_' . $transient;
    711                 $safe_transient = esc_sql($transient);
     734                $safe_transient = esc_sql( $transient );
    712735                if ( false === get_option( $safe_transient ) ) {
    713736                        $autoload = 'yes';
    714737                        if ( 0 != $expiration ) {
    715738                                $autoload = 'no';
    716                                 add_option($transient_timeout, time() + $expiration, '', 'no');
     739                                add_option( $transient_timeout, time() + $expiration, '', 'no' );
    717740                        }
    718                         return add_option($transient, $value, '', $autoload);
     741                        $result = add_option( $transient, $value, '', $autoload );
    719742                } else {
    720743                        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 );
    723746                }
    724747        }
     748        if ( $result ) {
     749                do_action( "set_transient_{$transient}", $transient, $value, $expiration );
     750                do_action( 'setted_transient', $transient, $value, $expiration );
     751        }
     752        return $result;
    725753}
    726754
    727755/**
     
    11451173                                $type = $headers['content-type'];
    11461174                                $allowed_types = array( 'video', 'audio' );
    11471175
    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
    11501177                                $url_parts = parse_url( $url );
    11511178                                $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
    11521179                                if ( !empty( $extension ) ) {
     
    32953322        return $current_suspend;
    32963323}
    32973324
    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 */
     3342function get_site_option( $option, $default = false, $use_cache = true ) {
    32993343        global $wpdb;
    33003344
    33013345        // 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 );
    33033347        if ( false !== $pre )
    33043348                return $pre;
    33053349
    33063350        if ( !is_multisite() ) {
    3307                 $value = get_option($key, $default);
     3351                $value = get_option( $option, $default );
    33083352        } else {
    3309                 $cache_key = "{$wpdb->siteid}:$key";
     3353                $cache_key = "{$wpdb->siteid}:$option";
    33103354
    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 );
    33133357
    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) );
    33153359
    33163360                if ( is_null($value) )
    33173361                        $value = $default;
     
    33213365                wp_cache_set( $cache_key, $value, 'site-options' );
    33223366        }
    33233367
    3324         return apply_filters( 'site_option_' . $key, $value );
     3368        return apply_filters( "site_option_{$option}", $value );
    33253369}
    33263370
    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 */
     3387function add_site_option( $option, $value ) {
    33293388        global $wpdb;
    33303389
    3331         $value = apply_filters( 'pre_add_site_option_' . $key, $value );
     3390        $value = apply_filters( "pre_add_site_option_{$option}", $value );
    33323391
    33333392        if ( !is_multisite() ) {
    3334                 $result =  add_option($key, $value);
     3393                $result = add_option( $option, $value );
    33353394        } else {
    3336                 $cache_key = "{$wpdb->siteid}:$key";
     3395                $cache_key = "{$wpdb->siteid}:$option";
    33373396
    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 );
    33403399
    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' );
    33433402
    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        }
    33453406
    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 );
    33473410        }
    3348 
    3349         do_action( "add_site_option_{$key}", $key, $value );
    3350         do_action( "add_site_option", $key, $value );
    3351 
    33523411        return $result;
    33533412}
    33543413
    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 */
     3430function delete_site_option( $option ) {
    33563431        global $wpdb;
    33573432
    3358         //wpmu_protect_special_option( $key ); @todo
     3433        //wpmu_protect_special_option( $option ); @todo
    33593434
    3360         do_action( 'pre_delete_site_option_' . $key );
     3435        do_action( 'pre_delete_site_option', $option );
     3436        do_action( "pre_delete_site_option_{$option}" );
    33613437
    33623438        if ( !is_multisite() ) {
    3363                 $result = delete_option($key);
     3439                $result = delete_option( $option );
    33643440        } 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 )
    33673443                        return false;
    3368                 $cache_key = "{$wpdb->siteid}:$key";
     3444                $cache_key = "{$wpdb->siteid}:$option";
    33693445                wp_cache_delete( $cache_key, 'site-options' );
    33703446
    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 ) );
    33723448        }
    33733449
    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        }
    33763454        return $result;
    33773455}
    33783456
    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 */
     3472function update_site_option( $option, $new_value ) {
    33813473        global $wpdb;
    33823474
    3383         $oldvalue = 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 );
    33853477
    3386         if ( $value == $oldvalue )
     3478        if ( $new_value === $old_value )
    33873479                return false;
    33883480
    33893481        if ( !is_multisite() ) {
    3390                 $result = update_option($key, $value);
     3482                $result = update_option( $option, $new_value );
    33913483        } else {
    3392                 $cache_key = "{$wpdb->siteid}:$key";
     3484                $cache_key = "{$wpdb->siteid}:$option";
    33933485
    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 );
    33983488
    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 ) );
    34013494        }
    34023495
    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        }
    34053500        return $result;
    34063501}
    34073502
    34083503/**
    34093504 * Delete a site transient
    34103505 *
    3411  * @since 2.890
     3506 * @see delete_transient()
     3507 * @since 2.9.0
    34123508 * @package WordPress
    34133509 * @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.
    34143513 *
    34153514 * @param string $transient Transient name. Expected to not be SQL-escaped
    3416  * @return bool true if successful, false otherwise
     3515 * @return bool True if successful, false otherwise
    34173516 */
    3418 function delete_site_transient($transient) {
    3419         global $_wp_using_ext_object_cache, $wpdb;
     3517function delete_site_transient( $transient ) {
     3518        global $_wp_using_ext_object_cache;
    34203519
    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 );
    34263529        }
     3530        return $result;
    34273531}
    34283532
    34293533/**
     
    34323536 * If the transient does not exist or does not have a value, then the return value
    34333537 * will be false.
    34343538 *
     3539 * @see get_transient()
    34353540 * @since 2.9.0
    34363541 * @package WordPress
    34373542 * @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.
    34383547 *
    34393548 * @param string $transient Transient name. Expected to not be SQL-escaped
    34403549 * @return mixed Value of transient
    34413550 */
    34423551function get_site_transient($transient) {
    3443         global $_wp_using_ext_object_cache, $wpdb;
     3552        global $_wp_using_ext_object_cache;
    34443553
    3445         $pre = apply_filters( 'pre_site_transient_' . $transient, false );
     3554        $pre = apply_filters( "pre_site_transient_{$transient}", false );
    34463555        if ( false !== $pre )
    34473556                return $pre;
    34483557
    34493558        if ( $_wp_using_ext_object_cache ) {
    3450                 $value = wp_cache_get($transient, 'site-transient');
     3559                $value = wp_cache_get( $transient, 'site-transient' );
    34513560        } 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_option = '_site_transient_timeout_' . $safe_transient;
     3564                $timeout = get_site_option( $transient_timeout_option );
    34553565                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 );
    34583568                        return false;
    34593569                }
    34603570
    3461                 $value = get_site_option($transient_option);
     3571                $value = get_site_option( $transient_option );
    34623572        }
    34633573
    3464         return apply_filters('site_transient_' . $transient, $value);
     3574        return apply_filters( "site_transient_{$transient}", $value );
    34653575}
    34663576
    34673577/**
     
    34703580 * You do not need to serialize values, if the value needs to be serialize, then
    34713581 * it will be serialized before it is set.
    34723582 *
     3583 * @see set_transient()
    34733584 * @since 2.9.0
    34743585 * @package WordPress
    34753586 * @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.
    34763593 *
    34773594 * @param string $transient Transient name. Expected to not be SQL-escaped
    34783595 * @param mixed $value Transient value.
     
    34803597 * @return bool False if value was not set and true if value was set.
    34813598 */
    34823599function set_site_transient($transient, $value, $expiration = 0) {
    3483         global $_wp_using_ext_object_cache, $wpdb;
     3600        global $_wp_using_ext_object_cache;
    34843601
     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 );
    34853606        if ( $_wp_using_ext_object_cache ) {
    3486                 return wp_cache_set($transient, $value, 'site-transient', $expiration);
     3607                $result = wp_cache_set($transient, $value, 'site-transient', $expiration);
    34873608        } else {
    34883609                $transient_timeout = '_site_transient_timeout_' . $transient;
    34893610                $transient = '_site_transient_' . $transient;
    34903611                $safe_transient = esc_sql($transient);
    34913612                if ( false === get_site_option( $safe_transient ) ) {
    34923613                        if ( 0 != $expiration )
    3493                                 add_site_option($transient_timeout, time() + $expiration);
    3494                         return add_site_option($transient, $value);
     3614                                add_site_option( $transient_timeout, time() + $expiration );
     3615                        $result = add_site_option($transient, $value);
    34953616                } else {
    34963617                        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 );
    34993620                }
    35003621        }
     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;
    35013627}
    35023628
    35033629/**