Ticket #7661: 7661.3.diff
| File 7661.3.diff, 16.7 KB (added by , 8 years ago) |
|---|
-
src/bp-core/classes/class-bp-core-user.php
diff --git src/bp-core/classes/class-bp-core-user.php src/bp-core/classes/class-bp-core-user.php index 94bb54168..64467f5ed 100644
class BP_Core_User { 166 166 $this->email = esc_attr( bp_core_get_user_email( $this->id ) ); 167 167 } 168 168 169 // Cache a few things that are fetched often.170 wp_cache_set( 'bp_user_fullname_' . $this->id, $this->fullname, 'bp' );171 wp_cache_set( 'bp_user_email_' . $this->id, $this->email, 'bp' );172 wp_cache_set( 'bp_user_url_' . $this->id, $this->user_url, 'bp' );173 174 169 $this->avatar = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'full', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->fullname ) ) ); 175 170 $this->avatar_thumb = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->fullname ) ) ); 176 171 $this->avatar_mini = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->fullname ), 'width' => 30, 'height' => 30 ) ); -
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..7f29f9e68 100644
function bp_core_get_userid_from_nicename( $user_nicename = '' ) { 304 304 * @param int $user_id User ID to check. 305 305 * @param string|bool $user_nicename Optional. user_nicename of user being checked. 306 306 * @param string|bool $user_login Optional. user_login of user being checked. 307 * @return string |bool The username of the matched user, or false.307 * @return string The username of the matched user or an empty string if no user is found. 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 if ( ! $user_nicename && ! $user_login ) { 352 312 // 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. 313 if ( bp_is_username_compatibility_mode() ) { 314 $username = get_the_author_meta( 'login', $user_id ); 315 } else { 316 $username = get_the_author_meta( 'nicename', $user_id ); 317 } 356 318 } 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 ); 319 $username = bp_is_username_compatibility_mode() ? $user_login : $user_nicename; 367 320 } 368 321 369 322 /** … … function bp_core_get_username( $user_id = 0, $user_nicename = false, $user_login 384 337 * 385 338 * @since 1.5.0 386 339 * 387 * @todo Refactor to use a WP core function, if possible.388 *389 340 * @param int $user_id User ID to check. 390 * @return string |bool The username of the matched user, or false.341 * @return string The username of the matched user or an empty string if no user is found. 391 342 */ 392 343 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 344 /** 434 345 * Filters the user_nicename based on originally provided user ID. 435 346 * … … function bp_members_get_user_nicename( $user_id ) { 437 348 * 438 349 * @param string $username User nice name determined by user ID. 439 350 */ 440 return apply_filters( 'bp_members_get_user_nicename', $user_nicename);351 return apply_filters( 'bp_members_get_user_nicename', get_the_author_meta( 'nicename', $user_id ) ); 441 352 } 442 353 443 354 /** … … function bp_members_get_user_nicename( $user_id ) { 447 358 * 448 359 * @param int $uid User ID to check. 449 360 * @return string The email for the matched user. Empty string if no user 450 * matche d the $uid.361 * matches the $user_id. 451 362 */ 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 363 function bp_core_get_user_email( $user_id ) { 469 364 /** 470 365 * Filters the user email for user based on user ID. 471 366 * … … function bp_core_get_user_email( $uid ) { 473 368 * 474 369 * @param string $email Email determined for the user. 475 370 */ 476 return apply_filters( 'bp_core_get_user_email', $email);371 return apply_filters( 'bp_core_get_user_email', get_the_author_meta( 'email', $user_id ) ); 477 372 } 478 373 479 374 /** … … function bp_core_get_userlink( $user_id, $no_anchor = false, $just_link = false 532 427 * @since 2.0.0 533 428 * 534 429 * @param array $user_ids Array of user IDs to get display names for. 535 * @return array 430 * @return array Associative array of the format "id" => "displayname". 536 431 */ 537 432 function bp_core_get_user_displaynames( $user_ids ) { 538 433 … … function bp_core_get_user_displaynames( $user_ids ) { 546 441 return array(); 547 442 } 548 443 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 } 444 // Warm the WP users cache with a targeted bulk update. 445 cache_users( $user_ids ); 601 446 602 447 $retval = array(); 603 448 foreach ( $user_ids as $user_id ) { 604 $retval[ $user_id ] = wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp');449 $retval[ $user_id ] = bp_core_get_user_displayname( $user_id ); 605 450 } 606 451 607 452 return $retval; … … function bp_core_get_user_displayname( $user_id_or_username ) { 631 476 return false; 632 477 } 633 478 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 479 /** 643 480 * Filters the display name for the passed in user. 644 481 * … … function bp_core_get_user_displayname( $user_id_or_username ) { 647 484 * @param string $fullname Display name for the user. 648 485 * @param int $user_id ID of the user to check. 649 486 */ 650 return apply_filters( 'bp_core_get_user_displayname', $fullname, $user_id );487 return apply_filters( 'bp_core_get_user_displayname', get_the_author_meta( 'display_name', $user_id ), $user_id ); 651 488 } 652 489 add_filter( 'bp_core_get_user_displayname', 'strip_tags', 1 ); 653 490 add_filter( 'bp_core_get_user_displayname', 'trim' ); -
src/bp-xprofile/bp-xprofile-cache.php
diff --git src/bp-xprofile/bp-xprofile-cache.php src/bp-xprofile/bp-xprofile-cache.php index ab5153b67..dc372e77b 100644
function xprofile_clear_profile_groups_object_cache( $group_obj ) { 189 189 add_action( 'xprofile_group_after_delete', 'xprofile_clear_profile_groups_object_cache' ); 190 190 add_action( 'xprofile_group_after_save', 'xprofile_clear_profile_groups_object_cache' ); 191 191 192 /**193 * Clear cached XProfile fullname data for user.194 *195 * @since 2.1.0196 *197 * @param int $user_id ID of user whose fullname cache to delete.198 */199 function xprofile_clear_profile_data_object_cache( $user_id = 0 ) {200 wp_cache_delete( 'bp_user_fullname_' . $user_id, 'bp' );201 }202 add_action( 'xprofile_updated_profile', 'xprofile_clear_profile_data_object_cache' );203 204 /**205 * Clear the fullname cache when field 1 is updated.206 *207 * The xprofile_clear_profile_data_object_cache() will make this redundant in most208 * cases, except where the field is updated directly with xprofile_set_field_data().209 *210 * @since 2.0.0211 *212 * @param object $data Data object to clear.213 */214 function xprofile_clear_fullname_cache_on_profile_field_edit( $data ) {215 if ( 1 == $data->field_id ) {216 wp_cache_delete( 'bp_user_fullname_' . $data->user_id, 'bp' );217 }218 }219 add_action( 'xprofile_data_after_save', 'xprofile_clear_fullname_cache_on_profile_field_edit' );220 221 192 /** 222 193 * Clear caches when a field object is modified. 223 194 * -
src/bp-xprofile/bp-xprofile-functions.php
diff --git src/bp-xprofile/bp-xprofile-functions.php src/bp-xprofile/bp-xprofile-functions.php index 14d1e71c2..e4c16d3d7 100644
function xprofile_sync_wp_profile( $user_id = 0 ) { 837 837 838 838 wp_update_user( array( 'ID' => $user_id, 'display_name' => $fullname ) ); 839 839 } 840 add_action( 'xprofile_updated_profile', 'xprofile_sync_wp_profile' );841 840 add_action( 'bp_core_signup_user', 'xprofile_sync_wp_profile' ); 842 841 add_action( 'bp_core_activated_user', 'xprofile_sync_wp_profile' ); 843 842 … … function xprofile_sync_bp_profile( &$errors, $update, &$user ) { 861 860 } 862 861 add_action( 'user_profile_update_errors', 'xprofile_sync_bp_profile', 10, 3 ); 863 862 863 /** 864 * Update the WP display, last, and first name fields when the xprofile display name field is updated. 865 * 866 * @since 3.0.0 867 * 868 * @param BP_XProfile_ProfileData $data Current instance of the profile data being saved. 869 */ 870 function xprofile_sync_wp_profile_on_single_field_set( $data ) { 871 872 if ( bp_xprofile_fullname_field_id() !== $data->field_id ) { 873 return; 874 } 875 876 xprofile_sync_wp_profile( $data->user_id ); 877 } 878 add_action( 'xprofile_data_after_save', 'xprofile_sync_wp_profile_on_single_field_set' ); 864 879 865 880 /** 866 881 * When a user is deleted, we need to clean up the database and remove all the -
tests/phpunit/testcases/members/functions.php
diff --git tests/phpunit/testcases/members/functions.php tests/phpunit/testcases/members/functions.php index 6aeff24d1..52f627b7c 100644
class BP_Tests_Members_Functions extends BP_UnitTestCase { 99 99 $this->assertFalse( bp_core_get_user_displayname( 'i_dont_exist' ) ); 100 100 } 101 101 102 /**103 * @group bp_core_get_user_displayname104 * @group cache105 */106 public function test_bp_core_get_user_displayname_xprofile_populate_cache() {107 $xprofile_is_active = bp_is_active( 'xprofile' );108 buddypress()->active_components['xprofile'] = '1';109 110 $u = self::factory()->user->create( array(111 'display_name' => 'Foo',112 ) );113 bp_core_get_user_displayname( $u );114 115 $this->assertSame( 'Foo', wp_cache_get( 'bp_user_fullname_' . $u, 'bp' ) );116 117 if ( ! $xprofile_is_active ) {118 unset( buddypress()->active_components['xprofile'] );119 }120 }121 122 /**123 * @group bp_core_get_user_displayname124 * @group cache125 */126 public function test_bp_core_get_user_displayname_xprofile_bust_cache_after_xprofile_update() {127 $xprofile_is_active = bp_is_active( 'xprofile' );128 buddypress()->active_components['xprofile'] = '1';129 130 $u = self::factory()->user->create();131 xprofile_set_field_data( 1, $u, 'Foo Foo' );132 133 $this->assertFalse( wp_cache_get( 'bp_user_fullname_' . $u, 'bp' ) );134 135 if ( ! $xprofile_is_active ) {136 unset( buddypress()->active_components['xprofile'] );137 }138 }139 140 102 /** 141 103 * @group bp_core_get_user_displayname 142 104 */ … … class BP_Tests_Members_Functions extends BP_UnitTestCase { 154 116 } 155 117 } 156 118 157 /**158 * @group bp_core_get_user_displayname159 */160 public function test_bp_core_get_user_displayname_xprofile_does_not_exist() {161 $bp = buddypress();162 $xprofile_is_active = bp_is_active( 'xprofile' );163 $bp->active_components['xprofile'] = '1';164 165 $u = self::factory()->user->create( array(166 'display_name' => 'Foo Foo',167 ) );168 169 // Delete directly because BP won't let you delete a required170 // field through the API171 global $wpdb;172 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u ) );173 wp_cache_delete( 'bp_user_fullname_' . $u, 'bp' );174 wp_cache_delete( "{$u}:1", 'bp_xprofile_data' );175 176 $this->assertSame( '', xprofile_get_field_data( 1, $u ) );177 $this->assertSame( 'Foo Foo', bp_core_get_user_displayname( $u ) );178 $this->assertSame( 'Foo Foo', xprofile_get_field_data( 1, $u ) );179 180 if ( ! $xprofile_is_active ) {181 unset( $bp->active_components['xprofile'] );182 }183 }184 185 119 /** 186 120 * @group bp_core_get_user_displaynames 187 121 */ … … class BP_Tests_Members_Functions extends BP_UnitTestCase { 195 129 public function test_bp_core_get_user_displaynames_all_uncached() { 196 130 $u1 = self::factory()->user->create(); 197 131 $u2 = self::factory()->user->create(); 198 199 132 xprofile_set_field_data( 1, $u1, 'Foo' ); 200 133 xprofile_set_field_data( 1, $u2, 'Bar' ); 201 134 … … class BP_Tests_Members_Functions extends BP_UnitTestCase { 215 148 $u2 = self::factory()->user->create( array( 216 149 'display_name' => 'Bar', 217 150 ) ); 218 219 xprofile_set_field_data( 1, $u1, 'Foo' );220 221 // Delete directly because BP won't let you delete a required222 // field through the API223 global $wpdb;224 $bp = buddypress();225 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u2 ) );226 wp_cache_delete( 'bp_user_fullname_' . $u2, 'bp' );227 wp_cache_delete( "{$u2}:1", 'bp_xprofile_data' );228 229 $expected = array(230 $u1 => 'Foo',231 $u2 => 'Bar',232 );233 234 $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );235 }236 237 /**238 * @group bp_core_get_user_displaynames239 */240 public function test_bp_core_get_user_displaynames_one_in_cache() {241 $u1 = self::factory()->user->create();242 151 xprofile_set_field_data( 1, $u1, 'Foo' ); 243 152 244 // Fake the cache for $u2245 $u2 = 123;246 wp_cache_set( 'bp_user_fullname_' . $u2, 'Bar', 'bp' );247 248 153 $expected = array( 249 154 $u1 => 'Foo', 250 155 $u2 => 'Bar',