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-messages.php (source)

   1  <?php
   2  
   3  define ( 'BP_MESSAGES_DB_VERSION', '1300' );
   4  
   5  /* Define the slug for the component */
   6  if ( !defined( 'BP_MESSAGES_SLUG' ) )
   7      define ( 'BP_MESSAGES_SLUG', 'messages' );
   8  
   9  require  ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-classes.php' );
  10  require  ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-cssjs.php' );
  11  require  ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-templatetags.php' );
  12  require  ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-filters.php' );
  13  
  14  /* Include deprecated functions if settings allow */
  15  if ( !defined( 'BP_IGNORE_DEPRECATED' ) )
  16      require  ( BP_PLUGIN_DIR . '/bp-messages/deprecated/bp-messages-deprecated.php' );
  17      
  18  function messages_install() {
  19      global $wpdb, $bp;
  20      
  21      if ( !empty($wpdb->charset) )
  22          $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  23      
  24      $sql[] = "CREATE TABLE {$bp->messages->table_name_threads} (
  25                    id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  26                    message_ids longtext NOT NULL,
  27                  sender_ids longtext NOT NULL,
  28                    first_post_date datetime NOT NULL,
  29                    last_post_date datetime NOT NULL,
  30                    last_message_id bigint(20) NOT NULL,
  31                  last_sender_id bigint(20) NOT NULL,
  32                  KEY last_message_id (last_message_id),
  33                  KEY last_sender_id (last_sender_id)
  34                  ) {$charset_collate};";
  35      
  36      $sql[] = "CREATE TABLE {$bp->messages->table_name_recipients} (
  37                    id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  38                    user_id bigint(20) NOT NULL,
  39                    thread_id bigint(20) NOT NULL,
  40                  sender_only tinyint(1) NOT NULL DEFAULT '0',
  41                    unread_count int(10) NOT NULL DEFAULT '0',
  42                  is_deleted tinyint(1) NOT NULL DEFAULT '0',
  43                  KEY user_id (user_id),
  44                  KEY thread_id (thread_id),
  45                  KEY is_deleted (is_deleted),
  46                  KEY sender_only (sender_only),
  47                  KEY unread_count (unread_count)
  48                  ) {$charset_collate};";
  49  
  50      $sql[] = "CREATE TABLE {$bp->messages->table_name_messages} (
  51                    id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  52                    sender_id bigint(20) NOT NULL,
  53                    subject varchar(200) NOT NULL,
  54                    message longtext NOT NULL,
  55                    date_sent datetime NOT NULL,
  56                  KEY sender_id (sender_id)
  57                  ) {$charset_collate};";
  58      
  59      $sql[] = "CREATE TABLE {$bp->messages->table_name_notices} (
  60                    id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  61                    subject varchar(200) NOT NULL,
  62                    message longtext NOT NULL,
  63                    date_sent datetime NOT NULL,
  64                  is_active tinyint(1) NOT NULL DEFAULT '0',
  65                  KEY is_active (is_active)
  66                  ) {$charset_collate};";
  67      
  68      require_once( ABSPATH . 'wp-admin/upgrade-functions.php' );
  69      dbDelta($sql);
  70      
  71      add_site_option( 'bp-messages-db-version', BP_MESSAGES_DB_VERSION );
  72  }
  73  
  74  function messages_setup_globals() {
  75      global $bp, $wpdb;
  76      
  77      $bp->messages->table_name_threads = $wpdb->base_prefix . 'bp_messages_threads';
  78      $bp->messages->table_name_messages = $wpdb->base_prefix . 'bp_messages_messages';
  79      $bp->messages->table_name_recipients = $wpdb->base_prefix . 'bp_messages_recipients';
  80      $bp->messages->table_name_notices = $wpdb->base_prefix . 'bp_messages_notices';
  81      $bp->messages->format_activity_function = 'messages_format_activity';
  82      $bp->messages->format_notification_function = 'messages_format_notifications';
  83      $bp->messages->image_base = BP_PLUGIN_URL . '/bp-messages/images';
  84      $bp->messages->slug = BP_MESSAGES_SLUG;
  85  
  86      $bp->version_numbers->messages = BP_MESSAGES_VERSION;
  87  }
  88  add_action( 'plugins_loaded', 'messages_setup_globals', 5 );    
  89  add_action( 'admin_menu', 'messages_setup_globals', 1 );
  90  
  91  function messages_check_installed() {    
  92      global $wpdb, $bp;
  93  
  94      if ( !is_site_admin() )
  95          return false;
  96      
  97      /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
  98      if ( get_site_option('bp-messages-db-version') < BP_MESSAGES_DB_VERSION )
  99          messages_install();
 100  }
 101  add_action( 'admin_menu', 'messages_check_installed', 1 );
 102  
 103  function messages_setup_nav() {
 104      global $bp;
 105  
 106      if ( $bp->current_component == $bp->messages->slug ) {
 107          $inbox_count = messages_get_unread_count();
 108          $inbox_display = ( $inbox_count ) ? ' style="display:inline;"' : ' style="display:none;"';
 109          $count_indicator = '&nbsp; <span' . $inbox_display . ' class="unread-count inbox-count">' . $inbox_count . '</span>';
 110      }
 111  
 112      /* Add 'Messages' to the main navigation */
 113      bp_core_new_nav_item( array( 'name' => __('Messages', 'buddypress'), 'slug' => $bp->messages->slug, 'position' => 50, 'show_for_displayed_user' => false, 'screen_function' => 'messages_screen_inbox', 'default_subnav_slug' => 'inbox'  ) );
 114      
 115      $messages_link = $bp->loggedin_user->domain . $bp->messages->slug . '/';
 116      
 117      /* Add the subnav items to the profile */
 118      bp_core_new_subnav_item( array( 'name' => __( 'Inbox', 'buddypress' ) . $count_indicator, 'slug' => 'inbox', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_inbox', 'position' => 10, 'user_has_access' => bp_is_home() ) );
 119      bp_core_new_subnav_item( array( 'name' => __( 'Sent Messages', 'buddypress' ), 'slug' => 'sentbox', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_sentbox', 'position' => 20, 'user_has_access' => bp_is_home() ) );
 120      bp_core_new_subnav_item( array( 'name' => __( 'Compose', 'buddypress' ), 'slug' => 'compose', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_compose', 'position' => 30, 'user_has_access' => bp_is_home() ) );
 121      
 122      if ( is_site_admin() )
 123          bp_core_new_subnav_item( array( 'name' => __( 'Notices', 'buddypress' ), 'slug' => 'notices', 'parent_url' => $messages_link, 'parent_slug' => $bp->messages->slug, 'screen_function' => 'messages_screen_notices', 'position' => 90, 'user_has_access' => is_site_admin() ) );
 124  
 125      if ( $bp->current_component == $bp->messages->slug ) {
 126          if ( bp_is_home() ) {
 127              $bp->bp_options_title = __( 'My Messages', 'buddypress' );            
 128          } else {
 129              $bp_options_avatar =  bp_core_fetch_avatar( array( 'item_id' => $bp->displayed_user->id, 'type' => 'thumb' ) );
 130              $bp->bp_options_title = $bp->displayed_user->fullname;
 131          }
 132      }
 133      
 134      do_action( 'messages_setup_nav' );
 135  }
 136  add_action( 'wp', 'messages_setup_nav', 2 );
 137  add_action( 'admin_menu', 'messages_setup_nav', 2 );
 138  
 139  
 140  /********************************************************************************
 141   * Screen Functions
 142   *
 143   * Screen functions are the controllers of BuddyPress. They will execute when their
 144   * specific URL is caught. They will first save or manipulate data using business
 145   * functions, then pass on the user to a template file.
 146   */
 147  
 148  function messages_screen_inbox() {
 149      do_action( 'messages_screen_inbox' );
 150      bp_core_load_template( apply_filters( 'messages_template_inbox', 'messages/index' ) );    
 151  }
 152  
 153  function messages_screen_sentbox() {
 154      do_action( 'messages_screen_sentbox' );
 155      bp_core_load_template( apply_filters( 'messages_template_sentbox', 'messages/sentbox' ) );
 156  }
 157  
 158  function messages_screen_compose() {
 159      global $bp;
 160      
 161      // Remove any saved message data from a previous session.
 162      messages_remove_callback_values();
 163  
 164      /* Check if the message form has been submitted */
 165      if ( isset( $_POST['send'] ) ) {
 166          
 167          /* Check the nonce */
 168          check_admin_referer( 'messages_send_message' );
 169          
 170          /* Check we have what we need */
 171          if ( empty( $_POST['subject'] ) || empty( $_POST['content'] ) ) {
 172              bp_core_add_message( __( 'There was an error sending that message, please try again', 'buddypress' ), 'error' );
 173          } else {
 174              /* If this is a notice, send it */
 175              if ( isset($_POST['send-notice']) ) {
 176                  messages_send_notice( $_POST['subject'], $_POST['content'] );
 177              } else {
 178                  /* Filter recipients into the format we need - array( 'username/userid', 'username/userid' ) */
 179                  if ( empty( $_POST['send_to_usernames'] ) ) {
 180                      if ( !empty( $_POST['send-to-input'] ) )
 181                          $recipients = explode( ',', $_POST['send-to-input'] );
 182                  } else {
 183                      $recipients = explode( ' ', $_POST['send_to_usernames'] );
 184                  }
 185              
 186                  /* Send the message */
 187                  if ( $thread_id = messages_new_message( array( 'recipients' => $recipients, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) ) {
 188                      bp_core_add_message( __( 'Message sent successfully!', 'buddypress' ) );
 189                      bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $thread_id . '/' );
 190                  } else {
 191                      bp_core_add_message( __( 'There was an error sending that message, please try again', 'buddypress' ), 'error' );
 192                  }
 193              }
 194          }
 195  
 196      }
 197      
 198      do_action( 'messages_screen_compose' );
 199      
 200      bp_core_load_template( apply_filters( 'messages_template_compose', 'messages/compose' ) );
 201  }
 202  
 203  function messages_screen_notices() {
 204      global $bp, $notice_id;
 205          
 206      if ( !is_site_admin() )
 207          return false;
 208          
 209      $notice_id = $bp->action_variables[1];
 210  
 211      if ( $notice_id && is_numeric($notice_id) ) {
 212          $notice = new BP_Messages_Notice($notice_id);
 213  
 214          if ( 'deactivate' == $bp->action_variables[0] ) {
 215              if ( !$notice->deactivate() ) {
 216                  bp_core_add_message( __('There was a problem deactivating that notice.', 'buddypress'), 'error' );    
 217              } else {
 218                  bp_core_add_message( __('Notice deactivated.', 'buddypress') );
 219              }
 220          } else if ( 'activate' == $bp->action_variables[0] ) {
 221              if ( !$notice->activate() ) {
 222                  bp_core_add_message( __('There was a problem activating that notice.', 'buddypress'), 'error' );
 223              } else {
 224                  bp_core_add_message( __('Notice activated.', 'buddypress') );
 225              }
 226          } else if ( 'delete' == $bp->action_variables[0] ) {
 227              if ( !$notice->delete() ) {
 228                  bp_core_add_message( __('There was a problem deleting that notice.', 'buddypress'), 'buddypress' );
 229              } else {
 230                  bp_core_add_message( __('Notice deleted.', 'buddypress') );
 231              }
 232          }
 233          bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/notices' );
 234      }
 235      
 236      do_action( 'messages_screen_notices' );
 237      
 238      bp_core_load_template( apply_filters( 'messages_template_notices', 'messages/notices' ) );    
 239  }
 240  
 241  function messages_screen_notification_settings() { 
 242      global $current_user; ?>
 243      <table class="notification-settings" id="messages-notification-settings">
 244          <tr>
 245              <th class="icon"></th>
 246              <th class="title"><?php _e( 'Messages', 'buddypress' ) ?></th>
 247              <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
 248              <th class="no"><?php _e( 'No', 'buddypress' )?></th>
 249          </tr>
 250          <tr>
 251              <td></td>
 252              <td><?php _e( 'A member sends you a new message', 'buddypress' ) ?></td>
 253              <td class="yes"><input type="radio" name="notifications[notification_messages_new_message]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_messages_new_message' ) || 'yes' == get_usermeta( $current_user->id, 'notification_messages_new_message' ) ) { ?>checked="checked" <?php } ?>/></td>
 254              <td class="no"><input type="radio" name="notifications[notification_messages_new_message]" value="no" <?php if ( 'no' == get_usermeta( $current_user->id, 'notification_messages_new_message' ) ) { ?>checked="checked" <?php } ?>/></td>
 255          </tr>
 256          <tr>
 257              <td></td>
 258              <td><?php _e( 'A new site notice is posted', 'buddypress' ) ?></td>
 259              <td class="yes"><input type="radio" name="notifications[notification_messages_new_notice]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_messages_new_notice' ) || 'yes' == get_usermeta( $current_user->id, 'notification_messages_new_notice' ) ) { ?>checked="checked" <?php } ?>/></td>
 260              <td class="no"><input type="radio" name="notifications[notification_messages_new_notice]" value="no" <?php if ( 'no' == get_usermeta( $current_user->id, 'notification_messages_new_notice' ) ) { ?>checked="checked" <?php } ?>/></td>
 261          </tr>
 262          
 263          <?php do_action( 'messages_screen_notification_settings' ) ?>
 264      </table>
 265  <?php    
 266  }
 267  add_action( 'bp_notification_settings', 'messages_screen_notification_settings', 2 );
 268  
 269  
 270  /********************************************************************************
 271   * Action Functions
 272   *
 273   * Action functions are exactly the same as screen functions, however they do not
 274   * have a template screen associated with them. Usually they will send the user
 275   * back to the default screen after execution.
 276   */
 277  
 278  function messages_action_view_message() {
 279      global $bp, $thread_id;
 280      
 281      if ( $bp->current_component != $bp->messages->slug || $bp->current_action != 'view' )
 282          return false;
 283          
 284      $thread_id = $bp->action_variables[0];
 285      
 286      if ( !$thread_id || !messages_is_valid_thread( $thread_id ) || ( !messages_check_thread_access($thread_id) && !is_site_admin() ) )
 287          bp_core_redirect( $bp->displayed_user->domain . $bp->current_component );
 288          
 289      /* Check if a new reply has been submitted */
 290      if ( isset( $_POST['send'] ) ) {
 291          
 292          /* Check the nonce */
 293          check_admin_referer( 'messages_send_message', 'send_message_nonce' );
 294          
 295          /* Send the reply */
 296          if ( messages_new_message( array( 'thread_id' => $thread_id, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) )
 297              bp_core_add_message( __( 'Your reply was sent successfully', 'buddypress' ) );
 298          else
 299              bp_core_add_message( __( 'There was a problem sending your reply, please try again', 'buddypress' ), 'error' );
 300          
 301          bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/view/' . $thread_id . '/' );        
 302      }
 303      
 304      bp_core_new_subnav_item( array( 'name' => sprintf( __( 'From: %s', 'buddypress'), BP_Messages_Thread::get_last_sender($thread_id) ), 'slug' => 'view', 'parent_url' => $bp->loggedin_user->domain . $bp->messages->slug . '/', 'parent_slug' => $bp->messages->slug, 'screen_function' => true, 'position' => 40, 'user_has_access' => bp_is_home() ) );
 305      bp_core_load_template( apply_filters( 'messages_template_view_message', 'messages/view' ) );
 306  }
 307  add_action( 'wp', 'messages_action_view_message', 3 );
 308  
 309  function messages_action_delete_message() {
 310      global $bp, $thread_id;
 311      
 312      if ( $bp->current_component != $bp->messages->slug || 'notices' == $bp->current_action || $bp->action_variables[0] != 'delete' )
 313          return false;
 314      
 315      $thread_id = $bp->action_variables[1];
 316  
 317      if ( !$thread_id || !is_numeric($thread_id) || !messages_check_thread_access($thread_id) ) {
 318          bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );
 319      } else {
 320          if ( !check_admin_referer( 'messages_delete_thread' ) )
 321              return false;
 322  
 323          // delete message
 324          if ( !messages_delete_thread($thread_id) ) {
 325              bp_core_add_message( __('There was an error deleting that message.', 'buddypress'), 'error' );
 326          } else {
 327              bp_core_add_message( __('Message deleted.', 'buddypress') );
 328          }
 329          bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
 330      }
 331  }
 332  add_action( 'wp', 'messages_action_delete_message', 3 );
 333  
 334  function messages_action_bulk_delete() {
 335      global $bp, $thread_ids;
 336      
 337      if ( $bp->current_component != $bp->messages->slug || $bp->action_variables[0] != 'bulk-delete' )
 338          return false;
 339      
 340      $thread_ids = $_POST['thread_ids'];
 341  
 342      if ( !$thread_ids || !messages_check_thread_access($thread_ids) ) {
 343          bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );            
 344      } else {
 345          if ( !check_admin_referer( 'messages_delete_thread' ) )
 346              return false;
 347              
 348          if ( !messages_delete_thread( $thread_ids ) ) {
 349              bp_core_add_message( __('There was an error deleting messages.', 'buddypress'), 'error' );
 350          } else {
 351              bp_core_add_message( __('Messages deleted.', 'buddypress') );
 352          }
 353          bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
 354      }
 355  }
 356  add_action( 'wp', 'messages_action_bulk_delete', 3 );
 357  
 358  
 359  /********************************************************************************
 360   * Activity & Notification Functions
 361   *
 362   * These functions handle the recording, deleting and formatting of activity and
 363   * notifications for the user and for this specific component.
 364   */
 365  
 366  function messages_record_activity( $args = true ) {
 367      if ( function_exists('bp_activity_record') ) {
 368          extract($args);
 369          bp_activity_record( $item_id, $component_name, $component_action, $is_private );
 370      } 
 371  }
 372  
 373  function messages_delete_activity( $args = true ) {
 374      if ( function_exists('bp_activity_delete') ) {
 375          extract($args);
 376          bp_activity_delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id );
 377      }
 378  }
 379  
 380  function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
 381      global $bp;
 382      
 383      if ( 'new_message' == $action ) {
 384          if ( (int)$total_items > 1 )
 385              return apply_filters( 'bp_messages_multiple_new_message_notification', '<a href="' . $bp->loggedin_user->domain . $bp->messages->slug . '/inbox" title="Inbox">' . sprintf( __('You have %d new messages', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );        
 386          else
 387              return apply_filters( 'bp_messages_single_new_message_notification', '<a href="' . $bp->loggedin_user->domain . $bp->messages->slug . '/inbox" title="Inbox">' . sprintf( __('You have %d new message', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );
 388      }
 389      
 390      do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
 391      
 392      return false;
 393  }
 394  
 395  
 396  /********************************************************************************
 397   * Business Functions
 398   *
 399   * Business functions are where all the magic happens in BuddyPress. They will
 400   * handle the actual saving or manipulation of information. Usually they will
 401   * hand off to a database class for data access, then return
 402   * true or false on success or failure.
 403   */
 404  
 405  function messages_new_message( $args = '' ) {
 406      global $bp;
 407      
 408      $defaults = array(
 409          'thread_id' => false, // false for a new message, thread id for a reply to a thread.
 410          'sender_id' => $bp->loggedin_user->id,
 411          'recipients' => false, // Can be an array of usernames, user_ids or mixed.
 412          'subject' => false,
 413          'content' => false,
 414          'date_sent' => time()
 415      );
 416  
 417      $r = wp_parse_args( $args, $defaults );
 418      extract( $r, EXTR_SKIP );    
 419      
 420      if ( !$sender_id || !$subject || !$content )
 421          return false;
 422          
 423      /* Create a new message object */    
 424      $message = new BP_Messages_Message;
 425      $message->thread_id = $thread_id;
 426      $message->sender_id = $sender_id;
 427      $message->subject = $subject;
 428      $message->message = $content;
 429      $message->date_sent = $date_sent;
 430  
 431      /* If we have a thread ID, use the existing recipients, otherwise use the recipients passed */
 432      if ( $thread_id ) {
 433          $thread = new BP_Messages_Thread($thread_id);
 434          $message->recipients = $thread->get_recipients();        
 435      } else {
 436          if ( empty( $recipients ) )
 437              return false;
 438          
 439          /* Loop the recipients and convert all usernames to user_ids where needed */
 440          foreach( (array) $recipients as $recipient ) {
 441              if ( is_numeric( trim( $recipient ) ) )
 442                  $recipient_ids[] = (int)trim( $recipient );
 443              
 444              if ( $recipient_id = bp_core_get_userid( trim( $recipient ) ) )
 445                  $recipient_ids[] = (int)$recipient_id;
 446          }
 447  
 448          /* Strip the sender from the recipient list if they exist */
 449          if ( $key = array_search( $sender_id, $recipient_ids ) )
 450              unset( $recipient_ids[$key] );    
 451  
 452          $message->recipients = $recipient_ids; 
 453      }
 454      
 455      if ( $message->send() ) {
 456          require_once ( BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php' );
 457  
 458          // Send screen notifications to the recipients
 459          foreach ( $message->recipients as $recipient ) {
 460              bp_core_add_notification( $message->id, $recipient, 'messages', 'new_message' );    
 461          }
 462          
 463          // Send email notifications to the recipients
 464          messages_notification_new_message( array( 'item_id' => $message->id, 'recipient_ids' => $message->recipients, 'thread_id' => $message->thread_id, 'component_name' => $bp->messages->slug, 'component_action' => 'message_sent', 'is_private' => 1 ) );
 465      
 466          do_action( 'messages_message_sent', &$message );
 467          
 468          return $message->thread_id;
 469      }
 470      
 471      return false;
 472  }
 473  
 474  
 475  function messages_send_notice( $subject, $message ) {
 476      if ( !is_site_admin() || empty( $subject ) || empty( $message ) ) {
 477          return false;
 478      } else {
 479          // Has access to send notices, lets do it.
 480          $notice = new BP_Messages_Notice;
 481          $notice->subject = $subject;
 482          $notice->message = $message;
 483          $notice->date_sent = time();
 484          $notice->is_active = 1;
 485          $notice->save(); // send it.
 486          
 487          do_action( 'messages_send_notice', $subject, $message );
 488          
 489          return true;
 490      }
 491  }
 492  
 493  function messages_delete_thread( $thread_ids ) {
 494      if ( is_array($thread_ids) ) {
 495          $error = 0;
 496          for ( $i = 0; $i < count($thread_ids); $i++ ) {
 497              if ( !$status = BP_Messages_Thread::delete($thread_ids[$i]) )
 498                  $error = 1;
 499          }
 500          
 501          if ( $error )
 502              return false;
 503          
 504          do_action( 'messages_delete_thread', $thread_ids );
 505          
 506          return true;
 507      } else {
 508          if ( !BP_Messages_Thread::delete($thread_ids) )
 509              return false;
 510          
 511          do_action( 'messages_delete_thread', $thread_ids );
 512          
 513          return true;
 514      }
 515  }
 516  
 517  function messages_check_thread_access( $thread_id, $user_id = false ) {
 518      global $bp;
 519      
 520      if ( !$user_id )
 521          $user_id = $bp->loggedin_user->id;
 522      
 523      return BP_Messages_Thread::check_access( $thread_id, $user_id );
 524  }
 525  
 526  function messages_add_callback_values( $recipients, $subject, $content ) {
 527      setcookie( 'bp_messages_send_to', $recipients, time()+60*60*24, COOKIEPATH );
 528      setcookie( 'bp_messages_subject', $subject, time()+60*60*24, COOKIEPATH );
 529      setcookie( 'bp_messages_content', $content, time()+60*60*24, COOKIEPATH );
 530  }
 531  
 532  function messages_remove_callback_values() {
 533      setcookie( 'bp_messages_send_to', false, time()-1000, COOKIEPATH );
 534      setcookie( 'bp_messages_subject', false, time()-1000, COOKIEPATH );
 535      setcookie( 'bp_messages_content', false, time()-1000, COOKIEPATH );
 536  }
 537  
 538  function messages_get_unread_count( $user_id = false ) {
 539      global $bp;
 540      
 541      if ( !$user_id ) 
 542          $user_id = $bp->loggedin_user->id;
 543          
 544      return BP_Messages_Thread::get_inbox_count( $user_id );
 545  }
 546  
 547  function messages_is_user_sender( $user_id, $message_id ) {
 548      return BP_Messages_Message::is_user_sender( $user_id, $message_id );
 549  }
 550  
 551  function messages_get_message_sender( $message_id ) {
 552      return BP_Messages_Message::get_message_sender( $message_id );
 553  }
 554  
 555  function messages_is_valid_thread( $thread_id ) {
 556      return BP_Messages_Thread::is_valid( $thread_id );
 557  }
 558  
 559  function messages_ajax_autocomplete_results() {
 560      global $bp;
 561      
 562      $friends = false;
 563  
 564      // Get the friend ids based on the search terms
 565      if ( function_exists( 'friends_search_friends' ) )
 566          $friends = friends_search_friends( $_GET['q'], $bp->loggedin_user->id, $_GET['limit'], 1 );
 567  
 568      $friends = apply_filters( 'bp_friends_autocomplete_list', $friends, $_GET['q'], $_GET['limit'] );
 569  
 570      if ( $friends['friends'] ) {
 571          foreach ( $friends['friends'] as $user_id ) {
 572              $ud = get_userdata($user_id);
 573              $username = $ud->user_login;
 574              echo bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'width' => 15, 'height' => 15 ) )  . ' ' . bp_core_get_user_displayname( $user_id ) . ' (' . $username . ')
 575              ';
 576          }        
 577      }
 578  }
 579  add_action( 'wp_ajax_messages_autocomplete_results', 'messages_ajax_autocomplete_results' );
 580  
 581  
 582  // List actions to clear super cached pages on, if super cache is installed
 583  add_action( 'messages_delete_thread', 'bp_core_clear_cache' );
 584  add_action( 'messages_send_notice', 'bp_core_clear_cache' );
 585  add_action( 'messages_message_sent', 'bp_core_clear_cache' );
 586  
 587  // Don't cache message inbox/sentbox/compose as it's too problematic
 588  add_action( 'messages_screen_compose', 'bp_core_clear_cache' );
 589  add_action( 'messages_screen_sentbox', 'bp_core_clear_cache' );
 590  add_action( 'messages_screen_inbox', 'bp_core_clear_cache' );
 591  
 592  ?>


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