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

287 lines 6.5 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 * @var int
7 */
8 public $id;
9
10 /**
11 * @var array
12 */
13 public $meta = array();
14
15 /**
16 * @var \WP_User
17 */
18 protected $user;
19
20 /**
21 * Class constructor.
22 *
23 * @param int $user_id The user ID.
24 * @param array|string $user_meta The user meta array, or a serialized string of user meta.
25 */
26 function __construct( $user_id, $user_meta = array() ) {
27 $this->id = absint( $user_id );
28 $this->meta = maybe_unserialize( $user_meta );
29
30 if ( $this->id ) {
31 $this->user = new \WP_User( $this->id );
32 }
33 }
34
35 /**
36 * Get various user meta data
37 *
38 * @param string $name
39 *
40 * @throws \Exception
41 *
42 * @return string
43 */
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'" );
59 }
60 }
61
62 /**
63 * @return string
64 */
65 function __toString() {
66 return $this->get_display_name();
67 }
68
69 /**
70 * Get the display name of the user
71 *
72 * @return string
73 */
74 function get_display_name() {
75 if ( 0 === $this->id ) {
76 if ( isset( $this->meta['system_user_name'] ) ) {
77 return esc_html( $this->meta['system_user_name'] );
78 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
79 return 'WP-CLI'; // No translation needed
80 }
81 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;
93 } else {
94 return $this->user->user_login;
95 }
96 }
97 }
98
99 /**
100 * Get the agent of the user
101 *
102 * @return string
103 */
104 function get_agent() {
105 $agent = '';
106
107 if ( ! empty( $this->meta['agent'] ) ) {
108 $agent = $this->meta['agent'];
109 } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) {
110 $agent = 'wp_cli'; // legacy
111 }
112
113 return $agent;
114 }
115
116 /**
117 * Return a Gravatar image as an HTML element.
118 *
119 * This function will not return an avatar if "Show Avatars" is unchecked in Settings > Discussion.
120 *
121 * @param int $size (optional) Size of Gravatar to return (in pixels), max is 512, default is 80
122 *
123 * @return string|bool An img HTML element, or false if avatars are disabled
124 */
125 function get_avatar_img( $size = 80 ) {
126 if ( ! get_option( 'show_avatars' ) ) {
127 return false;
128 }
129
130 if ( 0 === $this->id ) {
131 $stream = wp_stream_get_instance();
132 $url = $stream->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 $user_role = '';
184
185 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
186 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
187 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
188 $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] ];
191 } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
192 $user_role = $wp_roles->role_names['administrator'];
193 }
194
195 return $user_role;
196 }
197
198 /**
199 * True if user no longer exists, otherwise false
200 *
201 * @return bool
202 */
203 function is_deleted() {
204 return ( 0 !== $this->id && 0 === $this->user->ID );
205 }
206
207 /**
208 * True if user is WP-CLI, otherwise false
209 *
210 * @return bool
211 */
212 function is_wp_cli() {
213 return ( 'wp_cli' === $this->get_agent() );
214 }
215
216 /**
217 * True if doing WP Cron, otherwise false
218 *
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.
222 *
223 * @return bool
224 */
225 function is_doing_wp_cron() {
226 return (
227 wp_stream_is_cron_enabled()
228 &&
229 defined( 'DOING_CRON' )
230 &&
231 DOING_CRON
232 );
233 }
234
235 /**
236 * Look at the environment to detect if an agent is being used
237 *
238 * @return string
239 */
240 function get_current_agent() {
241 $agent = '';
242
243 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
244 $agent = 'wp_cli';
245 } elseif ( $this->is_doing_wp_cron() ) {
246 $agent = 'wp_cron';
247 }
248
249 /**
250 * Filter the current agent string
251 *
252 * @return string
253 */
254 $agent = apply_filters( 'wp_stream_current_agent', $agent );
255
256 return $agent;
257 }
258
259 /**
260 * Get the agent label
261 *
262 * @param string $agent
263 *
264 * @return string
265 */
266 function get_agent_label( $agent ) {
267 if ( 'wp_cli' === $agent ) {
268 $label = esc_html__( 'via WP-CLI', 'stream' );
269 } elseif ( 'wp_cron' === $agent ) {
270 $label = esc_html__( 'during WP Cron', 'stream' );
271 } else {
272 $label = '';
273 }
274
275 /**
276 * Filter agent labels
277 *
278 * @param string $agent
279 *
280 * @return string
281 */
282 $label = apply_filters( 'wp_stream_agent_label', $label, $agent );
283
284 return $label;
285 }
286 }
287