PluginProbe
Stream – Activity Log & Audit Trail / 3.9.2
Stream – Activity Log & Audit Trail v3.9.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 +61 -35 3.2.33.9.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
@@ -34,33 +49,40 @@
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 61 public 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'" );
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 87 public function __toString() {
66 88 return $this->get_display_name();
@@ -75,9 +97,9 @@
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() ) {
@@ -106,9 +128,9 @@
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,9 +139,9 @@
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 147 public function get_avatar_img( $size = 80 ) {
@@ -145,9 +167,9 @@
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 175 public function get_avatar_src( $size = 80 ) {
@@ -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
@@ -213,24 +243,20 @@
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 257 public function is_doing_wp_cron() {
226 - return (
227 - wp_stream_is_cron_enabled()
228 - &&
229 - defined( 'DOING_CRON' )
230 - &&
231 - DOING_CRON
232 - );
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
@@ -258,9 +284,9 @@
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 292 public function get_agent_label( $agent ) {
@@ -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 );