| @@ -85,9 +85,9 @@ | ||
| 85 | 85 | * |
| 86 | 86 | * @return string |
| 87 | 87 | */ |
| 88 | 88 | public function get_first_name() { |
| 89 | - return $this->get_prop( 'first_name', '' ); | |
| 89 | + return get_user_meta( $this->id, 'first_name', true ); | |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | 93 | * Get last name |
| @@ -94,11 +94,21 @@ | ||
| 94 | 94 | * |
| 95 | 95 | * @return string |
| 96 | 96 | */ |
| 97 | 97 | public function get_last_name() { |
| 98 | - return $this->get_prop( 'last_name', '' ); | |
| 98 | + return get_user_meta( $this->id, 'last_name', true ); | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | + /** | |
| 102 | + * Get password | |
| 103 | + * | |
| 104 | + * @return string | |
| 105 | + */ | |
| 106 | + public function get_password() { | |
| 107 | + $user = get_user_by( 'id', $this->id ); | |
| 108 | + return $user->user_pass; | |
| 109 | + } | |
| 110 | + | |
| 101 | 111 | /** |
| 102 | 112 | * Get staff display name |
| 103 | 113 | * |
| 104 | 114 | * @return string |
| @@ -145,10 +155,15 @@ | ||
| 145 | 155 | */ |
| 146 | 156 | public function get_image() { |
| 147 | 157 | $image = $this->get_prop( 'image' ); |
| 148 | 158 | |
| 159 | + if ( filter_var( $image, FILTER_VALIDATE_URL ) ) { | |
| 160 | + return $image; | |
| 161 | + } | |
| 162 | + | |
| 163 | + | |
| 149 | 164 | if ( ! $image ) { |
| 150 | - return get_avatar_url( $this->id ); | |
| 165 | + return false; | |
| 151 | 166 | } |
| 152 | 167 | |
| 153 | 168 | return wp_get_attachment_image_url( $image ); |
| 154 | 169 | } |
| @@ -185,12 +200,40 @@ | ||
| 185 | 200 | * |
| 186 | 201 | * @return mixed |
| 187 | 202 | */ |
| 188 | 203 | public function get_status() { |
| 204 | + $user = get_userdata( $this->id ); | |
| 205 | + | |
| 206 | + if ( user_can( $user, 'manage_options' ) ) { | |
| 207 | + return 1; | |
| 208 | + } | |
| 209 | + | |
| 189 | 210 | return $this->get_prop( 'status' ); |
| 190 | 211 | } |
| 191 | 212 | |
| 192 | 213 | /** |
| 214 | + * Get full name; | |
| 215 | + * | |
| 216 | + * @return string | |
| 217 | + */ | |
| 218 | + public function get_full_name() { | |
| 219 | + $first_name = $this->get_first_name(); | |
| 220 | + $last_name = $this->get_last_name(); | |
| 221 | + | |
| 222 | + if ( ! $first_name ) { | |
| 223 | + return $this->get_display_name(); | |
| 224 | + } | |
| 225 | + | |
| 226 | + if ( ! $last_name ) { | |
| 227 | + return $first_name; | |
| 228 | + } | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + return $first_name . ' ' . $last_name; | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 193 | 236 | * Get all data for a staff |
| 194 | 237 | * |
| 195 | 238 | * @return array |
| 196 | 239 | */ |
| @@ -196,9 +239,9 @@ | ||
| 196 | 239 | */ |
| 197 | 240 | public function get_data() { |
| 198 | 241 | return [ |
| 199 | 242 | 'id' => $this->get_id(), |
| 200 | - 'full_name' => $this->get_display_name(), | |
| 243 | + 'full_name' => $this->get_full_name(), | |
| 201 | 244 | 'first_name' => $this->get_first_name(), |
| 202 | 245 | 'last_name' => $this->get_last_name(), |
| 203 | 246 | 'user_name' => $this->get_user_name(), |
| 204 | 247 | 'email' => $this->get_email(), |
| @@ -279,8 +322,24 @@ | ||
| 279 | 322 | 'role' => 'timetics-staff', |
| 280 | 323 | ]; |
| 281 | 324 | |
| 282 | 325 | $args = wp_parse_args( $args, $defaults ); |
| 326 | + | |
| 327 | + $email = ! empty( $args['user_email'] ) ? $args['user_email'] : ''; | |
| 328 | + | |
| 329 | + if ( $email ) { | |
| 330 | + $user['user_email'] = $email; | |
| 331 | + } | |
| 332 | + | |
| 333 | + $user = get_user_by( 'email', $email ); | |
| 334 | + | |
| 335 | + // An existing account with this email is a conflict, not an instruction | |
| 336 | + // to promote it — silently granting the staff role let a caller take | |
| 337 | + // over any account by submitting its email. | |
| 338 | + if ( $user ) { | |
| 339 | + return new \WP_Error( 'timetics_staff_email_exists', __( 'A user with this email address already exists.', 'timetics' ) ); | |
| 340 | + } | |
| 341 | + | |
| 283 | 342 | $user_id = wp_insert_user( $args ); |
| 284 | 343 | |
| 285 | 344 | if ( ! is_wp_error( $user_id ) ) { |
| 286 | 345 | $this->set_id( $user_id ); |
| @@ -296,21 +355,33 @@ | ||
| 296 | 355 | * |
| 297 | 356 | * @return void |
| 298 | 357 | */ |
| 299 | 358 | public function update( $args = [] ) { |
| 300 | - $user = get_userdata( $this->id )->to_array(); | |
| 359 | + $user = get_userdata( $this->id )->to_array(); | |
| 360 | + $email = ! empty( $args['user_email'] ) ? $args['user_email'] : ''; | |
| 301 | 361 | |
| 302 | 362 | if ( ! empty( $args['user_pass'] ) ) { |
| 303 | 363 | $user['user_pass'] = $args['user_pass']; |
| 304 | 364 | } |
| 305 | 365 | |
| 306 | - if ( ! empty( $args['email'] ) ) { | |
| 307 | - $user['user_email'] = $args['email']; | |
| 366 | + if ( $email ) { | |
| 367 | + $user['user_email'] = $email; | |
| 308 | 368 | } |
| 309 | 369 | |
| 370 | + $user_data = get_user_by( 'email', $email ); | |
| 371 | + | |
| 372 | + // Only a conflict if it belongs to someone other than the staff member | |
| 373 | + // being edited — otherwise every update where they keep their own | |
| 374 | + // email would (incorrectly) hit this branch. See create() for why a | |
| 375 | + // match must never silently grant the staff role. | |
| 376 | + if ( $user_data && (int) $user_data->ID !== (int) $this->id ) { | |
| 377 | + return new \WP_Error( 'timetics_staff_email_exists', __( 'A user with this email address already exists.', 'timetics' ) ); | |
| 378 | + } | |
| 379 | + | |
| 310 | 380 | $updated = wp_update_user( $user ); |
| 311 | 381 | |
| 312 | 382 | if ( ! is_wp_error( $updated ) ) { |
| 383 | + | |
| 313 | 384 | $this->save_metadata( $args ); |
| 314 | 385 | } |
| 315 | 386 | |
| 316 | 387 | return $updated; |
| @@ -327,9 +398,13 @@ | ||
| 327 | 398 | continue; |
| 328 | 399 | } |
| 329 | 400 | |
| 330 | 401 | // Prepare meta key. |
| 331 | - $meta_key = $this->meta_prefix . $key; | |
| 402 | + if( $key == 'first_name' || $key == 'last_name' ) { | |
| 403 | + $meta_key = $key; | |
| 404 | + } else { | |
| 405 | + $meta_key = $this->meta_prefix . $key; | |
| 406 | + } | |
| 332 | 407 | |
| 333 | 408 | // Update appointment meta data. |
| 334 | 409 | update_user_meta( $this->id, $meta_key, $value ); |
| 335 | 410 | } |
| @@ -358,8 +433,14 @@ | ||
| 358 | 433 | if ( count( $user->roles ) > 1 ) { |
| 359 | 434 | $user->remove_role('timetics-staff'); |
| 360 | 435 | |
| 361 | 436 | return true; |
| 437 | + } | |
| 438 | + | |
| 439 | + if ( is_multisite() ) { | |
| 440 | + require_once ABSPATH . 'wp-admin/includes/ms.php'; | |
| 441 | + | |
| 442 | + return wpmu_delete_user( $this->id ); | |
| 362 | 443 | } |
| 363 | 444 | |
| 364 | 445 | return wp_delete_user( $this->id ); |
| 365 | 446 | } |