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

309 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WP_Stream;
3
4 class Author {
5 /**
6 * Hold Plugin class
7 * @var Plugin
8 */
9 public $plugin;
10
11 /**
12 * @var int
13 */
14 public $id;
15
16 /**
17 * @var array
18 */
19 public $meta = array();
20
21 /**
22 * @var \WP_User
23 */
24 protected $user;
25
26 /**
27 * Class constructor.
28 *
29 * @param int $user_id The user ID.
30 * @param array|string $user_meta The user meta array, or a serialized string of user meta.
31 */
32 function __construct( $user_id, $user_meta = array() ) {
33 $this->id = absint( $user_id );
34 $this->meta = maybe_unserialize( $user_meta );
35
36 if ( $this->id ) {
37 $this->user = new \WP_User( $this->id );
38 }
39
40 $this->plugin = wp_stream_get_instance();
41 }
42
43 /**
44 * Get various user meta data
45 *
46 * @param string $name
47 *
48 * @throws \Exception
49 *
50 * @return string
51 */
52 function __get( $name ) {
53 if ( 'display_name' === $name ) {
54 return $this->get_display_name();
55 } elseif ( 'avatar_img' === $name ) {
56 return $this->get_avatar_img();
57 } elseif ( 'avatar_src' === $name ) {
58 return $this->get_avatar_src();
59 } elseif ( 'role' === $name ) {
60 return $this->get_role();
61 } elseif ( 'agent' === $name ) {
62 return $this->get_agent();
63 } elseif ( ! empty( $this->user ) && 0 !== $this->user->ID ) {
64 return $this->user->$name;
65 } else {
66 throw new \Exception( "Unrecognized magic '$name'" );
67 }
68 }
69
70 /**
71 * Get the display name of the user
72 *
73 * @return string
74 */
75 function get_display_name() {
76 if ( 0 === $this->id ) {
77 if ( isset( $this->meta['system_user_name'] ) ) {
78 return esc_html( $this->meta['system_user_name'] );
79 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
80 return 'WP-CLI'; // No translation needed
81 }
82 return esc_html__( 'N/A', 'stream' );
83 } else {
84 if ( $this->is_deleted() ) {
85 if ( ! empty( $this->meta['display_name'] ) ) {
86 return $this->meta['display_name'];
87 } elseif ( ! empty( $this->meta['user_login'] ) ) {
88 return $this->meta['user_login'];
89 } else {
90 return esc_html__( 'N/A', 'stream' );
91 }
92 } elseif ( ! empty( $this->user->display_name ) ) {
93 return $this->user->display_name;
94 } else {
95 return $this->user->user_login;
96 }
97 }
98 }
99
100 /**
101 * Get the agent of the user
102 *
103 * @return string
104 */
105 function get_agent() {
106 $agent = '';
107
108 if ( ! empty( $this->meta['agent'] ) ) {
109 $agent = $this->meta['agent'];
110 } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
111 $agent = 'wp_cli'; // legacy
112 }
113
114 return $agent;
115 }
116
117 /**
118 * Return a Gravatar image as an HTML element.
119 *
120 * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.
121 *
122 * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80
123 *
124 * @return string|bool An img HTML element, or false if avatars are disabled
125 */
126 function get_avatar_img( $size = 80 ) {
127 if ( ! get_option( 'show_avatars' ) ) {
128 return false;
129 }
130
131 if ( 0 === $this->id ) {
132 $url = $this->plugin->locations['url'] . 'ui/stream-icons/wp-cli.png';
133 $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'] ) ) {
136 $email = $this->meta['user_email'];
137 $avatar = get_avatar( $email, $size );
138 } else {
139 $avatar = get_avatar( $this->id, $size );
140 }
141 }
142
143 return $avatar;
144 }
145
146 /**
147 * Return the URL of a Gravatar image.
148 *
149 * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80
150 *
151 * @return string|bool Gravatar image URL, or false on failure
152 */
153 function get_avatar_src( $size = 80 ) {
154 $img = $this->get_avatar_img( $size );
155
156 if ( ! $img ) {
157 return false;
158 }
159
160 if ( 1 === preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ) {
161 $src = html_entity_decode( $matches[2] );
162 } else {
163 return false;
164 }
165
166 return $src;
167 }
168
169 /**
170 * Tries to find a label for the record's user_role.
171 *
172 * If the user_role exists, use the label associated with it.
173 *
174 * Otherwise, if there is a user role label stored as Stream meta then use that.
175 * Otherwise, if the user exists, use the label associated with their current role.
176 * Otherwise, use the role slug as the label.
177 *
178 * @return string
179 */
180 function get_role() {
181 global $wp_roles;
182
183 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
184 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
185 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
186 $user_role = $this->meta['user_role_label'];
187 } elseif ( isset( $this->user->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) {
188 $user_role = $wp_roles->role_names[ $this->user->roles[0] ];
189 } else {
190 $user_role = '';
191 }
192
193 return $user_role;
194 }
195
196 /**
197 * Construct a URL for viewing user-specific records
198 *
199 * @return string
200 */
201 function get_records_page_url() {
202 $url = add_query_arg(
203 array(
204 'page' => $this->plugin->admin->records_page_slug,
205 'user_id' => absint( $this->id ),
206 ),
207 self_admin_url( $this->plugin->admin->admin_parent_page )
208 );
209
210 return $url;
211 }
212
213 /**
214 * True if user no longer exists, otherwise false
215 *
216 * @return bool
217 */
218 function is_deleted() {
219 return ( 0 !== $this->id && 0 === $this->user->ID );
220 }
221
222 /**
223 * True if user is WP-CLI, otherwise false
224 *
225 * @return bool
226 */
227 function is_wp_cli() {
228 return ( 'wp_cli' === $this->get_agent() );
229 }
230
231 /**
232 * True if doing WP Cron, otherwise false
233 *
234 * Note: If native WP Cron has been disabled and you are
235 * hitting the cron endpoint with a system cron job, this
236 * method will always return false.
237 *
238 * @return bool
239 */
240 function is_doing_wp_cron() {
241 return (
242 wp_stream_is_cron_enabled()
243 &&
244 defined( 'DOING_CRON' )
245 &&
246 DOING_CRON
247 );
248 }
249
250 /**
251 * @return string
252 */
253 function __toString() {
254 return $this->get_display_name();
255 }
256
257 /**
258 * Look at the environment to detect if an agent is being used
259 *
260 * @return string
261 */
262 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
285 *
286 * @return string
287 */
288 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
301 *
302 * @return string
303 */
304 $label = apply_filters( 'wp_stream_agent_label', $label, $agent );
305
306 return $label;
307 }
308 }
309