-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.class.php
136 lines (107 loc) · 2.67 KB
/
User.class.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php namespace JModel;
/**
* A model of a WordPress user, extends from WP_User
*
*/
class User extends \WP_User {
function __construct($userId = null) {
parent::__construct($userId);
}
/**
* Returns the users displayname
* @return string
*/
public function getDisplayName() {
return \get_the_author_meta('display_name', $this->ID);
}
/**
* Returns the role for the current user
* @return string | null
*/
public function getRole() {
return isset($this->roles[0]) ? $this->roles[0] : null;
}
/**
* Returns the authors post url
* @return string
*/
public function getPostsURL() {
return \get_author_posts_url($this->ID);
}
/**
* Alias of postsURL. Returns the users permalink, aka post url,
* @return string
*/
public function getPermalink() {
return $this->postsURL();
}
/**
* Returns a URL of the author avatar
* @return string
*/
public function getAvatar($size = 72) {
return \get_avatar($this->ID, $size);
}
/******************* Static Methods *******************/
/**
* Returns the author of the page as an instance of User
* @return User
*/
public static function getPostAuthor() {
$user = get_user_by('slug', get_query_var('author_name'));
return new User($user->ID);
}
/**
* Returns an instance of the currently logged in user
* @return User
*/
public static function getCurrentUser() {
global $current_user;
get_currentuserinfo();
return new User($current_user->ID);
}
/**
* Returns whether the current user is an admin or not
* @return boolean
*/
public static function currentUserIsAdmin() {
$cu = self::getCurrentUser();
return $cu->getRole() == 'administrator';
}
/**
* Returns an array of users with their assosicated IDs
* @param string $role desired role
* @return array<string>
*/
public static function getSelectOptions($role = null, $placeholder = 'Select a user') {
$users = self::all($role);
$ra = array(
'0' => $placeholder
);
foreach ($users as $u) {
$ra[$u->ID] = $u->display_name;
}
return $ra;
}
/**
* Returns an array of all users with the passed role
* @param strubg $role a valid role
* @return array<WP_User>
*/
public static function all($role = null) {
return get_users(array(
'role' => $role,
));
}
/**
* Returns the users role
* @param WP_User $u [description]
* @return string | null
*/
public static function getUserRole($u = null) {
if (!$u) return null;
if (!($u instanceof \WP_User)) $u = new \WP_User($u);
if (isset($u->roles[0])) return $u->roles[0];
return null;
}
}