Make WordPress Core


Ignore:
Timestamp:
08/31/2008 06:34:43 AM (18 years ago)
Author:
azaozz
Message:

Saving/restoring the user interface state, see #7654

File:
1 edited

Legend:

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

    r8770 r8784  
    642642        }
    643643        return true;
     644}
     645
     646/**
     647 * Saves and restores user interface settings stored in a cookie.
     648 *
     649 * @package WordPress
     650 * @subpackage Option
     651 * @since 2.7.0
     652 *
     653 * Checks if the current user-settings cookie is updated and stores it.
     654 * When no cookie exists (different browser used), adds the last saved cookie restoring the settings.
     655 */
     656function wp_user_settings() {
     657
     658        if ( ! is_admin() )
     659                return;
     660
     661        if ( ! $user = wp_get_current_user() )
     662                return;
     663
     664        $settings = get_user_option( 'user-settings', $user->ID );
     665
     666        if ( isset($_COOKIE['wp-settings-'.$user->ID]) ) {
     667                $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-'.$user->ID] );
     668
     669                if ( ! empty($cookie) && strpos($cookie, '=') ) {
     670                        if ( $cookie == $settings )
     671                                return;
     672
     673                        $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
     674                        $saved = isset($_COOKIE['wp-settings-time-'.$user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-'.$user->ID] ) : 0;
     675
     676                        if ( $saved > $last_time ) {
     677                                update_user_option( $user->ID, 'user-settings', $cookie );
     678                                update_user_option( $user->ID, 'user-settings-time', time() - 5 );
     679                                return;
     680                        }
     681                }
     682        }
     683
     684        setcookie('wp-settings-'.$user->ID, $settings, time() + 31536000, SITECOOKIEPATH);
     685        setcookie('wp-settings-time-'.$user->ID, time(), time() + 31536000, SITECOOKIEPATH);
     686}
     687
     688/**
     689 * Retrieve user interface setting value based on setting name.
     690 *
     691 * @package WordPress
     692 * @subpackage Option
     693 * @since 2.7.0
     694 *   
     695 * @param string $name The name of the setting.
     696 * @param string $default Optional default value to return when $name is not set.
     697 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
     698 */
     699function get_user_setting( $name, $default = false ) {
     700
     701        $arr = get_all_user_settings();
     702
     703        return isset($arr[$name]) ? $arr[$name] : $default;
     704}
     705
     706/**
     707 * Delete user interface settings.
     708 *
     709 * @package WordPress
     710 * @subpackage Option
     711 * @since 2.7.0
     712 *
     713 * Deleting settings would reset them to the defaults.
     714 *
     715 * @param mixed $names The name or array of names of the setting to be deleted.
     716 */
     717function delete_user_setting( $names ) {
     718        global $current_user;
     719
     720        $arr = get_all_user_settings();
     721        $names = (array) $names;
     722
     723        foreach ( $names as $name ) {
     724                if ( isset($arr[$name]) ) {
     725                        unset($arr[$name]);
     726                        $settings = '';
     727                }
     728        }
     729
     730        if ( isset($settings) ) {
     731                foreach ( $arr as $k => $v )
     732                        $settings .= $k . '=' . $v . '&';
     733
     734                $settings = rtrim($settings, '&');
     735
     736                update_user_option( $current_user->ID, 'user-settings', $settings );
     737                setcookie('wp-settings-'.$current_user->ID, $settings, time() + 31536000, SITECOOKIEPATH);
     738        }
     739}
     740
     741/**
     742 * Retrieve all user interface settings.
     743 *
     744 * @package WordPress
     745 * @subpackage Option
     746 * @since 2.7.0
     747 *
     748 * @return array the last saved user settings or empty array.
     749 */
     750function get_all_user_settings() {
     751        if ( ! $user = wp_get_current_user() )
     752                return array();
     753
     754        if ( isset($_COOKIE['wp-settings-'.$user->ID]) ) {
     755                $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-'.$user->ID] );
     756
     757                if ( $cookie && strpos($cookie, '=') ) { // the '=' cannot be 1st char
     758                        parse_str($cookie, $arr);
     759                        return $arr;
     760                }
     761        }
     762
     763        return array();
     764}
     765
     766function delete_all_user_settings() {
     767        if ( ! $user = wp_get_current_user() )
     768                return;
     769
     770        delete_usermeta( $user->ID, 'user-settings' );
     771        setcookie('wp-settings-'.$user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
    644772}
    645773
Note: See TracChangeset for help on using the changeset viewer.