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