PluginProbe
Stream – Activity Log & Audit Trail / 3.9.3
Stream – Activity Log & Audit Trail v3.9.3
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 3.9.3, at classes/class-author.php

313 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( "Unrecognized magic '$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 } else {
105 if ( $this->is_deleted() ) {
106 if ( ! empty( $this->meta['display_name'] ) ) {
107 return $this->meta['display_name'];
108 } elseif ( ! empty( $this->meta['user_login'] ) ) {
109 return $this->meta['user_login'];
110 } else {
111 return esc_html__( 'N/A', 'stream' );
112 }
113 } elseif ( ! empty( $this->user->display_name ) ) {
114 return $this->user->display_name;
115 } else {
116 return $this->user->user_login;
117 }
118 }
119 }
120
121 /**
122 * Get the agent of the user
123 *
124 * @return string
125 */
126 public function get_agent() {
127 $agent = '';
128
129 if ( ! empty( $this->meta['agent'] ) ) {
130 $agent = $this->meta['agent'];
131 } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
132 $agent = 'wp_cli'; // legacy.
133 }
134
135 return $agent;
136 }
137
138 /**
139 * Return a Gravatar image as an HTML element.
140 *
141 * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.
142 *
143 * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
144 *
145 * @return string|bool An img HTML element, or false if avatars are disabled
146 */
147 public function get_avatar_img( $size = 80 ) {
148 if ( ! get_option( 'show_avatars' ) ) {
149 return false;
150 }
151
152 if ( 0 === $this->id ) {
153 $stream = wp_stream_get_instance();
154 $url = $stream->locations['url'] . 'ui/stream-icons/wp-cli.png';
155 $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 ) );
156 } else {
157 if ( $this->is_deleted() && isset( $this->meta['user_email'] ) ) {
158 $email = $this->meta['user_email'];
159 $avatar = get_avatar( $email, $size );
160 } else {
161 $avatar = get_avatar( $this->id, $size );
162 }
163 }
164
165 return $avatar;
166 }
167
168 /**
169 * Return the URL of a Gravatar image.
170 *
171 * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80.
172 *
173 * @return string|bool Gravatar image URL, or false on failure
174 */
175 public function get_avatar_src( $size = 80 ) {
176 $img = $this->get_avatar_img( $size );
177
178 if ( ! $img ) {
179 return false;
180 }
181
182 if ( 1 === preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ) {
183 $src = html_entity_decode( $matches[2] );
184 } else {
185 return false;
186 }
187
188 return $src;
189 }
190
191 /**
192 * Tries to find a label for the record's user_role.
193 *
194 * If the user_role exists, use the label associated with it.
195 *
196 * Otherwise, if there is a user role label stored as Stream meta then use that.
197 * Otherwise, if the user exists, use the label associated with their current role.
198 * Otherwise, use the role slug as the label.
199 *
200 * @return string
201 */
202 public function get_role() {
203 global $wp_roles;
204
205 $user_role = '';
206
207 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
208 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
209 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
210 $user_role = $this->meta['user_role_label'];
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'];
223 }
224
225 return $user_role;
226 }
227
228 /**
229 * True if user no longer exists, otherwise false
230 *
231 * @return bool
232 */
233 public function is_deleted() {
234 return ( 0 !== $this->id && 0 === $this->user->ID );
235 }
236
237 /**
238 * True if user is WP-CLI, otherwise false
239 *
240 * @return bool
241 */
242 public function is_wp_cli() {
243 return ( 'wp_cli' === $this->get_agent() );
244 }
245
246 /**
247 * Check if the current request is part of a WP cron task.
248 *
249 * Note: This will return true for all manual or custom
250 * cron runs even if the default front-end cron is disabled.
251 *
252 * We're not using `wp_doing_cron()` since it was introduced
253 * only in WordPress 4.8.0.
254 *
255 * @return bool
256 */
257 public function is_doing_wp_cron() {
258 return ( defined( 'DOING_CRON' ) && DOING_CRON );
259 }
260
261 /**
262 * Look at the environment to detect if an agent is being used
263 *
264 * @return string
265 */
266 public function get_current_agent() {
267 $agent = '';
268
269 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
270 $agent = 'wp_cli';
271 } elseif ( $this->is_doing_wp_cron() ) {
272 $agent = 'wp_cron';
273 }
274
275 /**
276 * Filter the current agent string
277 *
278 * @return string
279 */
280 $agent = apply_filters( 'wp_stream_current_agent', $agent );
281
282 return $agent;
283 }
284
285 /**
286 * Get the agent label
287 *
288 * @param string $agent Key representing agent.
289 *
290 * @return string
291 */
292 public function get_agent_label( $agent ) {
293 if ( 'wp_cli' === $agent ) {
294 $label = esc_html__( 'via WP-CLI', 'stream' );
295 } elseif ( 'wp_cron' === $agent ) {
296 $label = esc_html__( 'during WP Cron', 'stream' );
297 } else {
298 $label = '';
299 }
300
301 /**
302 * Filter agent labels
303 *
304 * @param string $agent Key representing agent.
305 *
306 * @return string
307 */
308 $label = apply_filters( 'wp_stream_agent_label', $label, $agent );
309
310 return $label;
311 }
312 }
313