PluginProbe
Stream – Activity Log & Audit Trail / 4.2.2
Stream – Activity Log & Audit Trail v4.2.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
stream / classes / class-author.php

class-author.php in Stream – Activity Log & Audit Trail 4.2.2, at classes/class-author.php

309 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manages a single user, acting as a model.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Author
12 */
13 class Author {
14 /**
15 * Holds User ID.
16 *
17 * @var int
18 */
19 public $id;
20
21 /**
22 * Holds User meta data.
23 *
24 * @var array
25 */
26 public $meta = array();
27
28 /**
29 * Holds WP user object connected to "$this" instance.
30 *
31 * @var \WP_User
32 */
33 protected $user;
34
35 /**
36 * Class constructor.
37 *
38 * @param int $user_id The user ID.
39 * @param array $user_meta The user meta array.
40 */
41 public function __construct( $user_id, $user_meta = array() ) {
42 $this->id = absint( $user_id );
43 $this->meta = $user_meta;
44
45 if ( $this->id ) {
46 $this->user = new \WP_User( $this->id );
47 }
48 }
49
50 /**
51 * Get various user meta data
52 *
53 * @todo Make sure this is being covered in the unit tests.
54 *
55 * @param string $name User meta key.
56 *
57 * @throws \Exception Meta not found | User not found.
58 *
59 * @return string
60 */
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.' );
79 }
80 }
81
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 /**
92 * Get the display name of the user
93 *
94 * @return string
95 */
96 public function get_display_name() {
97 if ( 0 === $this->id ) {
98 if ( isset( $this->meta['system_user_name'] ) ) {
99 return esc_html( $this->meta['system_user_name'] );
100 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
101 return 'WP-CLI'; // No translation needed.
102 }
103 return esc_html__( 'N/A', 'stream' );
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'];
109 } else {
110 return $this->id;
111 }
112 } elseif ( ! empty( $this->user->display_name ) ) {
113 return $this->user->display_name;
114 } else {
115 return $this->user->user_login;
116 }
117 }
118
119 /**
120 * Get the agent of the user
121 *
122 * @return string
123 */
124 public function get_agent() {
125 $agent = '';
126
127 if ( ! empty( $this->meta['agent'] ) ) {
128 $agent = $this->meta['agent'];
129 } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
130 $agent = 'wp_cli'; // legacy.
131 }
132
133 return $agent;
134 }
135
136 /**
137 * Return a Gravatar image as an HTML element.
138 *
139 * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.
140 *
141 * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
142 *
143 * @return string|bool An img HTML element, or false if avatars are disabled
144 */
145 public function get_avatar_img( $size = 80 ) {
146 if ( ! get_option( 'show_avatars' ) ) {
147 return false;
148 }
149
150 if ( 0 === $this->id ) {
151 $stream = wp_stream_get_instance();
152 $url = $stream->locations['url'] . 'assets/wp-cli.png';
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 ) );
154 } elseif ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) {
155 $email = $this->meta['user_email'];
156 $avatar = get_avatar( $email, $size );
157 } else {
158 $avatar = get_avatar( $this->id, $size );
159 }
160
161 return $avatar;
162 }
163
164 /**
165 * Return the URL of a Gravatar image.
166 *
167 * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
168 *
169 * @return string|bool Gravatar image URL, or false on failure
170 */
171 public function get_avatar_src( $size = 80 ) {
172 $img = $this->get_avatar_img( $size );
173
174 if ( ! $img ) {
175 return false;
176 }
177
178 if ( 1 === preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ) {
179 $src = html_entity_decode( $matches[2], ENT_COMPAT );
180 } else {
181 return false;
182 }
183
184 return $src;
185 }
186
187 /**
188 * Tries to find a label for the record's user_role.
189 *
190 * If the user_role exists, use the label associated with it.
191 *
192 * Otherwise, if there is a user role label stored as Stream meta then use that.
193 * Otherwise, if the user exists, use the label associated with their current role.
194 * Otherwise, use the role slug as the label.
195 *
196 * @return string
197 */
198 public function get_role() {
199 global $wp_roles;
200
201 $user_role = '';
202
203 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
204 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
205 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
206 $user_role = $this->meta['user_role_label'];
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 );
217 } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
218 $user_role = $wp_roles->role_names['administrator'];
219 }
220
221 return $user_role;
222 }
223
224 /**
225 * True if user no longer exists, otherwise false
226 *
227 * @return bool
228 */
229 public function is_deleted() {
230 return ( 0 !== $this->id && 0 === $this->user->ID );
231 }
232
233 /**
234 * True if user is WP-CLI, otherwise false
235 *
236 * @return bool
237 */
238 public function is_wp_cli() {
239 return ( 'wp_cli' === $this->get_agent() );
240 }
241
242 /**
243 * Check if the current request is part of a WP cron task.
244 *
245 * Note: This will return true for all manual or custom
246 * cron runs even if the default front-end cron is disabled.
247 *
248 * We're not using `wp_doing_cron()` since it was introduced
249 * only in WordPress 4.8.0.
250 *
251 * @return bool
252 */
253 public function is_doing_wp_cron() {
254 return ( defined( 'DOING_CRON' ) && DOING_CRON );
255 }
256
257 /**
258 * Look at the environment to detect if an agent is being used
259 *
260 * @return string
261 */
262 public function get_current_agent() {
263 $agent = '';
264
265 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
266 $agent = 'wp_cli';
267 } elseif ( $this->is_doing_wp_cron() ) {
268 $agent = 'wp_cron';
269 }
270
271 /**
272 * Filter the current agent string
273 *
274 * @return string
275 */
276 $agent = apply_filters( 'wp_stream_current_agent', $agent );
277
278 return $agent;
279 }
280
281 /**
282 * Get the agent label
283 *
284 * @param string $agent Key representing agent.
285 *
286 * @return string
287 */
288 public function get_agent_label( $agent ) {
289 if ( 'wp_cli' === $agent ) {
290 $label = esc_html__( 'via WP-CLI', 'stream' );
291 } elseif ( 'wp_cron' === $agent ) {
292 $label = esc_html__( 'during WP Cron', 'stream' );
293 } else {
294 $label = '';
295 }
296
297 /**
298 * Filter agent labels
299 *
300 * @param string $agent Key representing agent.
301 *
302 * @return string
303 */
304 $label = apply_filters( 'wp_stream_agent_label', $label, $agent );
305
306 return $label;
307 }
308 }
309