+/**
+ * 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 );
+}
+
+/**
+ * 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 ) {
+
+ $arr = get_all_user_settings();
+
+ return isset($arr[$name]) ? $arr[$name] : $default;
+}
+
+/**
+ * Delete user interface settings.
+ *
+ * Deleting settings would reset them to the defaults.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.7.0
+ *
+ * @param mixed $names The name or array of names of the setting to be deleted.
+ */
+function delete_user_setting( $names ) {
+ global $current_user;
+
+ $arr = get_all_user_settings();
+ $names = (array) $names;
+
+ foreach ( $names as $name ) {
+ if ( isset($arr[$name]) ) {
+ unset($arr[$name]);
+ $settings = '';
+ }
+ }
+
+ if ( isset($settings) ) {
+ foreach ( $arr as $k => $v )
+ $settings .= $k . '=' . $v . '&';
+
+ $settings = rtrim($settings, '&');
+
+ update_user_option( $current_user->ID, 'user-settings', $settings );
+ setcookie('wp-settings-'.$current_user->ID, $settings, time() + 31536000, SITECOOKIEPATH);
+ }
+}
+
+/**
+ * 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() {
+ if ( ! $user = wp_get_current_user() )
+ return 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, $arr);
+ return $arr;
+ }
+ }
+
+ return array();
+}
+
+/**
+ * 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;
+
+ delete_usermeta( $user->ID, 'user-settings' );
+ setcookie('wp-settings-'.$user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
+}
+