What is it? BuddyPressDEV.org is the community of BuddyPress' developers that creates and extend this platform.
[ Index ]

PHP Cross Reference of BuddyPress SVN Code

title

Body

[close]

/ -> bp-friends.php (source)

   1  <?php
   2  
   3  define ( 'BP_FRIENDS_DB_VERSION', '1300' );
   4  
   5  /* Define the slug for the component */
   6  if ( !defined( 'BP_FRIENDS_SLUG' ) )
   7      define ( 'BP_FRIENDS_SLUG', 'friends' );
   8  
   9  require  ( BP_PLUGIN_DIR . '/bp-friends/bp-friends-classes.php' );
  10  require  ( BP_PLUGIN_DIR . '/bp-friends/bp-friends-templatetags.php' );
  11  
  12  /* Include deprecated functions if settings allow */
  13  if ( !defined( 'BP_IGNORE_DEPRECATED' ) )
  14      require  ( BP_PLUGIN_DIR . '/bp-friends/deprecated/bp-friends-deprecated.php' );
  15  
  16  function friends_install() {
  17      global $wpdb, $bp;
  18      
  19      if ( !empty($wpdb->charset) )
  20          $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  21          
  22      $sql[] = "CREATE TABLE {$bp->friends->table_name} (
  23                    id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  24                    initiator_user_id bigint(20) NOT NULL,
  25                    friend_user_id bigint(20) NOT NULL,
  26                    is_confirmed bool DEFAULT 0,
  27                  is_limited bool DEFAULT 0,
  28                    date_created datetime NOT NULL,
  29                  KEY initiator_user_id (initiator_user_id),
  30                  KEY friend_user_id (friend_user_id)
  31                  ) {$charset_collate};";
  32  
  33      require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
  34      dbDelta($sql);
  35      
  36      update_site_option( 'bp-friends-db-version', BP_FRIENDS_DB_VERSION );
  37  }
  38  
  39  function friends_setup_globals() {
  40      global $bp, $wpdb;
  41      
  42      $bp->friends->table_name = $wpdb->base_prefix . 'bp_friends';
  43      $bp->friends->format_activity_function = 'friends_format_activity';
  44      $bp->friends->format_notification_function = 'friends_format_notifications';
  45  
  46      $bp->friends->slug = BP_FRIENDS_SLUG;
  47  
  48      $bp->version_numbers->friends = BP_FRIENDS_VERSION;
  49  }
  50  add_action( 'plugins_loaded', 'friends_setup_globals', 5 );    
  51  add_action( 'admin_menu', 'friends_setup_globals', 1 );
  52  
  53  function friends_check_installed() {    
  54      global $wpdb, $bp;
  55  
  56      if ( !is_site_admin() )
  57          return false;
  58      
  59      /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
  60      if ( get_site_option('bp-friends-db-version') < BP_FRIENDS_DB_VERSION )
  61          friends_install();
  62  }
  63  add_action( 'admin_menu', 'friends_check_installed' );
  64  
  65  function friends_setup_nav() {
  66      global $bp;
  67      
  68      /* Add 'Friends' to the main navigation */
  69      bp_core_new_nav_item( array( 'name' => __('Friends', 'buddypress'), 'slug' => $bp->friends->slug, 'position' => 60, 'screen_function' => 'friends_screen_my_friends', 'default_subnav_slug' => 'my-friends' ) );
  70      
  71      $friends_link = $bp->loggedin_user->domain . $bp->friends->slug . '/';
  72      
  73      /* Add the subnav items to the friends nav item */
  74      bp_core_new_subnav_item( array( 'name' => __( 'My Friends', 'buddypress' ), 'slug' => 'my-friends', 'parent_url' => $friends_link, 'parent_slug' => $bp->friends->slug, 'screen_function' => 'friends_screen_my_friends', 'position' => 10, 'item_css_id' => 'friends-my-friends' ) );
  75      bp_core_new_subnav_item( array( 'name' => __( 'Requests', 'buddypress' ), 'slug' => 'requests', 'parent_url' => $friends_link, 'parent_slug' => $bp->friends->slug, 'screen_function' => 'friends_screen_requests', 'position' => 20 ) );
  76      
  77      if ( $bp->current_component == $bp->friends->slug ) {
  78          if ( bp_is_home() ) {
  79              $bp->bp_options_title = __( 'My Friends', 'buddypress' );
  80          } else {
  81              $bp->bp_options_avatar = bp_core_fetch_avatar( array( 'item_id' => $bp->displayed_user->id, 'type' => 'thumb' ) );
  82              $bp->bp_options_title = $bp->displayed_user->fullname; 
  83          }
  84      }
  85      
  86      do_action( 'friends_setup_nav' );
  87  }
  88  add_action( 'wp', 'friends_setup_nav', 2 );
  89  add_action( 'admin_menu', 'friends_setup_nav', 2 );
  90  
  91  
  92  /********************************************************************************
  93   * Screen Functions
  94   *
  95   * Screen functions are the controllers of BuddyPress. They will execute when their
  96   * specific URL is caught. They will first save or manipulate data using business
  97   * functions, then pass on the user to a template file.
  98   */
  99  
 100  function friends_screen_my_friends() {
 101      global $bp;
 102  
 103      // Delete any friendship acceptance notifications for the user when viewing a profile
 104      bp_core_delete_notifications_for_user_by_type( $bp->loggedin_user->id, 'friends', 'friendship_accepted' );
 105  
 106      do_action( 'friends_screen_my_friends' );
 107      
 108      bp_core_load_template( apply_filters( 'friends_template_my_friends', 'friends/index' ) );    
 109  }
 110  
 111  function friends_screen_requests() {
 112      global $bp;
 113              
 114      if ( isset($bp->action_variables) && 'accept' == $bp->action_variables[0] && is_numeric($bp->action_variables[1]) ) {
 115          /* Check the nonce */
 116          if ( !check_admin_referer( 'friends_accept_friendship' ) ) 
 117              return false;
 118                  
 119          if ( friends_accept_friendship( $bp->action_variables[1] ) ) {
 120              bp_core_add_message( __( 'Friendship accepted', 'buddypress' ) );
 121          } else {
 122              bp_core_add_message( __( 'Friendship could not be accepted', 'buddypress' ), 'error' );
 123          }
 124          bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
 125          
 126      } else if ( isset($bp->action_variables) && 'reject' == $bp->action_variables[0] && is_numeric($bp->action_variables[1]) ) {
 127          /* Check the nonce */
 128          if ( !check_admin_referer( 'friends_reject_friendship' ) ) 
 129              return false;        
 130          
 131          if ( friends_reject_friendship( $bp->action_variables[1] ) ) {
 132              bp_core_add_message( __( 'Friendship rejected', 'buddypress' ) );
 133          } else {
 134              bp_core_add_message( __( 'Friendship could not be rejected', 'buddypress' ), 'error' );
 135          }    
 136          bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
 137      }
 138      
 139      do_action( 'friends_screen_requests' );
 140      
 141      bp_core_load_template( apply_filters( 'friends_template_requests', 'friends/requests' ) );
 142  }
 143  
 144  function friends_screen_friend_finder() {
 145      do_action( 'friends_screen_friend_finder' );
 146       bp_core_load_template( apply_filters( 'friends_template_friend_finder', 'friends/friend-finder' ) );
 147  }
 148  
 149  function friends_screen_notification_settings() { 
 150      global $current_user; ?>
 151      <table class="notification-settings" id="friends-notification-settings">
 152          <tr>
 153              <th class="icon"></th>
 154              <th class="title"><?php _e( 'Friends', 'buddypress' ) ?></th>
 155              <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
 156              <th class="no"><?php _e( 'No', 'buddypress' )?></th>
 157          </tr>
 158          <tr>
 159              <td></td>
 160              <td><?php _e( 'A member sends you a friendship request', 'buddypress' ) ?></td>
 161              <td class="yes"><input type="radio" name="notifications[notification_friends_friendship_request]" value="yes" <?php if ( !get_usermeta( $current_user->id,'notification_friends_friendship_request') || 'yes' == get_usermeta( $current_user->id,'notification_friends_friendship_request') ) { ?>checked="checked" <?php } ?>/></td>
 162              <td class="no"><input type="radio" name="notifications[notification_friends_friendship_request]" value="no" <?php if ( get_usermeta( $current_user->id,'notification_friends_friendship_request') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
 163          </tr>
 164          <tr>
 165              <td></td>
 166              <td><?php _e( 'A member accepts your friendship request', 'buddypress' ) ?></td>
 167              <td class="yes"><input type="radio" name="notifications[notification_friends_friendship_accepted]" value="yes" <?php if ( !get_usermeta( $current_user->id,'notification_friends_friendship_accepted') || 'yes' == get_usermeta( $current_user->id,'notification_friends_friendship_accepted') ) { ?>checked="checked" <?php } ?>/></td>
 168              <td class="no"><input type="radio" name="notifications[notification_friends_friendship_accepted]" value="no" <?php if ( 'no' == get_usermeta( $current_user->id,'notification_friends_friendship_accepted') ) { ?>checked="checked" <?php } ?>/></td>
 169          </tr>
 170          
 171          <?php do_action( 'friends_screen_notification_settings' ); ?>
 172      </table>
 173  <?php    
 174  }
 175  add_action( 'bp_notification_settings', 'friends_screen_notification_settings' );
 176  
 177  
 178  /********************************************************************************
 179   * Action Functions
 180   *
 181   * Action functions are exactly the same as screen functions, however they do not
 182   * have a template screen associated with them. Usually they will send the user
 183   * back to the default screen after execution.
 184   */
 185  
 186  function friends_action_add_friend() {
 187      global $bp;
 188  
 189      if ( $bp->current_component != $bp->friends->slug || $bp->current_action != 'add-friend' )
 190          return false;
 191      
 192      $potential_friend_id = $bp->action_variables[0];
 193  
 194      if ( !is_numeric( $potential_friend_id ) || !isset( $potential_friend_id ) )
 195          return false;
 196  
 197      if ( $potential_friend_id == $bp->loggedin_user->id )
 198          return false;
 199      
 200      $friendship_status = BP_Friends_Friendship::check_is_friend( $bp->loggedin_user->id, $potential_friend_id );
 201  
 202      if ( 'not_friends' == $friendship_status ) {
 203          
 204          if ( !check_admin_referer( 'friends_add_friend' ) )
 205              return false;
 206              
 207          if ( !friends_add_friend( $bp->loggedin_user->id, $potential_friend_id ) ) {
 208              bp_core_add_message( __( 'Friendship could not be requested.', 'buddypress' ), 'error' );
 209          } else {
 210              bp_core_add_message( __( 'Friendship requested', 'buddypress' ) );
 211          }
 212      } else if ( 'is_friend' == $friendship_status ) {
 213          bp_core_add_message( __( 'You are already friends with this user', 'buddypress' ), 'error' );        
 214      } else {
 215          bp_core_add_message( __( 'You already have a pending friendship request with this user', 'buddypress' ), 'error' );        
 216      }
 217      
 218      bp_core_redirect( $_SERVER['HTTP_REFERER'] );
 219      
 220      return false;
 221  }
 222  add_action( 'init', 'friends_action_add_friend' );
 223  
 224  function friends_action_remove_friend() {
 225      global $bp;
 226      
 227      if ( $bp->current_component != $bp->friends->slug || $bp->current_action != 'remove-friend' )
 228          return false;
 229      
 230      $potential_friend_id = $bp->action_variables[0];
 231  
 232      if ( !is_numeric( $potential_friend_id ) || !isset( $potential_friend_id ) )
 233          return false;
 234  
 235      if ( $potential_friend_id == $bp->loggedin_user->id )
 236          return false;
 237          
 238      $friendship_status = BP_Friends_Friendship::check_is_friend( $bp->loggedin_user->id, $potential_friend_id );
 239      
 240      if ( 'is_friend' == $friendship_status ) {
 241          
 242          if ( !check_admin_referer( 'friends_remove_friend' ) )
 243              return false;
 244          
 245          if ( !friends_remove_friend( $bp->loggedin_user->id, $potential_friend_id ) ) {
 246              bp_core_add_message( __( 'Friendship could not be canceled.', 'buddypress' ), 'error' );
 247          } else {
 248              bp_core_add_message( __( 'Friendship canceled', 'buddypress' ) );
 249          }
 250      } else if ( 'is_friends' == $friendship_status ) {
 251          bp_core_add_message( __( 'You are not yet friends with this user', 'buddypress' ), 'error' );        
 252      } else {
 253          bp_core_add_message( __( 'You have a pending friendship request with this user', 'buddypress' ), 'error' );        
 254      }
 255      
 256      bp_core_redirect( $_SERVER['HTTP_REFERER'] );
 257      
 258      return false;
 259  }
 260  add_action( 'init', 'friends_action_remove_friend' );
 261  
 262  
 263  /********************************************************************************
 264   * Activity & Notification Functions
 265   *
 266   * These functions handle the recording, deleting and formatting of activity and
 267   * notifications for the user and for this specific component.
 268   */
 269  
 270  function friends_record_activity( $args ) {
 271      if ( function_exists('bp_activity_record') ) {
 272          extract( (array)$args );
 273          bp_activity_record( $item_id, $component_name, $component_action, $is_private, $secondary_item_id, $user_id, $secondary_user_id );
 274      }
 275  }
 276  add_action( 'friends_friendship_accepted', 'friends_record_activity' );
 277  
 278  function friends_delete_activity( $args ) {
 279      if ( function_exists('bp_activity_delete') ) {
 280          extract( (array)$args );
 281          bp_activity_delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id );
 282      }
 283  }
 284  
 285  function friends_format_activity( $item_id, $user_id, $action, $secondary_item_id = false, $for_secondary_user = false ) {
 286      global $bp;
 287      
 288      switch( $action ) {
 289          case 'friendship_accepted':
 290              $friendship = new BP_Friends_Friendship( $item_id, false, false );
 291  
 292              if ( !$friendship->initiator_user_id || !$friendship->friend_user_id )
 293                  return false;
 294              
 295              if ( $for_secondary_user ) {
 296                  
 297                  $user_1 = bp_core_get_userlink( $friendship->initiator_user_id );
 298                  $user_2 = bp_core_get_userlink($friendship->friend_user_id, false, false, true);
 299                  
 300                  return array( 
 301                      'primary_link' => bp_core_get_userlink( $friendship->friend_user_id, false, true ),
 302                      'content' => apply_filters( 'bp_friends_friendship_accepted_activity', sprintf( __( '%s and %s are now friends', 'buddypress' ), $user_1, $user_2 ) . ' <span class="time-since">%s</span>', $user_1, $user_2 )
 303                  );                
 304              } else {
 305                  
 306                  $user_1 = bp_core_get_userlink( $friendship->friend_user_id );
 307                  $user_2 = bp_core_get_userlink($friendship->initiator_user_id);
 308                  
 309                  return array( 
 310                      'primary_link' => bp_core_get_userlink( $friendship->friend_user_id, false, true ),
 311                      'content' => apply_filters( 'bp_friends_friendship_accepted_activity', sprintf( __( '%s and %s are now friends', 'buddypress' ), bp_core_get_userlink( $friendship->friend_user_id ), bp_core_get_userlink($friendship->initiator_user_id) ) . ' <span class="time-since">%s</span>', $user_1, $user_2 )
 312                  );            
 313              }
 314          break;
 315      }
 316      
 317      do_action( 'friends_format_activity', $action, $item_id, $user_id, $action, $secondary_item_id, $for_secondary_user );
 318      
 319      return false;
 320  }
 321  
 322  function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
 323      global $bp;
 324      
 325      switch ( $action ) {
 326          case 'friendship_accepted':
 327              if ( (int)$total_items > 1 ) {
 328                  return apply_filters( 'bp_friends_multiple_friendship_accepted_notification', '<a href="' . $bp->loggedin_user->domain . $bp->friends->slug . '/my-friends/newest" title="' . __( 'My Friends', 'buddypress' ) . '">' . sprintf( __('%d friends accepted your friendship requests', 'buddypress' ), (int)$total_items ) . '</a>', (int)$total_items );        
 329              } else {
 330                  $user_fullname = bp_core_get_user_displayname( $item_id );
 331                  $user_url = bp_core_get_userurl( $item_id );
 332                  return apply_filters( 'bp_friends_single_friendship_accepted_notification', '<a href="' . $user_url . '?new" title="' . $user_fullname .'\'s profile">' . sprintf( __( '%s accepted your friendship request', 'buddypress' ), $user_fullname ) . '</a>', $user_fullname );
 333              }    
 334          break;
 335          
 336          case 'friendship_request':
 337              if ( (int)$total_items > 1 ) {
 338                  return apply_filters( 'bp_friends_multiple_friendship_request_notification', '<a href="' . $bp->loggedin_user->domain . $bp->friends->slug . '/requests" title="' . __( 'Friendship requests', 'buddypress' ) . '">' . sprintf( __('You have %d pending friendship requests', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );        
 339              } else {
 340                  $user_fullname = bp_core_get_user_displayname( $item_id );
 341                  $user_url = bp_core_get_userurl( $item_id );
 342                  return apply_filters( 'bp_friends_single_friendship_request_notification', '<a href="' . $bp->loggedin_user->domain . $bp->friends->slug . '/requests" title="' . __( 'Friendship requests', 'buddypress' ) . '">' . sprintf( __('You have a friendship request from %s', 'buddypress' ), $user_fullname ) . '</a>', $user_fullname );
 343              }    
 344          break;
 345      }
 346  
 347      do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
 348      
 349      return false;
 350  }
 351  
 352  
 353  /********************************************************************************
 354   * Business Functions
 355   *
 356   * Business functions are where all the magic happens in BuddyPress. They will
 357   * handle the actual saving or manipulation of information. Usually they will
 358   * hand off to a database class for data access, then return
 359   * true or false on success or failure.
 360   */
 361  
 362  function friends_check_user_has_friends( $user_id ) {
 363      $friend_count = get_usermeta( $user_id, 'total_friend_count');
 364  
 365      if ( empty( $friend_count ) )
 366          return false;
 367      
 368      if ( !(int)$friend_count )
 369          return false;
 370      
 371      return true;
 372  }
 373  
 374  function friends_get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false, $filter = false ) {
 375      return BP_Friends_Friendship::get_friend_user_ids( $user_id, $friend_requests_only, $assoc_arr, $filter );
 376  }
 377  
 378  function friends_get_friendship_ids( $user_id, $friend_requests_only = false ) {
 379      return BP_Friends_Friendship::get_friendship_ids( $user_id, $friend_requests_only );
 380  }
 381  
 382  function friends_search_friends( $search_terms, $user_id, $pag_num = 10, $pag_page = 1 ) {
 383      return BP_Friends_Friendship::search_friends( $search_terms, $user_id, $pag_num, $pag_page );
 384  }
 385  
 386  function friends_get_friendship_requests( $user_id ) {
 387      $fship_ids = friends_get_friendship_ids( $user_id, true );
 388      
 389      return array( 'requests' => $fship_ids, 'total' => count($requests) );
 390  }
 391  
 392  function friends_get_recently_active( $user_id, $pag_num = false, $pag_page = false, $filter = false ) {
 393      if ( $filter )
 394          $friend_ids = friends_search_friends( $filter, $user_id, false );
 395      else
 396          $friend_ids = friends_get_friend_user_ids( $user_id );
 397      
 398      if ( !$friend_ids )
 399          return false;
 400      
 401      if ( $filter )
 402          $friend_ids = $friend_ids['friends'];
 403  
 404      $ids_and_activity = friends_get_bulk_last_active( implode( ',', (array)$friend_ids ) );
 405      
 406      if ( !$ids_and_activity )
 407          return false;
 408      
 409      $total_friends = count( $ids_and_activity );
 410      
 411      if ( $pag_num && $pag_page )
 412          return array( 'friends' => array_slice( $ids_and_activity, intval( ( $pag_page - 1 ) * $pag_num), intval( $pag_num ) ), 'total' => $total_friends );
 413      else
 414          return array( 'friends' => $ids_and_activity, 'total' => $total_friends );
 415  }
 416  
 417  function friends_get_alphabetically( $user_id, $pag_num = false, $pag_page = false, $filter = false ) {
 418      if ( $filter )
 419          $friend_ids = friends_search_friends( $filter, $user_id, false );
 420      else
 421          $friend_ids = friends_get_friend_user_ids( $user_id );
 422      
 423      if ( !$friend_ids )
 424          return false;
 425      
 426      if ( $filter )
 427          $friend_ids = $friend_ids['friends'];
 428          
 429      $sorted_ids = BP_Friends_Friendship::sort_by_name( implode( ',', $friend_ids ) );
 430      
 431      if ( !$sorted_ids )
 432          return false;
 433      
 434      $total_friends = count( $sorted_ids );
 435      
 436      if ( $pag_num && $pag_page )
 437          return array( 'friends' => array_slice( $sorted_ids, intval( ( $pag_page - 1 ) * $pag_num), intval( $pag_num ) ), 'total' => $total_friends );
 438      else
 439          return array( 'friends' => $sorted_ids, 'total' => $total_friends );
 440  }
 441  
 442  function friends_get_newest( $user_id, $pag_num = false, $pag_page = false, $filter = false ) {
 443      if ( $filter )
 444          $friend_ids = friends_search_friends( $filter, $user_id, false );
 445      else
 446          $friend_ids = friends_get_friend_user_ids( $user_id );
 447      
 448      if ( !$friend_ids )
 449          return false;    
 450  
 451      if ( $filter )
 452          $friend_ids = $friend_ids['friends'];
 453  
 454      $total_friends = count( $friend_ids );
 455      
 456      if ( $pag_num && $pag_page )
 457          return array( 'friends' => array_slice( $friend_ids, intval( ( $pag_page - 1 ) * $pag_num), intval( $pag_num ) ), 'total' => $total_friends );    
 458      else
 459          return array( 'friends' => $friend_ids, 'total' => $total_friends );    
 460  }
 461      
 462  function friends_get_bulk_last_active( $friend_ids ) {
 463      return BP_Friends_Friendship::get_bulk_last_active( $friend_ids );
 464  }
 465  
 466  function friends_get_friends_list( $user_id ) {
 467      global $bp;
 468      
 469      $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id );
 470  
 471      if ( !$friend_ids )
 472          return false;
 473  
 474      for ( $i = 0; $i < count($friend_ids); $i++ ) {
 475          if ( function_exists('bp_user_fullname') )
 476              $display_name = bp_core_get_user_displayname( $friend_ids[$i] );
 477          
 478          if ( $display_name != ' ' ) {
 479              $friends[] = array(
 480                  'id' => $friend_ids[$i],
 481                  'full_name' => $display_name
 482              );
 483          }
 484      }
 485      
 486      if ( $friends && is_array($friends) )
 487          usort($friends, 'friends_sort_by_name');
 488  
 489      if ( !$friends )
 490          return false;
 491  
 492      return $friends;
 493  }
 494  
 495  	function friends_sort_by_name($a, $b) {  
 496          return strcasecmp($a['full_name'], $b['full_name']);
 497      }
 498  
 499  function friends_get_friends_invite_list( $user_id = false, $group_id ) {
 500      global $bp;
 501      
 502      if ( !$user_id )
 503          $user_id = $bp->loggedin_user->id;
 504      
 505      $friend_ids = friends_get_alphabetically( $user_id );
 506  
 507      if ( (int) $friend_ids['total'] < 1 )
 508          return false;
 509  
 510      for ( $i = 0; $i < count($friend_ids['friends']); $i++ ) {
 511          if ( groups_check_user_has_invite( $friend_ids['friends'][$i]->user_id, $group_id ) || groups_is_user_member( $friend_ids['friends'][$i]->user_id, $group_id ) )
 512              continue;
 513              
 514          $display_name = bp_core_get_user_displayname( $friend_ids['friends'][$i]->user_id );
 515          
 516          if ( $display_name != ' ' ) {
 517              $friends[] = array(
 518                  'id' => $friend_ids['friends'][$i]->user_id,
 519                  'full_name' => $display_name
 520              );
 521          }
 522      }
 523  
 524      if ( !$friends )
 525          return false;
 526  
 527      return $friends;
 528  }
 529  
 530  function friends_count_invitable_friends( $user_id, $group_id ) {
 531      return BP_Friends_Friendship::get_invitable_friend_count( $user_id, $group_id );
 532  }
 533  
 534  function friends_get_friend_count_for_user( $user_id ) {
 535      return BP_Friends_Friendship::total_friend_count( $user_id );
 536  }
 537  
 538  function friends_search_users( $search_terms, $user_id, $pag_num = false, $pag_page = false ) {
 539      global $bp;
 540  
 541      $user_ids = BP_Friends_Friendship::search_users( $search_terms, $user_id, $pag_num, $pag_page );
 542      
 543      if ( !$user_ids )
 544          return false;
 545  
 546      for ( $i = 0; $i < count($user_ids); $i++ ) {
 547          $users[] = new BP_Core_User($user_ids[$i]);
 548      }
 549      
 550      return array( 'users' => $users, 'count' => BP_Friends_Friendship::search_users_count($search_terms) );
 551  }
 552  
 553  function friends_check_friendship( $user_id, $possible_friend_id ) {
 554      global $bp;
 555  
 556      if ( 'is_friend' == BP_Friends_Friendship::check_is_friend( $user_id, $possible_friend_id ) )
 557          return true;
 558      
 559      return false;
 560  }
 561  
 562  function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) {
 563      global $bp;
 564      
 565      $friendship = new BP_Friends_Friendship;
 566      
 567      if ( (int)$friendship->is_confirmed )
 568          return true;
 569          
 570      $friendship->initiator_user_id = $initiator_userid;
 571      $friendship->friend_user_id = $friend_userid;
 572      $friendship->is_confirmed = 0;
 573      $friendship->is_limited = 0;
 574      $friendship->date_created = time();
 575      
 576      if ( $force_accept )
 577          $friendship->is_confirmed = 1;
 578      
 579      if ( $friendship->save() ) {
 580          
 581          if ( !$force_accept ) {
 582              // Add the on screen notification
 583              bp_core_add_notification( $friendship->initiator_user_id, $friendship->friend_user_id, 'friends', 'friendship_request' );    
 584  
 585              // Send the email notification
 586              require_once ( BP_PLUGIN_DIR . '/bp-friends/bp-friends-notifications.php' );
 587              friends_notification_new_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
 588              
 589              do_action( 'friends_friendship_requested', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );    
 590          } else {
 591              do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
 592          }
 593              
 594          return true;
 595      }
 596      
 597      return false;
 598  }
 599  
 600  function friends_remove_friend( $initiator_userid, $friend_userid ) {
 601      global $bp;
 602          
 603      $friendship_id = BP_Friends_Friendship::get_friendship_id( $initiator_userid, $friend_userid );
 604      $friendship = new BP_Friends_Friendship( $friendship_id );
 605      
 606      // Remove the activity stream items
 607      friends_delete_activity( array( 'item_id' => $friendship_id, 'component_name' => $bp->friends->slug, 'component_action' => 'friendship_accepted', 'user_id' => $bp->displayed_user->id ) );
 608      
 609      do_action( 'friends_friendship_deleted', $friendship_id, $initiator_userid, $friend_userid );
 610      
 611      if ( $friendship->delete() ) {
 612          friends_update_friend_totals( $initiator_userid, $friend_userid, 'remove' );
 613          
 614          return true;
 615      }
 616      
 617      return false;
 618  }
 619  
 620  function friends_accept_friendship( $friendship_id ) {
 621      global $bp;
 622          
 623      $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
 624  
 625      if ( !$friendship->is_confirmed && BP_Friends_Friendship::accept( $friendship_id ) ) {
 626          friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id );
 627          
 628          // Remove the friend request notice
 629          bp_core_delete_notifications_for_user_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, 'friends', 'friendship_request' );    
 630          
 631          // Add a friend accepted notice for the initiating user
 632          bp_core_add_notification( $friendship->friend_user_id, $friendship->initiator_user_id, 'friends', 'friendship_accepted' );
 633          
 634          // Record in activity streams
 635          friends_record_activity( array( 'item_id' => $friendship_id, 'component_name' => $bp->friends->slug, 'component_action' => 'friendship_accepted', 'is_private' => 0, 'user_id' => $friendship->initiator_user_id, 'secondary_user_id' => $friendship->friend_user_id ) );
 636          
 637          // Send the email notification
 638          require_once ( BP_PLUGIN_DIR . '/bp-friends/bp-friends-notifications.php' );
 639          friends_notification_accepted_request( $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
 640  
 641          do_action( 'friends_friendship_accepted', $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id );
 642          
 643          return true;
 644      }
 645      
 646      return false;
 647  }
 648  
 649  function friends_reject_friendship( $friendship_id ) {        
 650      $friendship = new BP_Friends_Friendship( $friendship_id, true, false );
 651  
 652      if ( !$friendship->is_confirmed && BP_Friends_Friendship::reject( $friendship_id ) ) {
 653          // Remove the friend request notice
 654          bp_core_delete_notifications_for_user_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, 'friends', 'friendship_request' );    
 655          
 656          do_action( 'friends_friendship_rejected', $friendship_id );
 657          return true;
 658      }
 659      
 660      return false;
 661  }
 662  
 663  function friends_is_friendship_confirmed( $friendship_id ) {
 664      $friendship = new BP_Friends_Friendship( $friendship_id );
 665      return $friendship->is_confirmed;
 666  }
 667  
 668  function friends_update_friend_totals( $initiator_user_id, $friend_user_id, $status = 'add' ) {
 669      if ( 'add' == $status ) {
 670          update_usermeta( $initiator_user_id, 'total_friend_count', (int)get_usermeta( $initiator_user_id, 'total_friend_count' ) + 1 );
 671          update_usermeta( $friend_user_id, 'total_friend_count', (int)get_usermeta( $friend_user_id, 'total_friend_count' ) + 1 );
 672      } else {
 673          update_usermeta( $initiator_user_id, 'total_friend_count', (int)get_usermeta( $initiator_user_id, 'total_friend_count' ) - 1 );
 674          update_usermeta( $friend_user_id, 'total_friend_count', (int)get_usermeta( $friend_user_id, 'total_friend_count' ) - 1 );        
 675      }
 676  }
 677  
 678  function friends_remove_data( $user_id ) {
 679      BP_Friends_Friendship::delete_all_for_user($user_id);
 680      
 681      /* Remove usermeta */
 682      delete_usermeta( $user_id, 'total_friend_count' );
 683      
 684      /* Remove friendship requests FROM user */
 685      bp_core_delete_notifications_from_user( $user_id, $bp->friends->slug, 'friendship_request' );
 686  
 687      do_action( 'friends_remove_data', $user_id );
 688  }
 689  add_action( 'wpmu_delete_user', 'friends_remove_data', 1 );
 690  add_action( 'delete_user', 'friends_remove_data', 1 );
 691  
 692  function friends_clear_friend_object_cache( $friendship_id ) {
 693      if ( !$friendship = new BP_Friends_Friendship( $friendship_id ) )
 694          return false;
 695  
 696      wp_cache_delete( 'friends_friend_ids_' . $friendship->initiator_user_id, 'bp' );
 697      wp_cache_delete( 'friends_friend_ids_' . $friendship->friend_user_id, 'bp' );
 698      wp_cache_delete( 'popular_users', 'bp' );
 699  
 700      /* Clear the sitewide activity cache */
 701      wp_cache_delete( 'sitewide_activity', 'bp' );
 702  }
 703  
 704  // List actions to clear object caches on
 705  add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
 706  add_action( 'friends_friendship_deleted', 'friends_clear_friend_object_cache' );
 707  
 708  // List actions to clear super cached pages on, if super cache is installed
 709  add_action( 'friends_friendship_rejected', 'bp_core_clear_cache' );
 710  add_action( 'friends_friendship_accepted', 'bp_core_clear_cache' );
 711  add_action( 'friends_friendship_deleted', 'bp_core_clear_cache' );
 712  add_action( 'friends_friendship_requested', 'bp_core_clear_cache' );
 713  
 714  ?>


Generated: Sat Aug 22 14:49:03 2009 Cross-referenced by PHPXref 0.7 Thanks to