Make WordPress Core


Ignore:
Timestamp:
12/19/2011 11:15:32 PM (13 years ago)
Author:
ryan
Message:

Move option, setting, and transient functions from functions.php to option.php. see #19552

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r19593 r19602  
    55 * @package WordPress
    66 */
     7
     8require( ABSPATH . WPINC . '/option.php' );
    79
    810/**
     
    302304    else
    303305        return true;
    304 }
    305 
    306 /**
    307  * Retrieve option value based on name of option.
    308  *
    309  * If the option does not exist or does not have a value, then the return value
    310  * will be false. This is useful to check whether you need to install an option
    311  * and is commonly used during installation of plugin options and to test
    312  * whether upgrading is required.
    313  *
    314  * If the option was serialized then it will be unserialized when it is returned.
    315  *
    316  * @since 1.5.0
    317  * @package WordPress
    318  * @subpackage Option
    319  * @uses apply_filters() Calls 'pre_option_$option' before checking the option.
    320  *  Any value other than false will "short-circuit" the retrieval of the option
    321  *  and return the returned value. You should not try to override special options,
    322  *  but you will not be prevented from doing so.
    323  * @uses apply_filters() Calls 'option_$option', after checking the option, with
    324  *  the option value.
    325  *
    326  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
    327  * @param mixed $default Optional. Default value to return if the option does not exist.
    328  * @return mixed Value set for the option.
    329  */
    330 function get_option( $option, $default = false ) {
    331     global $wpdb;
    332 
    333     // Allow plugins to short-circuit options.
    334     $pre = apply_filters( 'pre_option_' . $option, false );
    335     if ( false !== $pre )
    336         return $pre;
    337 
    338     $option = trim($option);
    339     if ( empty($option) )
    340         return false;
    341 
    342     if ( defined( 'WP_SETUP_CONFIG' ) )
    343         return false;
    344 
    345     if ( ! defined( 'WP_INSTALLING' ) ) {
    346         // prevent non-existent options from triggering multiple queries
    347         $notoptions = wp_cache_get( 'notoptions', 'options' );
    348         if ( isset( $notoptions[$option] ) )
    349             return $default;
    350 
    351         $alloptions = wp_load_alloptions();
    352 
    353         if ( isset( $alloptions[$option] ) ) {
    354             $value = $alloptions[$option];
    355         } else {
    356             $value = wp_cache_get( $option, 'options' );
    357 
    358             if ( false === $value ) {
    359                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
    360 
    361                 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
    362                 if ( is_object( $row ) ) {
    363                     $value = $row->option_value;
    364                     wp_cache_add( $option, $value, 'options' );
    365                 } else { // option does not exist, so we must cache its non-existence
    366                     $notoptions[$option] = true;
    367                     wp_cache_set( 'notoptions', $notoptions, 'options' );
    368                     return $default;
    369                 }
    370             }
    371         }
    372     } else {
    373         $suppress = $wpdb->suppress_errors();
    374         $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
    375         $wpdb->suppress_errors( $suppress );
    376         if ( is_object( $row ) )
    377             $value = $row->option_value;
    378         else
    379             return $default;
    380     }
    381 
    382     // If home is not set use siteurl.
    383     if ( 'home' == $option && '' == $value )
    384         return get_option( 'siteurl' );
    385 
    386     if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
    387         $value = untrailingslashit( $value );
    388 
    389     return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
    390 }
    391 
    392 /**
    393  * Protect WordPress special option from being modified.
    394  *
    395  * Will die if $option is in protected list. Protected options are 'alloptions'
    396  * and 'notoptions' options.
    397  *
    398  * @since 2.2.0
    399  * @package WordPress
    400  * @subpackage Option
    401  *
    402  * @param string $option Option name.
    403  */
    404 function wp_protect_special_option( $option ) {
    405     $protected = array( 'alloptions', 'notoptions' );
    406     if ( in_array( $option, $protected ) )
    407         wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
    408 }
    409 
    410 /**
    411  * Print option value after sanitizing for forms.
    412  *
    413  * @uses attr Sanitizes value.
    414  * @since 1.5.0
    415  * @package WordPress
    416  * @subpackage Option
    417  *
    418  * @param string $option Option name.
    419  */
    420 function form_option( $option ) {
    421     echo esc_attr( get_option( $option ) );
    422 }
    423 
    424 /**
    425  * Loads and caches all autoloaded options, if available or all options.
    426  *
    427  * @since 2.2.0
    428  * @package WordPress
    429  * @subpackage Option
    430  *
    431  * @return array List of all options.
    432  */
    433 function wp_load_alloptions() {
    434     global $wpdb;
    435 
    436     if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
    437         $alloptions = wp_cache_get( 'alloptions', 'options' );
    438     else
    439         $alloptions = false;
    440 
    441     if ( !$alloptions ) {
    442         $suppress = $wpdb->suppress_errors();
    443         if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
    444             $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
    445         $wpdb->suppress_errors($suppress);
    446         $alloptions = array();
    447         foreach ( (array) $alloptions_db as $o ) {
    448             $alloptions[$o->option_name] = $o->option_value;
    449         }
    450         if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
    451             wp_cache_add( 'alloptions', $alloptions, 'options' );
    452     }
    453 
    454     return $alloptions;
    455 }
    456 
    457 /**
    458  * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
    459  *
    460  * @since 3.0.0
    461  * @package WordPress
    462  * @subpackage Option
    463  *
    464  * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
    465  */
    466 function wp_load_core_site_options( $site_id = null ) {
    467     global $wpdb, $_wp_using_ext_object_cache;
    468 
    469     if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
    470         return;
    471 
    472     if ( empty($site_id) )
    473         $site_id = $wpdb->siteid;
    474 
    475     $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled' );
    476 
    477     $core_options_in = "'" . implode("', '", $core_options) . "'";
    478     $options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) );
    479 
    480     foreach ( $options as $option ) {
    481         $key = $option->meta_key;
    482         $cache_key = "{$site_id}:$key";
    483         $option->meta_value = maybe_unserialize( $option->meta_value );
    484 
    485         wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
    486     }
    487 }
    488 
    489 /**
    490  * Update the value of an option that was already added.
    491  *
    492  * You do not need to serialize values. If the value needs to be serialized, then
    493  * it will be serialized before it is inserted into the database. Remember,
    494  * resources can not be serialized or added as an option.
    495  *
    496  * If the option does not exist, then the option will be added with the option
    497  * value, but you will not be able to set whether it is autoloaded. If you want
    498  * to set whether an option is autoloaded, then you need to use the add_option().
    499  *
    500  * @since 1.0.0
    501  * @package WordPress
    502  * @subpackage Option
    503  *
    504  * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
    505  *  option value to be stored.
    506  * @uses do_action() Calls 'update_option' hook before updating the option.
    507  * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
    508  *
    509  * @param string $option Option name. Expected to not be SQL-escaped.
    510  * @param mixed $newvalue Option value. Expected to not be SQL-escaped.
    511  * @return bool False if value was not updated and true if value was updated.
    512  */
    513 function update_option( $option, $newvalue ) {
    514     global $wpdb;
    515 
    516     $option = trim($option);
    517     if ( empty($option) )
    518         return false;
    519 
    520     wp_protect_special_option( $option );
    521 
    522     if ( is_object($newvalue) )
    523         $newvalue = clone $newvalue;
    524 
    525     $newvalue = sanitize_option( $option, $newvalue );
    526     $oldvalue = get_option( $option );
    527     $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );
    528 
    529     // If the new and old values are the same, no need to update.
    530     if ( $newvalue === $oldvalue )
    531         return false;
    532 
    533     if ( false === $oldvalue )
    534         return add_option( $option, $newvalue );
    535 
    536     $notoptions = wp_cache_get( 'notoptions', 'options' );
    537     if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
    538         unset( $notoptions[$option] );
    539         wp_cache_set( 'notoptions', $notoptions, 'options' );
    540     }
    541 
    542     $_newvalue = $newvalue;
    543     $newvalue = maybe_serialize( $newvalue );
    544 
    545     do_action( 'update_option', $option, $oldvalue, $_newvalue );
    546     if ( ! defined( 'WP_INSTALLING' ) ) {
    547         $alloptions = wp_load_alloptions();
    548         if ( isset( $alloptions[$option] ) ) {
    549             $alloptions[$option] = $_newvalue;
    550             wp_cache_set( 'alloptions', $alloptions, 'options' );
    551         } else {
    552             wp_cache_set( $option, $_newvalue, 'options' );
    553         }
    554     }
    555 
    556     $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );
    557 
    558     if ( $result ) {
    559         do_action( "update_option_{$option}", $oldvalue, $_newvalue );
    560         do_action( 'updated_option', $option, $oldvalue, $_newvalue );
    561         return true;
    562     }
    563     return false;
    564 }
    565 
    566 /**
    567  * Add a new option.
    568  *
    569  * You do not need to serialize values. If the value needs to be serialized, then
    570  * it will be serialized before it is inserted into the database. Remember,
    571  * resources can not be serialized or added as an option.
    572  *
    573  * You can create options without values and then update the values later.
    574  * Existing options will not be updated and checks are performed to ensure that you
    575  * aren't adding a protected WordPress option. Care should be taken to not name
    576  * options the same as the ones which are protected.
    577  *
    578  * @package WordPress
    579  * @subpackage Option
    580  * @since 1.0.0
    581  *
    582  * @uses do_action() Calls 'add_option' hook before adding the option.
    583  * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
    584  *
    585  * @param string $option Name of option to add. Expected to not be SQL-escaped.
    586  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
    587  * @param mixed $deprecated Optional. Description. Not used anymore.
    588  * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
    589  * @return bool False if option was not added and true if option was added.
    590  */
    591 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
    592     global $wpdb;
    593 
    594     if ( !empty( $deprecated ) )
    595         _deprecated_argument( __FUNCTION__, '2.3' );
    596 
    597     $option = trim($option);
    598     if ( empty($option) )
    599         return false;
    600 
    601     wp_protect_special_option( $option );
    602 
    603     if ( is_object($value) )
    604         $value = clone $value;
    605 
    606     $value = sanitize_option( $option, $value );
    607 
    608     // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
    609     $notoptions = wp_cache_get( 'notoptions', 'options' );
    610     if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
    611         if ( false !== get_option( $option ) )
    612             return false;
    613 
    614     $_value = $value;
    615     $value = maybe_serialize( $value );
    616     $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
    617     do_action( 'add_option', $option, $_value );
    618     if ( ! defined( 'WP_INSTALLING' ) ) {
    619         if ( 'yes' == $autoload ) {
    620             $alloptions = wp_load_alloptions();
    621             $alloptions[$option] = $value;
    622             wp_cache_set( 'alloptions', $alloptions, 'options' );
    623         } else {
    624             wp_cache_set( $option, $value, 'options' );
    625         }
    626     }
    627 
    628     // This option exists now
    629     $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
    630     if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
    631         unset( $notoptions[$option] );
    632         wp_cache_set( 'notoptions', $notoptions, 'options' );
    633     }
    634 
    635     $result = $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 ) );
    636 
    637     if ( $result ) {
    638         do_action( "add_option_{$option}", $option, $_value );
    639         do_action( 'added_option', $option, $_value );
    640         return true;
    641     }
    642     return false;
    643 }
    644 
    645 /**
    646  * Removes option by name. Prevents removal of protected WordPress options.
    647  *
    648  * @package WordPress
    649  * @subpackage Option
    650  * @since 1.2.0
    651  *
    652  * @uses do_action() Calls 'delete_option' hook before option is deleted.
    653  * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
    654  *
    655  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
    656  * @return bool True, if option is successfully deleted. False on failure.
    657  */
    658 function delete_option( $option ) {
    659     global $wpdb;
    660 
    661     wp_protect_special_option( $option );
    662 
    663     // Get the ID, if no ID then return
    664     $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
    665     if ( is_null( $row ) )
    666         return false;
    667     do_action( 'delete_option', $option );
    668     $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $option) );
    669     if ( ! defined( 'WP_INSTALLING' ) ) {
    670         if ( 'yes' == $row->autoload ) {
    671             $alloptions = wp_load_alloptions();
    672             if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
    673                 unset( $alloptions[$option] );
    674                 wp_cache_set( 'alloptions', $alloptions, 'options' );
    675             }
    676         } else {
    677             wp_cache_delete( $option, 'options' );
    678         }
    679     }
    680     if ( $result ) {
    681         do_action( "delete_option_$option", $option );
    682         do_action( 'deleted_option', $option );
    683         return true;
    684     }
    685     return false;
    686 }
    687 
    688 /**
    689  * Delete a transient.
    690  *
    691  * @since 2.8.0
    692  * @package WordPress
    693  * @subpackage Transient
    694  *
    695  * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
    696  * @uses do_action() Calls 'deleted_transient' hook on success.
    697  *
    698  * @param string $transient Transient name. Expected to not be SQL-escaped.
    699  * @return bool true if successful, false otherwise
    700  */
    701 function delete_transient( $transient ) {
    702     global $_wp_using_ext_object_cache;
    703 
    704     do_action( 'delete_transient_' . $transient, $transient );
    705 
    706     if ( $_wp_using_ext_object_cache ) {
    707         $result = wp_cache_delete( $transient, 'transient' );
    708     } else {
    709         $option_timeout = '_transient_timeout_' . $transient;
    710         $option = '_transient_' . $transient;
    711         $result = delete_option( $option );
    712         if ( $result )
    713             delete_option( $option_timeout );
    714     }
    715 
    716     if ( $result )
    717         do_action( 'deleted_transient', $transient );
    718     return $result;
    719 }
    720 
    721 /**
    722  * Get the value of a transient.
    723  *
    724  * If the transient does not exist or does not have a value, then the return value
    725  * will be false.
    726  *
    727  * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
    728  *  Any value other than false will "short-circuit" the retrieval of the transient
    729  *  and return the returned value.
    730  * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
    731  *  the transient value.
    732  *
    733  * @since 2.8.0
    734  * @package WordPress
    735  * @subpackage Transient
    736  *
    737  * @param string $transient Transient name. Expected to not be SQL-escaped
    738  * @return mixed Value of transient
    739  */
    740 function get_transient( $transient ) {
    741     global $_wp_using_ext_object_cache;
    742 
    743     $pre = apply_filters( 'pre_transient_' . $transient, false );
    744     if ( false !== $pre )
    745         return $pre;
    746 
    747     if ( $_wp_using_ext_object_cache ) {
    748         $value = wp_cache_get( $transient, 'transient' );
    749     } else {
    750         $transient_option = '_transient_' . $transient;
    751         if ( ! defined( 'WP_INSTALLING' ) ) {
    752             // If option is not in alloptions, it is not autoloaded and thus has a timeout
    753             $alloptions = wp_load_alloptions();
    754             if ( !isset( $alloptions[$transient_option] ) ) {
    755                 $transient_timeout = '_transient_timeout_' . $transient;
    756                 if ( get_option( $transient_timeout ) < time() ) {
    757                     delete_option( $transient_option  );
    758                     delete_option( $transient_timeout );
    759                     return false;
    760                 }
    761             }
    762         }
    763 
    764         $value = get_option( $transient_option );
    765     }
    766 
    767     return apply_filters( 'transient_' . $transient, $value );
    768 }
    769 
    770 /**
    771  * Set/update the value of a transient.
    772  *
    773  * You do not need to serialize values. If the value needs to be serialized, then
    774  * it will be serialized before it is set.
    775  *
    776  * @since 2.8.0
    777  * @package WordPress
    778  * @subpackage Transient
    779  *
    780  * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
    781  *  transient value to be stored.
    782  * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
    783  *
    784  * @param string $transient Transient name. Expected to not be SQL-escaped.
    785  * @param mixed $value Transient value. Expected to not be SQL-escaped.
    786  * @param int $expiration Time until expiration in seconds, default 0
    787  * @return bool False if value was not set and true if value was set.
    788  */
    789 function set_transient( $transient, $value, $expiration = 0 ) {
    790     global $_wp_using_ext_object_cache;
    791 
    792     $value = apply_filters( 'pre_set_transient_' . $transient, $value );
    793 
    794     if ( $_wp_using_ext_object_cache ) {
    795         $result = wp_cache_set( $transient, $value, 'transient', $expiration );
    796     } else {
    797         $transient_timeout = '_transient_timeout_' . $transient;
    798         $transient = '_transient_' . $transient;
    799         if ( false === get_option( $transient ) ) {
    800             $autoload = 'yes';
    801             if ( $expiration ) {
    802                 $autoload = 'no';
    803                 add_option( $transient_timeout, time() + $expiration, '', 'no' );
    804             }
    805             $result = add_option( $transient, $value, '', $autoload );
    806         } else {
    807             if ( $expiration )
    808                 update_option( $transient_timeout, time() + $expiration );
    809             $result = update_option( $transient, $value );
    810         }
    811     }
    812     if ( $result ) {
    813         do_action( 'set_transient_' . $transient );
    814         do_action( 'setted_transient', $transient );
    815     }
    816     return $result;
    817 }
    818 
    819 /**
    820  * Saves and restores user interface settings stored in a cookie.
    821  *
    822  * Checks if the current user-settings cookie is updated and stores it. When no
    823  * cookie exists (different browser used), adds the last saved cookie restoring
    824  * the settings.
    825  *
    826  * @package WordPress
    827  * @subpackage Option
    828  * @since 2.7.0
    829  */
    830 function wp_user_settings() {
    831 
    832     if ( ! is_admin() )
    833         return;
    834 
    835     if ( defined('DOING_AJAX') )
    836         return;
    837 
    838     if ( ! $user = wp_get_current_user() )
    839         return;
    840 
    841     $settings = get_user_option( 'user-settings', $user->ID );
    842 
    843     if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
    844         $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
    845 
    846         if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
    847             if ( $cookie == $settings )
    848                 return;
    849 
    850             $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
    851             $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
    852 
    853             if ( $saved > $last_time ) {
    854                 update_user_option( $user->ID, 'user-settings', $cookie, false );
    855                 update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
    856                 return;
    857             }
    858         }
    859     }
    860 
    861     setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
    862     setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
    863     $_COOKIE['wp-settings-' . $user->ID] = $settings;
    864 }
    865 
    866 /**
    867  * Retrieve user interface setting value based on setting name.
    868  *
    869  * @package WordPress
    870  * @subpackage Option
    871  * @since 2.7.0
    872  *
    873  * @param string $name The name of the setting.
    874  * @param string $default Optional default value to return when $name is not set.
    875  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
    876  */
    877 function get_user_setting( $name, $default = false ) {
    878 
    879     $all = get_all_user_settings();
    880 
    881     return isset($all[$name]) ? $all[$name] : $default;
    882 }
    883 
    884 /**
    885  * Add or update user interface setting.
    886  *
    887  * Both $name and $value can contain only ASCII letters, numbers and underscores.
    888  * This function has to be used before any output has started as it calls setcookie().
    889  *
    890  * @package WordPress
    891  * @subpackage Option
    892  * @since 2.8.0
    893  *
    894  * @param string $name The name of the setting.
    895  * @param string $value The value for the setting.
    896  * @return bool true if set successfully/false if not.
    897  */
    898 function set_user_setting( $name, $value ) {
    899 
    900     if ( headers_sent() )
    901         return false;
    902 
    903     $all = get_all_user_settings();
    904     $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
    905 
    906     if ( empty($name) )
    907         return false;
    908 
    909     $all[$name] = $value;
    910 
    911     return wp_set_all_user_settings($all);
    912 }
    913 
    914 /**
    915  * Delete user interface settings.
    916  *
    917  * Deleting settings would reset them to the defaults.
    918  * This function has to be used before any output has started as it calls setcookie().
    919  *
    920  * @package WordPress
    921  * @subpackage Option
    922  * @since 2.7.0
    923  *
    924  * @param mixed $names The name or array of names of the setting to be deleted.
    925  * @return bool true if deleted successfully/false if not.
    926  */
    927 function delete_user_setting( $names ) {
    928 
    929     if ( headers_sent() )
    930         return false;
    931 
    932     $all = get_all_user_settings();
    933     $names = (array) $names;
    934 
    935     foreach ( $names as $name ) {
    936         if ( isset($all[$name]) ) {
    937             unset($all[$name]);
    938             $deleted = true;
    939         }
    940     }
    941 
    942     if ( isset($deleted) )
    943         return wp_set_all_user_settings($all);
    944 
    945     return false;
    946 }
    947 
    948 /**
    949  * Retrieve all user interface settings.
    950  *
    951  * @package WordPress
    952  * @subpackage Option
    953  * @since 2.7.0
    954  *
    955  * @return array the last saved user settings or empty array.
    956  */
    957 function get_all_user_settings() {
    958     global $_updated_user_settings;
    959 
    960     if ( ! $user = wp_get_current_user() )
    961         return array();
    962 
    963     if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
    964         return $_updated_user_settings;
    965 
    966     $all = array();
    967     if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
    968         $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
    969 
    970         if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
    971             parse_str($cookie, $all);
    972 
    973     } else {
    974         $option = get_user_option('user-settings', $user->ID);
    975         if ( $option && is_string($option) )
    976             parse_str( $option, $all );
    977     }
    978 
    979     return $all;
    980 }
    981 
    982 /**
    983  * Private. Set all user interface settings.
    984  *
    985  * @package WordPress
    986  * @subpackage Option
    987  * @since 2.8.0
    988  *
    989  * @param unknown $all
    990  * @return bool
    991  */
    992 function wp_set_all_user_settings($all) {
    993     global $_updated_user_settings;
    994 
    995     if ( ! $user = wp_get_current_user() )
    996         return false;
    997 
    998     $_updated_user_settings = $all;
    999     $settings = '';
    1000     foreach ( $all as $k => $v ) {
    1001         $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
    1002         $settings .= $k . '=' . $v . '&';
    1003     }
    1004 
    1005     $settings = rtrim($settings, '&');
    1006 
    1007     update_user_option( $user->ID, 'user-settings', $settings, false );
    1008     update_user_option( $user->ID, 'user-settings-time', time(), false );
    1009 
    1010     return true;
    1011 }
    1012 
    1013 /**
    1014  * Delete the user settings of the current user.
    1015  *
    1016  * @package WordPress
    1017  * @subpackage Option
    1018  * @since 2.7.0
    1019  */
    1020 function delete_all_user_settings() {
    1021     if ( ! $user = wp_get_current_user() )
    1022         return;
    1023 
    1024     update_user_option( $user->ID, 'user-settings', '', false );
    1025     setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
    1026306}
    1027307
     
    38043084
    38053085/**
    3806  * Retrieve site option value based on name of option.
    3807  *
    3808  * @see get_option()
    3809  * @package WordPress
    3810  * @subpackage Option
    3811  * @since 2.8.0
    3812  *
    3813  * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option.
    3814  *  Any value other than false will "short-circuit" the retrieval of the option
    3815  *  and return the returned value.
    3816  * @uses apply_filters() Calls 'site_option_$option', after checking the  option, with
    3817  *  the option value.
    3818  *
    3819  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
    3820  * @param mixed $default Optional value to return if option doesn't exist. Default false.
    3821  * @param bool $use_cache Whether to use cache. Multisite only. Default true.
    3822  * @return mixed Value set for the option.
    3823  */
    3824 function get_site_option( $option, $default = false, $use_cache = true ) {
    3825     global $wpdb;
    3826 
    3827     // Allow plugins to short-circuit site options.
    3828     $pre = apply_filters( 'pre_site_option_' . $option, false );
    3829     if ( false !== $pre )
    3830         return $pre;
    3831 
    3832     if ( !is_multisite() ) {
    3833         $value = get_option($option, $default);
    3834     } else {
    3835         $cache_key = "{$wpdb->siteid}:$option";
    3836         if ( $use_cache )
    3837             $value = wp_cache_get($cache_key, 'site-options');
    3838 
    3839         if ( !isset($value) || (false === $value) ) {
    3840             $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
    3841 
    3842             // Has to be get_row instead of get_var because of funkiness with 0, false, null values
    3843             if ( is_object( $row ) ) {
    3844                 $value = $row->meta_value;
    3845                 $value = maybe_unserialize( $value );
    3846                 wp_cache_set( $cache_key, $value, 'site-options' );
    3847             } else {
    3848                 $value = $default;
    3849             }
    3850         }
    3851     }
    3852 
    3853     return apply_filters( 'site_option_' . $option, $value );
    3854 }
    3855 
    3856 /**
    3857  * Add a new site option.
    3858  *
    3859  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
    3860  *
    3861  * @see add_option()
    3862  * @package WordPress
    3863  * @subpackage Option
    3864  * @since 2.8.0
    3865  *
    3866  * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
    3867  *  option value to be stored.
    3868  * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
    3869  *
    3870  * @param string $option Name of option to add. Expected to not be SQL-escaped.
    3871  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
    3872  * @return bool False if option was not added and true if option was added.
    3873  */
    3874 function add_site_option( $option, $value ) {
    3875     global $wpdb;
    3876 
    3877     $value = apply_filters( 'pre_add_site_option_' . $option, $value );
    3878 
    3879     if ( !is_multisite() ) {
    3880         $result = add_option( $option, $value );
    3881     } else {
    3882         $cache_key = "{$wpdb->siteid}:$option";
    3883 
    3884         if ( false !== get_site_option( $option ) )
    3885             return false;
    3886 
    3887         $value = sanitize_option( $option, $value );
    3888         wp_cache_set( $cache_key, $value, 'site-options' );
    3889 
    3890         $_value = $value;
    3891         $value = maybe_serialize( $value );
    3892         $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $value ) );
    3893         $value = $_value;
    3894     }
    3895 
    3896     if ( $result ) {
    3897         do_action( "add_site_option_{$option}", $option, $value );
    3898         do_action( "add_site_option", $option, $value );
    3899         return true;
    3900     }
    3901     return false;
    3902 }
    3903 
    3904 /**
    3905  * Removes site option by name.
    3906  *
    3907  * @see delete_option()
    3908  * @package WordPress
    3909  * @subpackage Option
    3910  * @since 2.8.0
    3911  *
    3912  * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
    3913  * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
    3914  *  hooks on success.
    3915  *
    3916  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
    3917  * @return bool True, if succeed. False, if failure.
    3918  */
    3919 function delete_site_option( $option ) {
    3920     global $wpdb;
    3921 
    3922     // ms_protect_special_option( $option ); @todo
    3923 
    3924     do_action( 'pre_delete_site_option_' . $option );
    3925 
    3926     if ( !is_multisite() ) {
    3927         $result = delete_option( $option );
    3928     } else {
    3929         $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
    3930         if ( is_null( $row ) || !$row->meta_id )
    3931             return false;
    3932         $cache_key = "{$wpdb->siteid}:$option";
    3933         wp_cache_delete( $cache_key, 'site-options' );
    3934 
    3935         $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
    3936     }
    3937 
    3938     if ( $result ) {
    3939         do_action( "delete_site_option_{$option}", $option );
    3940         do_action( "delete_site_option", $option );
    3941         return true;
    3942     }
    3943     return false;
    3944 }
    3945 
    3946 /**
    3947  * Update the value of a site option that was already added.
    3948  *
    3949  * @see update_option()
    3950  * @since 2.8.0
    3951  * @package WordPress
    3952  * @subpackage Option
    3953  *
    3954  * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
    3955  *  option value to be stored.
    3956  * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
    3957  *
    3958  * @param string $option Name of option. Expected to not be SQL-escaped.
    3959  * @param mixed $value Option value. Expected to not be SQL-escaped.
    3960  * @return bool False if value was not updated and true if value was updated.
    3961  */
    3962 function update_site_option( $option, $value ) {
    3963     global $wpdb;
    3964 
    3965     $oldvalue = get_site_option( $option );
    3966     $value = apply_filters( 'pre_update_site_option_' . $option, $value, $oldvalue );
    3967 
    3968     if ( $value === $oldvalue )
    3969         return false;
    3970 
    3971     if ( false === $oldvalue )
    3972         return add_site_option( $option, $value );
    3973 
    3974     if ( !is_multisite() ) {
    3975         $result = update_option( $option, $value );
    3976     } else {
    3977         $value = sanitize_option( $option, $value );
    3978         $cache_key = "{$wpdb->siteid}:$option";
    3979         wp_cache_set( $cache_key, $value, 'site-options' );
    3980 
    3981         $_value = $value;
    3982         $value = maybe_serialize( $value );
    3983         $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
    3984         $value = $_value;
    3985     }
    3986 
    3987     if ( $result ) {
    3988         do_action( "update_site_option_{$option}", $option, $value, $oldvalue );
    3989         do_action( "update_site_option", $option, $value, $oldvalue );
    3990         return true;
    3991     }
    3992     return false;
    3993 }
    3994 
    3995 /**
    3996  * Delete a site transient.
    3997  *
    3998  * @since 2.9.0
    3999  * @package WordPress
    4000  * @subpackage Transient
    4001  *
    4002  * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
    4003  * @uses do_action() Calls 'deleted_site_transient' hook on success.
    4004  *
    4005  * @param string $transient Transient name. Expected to not be SQL-escaped.
    4006  * @return bool True if successful, false otherwise
    4007  */
    4008 function delete_site_transient( $transient ) {
    4009     global $_wp_using_ext_object_cache;
    4010 
    4011     do_action( 'delete_site_transient_' . $transient, $transient );
    4012     if ( $_wp_using_ext_object_cache ) {
    4013         $result = wp_cache_delete( $transient, 'site-transient' );
    4014     } else {
    4015         $option_timeout = '_site_transient_timeout_' . $transient;
    4016         $option = '_site_transient_' . $transient;
    4017         $result = delete_site_option( $option );
    4018         if ( $result )
    4019             delete_site_option( $option_timeout );
    4020     }
    4021     if ( $result )
    4022         do_action( 'deleted_site_transient', $transient );
    4023     return $result;
    4024 }
    4025 
    4026 /**
    4027  * Get the value of a site transient.
    4028  *
    4029  * If the transient does not exist or does not have a value, then the return value
    4030  * will be false.
    4031  *
    4032  * @see get_transient()
    4033  * @since 2.9.0
    4034  * @package WordPress
    4035  * @subpackage Transient
    4036  *
    4037  * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
    4038  *  Any value other than false will "short-circuit" the retrieval of the transient
    4039  *  and return the returned value.
    4040  * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
    4041  *  the transient value.
    4042  *
    4043  * @param string $transient Transient name. Expected to not be SQL-escaped.
    4044  * @return mixed Value of transient
    4045  */
    4046 function get_site_transient( $transient ) {
    4047     global $_wp_using_ext_object_cache;
    4048 
    4049     $pre = apply_filters( 'pre_site_transient_' . $transient, false );
    4050     if ( false !== $pre )
    4051         return $pre;
    4052 
    4053     if ( $_wp_using_ext_object_cache ) {
    4054         $value = wp_cache_get( $transient, 'site-transient' );
    4055     } else {
    4056         // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
    4057         $no_timeout = array('update_core', 'update_plugins', 'update_themes');
    4058         $transient_option = '_site_transient_' . $transient;
    4059         if ( ! in_array( $transient, $no_timeout ) ) {
    4060             $transient_timeout = '_site_transient_timeout_' . $transient;
    4061             $timeout = get_site_option( $transient_timeout );
    4062             if ( false !== $timeout && $timeout < time() ) {
    4063                 delete_site_option( $transient_option  );
    4064                 delete_site_option( $transient_timeout );
    4065                 return false;
    4066             }
    4067         }
    4068 
    4069         $value = get_site_option( $transient_option );
    4070     }
    4071 
    4072     return apply_filters( 'site_transient_' . $transient, $value );
    4073 }
    4074 
    4075 /**
    4076  * Set/update the value of a site transient.
    4077  *
    4078  * You do not need to serialize values, if the value needs to be serialize, then
    4079  * it will be serialized before it is set.
    4080  *
    4081  * @see set_transient()
    4082  * @since 2.9.0
    4083  * @package WordPress
    4084  * @subpackage Transient
    4085  *
    4086  * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
    4087  *  transient value to be stored.
    4088  * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
    4089  *
    4090  * @param string $transient Transient name. Expected to not be SQL-escaped.
    4091  * @param mixed $value Transient value. Expected to not be SQL-escaped.
    4092  * @param int $expiration Time until expiration in seconds, default 0
    4093  * @return bool False if value was not set and true if value was set.
    4094  */
    4095 function set_site_transient( $transient, $value, $expiration = 0 ) {
    4096     global $_wp_using_ext_object_cache;
    4097 
    4098     $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
    4099 
    4100     if ( $_wp_using_ext_object_cache ) {
    4101         $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
    4102     } else {
    4103         $transient_timeout = '_site_transient_timeout_' . $transient;
    4104         $transient = '_site_transient_' . $transient;
    4105         if ( false === get_site_option( $transient ) ) {
    4106             if ( $expiration )
    4107                 add_site_option( $transient_timeout, time() + $expiration );
    4108             $result = add_site_option( $transient, $value );
    4109         } else {
    4110             if ( $expiration )
    4111                 update_site_option( $transient_timeout, time() + $expiration );
    4112             $result = update_site_option( $transient, $value );
    4113         }
    4114     }
    4115     if ( $result ) {
    4116         do_action( 'set_site_transient_' . $transient );
    4117         do_action( 'setted_site_transient', $transient );
    4118     }
    4119     return $result;
    4120 }
    4121 
    4122 /**
    41233086 * Is main site?
    41243087 *
Note: See TracChangeset for help on using the changeset viewer.