PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / logs / classes / class-log-author.php

class-log-author.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at modules/logs/classes/class-log-author.php

315 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Logs Author class.
4 *
5 * @package MainWP\Dashboard\Module\Log
6 * @version 4.5.1
7 */
8
9 namespace MainWP\Dashboard\Module\Log;
10
11 defined( 'ABSPATH' ) || exit;
12
13 use MainWP\Dashboard\MainWP_Exception;
14
15 /**
16 * Class Log_Author.
17 *
18 * @package MainWP\Dashboard.
19 */
20 class Log_Author {
21
22 /**
23 * Holds User ID.
24 *
25 * @var int
26 */
27 public $id;
28
29 /**
30 * Holds User meta data.
31 *
32 * @var array
33 */
34 public $meta = array();
35
36 /**
37 * Holds WP user object connected to "$this" instance.
38 *
39 * @var \WP_User
40 */
41 protected $user;
42
43 /**
44 * Class constructor.
45 *
46 * @param int $user_id The user ID.
47 * @param array $user_meta The user meta array.
48 */
49 public function __construct( $user_id, $user_meta = array() ) {
50
51 if ( ! is_array( $user_meta ) ) {
52 $user_meta = array();
53 }
54
55 $this->id = absint( $user_id );
56 $this->meta = $user_meta;
57
58 if ( $this->id && ( ! isset( $user_meta['wp_user_id'] ) && ! isset( $user_meta['user_id'] ) ) ) {
59 $this->user = new \WP_User( $this->id ); // if is not child user id, load the dashboard user.
60 }
61 }
62
63 /**
64 * Get various user meta data
65 *
66 * @devtodo Make sure this is being covered in the unit tests.
67 *
68 * @param string $name User meta key.
69 *
70 * @throws \MainWP_Exception Meta not found | User not found.
71 *
72 * @return string
73 */
74 public function __get( $name ) {
75 switch ( $name ) {
76 case 'display_name':
77 case 'avatar_img':
78 case 'avatar_src':
79 case 'role':
80 case 'agent':
81 $getter = "get_{$name}";
82 return $this->$getter();
83 default:
84 if ( ! empty( $this->user ) && 0 !== $this->user->ID ) {
85 if ( is_null( $this->user->$name ) ) {
86 throw new MainWP_Exception( "Unrecognized magic '$name'" ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
87 }
88 return $this->user->$name;
89 }
90
91 throw new MainWP_Exception( 'User not found.' );
92 }
93 }
94
95 /**
96 * Returns string representation of this object
97 *
98 * @return string
99 */
100 public function __toString() {
101 return $this->get_display_name();
102 }
103
104 /**
105 * Get the full name display of the user
106 *
107 * @return string
108 */
109 public function get_full_name() { //phpcs:ignore -- NOSONAR - complex.
110 $fullname = '';
111 // child users.
112 if ( ! empty( $this->meta['full_name'] ) ) {
113 $fullname = esc_html( $this->meta['full_name'] );
114 }
115 if ( empty( $fullname ) && ! empty( $this->meta['display_name'] ) ) {
116 $fullname = esc_html( $this->meta['display_name'] );
117 }
118
119 if ( empty( $fullname ) && isset( $this->meta['first_name'] ) && isset( $this->meta['last_name'] ) ) {
120 $first_name = trim( (string) $this->meta['first_name'] );
121 $last_name = trim( (string) $this->meta['last_name'] );
122 $full_name = trim( $first_name . ' ' . $last_name );
123 if ( '' !== $full_name ) {
124 $fullname = esc_html( $full_name );
125 }
126 }
127
128 if ( empty( $fullname ) && ! empty( $this->meta['username'] ) ) {
129 $fullname = esc_html( $this->meta['username'] );
130 }
131 return $fullname;
132 }
133
134 /**
135 * Get the display name of the user
136 *
137 * @return string
138 */
139 public function get_display_name() { //phpcs:ignore -- NOSONAR - complex.
140
141 // child users.
142 if ( isset( $this->meta['wp_user_id'] ) || isset( $this->meta['user_id'] ) ) { // 'user_id' to compare with old child wp.
143 if ( ! empty( $this->meta['action_user'] ) ) {
144 $act_user = esc_html( $this->meta['action_user'] );
145 if ( 'wp_cron' === $act_user ) {
146 $act_user = 'during WP Cron';
147 }
148 return $act_user;
149 } elseif ( ! empty( $this->meta['system_user_name'] ) ) {
150 return esc_html( $this->meta['system_user_name'] );
151 }
152 } elseif ( empty( $this->id ) ) { // dashboard user.
153 if ( isset( $this->meta['system_user_name'] ) ) {
154 return esc_html( $this->meta['system_user_name'] );
155 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
156 return 'WP-CLI'; // No translation needed.
157 }
158 } elseif ( $this->is_deleted() ) { // dashboard user.
159 if ( ! empty( $this->meta['user_login'] ) ) {
160 return $this->meta['user_login'];
161 } elseif ( ! empty( $this->meta['display_name'] ) ) {
162 return $this->meta['display_name'];
163 }
164 } elseif ( ! empty( $this->user ) ) { // dashboard user.
165 if ( ! empty( $this->user_login ) ) {
166 return $this->user->user_login;
167 } elseif ( ! empty( $this->user->display_name ) ) {
168 return $this->user->display_name;
169 }
170 }
171 return '';
172 }
173
174 /**
175 * Get the agent of the user
176 *
177 * @return string
178 */
179 public function get_agent() {
180 $agent = '';
181
182 if ( ! empty( $this->meta['agent'] ) ) {
183 $agent = $this->meta['agent'];
184 }
185
186 return $agent;
187 }
188
189 /**
190 * Tries to find a label for the record's user_role.
191 *
192 * If the user_role exists, use the label associated with it.
193 *
194 * Otherwise, if there is a user role label stored as Log meta then use that.
195 * Otherwise, if the user exists, use the label associated with their current role.
196 * Otherwise, use the role slug as the label.
197 *
198 * @return string
199 */
200 public function get_role() {
201 global $wp_roles;
202
203 $user_role = '';
204
205 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
206 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
207 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
208 $user_role = $this->meta['user_role_label'];
209 } elseif ( ! empty( $this->user->roles ) ) {
210 $roles = array_map(
211 function ( $role ) use ( $wp_roles ) {
212 return $wp_roles->role_names[ $role ];
213 },
214 $this->user->roles
215 );
216
217 $separator = apply_filters( 'mainwp_module_log_get_role_list_separator', ' - ' );
218 $user_role = implode( $separator, $roles );
219 } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
220 $user_role = $wp_roles->role_names['administrator'];
221 }
222
223 return $user_role;
224 }
225
226 /**
227 * True if user no longer exists, otherwise false
228 *
229 * @return bool
230 */
231 public function is_deleted() {
232 return 0 !== $this->id && ( empty( $this->user ) || empty( $this->user->ID ) );
233 }
234
235 /**
236 * True if user is WP-CLI, otherwise false
237 *
238 * @return bool
239 */
240 public function is_wp_cli() {
241 return 'wp_cli' === $this->get_agent();
242 }
243
244 /**
245 * Check if the current request is part of a WP cron task.
246 *
247 * Note: This will return true for all manual or custom
248 * cron runs even if the default front-end cron is disabled.
249 *
250 * We're not using `wp_doing_cron()` since it was introduced
251 * only in WordPress 4.8.0.
252 *
253 * @return bool
254 */
255 public function is_doing_wp_cron() {
256 return defined( 'DOING_CRON' ) && DOING_CRON;
257 }
258
259 /**
260 * Look at the environment to detect if an agent is being used
261 *
262 * @return string
263 */
264 public function get_current_agent() {
265 $agent = '';
266
267 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
268 $agent = 'wp_cli';
269 } elseif ( $this->is_doing_wp_cron() ) {
270 $agent = 'wp_cron';
271 } elseif ( defined( 'MAINWP_REST_API_DOING' ) && MAINWP_REST_API_DOING ) {
272 $agent = 'wp_rest_api';
273 }
274
275 /**
276 * Filter the current agent string
277 *
278 * @return string
279 */
280 $agent = apply_filters( 'mainwp_module_log_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', 'mainwp' );
295 } elseif ( 'wp_cron' === $agent ) {
296 $label = esc_html__( 'during WP Cron', 'mainwp' );
297 } elseif ( 'wp_rest_api' === $agent ) {
298 $label = esc_html__( 'during WP REST API', 'mainwp' );
299 } else {
300 $label = '';
301 }
302
303 /**
304 * Filter agent labels
305 *
306 * @param string $agent Key representing agent.
307 *
308 * @return string
309 */
310 $label = apply_filters( 'mainwp_module_log_agent_label', $label, $agent );
311
312 return $label;
313 }
314 }
315