Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/23/2021 08:07:07 PM (5 years ago)
Author:
imath
Message:

BP Messages: introduce a functionality to exit a messages thread

Exiting a messages thread is removing the user from the list of the recipients of the thread. The thread can carry on between the other recipients.

Props Oelita, vapvarun

Fixes #7540

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r13148 r13196  
    531531                 */
    532532                do_action( 'bp_messages_thread_after_delete', $thread_id, $message_ids, $user_id );
     533
     534                return true;
     535        }
     536
     537        /**
     538         * Exit a user from a thread.
     539         *
     540         * @since 10.0.0
     541         *
     542         * @global wpdb $wpdb WordPress database object.
     543         *
     544         * @param int $thread_id The message thread ID.
     545         * @param int $user_id The ID of the user in the thread.
     546         *                     Defaults to the current logged-in user.
     547         *
     548         * @return bool
     549         */
     550        public static function exit_thread( $thread_id = 0, $user_id = 0 ) {
     551                global $wpdb;
     552
     553                $thread_id = (int) $thread_id;
     554                $user_id   = (int) $user_id;
     555
     556                if ( empty( $user_id ) ) {
     557                        $user_id = bp_loggedin_user_id();
     558                }
     559
     560                // Check the user is a recipient of the thread and recipients count > 2.
     561                $recipients    = self::get_recipients_for_thread( $thread_id );
     562                $recipient_ids = wp_list_pluck( $recipients, 'user_id' );
     563
     564                if ( 2 >= count( $recipient_ids ) || ! in_array( $user_id, $recipient_ids, true ) ) {
     565                        return false;
     566                }
     567
     568                /**
     569                 * Fires before a user exits a thread.
     570                 *
     571                 * @since 10.0.0
     572                 *
     573                 * @param int $thread_id ID of the thread being deleted.
     574                 * @param int $user_id   ID of the user that the thread is being deleted for.
     575                 */
     576                do_action( 'bp_messages_thread_before_exit', $thread_id, $user_id );
     577
     578                $bp = buddypress();
     579
     580                // Delete the user from messages recipients
     581                $exited = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $thread_id, $user_id ) );
     582
     583                // Bail if the user wasn't removed from the recipients list.
     584                if ( empty( $exited ) ) {
     585                        return false;
     586                }
     587
     588                /**
     589                 * Fires after a user exits a thread.
     590                 *
     591                 * @since 10.0.0
     592                 *
     593                 * @param int   $thread_id ID of the thread being deleted.
     594                 * @param int   $user_id   ID of the user the threads were deleted for.
     595                 */
     596                do_action( 'bp_messages_thread_after_exit', $thread_id, $user_id );
    533597
    534598                return true;
Note: See TracChangeset for help on using the changeset viewer.