messages_new_message( array|string $args = '' )
Create a new message.
Description
Parameters
- $args
-
(Optional) Array of arguments
Default value: ''
Return
(int|bool|WP_Error) ID of the message thread on success, false on failure.
Source
File: bp-messages/bp-messages-functions.php
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | function messages_new_message( $args = '' ) { // Parse the default arguments. $r = bp_parse_args( $args , array ( 'sender_id' => bp_loggedin_user_id(), 'thread_id' => false, // False for a new message, thread id for a reply to a thread. 'recipients' => array (), // Can be an array of usernames, user_ids or mixed. 'subject' => false, 'content' => false, 'date_sent' => bp_core_current_time(), 'append_thread' => true, 'error_type' => 'bool' ), 'messages_new_message' ); // Bail if no sender or no content. if ( empty ( $r [ 'sender_id' ] ) || empty ( $r [ 'content' ] ) ) { if ( 'wp_error' === $r [ 'error_type' ] ) { if ( empty ( $r [ 'sender_id' ] ) ) { $error_code = 'messages_empty_sender' ; $feedback = __( 'Your message was not sent. Please use a valid sender.' , 'buddyboss' ); } else { $error_code = 'messages_empty_content' ; $feedback = __( 'Your message was not sent. Please enter some content.' , 'buddyboss' ); } return new WP_Error( $error_code , $feedback ); } else { return false; } } // Create a new message object. $message = new BP_Messages_Message; $message ->thread_id = $r [ 'thread_id' ]; $message ->sender_id = $r [ 'sender_id' ]; $message ->subject = $r [ 'subject' ]; $message ->message = $r [ 'content' ]; $message ->date_sent = $r [ 'date_sent' ]; $new_reply = false; // If we have a thread ID... if ( ! empty ( $r [ 'thread_id' ] ) ) { // ...use the existing recipients $thread = new BP_Messages_Thread( $r [ 'thread_id' ] ); $message ->recipients = $thread ->get_recipients(); // Strip the sender from the recipient list, and unset them if they are // not alone. If they are alone, let them talk to themselves. if ( isset( $message ->recipients[ $r [ 'sender_id' ] ] ) && ( count ( $message ->recipients ) > 1 ) ) { unset( $message ->recipients[ $r [ 'sender_id' ] ] ); } // Set a default reply subject if none was sent. if ( empty ( $message ->subject ) ) { $re = __( 'Re: ' , 'buddyboss' ); if ( strpos ( $thread ->messages[0]->subject, $re ) === 0 ) { $message ->subject = $thread ->messages[0]->subject; } else { $message ->subject = $re . $thread ->messages[0]->subject; } } $new_reply = true; // ...otherwise use the recipients passed } else { // Bail if no recipients. if ( empty ( $r [ 'recipients' ] ) ) { if ( 'wp_error' === $r [ 'error_type' ] ) { return new WP_Error( 'message_empty_recipients' , __( 'Message could not be sent. Please enter a recipient.' , 'buddyboss' ) ); } else { return false; } } // Set a default subject if none exists. if ( empty ( $message ->subject ) ) { $message ->subject = __( 'No Subject' , 'buddyboss' ); } // Setup the recipients array. $recipient_ids = array (); // Invalid recipients are added to an array, for future enhancements. $invalid_recipients = array (); // Loop the recipients and convert all usernames to user_ids where needed. foreach ( ( array ) $r [ 'recipients' ] as $recipient ) { // Trim spaces and skip if empty. $recipient = trim( $recipient ); if ( empty ( $recipient ) ) { continue ; } // Check user_login / nicename columns first if ( bp_is_username_compatibility_mode() ) { $recipient_id = bp_core_get_userid( urldecode( $recipient ) ); } else { $recipient_id = bp_core_get_userid_from_nicename( $recipient ); } // Check against user ID column if no match and if passed recipient is numeric. if ( empty ( $recipient_id ) && is_numeric ( $recipient ) ) { if ( bp_core_get_core_userdata( (int) $recipient ) ) { $recipient_id = (int) $recipient ; } } // Decide which group to add this recipient to. if ( empty ( $recipient_id ) ) { $invalid_recipients [] = $recipient ; } else { $recipient_ids [] = (int) $recipient_id ; } } // Strip the sender from the recipient list, and unset them if they are // not alone. If they are alone, let them talk to themselves. $self_send = array_search ( $r [ 'sender_id' ], $recipient_ids ); if ( ! empty ( $self_send ) && ( count ( $recipient_ids ) > 1 ) ) { unset( $recipient_ids [ $self_send ] ); } // Remove duplicates & bail if no recipients. $recipient_ids = array_unique ( $recipient_ids ); if ( empty ( $recipient_ids ) ) { if ( 'wp_error' === $r [ 'error_type' ] ) { return new WP_Error( 'message_invalid_recipients' , __( 'Message could not be sent because you have entered an invalid username. Please try again.' , 'buddyboss' ) ); } else { return false; } } // Format this to match existing recipients. foreach ( ( array ) $recipient_ids as $i => $recipient_id ) { $message ->recipients[ $i ] = new stdClass; $message ->recipients[ $i ]->user_id = $recipient_id ; } $previous_thread = BP_Messages_Message::get_existing_thread( $recipient_ids , $r [ 'sender_id' ] ); if ( $previous_thread && $r [ 'append_thread' ] ) { $message ->thread_id = $r [ 'thread_id' ] = (int) $previous_thread ; // Set a default reply subject if none was sent. if ( empty ( $message ->subject ) ) { $message ->subject = sprintf( __( '%s' , 'buddyboss' ), wp_trim_words( $thread ->messages[0]->subject , messages_get_default_subject_length()) ); } } } // check if force friendship is enabled and check recipients if ( bp_force_friendship_to_message() && bp_is_active( 'friends' ) ) { $error_messages = array ( 'new_message' => __( 'You need to be connected with this member in order to send a message.' , 'buddyboss' ), 'new_reply' => __( 'You need to be connected with this member to continue this conversation.' , 'buddyboss' ), 'new_group_message' => __( 'You need to be connected with all recipients in order to send them a message.' , 'buddyboss' ), 'new_group_reply' => __( 'You need to be connected with all recipients to continue this conversation.' , 'buddyboss' ), ); foreach ( ( array ) $message ->recipients as $i => $recipient ) { if ( ! friends_check_friendship( $message ->sender_id, $recipient ->user_id ) ) { if ( 'wp_error' === $r [ 'error_type' ] ) { if ( $new_reply && sizeof( $message ->recipients ) == 1 ) { return new WP_Error( 'message_invalid_recipients' , $error_messages [ 'new_reply' ] ); } else if ( $new_reply && sizeof( $message ->recipients ) > 1 ) { return new WP_Error( 'message_invalid_recipients' , $error_messages [ 'new_group_reply' ] ); } else if ( sizeof( $message ->recipients ) > 1 ) { return new WP_Error( 'message_invalid_recipients' , $error_messages [ 'new_group_message' ] ); } else { return new WP_Error( 'message_invalid_recipients' , $error_messages [ 'new_message' ] ); } } else { return false; } } } } // preapre to upadte the deleted user's last message if message sending successfull $last_message_data = BP_Messages_Thread::prepare_last_message_status( $message ->thread_id ); // Bail if message failed to send. $send = $message ->send(); if ( false === is_int ( $send ) ) { if ( 'wp_error' === $r [ 'error_type' ] ) { if ( is_wp_error( $send ) ) { return $send ; } else { return new WP_Error( 'message_generic_error' , __( 'Message was not sent. Please try again.' , 'buddyboss' ) ); } } return false; } // only update after the send() BP_Messages_Thread::update_last_message_status( $last_message_data ); /** * Fires after a message has been successfully sent. * * @since BuddyPress 1.1.0 * * @param BP_Messages_Message $message Message object. Passed by reference. */ do_action_ref_array( 'messages_message_sent' , array ( & $message ) ); // Return the thread ID. return $message ->thread_id; } |
Changelog
Version | Description |
---|---|
BuddyPress 2.4.0 Added 'error_type' as an additional $args parameter. | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.