Skip to:
Content

BuddyPress.org

Changeset 8618


Ignore:
Timestamp:
07/15/2014 10:03:53 PM (12 years ago)
Author:
johnjamesjacoby
Message:

Clean up messages_new_message() to use bp_parse_args().

Also re-allows previous functionality where members could respond to threads they are alone in. If someone wants to talk to themselves, who are we to stop them?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-functions.php

    r8481 r8618  
    3939function messages_new_message( $args = '' ) {
    4040
    41         $defaults = array (
     41        // Parse the default arguments
     42        $r = bp_parse_args( $args, array(
    4243                'sender_id'  => bp_loggedin_user_id(),
    43                 'thread_id'  => false, // false for a new message, thread id for a reply to a thread.
    44                 'recipients' => false, // Can be an array of usernames, user_ids or mixed.
     44                'thread_id'  => false,   // false for a new message, thread id for a reply to a thread.
     45                'recipients' => array(), // Can be an array of usernames, user_ids or mixed.
    4546                'subject'    => false,
    4647                'content'    => false,
    4748                'date_sent'  => bp_core_current_time()
    48         );
    49 
    50         $r = wp_parse_args( $args, $defaults );
    51         extract( $r, EXTR_SKIP );
    52 
    53         if ( empty( $sender_id ) || empty( $content ) )
     49        ), 'messages_new_message' );
     50
     51        // Bail if no sender or no content
     52        if ( empty( $r['sender_id'] ) || empty( $r['content'] ) ) {
    5453                return false;
     54        }
    5555
    5656        // Create a new message object
    5757        $message            = new BP_Messages_Message;
    58         $message->thread_id = $thread_id;
    59         $message->sender_id = $sender_id;
    60         $message->subject   = $subject;
    61         $message->message   = $content;
    62         $message->date_sent = $date_sent;
    63 
    64         // If we have a thread ID, use the existing recipients, otherwise use the recipients passed
    65         if ( !empty( $thread_id ) ) {
    66                 $thread = new BP_Messages_Thread( $thread_id );
     58        $message->thread_id = $r['thread_id'];
     59        $message->sender_id = $r['sender_id'];
     60        $message->subject   = $r['subject'];
     61        $message->message   = $r['content'];
     62        $message->date_sent = $r['date_sent'];
     63
     64        // If we have a thread ID...
     65        if ( ! empty( $r['thread_id'] ) ) {
     66
     67                // ...use the existing recipients
     68                $thread              = new BP_Messages_Thread( $r['thread_id'] );
    6769                $message->recipients = $thread->get_recipients();
    6870
    69                 // Strip the sender from the recipient list if they exist
    70                 if ( isset( $message->recipients[$sender_id] ) )
    71                         unset( $message->recipients[$sender_id] );
    72 
    73                 if ( empty( $message->subject ) )
     71                // Strip the sender from the recipient list, and unset them if they are
     72                // not alone. If they are alone, let them talk to themselves.
     73                if ( isset( $message->recipients[ $r['sender_id'] ] ) && ( count( $message->recipients ) > 1 ) ) {
     74                        unset( $message->recipients[ $r['sender_id'] ] );
     75                }
     76
     77                // Set a default reply subject if none was sent
     78                if ( empty( $message->subject ) ) {
    7479                        $message->subject = sprintf( __( 'Re: %s', 'buddypress' ), $thread->messages[0]->subject );
    75 
    76         // No thread ID, so make some adjustments
     80                }
     81
     82        // ...otherwise use the recipients passed
    7783        } else {
    78                 if ( empty( $recipients ) )
     84
     85                // Bail if no recipients
     86                if ( empty( $r['recipients'] ) ) {
    7987                        return false;
    80 
    81                 if ( empty( $message->subject ) )
     88                }
     89
     90                // Set a default subject if none exists
     91                if ( empty( $message->subject ) ) {
    8292                        $message->subject = __( 'No Subject', 'buddypress' );
    83 
     93                }
     94
     95                // Setup the recipients array
    8496                $recipient_ids      = array();
    8597
     
    88100
    89101                // Loop the recipients and convert all usernames to user_ids where needed
    90                 foreach( (array) $recipients as $recipient ) {
     102                foreach( (array) $r['recipients'] as $recipient ) {
     103
     104                        // Trim spaces and skip if empty
    91105                        $recipient = trim( $recipient );
    92 
    93                         if ( empty( $recipient ) )
     106                        if ( empty( $recipient ) ) {
    94107                                continue;
    95 
    96                         $recipient_id = false;
    97 
    98                         // check user_login / nicename columns first
     108                        }
     109
     110                        // Check user_login / nicename columns first
    99111                        // @see http://buddypress.trac.wordpress.org/ticket/5151
    100112                        if ( bp_is_username_compatibility_mode() ) {
     
    104116                        }
    105117
    106                         // check against user ID column if no match and if passed recipient is numeric
    107                         if ( ! $recipient_id && is_numeric( $recipient ) ) {
     118                        // Check against user ID column if no match and if passed recipient is numeric
     119                        if ( empty( $recipient_id ) && is_numeric( $recipient ) ) {
    108120                                if ( bp_core_get_core_userdata( (int) $recipient ) ) {
    109121                                        $recipient_id = (int) $recipient;
     
    111123                        }
    112124
    113                         if ( ! $recipient_id ) {
     125                        // Decide which group to add this recipient to
     126                        if ( empty( $recipient_id ) ) {
    114127                                $invalid_recipients[] = $recipient;
    115128                        } else {
     
    118131                }
    119132
    120                 // Strip the sender from the recipient list if they exist
    121                 if ( $key = array_search( $sender_id, (array) $recipient_ids ) )
    122                         unset( $recipient_ids[$key] );
    123 
    124                 // Remove duplicates
    125                 $recipient_ids = array_unique( (array) $recipient_ids );
    126 
    127                 if ( empty( $recipient_ids ) )
     133                // Strip the sender from the recipient list, and unset them if they are
     134                // not alone. If they are alone, let them talk to themselves.
     135                $self_send = array_search( $r['sender_id'], $recipient_ids );
     136                if ( ! empty( $self_send ) && ( count( $recipient_ids ) > 1 ) ) {
     137                        unset( $recipient_ids[ $self_send ] );
     138                }
     139
     140                // Remove duplicates & bail if no recipients
     141                $recipient_ids = array_unique( $recipient_ids );
     142                if ( empty( $recipient_ids ) ) {
    128143                        return false;
     144                }
    129145
    130146                // Format this to match existing recipients
     
    135151        }
    136152
    137         if ( $message->send() ) {
    138                 do_action_ref_array( 'messages_message_sent', array( &$message ) );
    139 
    140                 return $message->thread_id;
    141         }
    142 
    143         return false;
     153        // Bail if message failed to send
     154        if ( ! $message->send() ) {
     155                return false;
     156        }
     157
     158        // Allow additional actions when a message is sent successfully
     159        do_action_ref_array( 'messages_message_sent', array( &$message ) );
     160
     161        // Return the thread ID
     162        return $message->thread_id;
    144163}
    145164
Note: See TracChangeset for help on using the changeset viewer.