User.php
5.45 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
namespace WPML\LIB\WP;
use function WPML\FP\curryN;
use function WPML\FP\partialRight;
use WPML\FP\Fns;
class User {
const CAP_MANAGE_TRANSLATIONS = 'manage_translations';
const CAP_MANAGE_OPTIONS = 'manage_options';
const CAP_ADMINISTRATOR = 'administrator';
const CAP_TRANSLATE = 'translate';
const CAP_MANAGE_TRANSLATION_MANAGEMENT = 'wpml_manage_translation_management';
/** @var array Calling user_can() is a very memory heavy function. */
private static $userCanCache = [];
/**
* @param int|WP_User $user
* @param string $capability
*
* @return bool
*/
public static function userCan( $user, $capability ) {
if ( $user instanceof \WP_User ) {
$user = $user->ID;
}
if ( ! isset( self::$userCanCache[ $user ] ) ) {
self::$userCanCache[ $user ] = [];
}
if ( ! isset( self::$userCanCache[ $user ][ $capability ] ) ) {
self::$userCanCache[ $user ][ $capability ] = user_can( $user, $capability );
}
return self::$userCanCache[ $user ][ $capability ];
}
/**
* @param string $capability
*
* @return bool
*/
public static function currentUserCan( $capability ) {
return self::userCan( self::getCurrentId(), $capability );
}
/**
* @return int
*/
public static function getCurrentId() {
return get_current_user_id();
}
/**
* @return \WP_User|null
*/
public static function getCurrent() {
return wp_get_current_user();
}
/**
* Curried function to update the user meta.
*
* @param int $userId
* @param string $metaKey
* @param mixed $metaValue
*
* @return callable|int|bool
*/
public static function updateMeta( $userId = null, $metaKey = null, $metaValue = null ) {
return call_user_func_array( curryN( 3, 'update_user_meta' ), func_get_args() );
}
/**
* Curried function to get the user meta
*
* @param int $userId
* @param string $metaKey
*
* @return callable|mixed
*/
public static function getMetaSingle( $userId = null, $metaKey = null ) {
return call_user_func_array( curryN( 2, partialRight( 'get_user_meta', true ) ), func_get_args() );
}
/**
* Curried function to get the user meta
*
* @param int $userId
* @param string $metaKey
*
* @return callable|bool
*/
public static function deleteMeta( $userId = null, $metaKey = null ) {
return call_user_func_array( curryN( 2, 'delete_user_meta' ), func_get_args() );
}
/**
* @param int|null $userId
*
* @return callable|\WP_User
*/
public static function get( $userId = null ) {
return call_user_func_array( curryN( 1, function ( $userId ) {
return new \WP_User( $userId );
} ), func_get_args() );
}
/**
* @param array|null $data
*
* @return callable|int|\WP_Error
*/
public static function insert( $data = null ) {
return call_user_func_array( curryN( 1, 'wp_insert_user' ), func_get_args() );
}
/**
* @param int|null $userId
*
* @return callable|int
*/
public static function notifyNew( $userId = null ) {
return call_user_func_array( curryN( 1, Fns::tap( 'wp_send_new_user_notifications' ) ), func_get_args() );
}
/**
* Add the avatar to a user.
*
* @param object|\WP_User $user
*
* @return callable|object
*/
public static function withAvatar( $user = null ) {
$withAvatar = function ( $user ) {
$user->avatar = get_avatar( $user->ID );
$user->avatarUrl = get_avatar_url( $user->ID );
return $user;
};
return call_user_func_array( curryN( 1, $withAvatar ), func_get_args() );
}
/**
* Add the edit link to a user.
*
* @param object|\WP_User $user
*
* @return callable|object
*/
public static function withEditLink( $user = null ) {
$withEditLink = function ( $user ) {
$user->editLink = esc_url(
add_query_arg(
'wp_http_referer',
urlencode( esc_url( stripslashes( $_SERVER['REQUEST_URI'] ) ) ),
"user-edit.php?user_id={$user->ID}"
)
);
return $user;
};
return call_user_func_array( curryN( 1, $withEditLink ), func_get_args() );
}
/**
* Checks if the given user has the requested capability.
* The current user is used if no user is defined.
*
* @param string $capability Capability to check for.
* @param ?\WP_User $user User to check. Using current user if not defined.
*/
public static function hasCap( $capabilitiy, \WP_User $user = null ) {
$user = $user ?: self::getCurrent();
return $user->has_cap( $capabilitiy );
}
/**
* Check if user can manage translations (Translation Manager).
* Alias for self::hasCap( User::CAP_MANAGE_TRANSLATIONS ).
*
* @param ?\WP_User $user User to check. Using current user if not defined.
*/
public static function canManageTranslations( \WP_User $user = null ) {
return self::hasCap( self::CAP_MANAGE_TRANSLATIONS, $user ) || self::isAdministrator( $user );
}
/**
* Check if user can manage options (Administrator).
* Alias for self::hasCap( User::CAP_MANAGE_OPTIONS ).
*
* @param ?\WP_User $user User to check. Using current user if not defined.
*/
public static function canManageOptions( \WP_User $user = null ) {
return self::hasCap( self::CAP_MANAGE_OPTIONS, $user );
}
/**
* @param \WP_User|null $user User to check. Using current user if not defined.
*
* @return bool
*/
public static function isAdministrator( \WP_User $user = null ) {
return self::hasCap( self::CAP_ADMINISTRATOR, $user );
}
/**
* @param \WP_User|null $user User to check. Using current user if not defined.
*
* @return bool
*/
public static function isEditor(\WP_User $user = null) {
return self::hasCap( 'editor', $user );
}
}