-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-user-preferences.php
116 lines (100 loc) · 2.8 KB
/
wp-user-preferences.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Plugin Name: WP User Preferences
* Plugin URI: https://wordpress.org/plugins/wp-user-preferences/
* Description: Prefer user settings over site & network settings
* Author: John James Jacoby
* Version: 0.1.0
* Author URI: https://profiles.wordpress.org/johnjamesjacoby/
* License: GPL v2 or later
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( ! function_exists( 'wp_get_current_user_preference' ) ) :
/**
* Get preference for the currently logged in user. If no user is logged in,
* a site or network preference will be returned if one is set.
*
* @since 0.1.0
*
* @param string $key
* @return mixed
*/
function wp_get_current_user_preference( $key = '' ) {
return wp_get_user_preference( get_current_user_id(), $key );
}
endif;
if ( ! function_exists( 'wp_get_user_preference' ) ) :
/**
* Get a user preference
*
* @since 0.1.0
*
* @param int $user_id
* @param string $key
*
* @return mixed
*/
function wp_get_user_preference( $user_id = 0, $key = '' ) {
// Get user/site/network preference map
$keys = wp_map_user_preference_key( $key );
// Check usermeta first
$retval = get_usermeta( $user_id, $keys['user'] );
// Nothing, so check site option
if ( false === $retval ) {
$retval = get_option( $keys['site'] );
// Nothing, so check network option if multisite
if ( false === $retval && is_multisite() ) {
$retval = get_site_option( $keys['network'] );
}
}
// Filter & return
return apply_filters( 'wp_get_user_preference', $retval, $user_id, $key );
}
endif;
if ( ! function_exists( 'wp_map_user_preference_key' ) ) :
/**
* Return an array of key/value pairs for user/site/network settings, based on
* the usermeta key being passed in.
*
* This function exists because not all user, site, & network metadata keys are
* the same, even though they return the same type of data back. By passing in
* the usermeta key, we can make decisions about what site & network keys map to
* a user preference, and fallback to them if no user preference is set yet.
*
* @since 0.1.0
*
* @param string $key
*
* @return array
*/
function wp_map_user_preference_key( $key = '' ) {
// Which usermeta key are we mapping to?
switch ( $key ) {
// These keys are different between user/site/network
case 'some_other_preference' :
$retval = array(
'user' => 'one',
'site' => 'two',
'network' => 'three'
);
break;
// These keys are the same between user/site/network
case 'admin_color' :
case 'timezone' :
case 'time_format' :
case 'date_format' :
case 'WPLANG' :
default :
// Default return value
$retval = array(
'user' => $key,
'site' => $key,
'network' => $key
);
break;
}
// Filter & return
return apply_filters( 'wp_map_user_preference_key', $retval, $key );
}
endif;