bp_admin_repair_friend_count

Recalculate friend counts for each user.

Description

(array) bp_admin_repair_friend_count(); 

Returns (array)


Usage

  1. if ( !function_exists( 'bp_admin_repair_friend_count' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/admin/bp-core-admin-tools.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_admin_repair_friend_count(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-core/admin/bp-core-admin-tools.php  
  1. function bp_admin_repair_friend_count() { 
  2. global $wpdb; 
  3.  
  4. if ( ! bp_is_active( 'friends' ) ) { 
  5. return; 
  6.  
  7. $statement = __( 'Counting the number of friends for each user… %s', buddypress ); 
  8. $result = __( 'Failed!', buddypress ); 
  9.  
  10. $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_friend_count' );"; 
  11. if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) { 
  12. return array( 1, sprintf( $statement, $result ) ); 
  13.  
  14. $bp =buddypress); 
  15.  
  16. // Walk through all users on the site. 
  17. $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c; 
  18.  
  19. $updated = array(); 
  20. if ( $total_users > 0 ) { 
  21. $per_query = 500; 
  22. $offset = 0; 
  23. while ( $offset < $total_users ) { 
  24. // Only bother updating counts for users who actually have friendships. 
  25. $friendships = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id FROM {$bp->friends->table_name} WHERE is_confirmed = 1 AND ( ( initiator_user_id > %d AND initiator_user_id <= %d ) OR ( friend_user_id > %d AND friend_user_id <= %d ) )", $offset, $offset + $per_query, $offset, $offset + $per_query ) ); 
  26.  
  27. // The previous query will turn up duplicates, so we 
  28. // filter them here. 
  29. foreach ( $friendships as $friendship ) { 
  30. if ( ! isset( $updated[ $friendship->initiator_user_id ] ) ) { 
  31. BP_Friends_Friendship::total_friend_count( $friendship->initiator_user_id ); 
  32. $updated[ $friendship->initiator_user_id ] = 1; 
  33.  
  34. if ( ! isset( $updated[ $friendship->friend_user_id ] ) ) { 
  35. BP_Friends_Friendship::total_friend_count( $friendship->friend_user_id ); 
  36. $updated[ $friendship->friend_user_id ] = 1; 
  37.  
  38. $offset += $per_query; 
  39. } else { 
  40. return array( 2, sprintf( $statement, $result ) ); 
  41.  
  42. return array( 0, sprintf( $statement, __( 'Complete!', buddypress ) ) );