PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
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 +88 -66 3.2.14.0.2 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
@@ -22,9 +37,9 @@
22 37 *
23 38 * @param int $user_id The user ID.
24 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 43 $this->meta = $user_meta;
29 44
30 45 if ( $this->id ) {
@@ -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( sprintf( "Unrecognized magic '%s'", esc_attr( $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,30 +92,28 @@
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 - } else {
83 - if ( $this->is_deleted() ) {
84 - if ( ! empty( $this->meta['display_name'] ) ) {
85 - return $this->meta['display_name'];
86 - } elseif ( ! empty( $this->meta['user_login'] ) ) {
87 - return $this->meta['user_login'];
88 - } else {
89 - return esc_html__( 'N/A', 'stream' );
90 - }
91 - } elseif ( ! empty( $this->user->display_name ) ) {
92 - return $this->user->display_name;
104 + } elseif ( $this->is_deleted() ) {
105 + if ( ! empty( $this->meta['display_name'] ) ) {
106 + return $this->meta['display_name'];
107 + } elseif ( ! empty( $this->meta['user_login'] ) ) {
108 + return $this->meta['user_login'];
93 109 } else {
94 - return $this->user->user_login;
110 + return $this->id;
95 111 }
112 + } elseif ( ! empty( $this->user->display_name ) ) {
113 + return $this->user->display_name;
114 + } else {
115 + return $this->user->user_login;
96 116 }
97 117 }
98 118
99 119 /**
@@ -100,15 +120,15 @@
100 120 * Get the agent of the user
101 121 *
102 122 * @return string
103 123 */
104 - function get_agent() {
124 + public function get_agent() {
105 125 $agent = '';
106 126
107 127 if ( ! empty( $this->meta['agent'] ) ) {
108 128 $agent = $this->meta['agent'];
109 129 } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
110 - $agent = 'wp_cli'; // legacy
130 + $agent = 'wp_cli'; // legacy.
111 131 }
112 132
113 133 return $agent;
114 134 }
@@ -117,13 +137,13 @@
117 137 * Return a Gravatar image as an HTML element.
118 138 *
119 139 * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.
120 140 *
121 - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80
141 + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
122 142 *
123 143 * @return string|bool An img HTML element, or false if avatars are disabled
124 144 */
125 - function get_avatar_img( $size = 80 ) {
145 + public function get_avatar_img( $size = 80 ) {
126 146 if ( ! get_option( 'show_avatars' ) ) {
127 147 return false;
128 148 }
129 149
@@ -130,15 +150,13 @@
130 150 if ( 0 === $this->id ) {
131 151 $stream = wp_stream_get_instance();
132 152 $url = $stream->locations['url'] . 'ui/stream-icons/wp-cli.png';
133 153 $avatar = sprintf( '<img alt="%1$s" src="%2$s" class="avatar avatar-%3$s photo" height="%3$s" width="%3$s">', esc_attr( $this->get_display_name() ), esc_url( $url ), esc_attr( $size ) );
134 - } else {
135 - if ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) {
154 + } elseif ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) {
136 155 $email = $this->meta['user_email'];
137 156 $avatar = get_avatar( $email, $size );
138 - } else {
139 - $avatar = get_avatar( $this->id, $size );
140 - }
157 + } else {
158 + $avatar = get_avatar( $this->id, $size );
141 159 }
142 160
143 161 return $avatar;
144 162 }
@@ -145,13 +163,13 @@
145 163
146 164 /**
147 165 * Return the URL of a Gravatar image.
148 166 *
149 - * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80
167 + * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
150 168 *
151 169 * @return string|bool Gravatar image URL, or false on failure
152 170 */
153 - function get_avatar_src( $size = 80 ) {
171 + public function get_avatar_src( $size = 80 ) {
154 172 $img = $this->get_avatar_img( $size );
155 173
156 174 if ( ! $img ) {
157 175 return false;
@@ -157,9 +175,9 @@
157 175 return false;
158 176 }
159 177
160 178 if ( 1 === preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ) {
161 - $src = html_entity_decode( $matches[2] );
179 + $src = html_entity_decode( $matches[2], ENT_COMPAT );
162 180 } else {
163 181 return false;
164 182 }
165 183
@@ -176,9 +194,9 @@
176 194 * Otherwise, use the role slug as the label.
177 195 *
178 196 * @return string
179 197 */
180 - function get_role() {
198 + public function get_role() {
181 199 global $wp_roles;
182 200
183 201 $user_role = '';
184 202
@@ -185,10 +203,18 @@
185 203 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
186 204 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
187 205 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
188 206 $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] ];
207 + } elseif ( ! empty( $this->user->roles ) ) {
208 + $roles = array_map(
209 + function ( $role ) use ( $wp_roles ) {
210 + return $wp_roles->role_names[ $role ];
211 + },
212 + $this->user->roles
213 + );
214 +
215 + $separator = apply_filters( 'wp_stream_get_role_list_separator', ' - ' );
216 + $user_role = implode( $separator, $roles );
191 217 } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
192 218 $user_role = $wp_roles->role_names['administrator'];
193 219 }
194 220
@@ -199,9 +225,9 @@
199 225 * True if user no longer exists, otherwise false
200 226 *
201 227 * @return bool
202 228 */
203 - function is_deleted() {
229 + public function is_deleted() {
204 230 return ( 0 !== $this->id && 0 === $this->user->ID );
205 231 }
206 232
207 233 /**
@@ -208,29 +234,25 @@
208 234 * True if user is WP-CLI, otherwise false
209 235 *
210 236 * @return bool
211 237 */
212 - function is_wp_cli() {
238 + public function is_wp_cli() {
213 239 return ( 'wp_cli' === $this->get_agent() );
214 240 }
215 241
216 242 /**
217 - * True if doing WP Cron, otherwise false
243 + * Check if the current request is part of a WP cron task.
218 244 *
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.
245 + * Note: This will return true for all manual or custom
246 + * cron runs even if the default front-end cron is disabled.
222 247 *
248 + * We're not using `wp_doing_cron()` since it was introduced
249 + * only in WordPress 4.8.0.
250 + *
223 251 * @return bool
224 252 */
225 - function is_doing_wp_cron() {
226 - return (
227 - wp_stream_is_cron_enabled()
228 - &&
229 - defined( 'DOING_CRON' )
230 - &&
231 - DOING_CRON
232 - );
253 + public function is_doing_wp_cron() {
254 + return ( defined( 'DOING_CRON' ) && DOING_CRON );
233 255 }
234 256
235 257 /**
236 258 * Look at the environment to detect if an agent is being used
@@ -236,9 +258,9 @@
236 258 * Look at the environment to detect if an agent is being used
237 259 *
238 260 * @return string
239 261 */
240 - function get_current_agent() {
262 + public function get_current_agent() {
241 263 $agent = '';
242 264
243 265 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
244 266 $agent = 'wp_cli';
@@ -258,13 +280,13 @@
258 280
259 281 /**
260 282 * Get the agent label
261 283 *
262 - * @param string $agent
284 + * @param string $agent Key representing agent.
263 285 *
264 286 * @return string
265 287 */
266 - function get_agent_label( $agent ) {
288 + public function get_agent_label( $agent ) {
267 289 if ( 'wp_cli' === $agent ) {
268 290 $label = esc_html__( 'via WP-CLI', 'stream' );
269 291 } elseif ( 'wp_cron' === $agent ) {
270 292 $label = esc_html__( 'during WP Cron', 'stream' );
@@ -274,9 +296,9 @@
274 296
275 297 /**
276 298 * Filter agent labels
277 299 *
278 - * @param string $agent
300 + * @param string $agent Key representing agent.
279 301 *
280 302 * @return string
281 303 */
282 304 $label = apply_filters( 'wp_stream_agent_label', $label, $agent );