BP_Messages_Message
Single message class.
Description
Source
File: bp-messages/classes/class-bp-messages-message.php
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | class BP_Messages_Message { public static $last_inserted_id ; /** * ID of the message. * * @var int */ public $id ; /** * ID of the message thread. * * @var int */ public $thread_id ; /** * ID of the sender. * * @var int */ public $sender_id ; /** * Subject line of the message. * * @var string */ public $subject ; /** * Content of the message. * * @var string */ public $message ; /** * Date the message was sent. * * @var string */ public $date_sent ; /** * Message recipients. * * @var bool|array */ public $recipients = false; /** * Constructor. * * @param int|null $id Optional. ID of the message. */ public function __construct( $id = null ) { $this ->date_sent = bp_core_current_time(); $this ->sender_id = bp_loggedin_user_id(); if ( ! empty ( $id ) ) { $this ->populate( $id ); } } /** * Set up data related to a specific message object. * * @param int $id ID of the message. */ public function populate( $id ) { global $wpdb ; $bp = buddypress(); if ( $message = $wpdb ->get_row( $wpdb ->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d" , $id ) ) ) { $this ->id = (int) $message ->id; $this ->thread_id = (int) $message ->thread_id; $this ->sender_id = (int) $message ->sender_id; $this ->subject = $message ->subject; $this ->message = $message ->message; $this ->date_sent = $message ->date_sent; } } /** * Send a message. * * @return int|bool ID of the newly created message on success, false on failure. */ public function send() { global $wpdb ; $bp = buddypress(); $this ->sender_id = apply_filters( 'messages_message_sender_id_before_save' , $this ->sender_id, $this ->id ); $this ->thread_id = apply_filters( 'messages_message_thread_id_before_save' , $this ->thread_id, $this ->id ); $this ->subject = apply_filters( 'messages_message_subject_before_save' , $this ->subject, $this ->id ); $this ->message = apply_filters( 'messages_message_content_before_save' , $this ->message, $this ->id ); $this ->date_sent = apply_filters( 'messages_message_date_sent_before_save' , $this ->date_sent, $this ->id ); /** * Fires before the current message item gets saved. * * Please use this hook to filter the properties above. Each part will be passed in. * * @since BuddyPress 1.0.0 * * @param BP_Messages_Message $this Current instance of the message item being saved. Passed by reference. */ do_action_ref_array( 'messages_message_before_save' , array ( & $this ) ); // Make sure we have at least one recipient before sending. if ( empty ( $this ->recipients ) ) { return false; } $new_thread = false; // If we have no thread_id then this is the first message of a new thread. if ( empty ( $this ->thread_id ) ) { $this ->thread_id = (int) $wpdb ->get_var( "SELECT MAX(thread_id) FROM {$bp->messages->table_name_messages}" ) + 1; $new_thread = true; } // First insert the message into the messages table. if ( ! $wpdb ->query( $wpdb ->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( thread_id, sender_id, subject, message, date_sent ) VALUES ( %d, %d, %s, %s, %s )" , $this ->thread_id, $this ->sender_id, $this ->subject, $this ->message, $this ->date_sent ) ) ) { return false; } static :: $last_inserted_id = $this ->id = $wpdb ->insert_id; $recipient_ids = array (); if ( $new_thread ) { // Add an recipient entry for all recipients. foreach ( ( array ) $this ->recipients as $recipient ) { $wpdb ->query( $wpdb ->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, unread_count ) VALUES ( %d, %d, 1 )" , $recipient ->user_id, $this ->thread_id ) ); $recipient_ids [] = $recipient ->user_id; } // Add a sender recipient entry if the sender is not in the list of recipients. if ( ! in_array( $this ->sender_id, $recipient_ids ) ) { $wpdb ->query( $wpdb ->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id ) VALUES ( %d, %d )" , $this ->sender_id, $this ->thread_id ) ); } } else { // Update the unread count for all recipients. $wpdb ->query( $wpdb ->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = unread_count + 1, is_deleted = 0 WHERE thread_id = %d AND user_id != %d" , $this ->thread_id, $this ->sender_id ) ); } messages_remove_callback_values(); /** * Fires after the current message item has been saved. * * @since BuddyPress 1.0.0 * * @param BP_Messages_Message $this Current instance of the message item being saved. Passed by reference. */ do_action_ref_array( 'messages_message_after_save' , array ( & $this ) ); return $this ->id; } /** * Get a list of recipients for a message. * * @return object $value List of recipients for a message. */ public function get_recipients() { global $wpdb ; $bp = buddypress(); return $wpdb ->get_results( $wpdb ->prepare( "SELECT user_id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d" , $this ->thread_id ) ); } /** Static Functions **************************************************/ /** * Get list of recipient IDs from their usernames. * * @param array $recipient_usernames Usernames of recipients. * * @return bool|array $recipient_ids Array of Recepient IDs. */ public static function get_recipient_ids( $recipient_usernames ) { $recipient_ids = false; if ( ! $recipient_usernames ) { return $recipient_ids ; } if ( is_array ( $recipient_usernames ) ) { $rec_un_count = count ( $recipient_usernames ); for ( $i = 0, $count = $rec_un_count ; $i < $count ; ++ $i ) { if ( $rid = bp_core_get_userid( trim( $recipient_usernames [ $i ] ) ) ) { $recipient_ids [] = $rid ; } } } /** * Filters the array of recipients IDs. * * @since BuddyPress 2.8.0 * * @param array $recipient_ids Array of recipients IDs that were retrieved based on submitted usernames. * @param array $recipient_usernames Array of recipients usernames that were submitted by a user. */ return apply_filters( 'messages_message_get_recipient_ids' , $recipient_ids , $recipient_usernames ); } /** * Get the ID of the message last sent by the logged-in user for a given thread. * * @param int $thread_id ID of the thread. * * @return int|null ID of the message if found, otherwise null. */ public static function get_last_sent_for_user( $thread_id ) { global $wpdb ; $bp = buddypress(); $query = $wpdb ->get_var( $wpdb ->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND thread_id = %d ORDER BY date_sent DESC LIMIT 1" , bp_loggedin_user_id(), $thread_id ) ); return is_numeric ( $query ) ? (int) $query : $query ; } /** * Check whether a user is the sender of a message. * * @param int $user_id ID of the user. * @param int $message_id ID of the message. * * @return int|null Returns the ID of the message if the user is the * sender, otherwise null. */ public static function is_user_sender( $user_id , $message_id ) { global $wpdb ; $bp = buddypress(); $query = $wpdb ->get_var( $wpdb ->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND id = %d" , $user_id , $message_id ) ); return is_numeric ( $query ) ? (int) $query : $query ; } /** * Get the ID of the sender of a message. * * @param int $message_id ID of the message. * * @return int|null The ID of the sender if found, otherwise null. */ public static function get_message_sender( $message_id ) { global $wpdb ; $bp = buddypress(); $query = $wpdb ->get_var( $wpdb ->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE id = %d" , $message_id ) ); return is_numeric ( $query ) ? (int) $query : $query ; } /** * Delete all the message send by user * * @BuddyBoss 1.0.0 * * @param int $user_id user id whom message should get deleted */ public static function delete_user_message( $user_id ){ global $wpdb ; $bp = buddypress(); // Get the message ids in order to delete their metas. $message_ids = $wpdb ->get_col( $wpdb ->prepare( "SELECT DISTINCT (id) FROM {$bp->messages->table_name_messages} WHERE sender_id = %d" , $user_id ) ); $wpdb ->query( $wpdb ->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE sender_id = %d" , $user_id ) ); // Delete message meta. foreach ( $message_ids as $message_id ) { bp_messages_delete_meta( $message_id ); } // delete all the meta recipients from user table. $wpdb ->query( $wpdb ->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE user_id = %d" , $user_id ) ); } /** * Get existsing thread which matches the recipients * * @since BuddyBoss 1.0.0 * * @param array $recipient_ids * @param integer $sender */ public static function get_existing_thread( $recipient_ids , $sender = 0 ) { global $wpdb ; $bp = buddypress(); // add the sender into the recipient list and order by id ascending $recipient_ids [] = $sender ; $recipient_ids = array_filter ( array_unique ( array_values ( $recipient_ids ))); sort( $recipient_ids ); $results = $wpdb ->get_results( $sql = $wpdb ->prepare( "SELECT r.thread_id as thread_id, GROUP_CONCAT(DISTINCT user_id ORDER BY user_id separator ',' ) as recipient_list, MAX(m.date_sent) AS date_sent FROM { $bp ->messages->table_name_recipients} r INNER JOIN { $bp ->messages->table_name_messages} m ON m.thread_id = r.thread_id GROUP BY r.thread_id HAVING recipient_list = %s ORDER BY date_sent DESC LIMIT 1 ", implode( ',' , $recipient_ids ) ) ); if ( ! $results ) { return null; } $thread_id = $results [0]->thread_id; if ( ! $is_active_recipient = BP_Messages_Thread::is_thread_recipient( $thread_id , $sender ) ) { return null; } return $thread_id ; } } |
Methods
- __construct — Constructor.
- convert_orderby_to_order_by_term — Convert the 'orderby' param into a proper SQL term/column.
- delete_user_message — Delete all the message send by user
- get — Query for messages
- get_date_query_sql — Get the SQL for the 'date_query' param in BP_Messages_Message::get().
- get_existing_thread — Get existsing thread which matches the recipients
- get_existing_threads — Get existsing threads which matches the recipients
- get_last_sent_for_user — Get the ID of the message last sent by the logged-in user for a given thread.
- get_message_sender — Get the ID of the sender of a message.
- get_meta_query_sql — Get the SQL for the 'meta_query' param in BP_Messages_Message::get()
- get_recipient_ids — Get list of recipient IDs from their usernames.
- get_recipients — Get a list of recipients for a message.
- is_user_sender — Check whether a user is the sender of a message.
- populate — Set up data related to a specific message object.
- send — Send a message.
- strip_leading_and — Strips the leading AND and any surrounding whitespace from a string.
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.