/**
  * Mark messages in a thread as deleted or delete all messages in a thread.
  *
  * Note: All messages in a thread are deleted once every recipient in a thread
  * has marked the thread as deleted.
  *
  * @since 1.0.0
  *
  * @param int $thread_id The message thread ID.
  *
  * @return bool
  */
 public static function delete($thread_id = 0)
 {
     global $wpdb;
     $thread_id = (int) $thread_id;
     /**
      * Fires before a message thread is marked as deleted.
      *
      * @since 2.2.0
      *
      * @param int $thread_id ID of the thread being deleted.
      */
     do_action('bp_messages_thread_before_mark_delete', $thread_id);
     $bp = buddypress();
     // Mark messages as deleted
     //
     // @todo the reliance on bp_loggedin_user_id() sucks for plugins
     //       refactor this method to accept a $user_id parameter
     $wpdb->query($wpdb->prepare("UPDATE {$bp->messages->table_name_recipients} SET is_deleted = 1 WHERE thread_id = %d AND user_id = %d", $thread_id, bp_loggedin_user_id()));
     // Get the message ids in order to pass to the action
     $message_ids = $wpdb->get_col($wpdb->prepare("SELECT id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id));
     // Check to see if any more recipients remain for this message
     $recipients = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0", $thread_id));
     // No more recipients so delete all messages associated with the thread
     if (empty($recipients)) {
         /**
          * Fires before an entire message thread is deleted.
          *
          * @since 2.2.0
          *
          * @param int   $thread_id   ID of the thread being deleted.
          * @param array $message_ids IDs of messages being deleted.
          */
         do_action('bp_messages_thread_before_delete', $thread_id, $message_ids);
         // Delete all the messages
         $wpdb->query($wpdb->prepare("DELETE FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id));
         // Do something for each message ID
         foreach ($message_ids as $message_id) {
             // Delete message meta
             bp_messages_delete_meta($message_id);
             /**
              * Fires after a message is deleted. This hook is poorly named.
              *
              * @since 1.0.0
              *
              * @param int $message_id ID of the message.
              */
             do_action('messages_thread_deleted_thread', $message_id);
         }
         // Delete all the recipients
         $wpdb->query($wpdb->prepare("DELETE FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id));
     }
     /**
      * Fires after a message thread is either marked as deleted or deleted.
      *
      * @since 2.2.0
      *
      * @param int   $thread_id   ID of the thread being deleted.
      * @param array $message_ids IDs of messages being deleted.
      */
     do_action('bp_messages_thread_after_delete', $thread_id, $message_ids);
     return true;
 }
/**
 * Save or delete star message meta according to a message's star status.
 *
 * @since 2.3.0
 *
 * @param array $args {
 *     Array of arguments.
 *     @type string $action     The star action. Either 'star' or 'unstar'. Default: 'star'.
 *     @type int    $thread_id  The message thread ID. Default: 0. If not zero, this takes precedence over
 *                              $message_id.
 *     @type int    $message_id The indivudal message ID to star or unstar.  Default: 0.
 *     @type int    $user_id    The user ID. Defaults to the logged-in user ID.
 *     @type bool   $bulk       Whether to mark all messages in a thread as a certain action. Only relevant
 *                              when $action is 'unstar' at the moment. Default: false.
 * }
 * @return bool
 */
function bp_messages_star_set_action($args = array())
{
    $r = wp_parse_args($args, array('action' => 'star', 'thread_id' => 0, 'message_id' => 0, 'user_id' => bp_loggedin_user_id(), 'bulk' => false));
    // Set thread ID.
    if (!empty($r['thread_id'])) {
        $thread_id = (int) $r['thread_id'];
    } else {
        $thread_id = messages_get_message_thread_id($r['message_id']);
    }
    if (empty($thread_id)) {
        return false;
    }
    // Check if user has access to thread.
    if (!messages_check_thread_access($thread_id, $r['user_id'])) {
        return false;
    }
    $is_starred = bp_messages_is_message_starred($r['message_id'], $r['user_id']);
    // Star.
    if ('star' == $r['action']) {
        if (true === $is_starred) {
            return true;
        } else {
            bp_messages_add_meta($r['message_id'], 'starred_by_user', $r['user_id']);
            return true;
        }
        // Unstar.
    } else {
        // Unstar one message.
        if (false === $r['bulk']) {
            if (false === $is_starred) {
                return true;
            } else {
                bp_messages_delete_meta($r['message_id'], 'starred_by_user', $r['user_id']);
                return true;
            }
            // Unstar all messages in a thread.
        } else {
            $thread = new BP_Messages_Thread($thread_id);
            $mids = wp_list_pluck($thread->messages, 'id');
            foreach ($mids as $mid) {
                if (true === bp_messages_is_message_starred($mid, $r['user_id'])) {
                    bp_messages_delete_meta($mid, 'starred_by_user', $r['user_id']);
                }
            }
            return true;
        }
    }
}