| @@ -9,8 +9,10 @@ | ||
| 9 | 9 | namespace MainWP\Dashboard\Module\Log; |
| 10 | 10 | |
| 11 | 11 | defined( 'ABSPATH' ) || exit; |
| 12 | 12 | |
| 13 | +use MainWP\Dashboard\MainWP_Exception; | |
| 14 | + | |
| 13 | 15 | /** |
| 14 | 16 | * Class Log_Author. |
| 15 | 17 | * |
| 16 | 18 | * @package MainWP\Dashboard. |
| @@ -16,246 +18,297 @@ | ||
| 16 | 18 | * @package MainWP\Dashboard. |
| 17 | 19 | */ |
| 18 | 20 | class Log_Author { |
| 19 | 21 | |
| 20 | - /** | |
| 21 | - * Holds User ID. | |
| 22 | - * | |
| 23 | - * @var int | |
| 24 | - */ | |
| 25 | - public $id; | |
| 22 | + /** | |
| 23 | + * Holds User ID. | |
| 24 | + * | |
| 25 | + * @var int | |
| 26 | + */ | |
| 27 | + public $id; | |
| 26 | 28 | |
| 27 | - /** | |
| 28 | - * Holds User meta data. | |
| 29 | - * | |
| 30 | - * @var array | |
| 31 | - */ | |
| 32 | - public $meta = array(); | |
| 29 | + /** | |
| 30 | + * Holds User meta data. | |
| 31 | + * | |
| 32 | + * @var array | |
| 33 | + */ | |
| 34 | + public $meta = array(); | |
| 33 | 35 | |
| 34 | - /** | |
| 35 | - * Holds WP user object connected to "$this" instance. | |
| 36 | - * | |
| 37 | - * @var \WP_User | |
| 38 | - */ | |
| 39 | - protected $user; | |
| 36 | + /** | |
| 37 | + * Holds WP user object connected to "$this" instance. | |
| 38 | + * | |
| 39 | + * @var \WP_User | |
| 40 | + */ | |
| 41 | + protected $user; | |
| 40 | 42 | |
| 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; | |
| 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 | 50 | |
| 51 | - if ( $this->id ) { | |
| 52 | - $this->user = new \WP_User( $this->id ); | |
| 53 | - } | |
| 54 | - } | |
| 51 | + if ( ! is_array( $user_meta ) ) { | |
| 52 | + $user_meta = array(); | |
| 53 | + } | |
| 55 | 54 | |
| 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 | - } | |
| 55 | + $this->id = absint( $user_id ); | |
| 56 | + $this->meta = $user_meta; | |
| 83 | 57 | |
| 84 | - throw new \Exception( 'User not found.' ); | |
| 85 | - } | |
| 86 | - } | |
| 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 | + } | |
| 87 | 62 | |
| 88 | - /** | |
| 89 | - * Returns string representation of this object | |
| 90 | - * | |
| 91 | - * @return string | |
| 92 | - */ | |
| 93 | - public function __toString() { | |
| 94 | - return $this->get_display_name(); | |
| 95 | - } | |
| 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 | + } | |
| 96 | 90 | |
| 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 | - } | |
| 91 | + throw new MainWP_Exception( 'User not found.' ); | |
| 92 | + } | |
| 93 | + } | |
| 124 | 94 | |
| 125 | - /** | |
| 126 | - * Get the agent of the user | |
| 127 | - * | |
| 128 | - * @return string | |
| 129 | - */ | |
| 130 | - public function get_agent() { | |
| 131 | - $agent = ''; | |
| 95 | + /** | |
| 96 | + * Returns string representation of this object | |
| 97 | + * | |
| 98 | + * @return string | |
| 99 | + */ | |
| 100 | + public function __toString() { | |
| 101 | + return $this->get_display_name(); | |
| 102 | + } | |
| 132 | 103 | |
| 133 | - if ( ! empty( $this->meta['agent'] ) ) { | |
| 134 | - $agent = $this->meta['agent']; | |
| 135 | - } | |
| 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 | + } | |
| 136 | 118 | |
| 137 | - return $agent; | |
| 138 | - } | |
| 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 | + } | |
| 139 | 127 | |
| 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; | |
| 128 | + if ( empty( $fullname ) && ! empty( $this->meta['username'] ) ) { | |
| 129 | + $fullname = esc_html( $this->meta['username'] ); | |
| 130 | + } | |
| 131 | + return $fullname; | |
| 132 | + } | |
| 153 | 133 | |
| 154 | - $user_role = ''; | |
| 134 | + /** | |
| 135 | + * Get the display name of the user | |
| 136 | + * | |
| 137 | + * @return string | |
| 138 | + */ | |
| 139 | + public function get_display_name() { //phpcs:ignore -- NOSONAR - complex. | |
| 155 | 140 | |
| 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 | - ); | |
| 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 | + } | |
| 167 | 173 | |
| 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 | - } | |
| 174 | + /** | |
| 175 | + * Get the agent of the user | |
| 176 | + * | |
| 177 | + * @return string | |
| 178 | + */ | |
| 179 | + public function get_agent() { | |
| 180 | + $agent = ''; | |
| 173 | 181 | |
| 174 | - return $user_role; | |
| 175 | - } | |
| 182 | + if ( ! empty( $this->meta['agent'] ) ) { | |
| 183 | + $agent = $this->meta['agent']; | |
| 184 | + } | |
| 176 | 185 | |
| 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 | - } | |
| 186 | + return $agent; | |
| 187 | + } | |
| 185 | 188 | |
| 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 | - } | |
| 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; | |
| 194 | 202 | |
| 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 | - } | |
| 203 | + $user_role = ''; | |
| 209 | 204 | |
| 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 = ''; | |
| 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 | + ); | |
| 217 | 216 | |
| 218 | - if ( defined( '\WP_CLI' ) && \WP_CLI ) { | |
| 219 | - $agent = 'wp_cli'; | |
| 220 | - } elseif ( $this->is_doing_wp_cron() ) { | |
| 221 | - $agent = 'wp_cron'; | |
| 222 | - } | |
| 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 | + } | |
| 223 | 222 | |
| 224 | - /** | |
| 225 | - * Filter the current agent string | |
| 226 | - * | |
| 227 | - * @return string | |
| 228 | - */ | |
| 229 | - $agent = apply_filters( 'mainwp_module_log_current_agent', $agent ); | |
| 223 | + return $user_role; | |
| 224 | + } | |
| 230 | 225 | |
| 231 | - return $agent; | |
| 232 | - } | |
| 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 | + } | |
| 233 | 234 | |
| 234 | - /** | |
| 235 | - * Get the agent label | |
| 236 | - * | |
| 237 | - * @param string $agent Key representing agent. | |
| 238 | - * | |
| 239 | - * @return string | |
| 240 | - */ | |
| 241 | - public function get_agent_label( $agent ) { | |
| 242 | - if ( 'wp_cli' === $agent ) { | |
| 243 | - $label = esc_html__( 'via WP-CLI', 'mainwp' ); | |
| 244 | - } elseif ( 'wp_cron' === $agent ) { | |
| 245 | - $label = esc_html__( 'during WP Cron', 'mainwp' ); | |
| 246 | - } else { | |
| 247 | - $label = ''; | |
| 248 | - } | |
| 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 | + } | |
| 249 | 243 | |
| 250 | - /** | |
| 251 | - * Filter agent labels | |
| 252 | - * | |
| 253 | - * @param string $agent Key representing agent. | |
| 254 | - * | |
| 255 | - * @return string | |
| 256 | - */ | |
| 257 | - $label = apply_filters( 'mainwp_module_log_agent_label', $label, $agent ); | |
| 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 | 258 | |
| 259 | - return $label; | |
| 260 | - } | |
| 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 | + } | |
| 261 | 314 | } |