mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-31 02:34:38 +00:00
Saving/restoring the user interface state, see #7654
git-svn-id: https://develop.svn.wordpress.org/trunk@8784 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -643,6 +643,134 @@ function delete_option( $name ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves and restores user interface settings stored in a cookie.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Option
|
||||
* @since 2.7.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
function wp_user_settings() {
|
||||
|
||||
if ( ! is_admin() )
|
||||
return;
|
||||
|
||||
if ( ! $user = wp_get_current_user() )
|
||||
return;
|
||||
|
||||
$settings = get_user_option( 'user-settings', $user->ID );
|
||||
|
||||
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 );
|
||||
$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 );
|
||||
update_user_option( $user->ID, 'user-settings-time', time() - 5 );
|
||||
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.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Option
|
||||
* @since 2.7.0
|
||||
*
|
||||
* Deleting settings would reset them to the defaults.
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize data, if needed.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user