| @@ -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. |
| @@ -44,13 +46,18 @@ | ||
| 44 | 46 | * @param int $user_id The user ID. |
| 45 | 47 | * @param array $user_meta The user meta array. |
| 46 | 48 | */ |
| 47 | 49 | public function __construct( $user_id, $user_meta = array() ) { |
| 50 | + | |
| 51 | + if ( ! is_array( $user_meta ) ) { | |
| 52 | + $user_meta = array(); | |
| 53 | + } | |
| 54 | + | |
| 48 | 55 | $this->id = absint( $user_id ); |
| 49 | 56 | $this->meta = $user_meta; |
| 50 | 57 | |
| 51 | - if ( $this->id ) { | |
| 52 | - $this->user = new \WP_User( $this->id ); | |
| 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. | |
| 53 | 60 | } |
| 54 | 61 | } |
| 55 | 62 | |
| 56 | 63 | /** |
| @@ -94,33 +101,75 @@ | ||
| 94 | 101 | return $this->get_display_name(); |
| 95 | 102 | } |
| 96 | 103 | |
| 97 | 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 | + /** | |
| 98 | 135 | * Get the display name of the user |
| 99 | 136 | * |
| 100 | 137 | * @return string |
| 101 | 138 | */ |
| 102 | - public function get_display_name() { | |
| 103 | - if ( empty( $this->id ) ) { | |
| 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. | |
| 104 | 153 | if ( isset( $this->meta['system_user_name'] ) ) { |
| 105 | 154 | return esc_html( $this->meta['system_user_name'] ); |
| 106 | 155 | } elseif ( 'wp_cli' === $this->get_current_agent() ) { |
| 107 | 156 | return 'WP-CLI'; // No translation needed. |
| 108 | 157 | } |
| 109 | - return esc_html__( 'N/A', 'mainwp' ); | |
| 110 | - } elseif ( $this->is_deleted() ) { | |
| 111 | - if ( ! empty( $this->meta['display_name'] ) ) { | |
| 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'] ) ) { | |
| 112 | 162 | 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 | 163 | } |
| 118 | - } elseif ( ! empty( $this->user->display_name ) ) { | |
| 119 | - return $this->user->display_name; | |
| 120 | - } else { | |
| 121 | - return $this->user->user_login; | |
| 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 | + } | |
| 122 | 170 | } |
| 171 | + return ''; | |
| 123 | 172 | } |
| 124 | 173 | |
| 125 | 174 | /** |
| 126 | 175 | * Get the agent of the user |
| @@ -179,9 +228,9 @@ | ||
| 179 | 228 | * |
| 180 | 229 | * @return bool |
| 181 | 230 | */ |
| 182 | 231 | public function is_deleted() { |
| 183 | - return 0 !== $this->id && empty( $this->user->ID ); | |
| 232 | + return 0 !== $this->id && ( empty( $this->user ) || empty( $this->user->ID ) ); | |
| 184 | 233 | } |
| 185 | 234 | |
| 186 | 235 | /** |
| 187 | 236 | * True if user is WP-CLI, otherwise false |