Changeset 8618
- Timestamp:
- 07/15/2014 10:03:53 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-messages/bp-messages-functions.php
r8481 r8618 39 39 function messages_new_message( $args = '' ) { 40 40 41 $defaults = array ( 41 // Parse the default arguments 42 $r = bp_parse_args( $args, array( 42 43 '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. 45 46 'subject' => false, 46 47 'content' => false, 47 48 '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'] ) ) { 54 53 return false; 54 } 55 55 56 56 // Create a new message object 57 57 $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'] ); 67 69 $message->recipients = $thread->get_recipients(); 68 70 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 ) ) { 74 79 $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 77 83 } else { 78 if ( empty( $recipients ) ) 84 85 // Bail if no recipients 86 if ( empty( $r['recipients'] ) ) { 79 87 return false; 80 81 if ( empty( $message->subject ) ) 88 } 89 90 // Set a default subject if none exists 91 if ( empty( $message->subject ) ) { 82 92 $message->subject = __( 'No Subject', 'buddypress' ); 83 93 } 94 95 // Setup the recipients array 84 96 $recipient_ids = array(); 85 97 … … 88 100 89 101 // 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 91 105 $recipient = trim( $recipient ); 92 93 if ( empty( $recipient ) ) 106 if ( empty( $recipient ) ) { 94 107 continue; 95 96 $recipient_id = false; 97 98 // check user_login / nicename columns first 108 } 109 110 // Check user_login / nicename columns first 99 111 // @see http://buddypress.trac.wordpress.org/ticket/5151 100 112 if ( bp_is_username_compatibility_mode() ) { … … 104 116 } 105 117 106 // check against user ID column if no match and if passed recipient is numeric107 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 ) ) { 108 120 if ( bp_core_get_core_userdata( (int) $recipient ) ) { 109 121 $recipient_id = (int) $recipient; … … 111 123 } 112 124 113 if ( ! $recipient_id ) { 125 // Decide which group to add this recipient to 126 if ( empty( $recipient_id ) ) { 114 127 $invalid_recipients[] = $recipient; 115 128 } else { … … 118 131 } 119 132 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 ) ) { 128 143 return false; 144 } 129 145 130 146 // Format this to match existing recipients … … 135 151 } 136 152 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; 144 163 } 145 164
Note: See TracChangeset
for help on using the changeset viewer.