PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.4
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.4
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 5.4, at modules/logs/classes/class-log-author.php

281 lines 7.8 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 display name of the user
106 *
107 * @return string
108 */
109 public function get_display_name() { //phpcs:ignore -- NOSONAR - complex.
110
111 // child users.
112 if ( isset( $this->meta['wp_user_id'] ) || isset( $this->meta['user_id'] ) ) { // 'user_id' to compare with old child wp.
113 if ( ! empty( $this->meta['action_user'] ) ) {
114 return esc_html( $this->meta['action_user'] );
115 } elseif ( ! empty( $this->meta['system_user_name'] ) ) {
116 return esc_html( $this->meta['system_user_name'] );
117 }
118 } elseif ( empty( $this->id ) ) { // dashboard user.
119 if ( isset( $this->meta['system_user_name'] ) ) {
120 return esc_html( $this->meta['system_user_name'] );
121 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
122 return 'WP-CLI'; // No translation needed.
123 }
124 } elseif ( $this->is_deleted() ) { // dashboard user.
125 if ( ! empty( $this->meta['display_name'] ) ) {
126 return $this->meta['display_name'];
127 } elseif ( ! empty( $this->meta['user_login'] ) ) {
128 return $this->meta['user_login'];
129 }
130 } elseif ( ! empty( $this->user ) ) { // dashboard user.
131 if ( ! empty( $this->user->display_name ) ) {
132 return $this->user->display_name;
133 } elseif ( ! empty( $this->user_login ) ) {
134 return $this->user->user_login;
135 }
136 }
137 return '';
138 }
139
140 /**
141 * Get the agent of the user
142 *
143 * @return string
144 */
145 public function get_agent() {
146 $agent = '';
147
148 if ( ! empty( $this->meta['agent'] ) ) {
149 $agent = $this->meta['agent'];
150 }
151
152 return $agent;
153 }
154
155 /**
156 * Tries to find a label for the record's user_role.
157 *
158 * If the user_role exists, use the label associated with it.
159 *
160 * Otherwise, if there is a user role label stored as Log meta then use that.
161 * Otherwise, if the user exists, use the label associated with their current role.
162 * Otherwise, use the role slug as the label.
163 *
164 * @return string
165 */
166 public function get_role() {
167 global $wp_roles;
168
169 $user_role = '';
170
171 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
172 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
173 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
174 $user_role = $this->meta['user_role_label'];
175 } elseif ( ! empty( $this->user->roles ) ) {
176 $roles = array_map(
177 function ( $role ) use ( $wp_roles ) {
178 return $wp_roles->role_names[ $role ];
179 },
180 $this->user->roles
181 );
182
183 $separator = apply_filters( 'mainwp_module_log_get_role_list_separator', ' - ' );
184 $user_role = implode( $separator, $roles );
185 } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
186 $user_role = $wp_roles->role_names['administrator'];
187 }
188
189 return $user_role;
190 }
191
192 /**
193 * True if user no longer exists, otherwise false
194 *
195 * @return bool
196 */
197 public function is_deleted() {
198 return 0 !== $this->id && ( empty( $this->user ) || empty( $this->user->ID ) );
199 }
200
201 /**
202 * True if user is WP-CLI, otherwise false
203 *
204 * @return bool
205 */
206 public function is_wp_cli() {
207 return 'wp_cli' === $this->get_agent();
208 }
209
210 /**
211 * Check if the current request is part of a WP cron task.
212 *
213 * Note: This will return true for all manual or custom
214 * cron runs even if the default front-end cron is disabled.
215 *
216 * We're not using `wp_doing_cron()` since it was introduced
217 * only in WordPress 4.8.0.
218 *
219 * @return bool
220 */
221 public function is_doing_wp_cron() {
222 return defined( 'DOING_CRON' ) && DOING_CRON;
223 }
224
225 /**
226 * Look at the environment to detect if an agent is being used
227 *
228 * @return string
229 */
230 public function get_current_agent() {
231 $agent = '';
232
233 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
234 $agent = 'wp_cli';
235 } elseif ( $this->is_doing_wp_cron() ) {
236 $agent = 'wp_cron';
237 } elseif ( defined( 'MAINWP_REST_API_DOING' ) && MAINWP_REST_API_DOING ) {
238 $agent = 'wp_rest_api';
239 }
240
241 /**
242 * Filter the current agent string
243 *
244 * @return string
245 */
246 $agent = apply_filters( 'mainwp_module_log_current_agent', $agent );
247
248 return $agent;
249 }
250
251 /**
252 * Get the agent label
253 *
254 * @param string $agent Key representing agent.
255 *
256 * @return string
257 */
258 public function get_agent_label( $agent ) {
259 if ( 'wp_cli' === $agent ) {
260 $label = esc_html__( 'via WP-CLI', 'mainwp' );
261 } elseif ( 'wp_cron' === $agent ) {
262 $label = esc_html__( 'during WP Cron', 'mainwp' );
263 } elseif ( 'wp_rest_api' === $agent ) {
264 $label = esc_html__( 'during WP REST API', 'mainwp' );
265 } else {
266 $label = '';
267 }
268
269 /**
270 * Filter agent labels
271 *
272 * @param string $agent Key representing agent.
273 *
274 * @return string
275 */
276 $label = apply_filters( 'mainwp_module_log_agent_label', $label, $agent );
277
278 return $label;
279 }
280 }
281