Ticket #7661: 7661.1.diff
| File 7661.1.diff, 9.2 KB (added by , 8 years ago) |
|---|
-
src/bp-members/bp-members-functions.php
diff --git src/bp-members/bp-members-functions.php src/bp-members/bp-members-functions.php index 1e1e21fd6..91f932bd3 100644
function bp_core_get_userid_from_nicename( $user_nicename = '' ) { 307 307 * @return string|bool The username of the matched user, or false. 308 308 */ 309 309 function bp_core_get_username( $user_id = 0, $user_nicename = false, $user_login = false ) { 310 $bp = buddypress();311 312 // Check cache for user nicename.313 $username = wp_cache_get( 'bp_user_username_' . $user_id, 'bp' );314 if ( false === $username ) {315 316 // Cache not found so prepare to update it.317 $update_cache = true;318 319 // Nicename and login were not passed.320 if ( empty( $user_nicename ) && empty( $user_login ) ) {321 322 // User ID matches logged in user.323 if ( bp_loggedin_user_id() == $user_id ) {324 $userdata = &$bp->loggedin_user->userdata;325 326 // User ID matches displayed in user.327 } elseif ( bp_displayed_user_id() == $user_id ) {328 $userdata = &$bp->displayed_user->userdata;329 330 // No user ID match.331 } else {332 $userdata = false;333 }334 335 // No match so go dig.336 if ( empty( $userdata ) ) {337 338 // User not found so return false.339 if ( !$userdata = bp_core_get_core_userdata( $user_id ) ) {340 return false;341 }342 }343 344 // Update the $user_id for later.345 $user_id = $userdata->ID;346 347 // Two possible options.348 $user_nicename = $userdata->user_nicename;349 $user_login = $userdata->user_login;350 }351 310 311 // Nicename and login were not passed. 312 if ( ! $user_nicename && ! $user_login ) { 352 313 // Pull an audible and maybe use the login over the nicename. 353 $username = bp_is_username_compatibility_mode() ? $user_login : $user_nicename; 354 355 // Username found in cache so don't update it again. 314 if ( bp_is_username_compatibility_mode() ) { 315 $username = get_the_author_meta( 'login', $user_id ); 316 } else { 317 $username = get_the_author_meta( 'nicename', $user_id ); 318 } 356 319 } else { 357 $update_cache = false; 358 } 359 360 // Add this to cache. 361 if ( ( true === $update_cache ) && !empty( $username ) ) { 362 wp_cache_set( 'bp_user_username_' . $user_id, $username, 'bp' ); 363 364 // @todo bust this cache if no $username found? 365 // } else { 366 // wp_cache_delete( 'bp_user_username_' . $user_id ); 320 $username = bp_is_username_compatibility_mode() ? $user_login : $user_nicename; 367 321 } 368 322 369 323 /** … … function bp_core_get_username( $user_id = 0, $user_nicename = false, $user_login 384 338 * 385 339 * @since 1.5.0 386 340 * 387 * @todo Refactor to use a WP core function, if possible.388 *389 341 * @param int $user_id User ID to check. 390 342 * @return string|bool The username of the matched user, or false. 391 343 */ 392 344 function bp_members_get_user_nicename( $user_id ) { 393 $bp = buddypress();394 395 if ( !$user_nicename = wp_cache_get( 'bp_members_user_nicename_' . $user_id, 'bp' ) ) {396 $update_cache = true;397 398 // User ID matches logged in user.399 if ( bp_loggedin_user_id() == $user_id ) {400 $userdata = &$bp->loggedin_user->userdata;401 402 // User ID matches displayed in user.403 } elseif ( bp_displayed_user_id() == $user_id ) {404 $userdata = &$bp->displayed_user->userdata;405 406 // No user ID match.407 } else {408 $userdata = false;409 }410 411 // No match so go dig.412 if ( empty( $userdata ) ) {413 414 // User not found so return false.415 if ( !$userdata = bp_core_get_core_userdata( $user_id ) ) {416 return false;417 }418 }419 420 // User nicename found.421 $user_nicename = $userdata->user_nicename;422 423 // Nicename found in cache so don't update it again.424 } else {425 $update_cache = false;426 }427 428 // Add this to cache.429 if ( true == $update_cache && !empty( $user_nicename ) ) {430 wp_cache_set( 'bp_members_user_nicename_' . $user_id, $user_nicename, 'bp' );431 }432 433 345 /** 434 346 * Filters the user_nicename based on originally provided user ID. 435 347 * … … function bp_members_get_user_nicename( $user_id ) { 437 349 * 438 350 * @param string $username User nice name determined by user ID. 439 351 */ 440 return apply_filters( 'bp_members_get_user_nicename', $user_nicename);352 return apply_filters( 'bp_members_get_user_nicename', get_the_author_meta( 'nicename', $user_id ) ); 441 353 } 442 354 443 355 /** … … function bp_members_get_user_nicename( $user_id ) { 449 361 * @return string The email for the matched user. Empty string if no user 450 362 * matched the $uid. 451 363 */ 452 function bp_core_get_user_email( $uid ) { 453 454 if ( !$email = wp_cache_get( 'bp_user_email_' . $uid, 'bp' ) ) { 455 456 // User exists. 457 $ud = bp_core_get_core_userdata( $uid ); 458 if ( ! empty( $ud ) ) { 459 $email = $ud->user_email; 460 461 // User was deleted. 462 } else { 463 $email = ''; 464 } 465 466 wp_cache_set( 'bp_user_email_' . $uid, $email, 'bp' ); 467 } 468 364 function bp_core_get_user_email( $user_id ) { 469 365 /** 470 366 * Filters the user email for user based on user ID. 471 367 * … … function bp_core_get_user_email( $uid ) { 473 369 * 474 370 * @param string $email Email determined for the user. 475 371 */ 476 return apply_filters( 'bp_core_get_user_email', $email);372 return apply_filters( 'bp_core_get_user_email', get_the_author_meta( 'email', $user_id ) ); 477 373 } 478 374 479 375 /** … … function bp_core_get_user_displaynames( $user_ids ) { 546 442 return array(); 547 443 } 548 444 549 $uncached_ids = array(); 550 foreach ( $user_ids as $user_id ) { 551 if ( false === wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp' ) ) { 552 $uncached_ids[] = $user_id; 553 } 554 } 555 556 // Prime caches. 557 if ( ! empty( $uncached_ids ) ) { 558 if ( bp_is_active( 'xprofile' ) ) { 559 $fullname_data = BP_XProfile_ProfileData::get_value_byid( 1, $uncached_ids ); 560 561 // Key by user_id. 562 $fullnames = array(); 563 foreach ( $fullname_data as $fd ) { 564 if ( ! empty( $fd->value ) ) { 565 $fullnames[ intval( $fd->user_id ) ] = $fd->value; 566 } 567 } 568 569 // If xprofiledata is not found for any users, we'll look 570 // them up separately. 571 $no_xprofile_ids = array_diff( $uncached_ids, array_keys( $fullnames ) ); 572 } else { 573 $fullnames = array(); 574 $no_xprofile_ids = $user_ids; 575 } 576 577 if ( ! empty( $no_xprofile_ids ) ) { 578 // Use WP_User_Query because we don't need BP information. 579 $query = new WP_User_Query( array( 580 'include' => $no_xprofile_ids, 581 'fields' => array( 'ID', 'user_nicename', 'display_name', ), 582 'count_total' => false, 583 'blog_id' => 0, 584 ) ); 585 586 foreach ( $query->results as $qr ) { 587 $fullnames[ $qr->ID ] = ! empty( $qr->display_name ) ? $qr->display_name : $qr->user_nicename; 588 589 // If xprofile is active, set this value as the 590 // xprofile display name as well. 591 if ( bp_is_active( 'xprofile' ) ) { 592 xprofile_set_field_data( 1, $qr->ID, $fullnames[ $qr->ID ] ); 593 } 594 } 595 } 596 597 foreach ( $fullnames as $fuser_id => $fname ) { 598 wp_cache_set( 'bp_user_fullname_' . $fuser_id, $fname, 'bp' ); 599 } 600 } 445 // Warm the WP users cache. 446 $query = new WP_User_Query( array( 447 'include' => $user_ids, 448 'fields' => 'all_with_meta' 449 ) ); 601 450 602 451 $retval = array(); 603 452 foreach ( $user_ids as $user_id ) { 604 $retval[ $user_id ] = wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp');453 $retval[ $user_id ] = get_the_author_meta( 'display_name', $user_id ); 605 454 } 606 455 607 456 return $retval; … … function bp_core_get_user_displayname( $user_id_or_username ) { 631 480 return false; 632 481 } 633 482 634 $display_names = bp_core_get_user_displaynames( array( $user_id ) );635 636 if ( ! isset( $display_names[ $user_id ] ) ) {637 $fullname = false;638 } else {639 $fullname = $display_names[ $user_id ];640 }641 642 483 /** 643 484 * Filters the display name for the passed in user. 644 485 * … … function bp_core_get_user_displayname( $user_id_or_username ) { 647 488 * @param string $fullname Display name for the user. 648 489 * @param int $user_id ID of the user to check. 649 490 */ 650 return apply_filters( 'bp_core_get_user_displayname', $fullname, $user_id );491 return apply_filters( 'bp_core_get_user_displayname', get_the_author_meta( 'display_name', $user_id ), $user_id ); 651 492 } 652 493 add_filter( 'bp_core_get_user_displayname', 'strip_tags', 1 ); 653 494 add_filter( 'bp_core_get_user_displayname', 'trim' ); -
tests/phpunit/testcases/members/functions.php
diff --git tests/phpunit/testcases/members/functions.php tests/phpunit/testcases/members/functions.php index 6aeff24d1..4e8283a1f 100644
class BP_Tests_Members_Functions extends BP_UnitTestCase { 195 195 public function test_bp_core_get_user_displaynames_all_uncached() { 196 196 $u1 = self::factory()->user->create(); 197 197 $u2 = self::factory()->user->create(); 198 198 // @TODO: This method doesn't sync info to WP 199 199 xprofile_set_field_data( 1, $u1, 'Foo' ); 200 200 xprofile_set_field_data( 1, $u2, 'Bar' ); 201 201 … … class BP_Tests_Members_Functions extends BP_UnitTestCase { 215 215 $u2 = self::factory()->user->create( array( 216 216 'display_name' => 'Bar', 217 217 ) ); 218 218 // @TODO: This method doesn't sync info to WP 219 219 xprofile_set_field_data( 1, $u1, 'Foo' ); 220 220 221 221 // Delete directly because BP won't let you delete a required … … class BP_Tests_Members_Functions extends BP_UnitTestCase { 239 239 */ 240 240 public function test_bp_core_get_user_displaynames_one_in_cache() { 241 241 $u1 = self::factory()->user->create(); 242 // @TODO: This method doesn't sync info to WP 242 243 xprofile_set_field_data( 1, $u1, 'Foo' ); 243 244 244 245 // Fake the cache for $u2