PluginProbe
Stream – Activity Log & Audit Trail / 3.8.0
Stream – Activity Log & Audit Trail v3.8.0
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 +77 -51 3.13.8.0 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,36 +49,43 @@
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 + *
63 85 * @return string
64 86 */
65 - function __toString() {
87 + public function __toString() {
66 88 return $this->get_display_name();
67 89 }
68 90
69 91 /**
@@ -70,14 +92,14 @@
70 92 * Get the display name of the user
71 93 *
72 94 * @return string
73 95 */
74 - function get_display_name() {
96 + public function get_display_name() {
75 97 if ( 0 === $this->id ) {
76 98 if ( isset( $this->meta['system_user_name'] ) ) {
77 99 return esc_html( $this->meta['system_user_name'] );
78 100 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
79 - return 'WP-CLI'; // No translation needed
101 + return 'WP-CLI'; // No translation needed.
80 102 }
81 103 return esc_html__( 'N/A', 'stream' );
82 104 } else {
83 105 if ( $this->is_deleted() ) {
@@ -100,15 +122,15 @@
100 122 * Get the agent of the user
101 123 *
102 124 * @return string
103 125 */
104 - function get_agent() {
126 + public function get_agent() {
105 127 $agent = '';
106 128
107 129 if ( ! empty( $this->meta['agent'] ) ) {
108 130 $agent = $this->meta['agent'];
109 131 } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
110 - $agent = 'wp_cli'; // legacy
132 + $agent = 'wp_cli'; // legacy.
111 133 }
112 134
113 135 return $agent;
114 136 }
@@ -117,13 +139,13 @@
117 139 * Return a Gravatar image as an HTML element.
118 140 *
119 141 * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.
120 142 *
121 - * @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.
122 144 *
123 145 * @return string|bool An img HTML element, or false if avatars are disabled
124 146 */
125 - function get_avatar_img( $size = 80 ) {
147 + public function get_avatar_img( $size = 80 ) {
126 148 if ( ! get_option( 'show_avatars' ) ) {
127 149 return false;
128 150 }
129 151
@@ -145,13 +167,13 @@
145 167
146 168 /**
147 169 * Return the URL of a Gravatar image.
148 170 *
149 - * @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.
150 172 *
151 173 * @return string|bool Gravatar image URL, or false on failure
152 174 */
153 - function get_avatar_src( $size = 80 ) {
175 + public function get_avatar_src( $size = 80 ) {
154 176 $img = $this->get_avatar_img( $size );
155 177
156 178 if ( ! $img ) {
157 179 return false;
@@ -176,9 +198,9 @@
176 198 * Otherwise, use the role slug as the label.
177 199 *
178 200 * @return string
179 201 */
180 - function get_role() {
202 + public function get_role() {
181 203 global $wp_roles;
182 204
183 205 $user_role = '';
184 206
@@ -185,10 +207,18 @@
185 207 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
186 208 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
187 209 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
188 210 $user_role = $this->meta['user_role_label'];
189 - } elseif ( isset( $this->user->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) {
190 - $user_role = $wp_roles->role_names[ $this->user->roles[0] ];
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 );
191 221 } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
192 222 $user_role = $wp_roles->role_names['administrator'];
193 223 }
194 224
@@ -199,9 +229,9 @@
199 229 * True if user no longer exists, otherwise false
200 230 *
201 231 * @return bool
202 232 */
203 - function is_deleted() {
233 + public function is_deleted() {
204 234 return ( 0 !== $this->id && 0 === $this->user->ID );
205 235 }
206 236
207 237 /**
@@ -208,29 +238,25 @@
208 238 * True if user is WP-CLI, otherwise false
209 239 *
210 240 * @return bool
211 241 */
212 - function is_wp_cli() {
242 + public function is_wp_cli() {
213 243 return ( 'wp_cli' === $this->get_agent() );
214 244 }
215 245
216 246 /**
217 - * True if doing WP Cron, otherwise false
247 + * Check if the current request is part of a WP cron task.
218 248 *
219 - * Note: If native WP Cron has been disabled and you are
220 - * hitting the cron endpoint with a system cron job, this
221 - * 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.
222 251 *
252 + * We're not using `wp_doing_cron()` since it was introduced
253 + * only in WordPress 4.8.0.
254 + *
223 255 * @return bool
224 256 */
225 - function is_doing_wp_cron() {
226 - return (
227 - wp_stream_is_cron_enabled()
228 - &&
229 - defined( 'DOING_CRON' )
230 - &&
231 - DOING_CRON
232 - );
257 + public function is_doing_wp_cron() {
258 + return ( defined( 'DOING_CRON' ) && DOING_CRON );
233 259 }
234 260
235 261 /**
236 262 * Look at the environment to detect if an agent is being used
@@ -236,9 +262,9 @@
236 262 * Look at the environment to detect if an agent is being used
237 263 *
238 264 * @return string
239 265 */
240 - function get_current_agent() {
266 + public function get_current_agent() {
241 267 $agent = '';
242 268
243 269 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
244 270 $agent = 'wp_cli';
@@ -258,13 +284,13 @@
258 284
259 285 /**
260 286 * Get the agent label
261 287 *
262 - * @param string $agent
288 + * @param string $agent Key representing agent.
263 289 *
264 290 * @return string
265 291 */
266 - function get_agent_label( $agent ) {
292 + public function get_agent_label( $agent ) {
267 293 if ( 'wp_cli' === $agent ) {
268 294 $label = esc_html__( 'via WP-CLI', 'stream' );
269 295 } elseif ( 'wp_cron' === $agent ) {
270 296 $label = esc_html__( 'during WP Cron', 'stream' );
@@ -274,9 +300,9 @@
274 300
275 301 /**
276 302 * Filter agent labels
277 303 *
278 - * @param string $agent
304 + * @param string $agent Key representing agent.
279 305 *
280 306 * @return string
281 307 */
282 308 $label = apply_filters( 'wp_stream_agent_label', $label, $agent );