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

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