+/**
+ * Delete a transient
+ *
+ * @since 2.8.0
+ * @package WordPress
+ * @subpackage Transient
+ *
+ * @param string $transient Transient name. Expected to not be SQL-escaped
+ * @return bool true if successful, false otherwise
+ */
+function delete_transient($transient) {
+ global $_wp_using_ext_object_cache, $wpdb;
+
+ if ( $_wp_using_ext_object_cache ) {
+ return wp_cache_delete($transient, 'transient');
+ } else {
+ $transient = '_transient_' . $wpdb->escape($transient);
+ return delete_option($transient);
+ }
+}
+
+/**
+ * Get the value of a transient
+ *
+ * If the transient does not exist or does not have a value, then the return value
+ * will be false.
+ *
+ * @since 2.8.0
+ * @package WordPress
+ * @subpackage Transient
+ *
+ * @param string $transient Transient name. Expected to not be SQL-escaped
+ * @return mixed Value of transient
+ */
+function get_transient($transient) {
+ global $_wp_using_ext_object_cache, $wpdb;
+
+ $pre = apply_filters( 'pre_transient_' . $transient, false );
+ if ( false !== $pre )
+ return $pre;
+
+ if ( $_wp_using_ext_object_cache ) {
+ $value = wp_cache_get($transient, 'transient');
+ } else {
+ $transient_option = '_transient_' . $wpdb->escape($transient);
+ // If option is not in alloptions, it is not autoloaded and thus has a timeout
+ $alloptions = wp_load_alloptions();
+ if ( !isset( $alloptions[$transient_option] ) ) {
+ $transient_timeout = '_transient_timeout_' . $wpdb->escape($transient);
+ if ( get_option($transient_timeout) < time() ) {
+ delete_option($transient_option);
+ delete_option($transient_timeout);
+ return false;
+ }
+ }
+
+ $value = get_option($transient_option);
+ }
+
+ return apply_filters('transient_' . $transient, $value);
+}
+
+/**
+ * Set/update the value of a transient
+ *
+ * You do not need to serialize values, if the value needs to be serialize, then
+ * it will be serialized before it is set.
+ *
+ * @since 2.8.0
+ * @package WordPress
+ * @subpackage Transient
+ *
+ * @param string $transient Transient name. Expected to not be SQL-escaped
+ * @param mixed $value Transient value.
+ * @param int $expiration Time until expiration in seconds, default 0
+ * @return bool False if value was not set and true if value was set.
+ */
+function set_transient($transient, $value, $expiration = 0) {
+ global $_wp_using_ext_object_cache, $wpdb;
+
+ if ( $_wp_using_ext_object_cache ) {
+ return wp_cache_set($transient, $value, 'transient', $expiration);
+ } else {
+ $transient_timeout = '_transient_timeout_' . $transient;
+ $transient = '_transient_' . $transient;
+ $safe_transient = $wpdb->escape($transient);
+ if ( false === get_option( $safe_transient ) ) {
+ $autoload = 'yes';
+ if ( 0 != $expiration ) {
+ $autoload = 'no';
+ add_option($transient_timeout, time() + $expiration, '', 'no');
+ }
+ return add_option($transient, $value, '', $autoload);
+ } else {
+ if ( 0 != $expiration )
+ update_option($transient_timeout, time() + $expiration);
+ return update_option($transient, $value);
+ }
+ }
+}
+
+/**
+ * Saves and restores user interface settings stored in a cookie.
+ *
+ * Checks if the current user-settings cookie is updated and stores it. When no
+ * cookie exists (different browser used), adds the last saved cookie restoring
+ * the settings.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.7.0
+ */
+function wp_user_settings() {
+
+ if ( ! is_admin() )
+ return;
+
+ if ( defined('DOING_AJAX') )
+ return;
+
+ if ( ! $user = wp_get_current_user() )
+ return;
+
+ $settings = get_user_option( 'user-settings', $user->ID, false );
+
+ if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
+ $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
+
+ if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
+ if ( $cookie == $settings )
+ return;
+
+ $last_time = (int) get_user_option( 'user-settings-time', $user->ID, false );
+ $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
+
+ if ( $saved > $last_time ) {
+ update_user_option( $user->ID, 'user-settings', $cookie, false );
+ update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
+ return;
+ }
+ }
+ }
+
+ setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
+ setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
+ $_COOKIE['wp-settings-' . $user->ID] = $settings;
+}
+
+/**
+ * Retrieve user interface setting value based on setting name.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.7.0
+ *
+ * @param string $name The name of the setting.
+ * @param string $default Optional default value to return when $name is not set.
+ * @return mixed the last saved user setting or the default value/false if it doesn't exist.
+ */
+function get_user_setting( $name, $default = false ) {
+
+ $all = get_all_user_settings();
+
+ return isset($all[$name]) ? $all[$name] : $default;
+}
+
+/**
+ * Add or update user interface setting.
+ *
+ * Both $name and $value can contain only ASCII letters, numbers and underscores.
+ * This function has to be used before any output has started as it calls setcookie().
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.8.0
+ *
+ * @param string $name The name of the setting.
+ * @param string $value The value for the setting.
+ * @return bool true if set successfully/false if not.
+ */
+function set_user_setting( $name, $value ) {
+
+ if ( headers_sent() )
+ return false;
+
+ $all = get_all_user_settings();
+ $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
+
+ if ( empty($name) )
+ return false;
+
+ $all[$name] = $value;
+
+ return wp_set_all_user_settings($all);
+}
+
+/**
+ * Delete user interface settings.
+ *
+ * Deleting settings would reset them to the defaults.
+ * This function has to be used before any output has started as it calls setcookie().
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.7.0
+ *
+ * @param mixed $names The name or array of names of the setting to be deleted.
+ * @return bool true if deleted successfully/false if not.
+ */
+function delete_user_setting( $names ) {
+
+ if ( headers_sent() )
+ return false;
+
+ $all = get_all_user_settings();
+ $names = (array) $names;
+
+ foreach ( $names as $name ) {
+ if ( isset($all[$name]) ) {
+ unset($all[$name]);
+ $deleted = true;
+ }
+ }
+
+ if ( isset($deleted) )
+ return wp_set_all_user_settings($all);
+
+ return false;
+}
+
+/**
+ * Retrieve all user interface settings.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.7.0
+ *
+ * @return array the last saved user settings or empty array.
+ */
+function get_all_user_settings() {
+ global $_updated_user_settings;
+
+ if ( ! $user = wp_get_current_user() )
+ return array();
+
+ if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
+ return $_updated_user_settings;
+
+ $all = array();
+ if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
+ $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
+
+ if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
+ parse_str($cookie, $all);
+
+ } else {
+ $option = get_user_option('user-settings', $user->ID);
+ if ( $option && is_string($option) )
+ parse_str( $option, $all );
+ }
+
+ return $all;
+}
+
+/**
+ * Private. Set all user interface settings.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.8.0
+ *
+ */
+function wp_set_all_user_settings($all) {
+ global $_updated_user_settings;
+
+ if ( ! $user = wp_get_current_user() )
+ return false;
+
+ $_updated_user_settings = $all;
+ $settings = '';
+ foreach ( $all as $k => $v ) {
+ $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
+ $settings .= $k . '=' . $v . '&';
+ }
+
+ $settings = rtrim($settings, '&');
+
+ update_user_option( $user->ID, 'user-settings', $settings, false );
+ update_user_option( $user->ID, 'user-settings-time', time(), false );
+
+ return true;
+}
+
+/**
+ * Delete the user settings of the current user.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.7.0
+ */
+function delete_all_user_settings() {
+ if ( ! $user = wp_get_current_user() )
+ return;
+
+ update_user_option( $user->ID, 'user-settings', '', false );
+ setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
+}
+