PluginProbe
Stream – Activity Log & Audit Trail / 3.6.1
Stream – Activity Log & Audit Trail v3.6.1
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
← All changes | classes/class-author.php +87 -59 3.0.53.6.1 View file →
@@ -1,19 +1,34 @@
1 1 <?php
2 +/**
3 + * Manages a single user, acting as a model.
4 + *
5 + * @package WP_Stream
6 + */
7 +
2 8 namespace WP_Stream;
3 9
10 +/**
11 + * Class - Author
12 + */
4 13 class Author {
5 14 /**
15 + * Holds User ID.
16 + *
6 17 * @var int
7 18 */
8 19 public $id;
9 20
10 21 /**
22 + * Holds User meta data.
23 + *
11 24 * @var array
12 25 */
13 26 public $meta = array();
14 27
15 28 /**
29 + * Holds WP user object connected to "$this" instance.
30 + *
16 31 * @var \WP_User
17 32 */
18 33 protected $user;
19 34
@@ -19,14 +34,14 @@
19 34
20 35 /**
21 36 * Class constructor.
22 37 *
23 - * @param int $user_id The user ID.
24 - * @param array|string $user_meta The user meta array, or a serialized string of user meta.
38 + * @param int $user_id The user ID.
39 + * @param array $user_meta The user meta array.
25 40 */
26 - function __construct( $user_id, $user_meta = array() ) {
41 + public function __construct( $user_id, $user_meta = array() ) {
27 42 $this->id = absint( $user_id );
28 - $this->meta = maybe_unserialize( $user_meta );
43 + $this->meta = $user_meta;
29 44
30 45 if ( $this->id ) {
31 46 $this->user = new \WP_User( $this->id );
32 47 }
@@ -34,43 +49,57 @@
34 49
35 50 /**
36 51 * Get various user meta data
37 52 *
38 - * @param string $name
53 + * @todo Make sure this is being covered in the unit tests.
39 54 *
40 - * @throws \Exception
55 + * @param string $name User meta key.
41 56 *
57 + * @throws \Exception Meta not found | User not found.
58 + *
42 59 * @return string
43 60 */
44 - function __get( $name ) {
45 - if ( 'display_name' === $name ) {
46 - return $this->get_display_name();
47 - } elseif ( 'avatar_img' === $name ) {
48 - return $this->get_avatar_img();
49 - } elseif ( 'avatar_src' === $name ) {
50 - return $this->get_avatar_src();
51 - } elseif ( 'role' === $name ) {
52 - return $this->get_role();
53 - } elseif ( 'agent' === $name ) {
54 - return $this->get_agent();
55 - } elseif ( ! empty( $this->user ) && 0 !== $this->user->ID ) {
56 - return $this->user->$name;
57 - } else {
58 - throw new \Exception( "Unrecognized magic '$name'" );
61 + public function __get( $name ) {
62 + switch ( $name ) {
63 + case 'display_name':
64 + case 'avatar_img':
65 + case 'avatar_src':
66 + case 'role':
67 + case 'agent':
68 + $getter = "get_{$name}";
69 + return $this->$getter();
70 + default:
71 + if ( ! empty( $this->user ) && 0 !== $this->user->ID ) {
72 + if ( is_null( $this->user->$name ) ) {
73 + throw new \Exception( "Unrecognized magic '$name'" );
74 + }
75 + return $this->user->$name;
76 + }
77 +
78 + throw new \Exception( 'User not found.' );
59 79 }
60 80 }
61 81
62 82 /**
83 + * Returns string representation of this object
84 + *
85 + * @return string
86 + */
87 + public function __toString() {
88 + return $this->get_display_name();
89 + }
90 +
91 + /**
63 92 * Get the display name of the user
64 93 *
65 94 * @return string
66 95 */
67 - function get_display_name() {
96 + public function get_display_name() {
68 97 if ( 0 === $this->id ) {
69 98 if ( isset( $this->meta['system_user_name'] ) ) {
70 99 return esc_html( $this->meta['system_user_name'] );
71 100 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
72 - return 'WP-CLI'; // No translation needed
101 + return 'WP-CLI'; // No translation needed.
73 102 }
74 103 return esc_html__( 'N/A', 'stream' );
75 104 } else {
76 105 if ( $this->is_deleted() ) {
@@ -93,15 +122,15 @@
93 122 * Get the agent of the user
94 123 *
95 124 * @return string
96 125 */
97 - function get_agent() {
126 + public function get_agent() {
98 127 $agent = '';
99 128
100 129 if ( ! empty( $this->meta['agent'] ) ) {
101 130 $agent = $this->meta['agent'];
102 131 } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
103 - $agent = 'wp_cli'; // legacy
132 + $agent = 'wp_cli'; // legacy.
104 133 }
105 134
106 135 return $agent;
107 136 }
@@ -110,13 +139,13 @@
110 139 * Return a Gravatar image as an HTML element.
111 140 *
112 141 * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.
113 142 *
114 - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80
143 + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
115 144 *
116 145 * @return string|bool An img HTML element, or false if avatars are disabled
117 146 */
118 - function get_avatar_img( $size = 80 ) {
147 + public function get_avatar_img( $size = 80 ) {
119 148 if ( ! get_option( 'show_avatars' ) ) {
120 149 return false;
121 150 }
122 151
@@ -138,13 +167,13 @@
138 167
139 168 /**
140 169 * Return the URL of a Gravatar image.
141 170 *
142 - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80
171 + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
143 172 *
144 173 * @return string|bool Gravatar image URL, or false on failure
145 174 */
146 - function get_avatar_src( $size = 80 ) {
175 + public function get_avatar_src( $size = 80 ) {
147 176 $img = $this->get_avatar_img( $size );
148 177
149 178 if ( ! $img ) {
150 179 return false;
@@ -169,19 +198,29 @@
169 198 * Otherwise, use the role slug as the label.
170 199 *
171 200 * @return string
172 201 */
173 - function get_role() {
202 + public function get_role() {
174 203 global $wp_roles;
175 204
205 + $user_role = '';
206 +
176 207 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
177 208 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
178 209 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
179 210 $user_role = $this->meta['user_role_label'];
180 - } elseif ( isset( $this->user->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) {
181 - $user_role = $wp_roles->role_names[ $this->user->roles[0] ];
182 - } else {
183 - $user_role = '';
211 + } elseif ( ! empty( $this->user->roles ) ) {
212 + $roles = array_map(
213 + function( $role ) use ( $wp_roles ) {
214 + return $wp_roles->role_names[ $role ];
215 + },
216 + $this->user->roles
217 + );
218 +
219 + $separator = apply_filters( 'wp_stream_get_role_list_separator', ' - ' );
220 + $user_role = implode( $separator, $roles );
221 + } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
222 + $user_role = $wp_roles->role_names['administrator'];
184 223 }
185 224
186 225 return $user_role;
187 226 }
@@ -190,9 +229,9 @@
190 229 * True if user no longer exists, otherwise false
191 230 *
192 231 * @return bool
193 232 */
194 - function is_deleted() {
233 + public function is_deleted() {
195 234 return ( 0 !== $this->id && 0 === $this->user->ID );
196 235 }
197 236
198 237 /**
@@ -199,44 +238,33 @@
199 238 * True if user is WP-CLI, otherwise false
200 239 *
201 240 * @return bool
202 241 */
203 - function is_wp_cli() {
242 + public function is_wp_cli() {
204 243 return ( 'wp_cli' === $this->get_agent() );
205 244 }
206 245
207 246 /**
208 - * True if doing WP Cron, otherwise false
247 + * Check if the current request is part of a WP cron task.
209 248 *
210 - * Note: If native WP Cron has been disabled and you are
211 - * hitting the cron endpoint with a system cron job, this
212 - * method will always return false.
249 + * Note: This will return true for all manual or custom
250 + * cron runs even if the default front-end cron is disabled.
213 251 *
252 + * We're not using `wp_doing_cron()` since it was introduced
253 + * only in WordPress 4.8.0.
254 + *
214 255 * @return bool
215 256 */
216 - function is_doing_wp_cron() {
217 - return (
218 - wp_stream_is_cron_enabled()
219 - &&
220 - defined( 'DOING_CRON' )
221 - &&
222 - DOING_CRON
223 - );
257 + public function is_doing_wp_cron() {
258 + return ( defined( 'DOING_CRON' ) && DOING_CRON );
224 259 }
225 260
226 261 /**
227 - * @return string
228 - */
229 - function __toString() {
230 - return $this->get_display_name();
231 - }
232 -
233 - /**
234 262 * Look at the environment to detect if an agent is being used
235 263 *
236 264 * @return string
237 265 */
238 - function get_current_agent() {
266 + public function get_current_agent() {
239 267 $agent = '';
240 268
241 269 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
242 270 $agent = 'wp_cli';
@@ -256,13 +284,13 @@
256 284
257 285 /**
258 286 * Get the agent label
259 287 *
260 - * @param string $agent
288 + * @param string $agent Key representing agent.
261 289 *
262 290 * @return string
263 291 */
264 - function get_agent_label( $agent ) {
292 + public function get_agent_label( $agent ) {
265 293 if ( 'wp_cli' === $agent ) {
266 294 $label = esc_html__( 'via WP-CLI', 'stream' );
267 295 } elseif ( 'wp_cron' === $agent ) {
268 296 $label = esc_html__( 'during WP Cron', 'stream' );
@@ -272,9 +300,9 @@
272 300
273 301 /**
274 302 * Filter agent labels
275 303 *
276 - * @param string $agent
304 + * @param string $agent Key representing agent.
277 305 *
278 306 * @return string
279 307 */
280 308 $label = apply_filters( 'wp_stream_agent_label', $label, $agent );