wp_user_settings()
云策文档标注
概述
wp_user_settings() 函数用于保存和恢复存储在 cookie 中的用户界面设置。它检查当前用户设置 cookie 是否更新并存储,当 cookie 不存在时(如使用不同浏览器),会添加最后保存的 cookie 以恢复设置。
关键要点
- 函数仅在管理界面且非 Ajax 请求时执行,确保用户已登录并是当前博客成员。
- 通过比较 cookie 和数据库中的用户设置值,决定是否更新用户选项或设置新 cookie。
- 使用 get_user_option() 和 update_user_option() 处理用户设置,涉及时间戳验证以同步数据。
- 设置 cookie 时考虑安全性和路径,使用 SITECOOKIEPATH 和 secure 标志。
代码示例
function wp_user_settings() {
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}
$user_id = get_current_user_id();
if ( ! $user_id ) {
return;
}
if ( ! is_user_member_of_blog() ) {
return;
}
$settings = (string) 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 ] );
// No change or both empty.
if ( $cookie === $settings ) {
return;
}
$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
$current = 0;
if ( isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ) {
$current = (int) preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] );
}
// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is.
if ( $current > $last_saved ) {
update_user_option( $user_id, 'user-settings', $cookie, false );
update_user_option( $user_id, 'user-settings-time', time() - 5, false );
return;
}
}
// The cookie is not set in the current browser or the saved value is newer.
$secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
$_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
}注意事项
- 函数依赖于多个 WordPress 核心函数,如 is_admin() 和 get_current_user_id(),确保在正确上下文中调用。
- cookie 名称基于用户 ID 动态生成,避免冲突,并处理特殊字符以确保数据完整性。
- 时间戳比较逻辑用于决定数据同步方向,优先使用较新的值。
原文内容
Saves and restores user interface settings stored in a cookie.
Description
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.
Source
function wp_user_settings() {
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}
$user_id = get_current_user_id();
if ( ! $user_id ) {
return;
}
if ( ! is_user_member_of_blog() ) {
return;
}
$settings = (string) 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 ] );
// No change or both empty.
if ( $cookie === $settings ) {
return;
}
$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
$current = 0;
if ( isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ) {
$current = (int) preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] );
}
// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is.
if ( $current > $last_saved ) {
update_user_option( $user_id, 'user-settings', $cookie, false );
update_user_option( $user_id, 'user-settings-time', time() - 5, false );
return;
}
}
// The cookie is not set in the current browser or the saved value is newer.
$secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
$_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |