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

268 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 * 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 $this->id = absint( $user_id );
51 $this->meta = $user_meta;
52
53 if ( $this->id ) {
54 $this->user = new \WP_User( $this->id );
55 }
56 }
57
58 /**
59 * Get various user meta data
60 *
61 * @devtodo Make sure this is being covered in the unit tests.
62 *
63 * @param string $name User meta key.
64 *
65 * @throws \MainWP_Exception Meta not found | User not found.
66 *
67 * @return string
68 */
69 public function __get( $name ) {
70 switch ( $name ) {
71 case 'display_name':
72 case 'avatar_img':
73 case 'avatar_src':
74 case 'role':
75 case 'agent':
76 $getter = "get_{$name}";
77 return $this->$getter();
78 default:
79 if ( ! empty( $this->user ) && 0 !== $this->user->ID ) {
80 if ( is_null( $this->user->$name ) ) {
81 throw new MainWP_Exception( "Unrecognized magic '$name'" ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
82 }
83 return $this->user->$name;
84 }
85
86 throw new MainWP_Exception( 'User not found.' );
87 }
88 }
89
90 /**
91 * Returns string representation of this object
92 *
93 * @return string
94 */
95 public function __toString() {
96 return $this->get_display_name();
97 }
98
99 /**
100 * Get the display name of the user
101 *
102 * @return string
103 */
104 public function get_display_name() {
105 if ( empty( $this->id ) ) {
106 if ( isset( $this->meta['system_user_name'] ) ) {
107 return esc_html( $this->meta['system_user_name'] );
108 } elseif ( 'wp_cli' === $this->get_current_agent() ) {
109 return 'WP-CLI'; // No translation needed.
110 }
111 return esc_html__( 'N/A', 'mainwp' );
112 } elseif ( $this->is_deleted() ) {
113 if ( ! empty( $this->meta['display_name'] ) ) {
114 return $this->meta['display_name'];
115 } elseif ( ! empty( $this->meta['user_login'] ) ) {
116 return $this->meta['user_login'];
117 } else {
118 return esc_html__( 'N/A', 'mainwp' );
119 }
120 } elseif ( ! empty( $this->user->display_name ) ) {
121 return $this->user->display_name;
122 } else {
123 return $this->user->user_login;
124 }
125 }
126
127 /**
128 * Get the agent of the user
129 *
130 * @return string
131 */
132 public function get_agent() {
133 $agent = '';
134
135 if ( ! empty( $this->meta['agent'] ) ) {
136 $agent = $this->meta['agent'];
137 }
138
139 return $agent;
140 }
141
142 /**
143 * Tries to find a label for the record's user_role.
144 *
145 * If the user_role exists, use the label associated with it.
146 *
147 * Otherwise, if there is a user role label stored as Log meta then use that.
148 * Otherwise, if the user exists, use the label associated with their current role.
149 * Otherwise, use the role slug as the label.
150 *
151 * @return string
152 */
153 public function get_role() {
154 global $wp_roles;
155
156 $user_role = '';
157
158 if ( ! empty( $this->meta['user_role'] ) && isset( $wp_roles->role_names[ $this->meta['user_role'] ] ) ) {
159 $user_role = $wp_roles->role_names[ $this->meta['user_role'] ];
160 } elseif ( ! empty( $this->meta['user_role_label'] ) ) {
161 $user_role = $this->meta['user_role_label'];
162 } elseif ( ! empty( $this->user->roles ) ) {
163 $roles = array_map(
164 function ( $role ) use ( $wp_roles ) {
165 return $wp_roles->role_names[ $role ];
166 },
167 $this->user->roles
168 );
169
170 $separator = apply_filters( 'mainwp_module_log_get_role_list_separator', ' - ' );
171 $user_role = implode( $separator, $roles );
172 } elseif ( is_multisite() && is_super_admin( $this->id ) ) {
173 $user_role = $wp_roles->role_names['administrator'];
174 }
175
176 return $user_role;
177 }
178
179 /**
180 * True if user no longer exists, otherwise false
181 *
182 * @return bool
183 */
184 public function is_deleted() {
185 return 0 !== $this->id && empty( $this->user->ID );
186 }
187
188 /**
189 * True if user is WP-CLI, otherwise false
190 *
191 * @return bool
192 */
193 public function is_wp_cli() {
194 return 'wp_cli' === $this->get_agent();
195 }
196
197 /**
198 * Check if the current request is part of a WP cron task.
199 *
200 * Note: This will return true for all manual or custom
201 * cron runs even if the default front-end cron is disabled.
202 *
203 * We're not using `wp_doing_cron()` since it was introduced
204 * only in WordPress 4.8.0.
205 *
206 * @return bool
207 */
208 public function is_doing_wp_cron() {
209 return defined( 'DOING_CRON' ) && DOING_CRON;
210 }
211
212 /**
213 * Look at the environment to detect if an agent is being used
214 *
215 * @return string
216 */
217 public function get_current_agent() {
218 $agent = '';
219
220 if ( defined( '\WP_CLI' ) && \WP_CLI ) {
221 $agent = 'wp_cli';
222 } elseif ( $this->is_doing_wp_cron() ) {
223 $agent = 'wp_cron';
224 } elseif ( defined( 'MAINWP_REST_API_DOING' ) && MAINWP_REST_API_DOING ) {
225 $agent = 'wp_rest_api';
226 }
227
228 /**
229 * Filter the current agent string
230 *
231 * @return string
232 */
233 $agent = apply_filters( 'mainwp_module_log_current_agent', $agent );
234
235 return $agent;
236 }
237
238 /**
239 * Get the agent label
240 *
241 * @param string $agent Key representing agent.
242 *
243 * @return string
244 */
245 public function get_agent_label( $agent ) {
246 if ( 'wp_cli' === $agent ) {
247 $label = esc_html__( 'via WP-CLI', 'mainwp' );
248 } elseif ( 'wp_cron' === $agent ) {
249 $label = esc_html__( 'during WP Cron', 'mainwp' );
250 } elseif ( 'wp_rest_api' === $agent ) {
251 $label = esc_html__( 'during WP REST API', 'mainwp' );
252 } else {
253 $label = '';
254 }
255
256 /**
257 * Filter agent labels
258 *
259 * @param string $agent Key representing agent.
260 *
261 * @return string
262 */
263 $label = apply_filters( 'mainwp_module_log_agent_label', $label, $agent );
264
265 return $label;
266 }
267 }
268