[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Messages Classes. 4 * 5 * @package BuddyPress 6 * @subpackage MessagesClasses 7 * @since 1.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Single message class. 15 */ 16 class BP_Messages_Message { 17 /** 18 * ID of the message. 19 * 20 * @var int 21 */ 22 public $id; 23 24 /** 25 * ID of the message thread. 26 * 27 * @var int 28 */ 29 public $thread_id; 30 31 /** 32 * ID of the sender. 33 * 34 * @var int 35 */ 36 public $sender_id; 37 38 /** 39 * Subject line of the message. 40 * 41 * @var string 42 */ 43 public $subject; 44 45 /** 46 * Content of the message. 47 * 48 * @var string 49 */ 50 public $message; 51 52 /** 53 * Date the message was sent. 54 * 55 * @var string 56 */ 57 public $date_sent; 58 59 /** 60 * Message recipients. 61 * 62 * @var bool|array 63 */ 64 public $recipients = false; 65 66 /** 67 * Constructor. 68 * 69 * @param int|null $id Optional. ID of the message. 70 */ 71 public function __construct( $id = null ) { 72 $this->date_sent = bp_core_current_time(); 73 $this->sender_id = bp_loggedin_user_id(); 74 75 if ( ! empty( $id ) ) { 76 $this->populate( $id ); 77 } 78 } 79 80 /** 81 * Set up data related to a specific message object. 82 * 83 * @param int $id ID of the message. 84 */ 85 public function populate( $id ) { 86 global $wpdb; 87 88 $bp = buddypress(); 89 90 if ( $message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $id ) ) ) { 91 $this->id = (int) $message->id; 92 $this->thread_id = (int) $message->thread_id; 93 $this->sender_id = (int) $message->sender_id; 94 $this->subject = $message->subject; 95 $this->message = $message->message; 96 $this->date_sent = $message->date_sent; 97 } 98 } 99 100 /** 101 * Send a message. 102 * 103 * @return int|bool ID of the newly created message on success, false on failure. 104 */ 105 public function send() { 106 global $wpdb; 107 108 $bp = buddypress(); 109 110 $this->sender_id = apply_filters( 'messages_message_sender_id_before_save', $this->sender_id, $this->id ); 111 $this->thread_id = apply_filters( 'messages_message_thread_id_before_save', $this->thread_id, $this->id ); 112 $this->subject = apply_filters( 'messages_message_subject_before_save', $this->subject, $this->id ); 113 $this->message = apply_filters( 'messages_message_content_before_save', $this->message, $this->id ); 114 $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id ); 115 116 /** 117 * Fires before the current message item gets saved. 118 * 119 * Please use this hook to filter the properties above. Each part will be passed in. 120 * 121 * @since 1.0.0 122 * 123 * @param BP_Messages_Message $this Current instance of the message item being saved. Passed by reference. 124 */ 125 do_action_ref_array( 'messages_message_before_save', array( &$this ) ); 126 127 // Make sure we have at least one recipient before sending. 128 if ( empty( $this->recipients ) ) { 129 return false; 130 } 131 132 $new_thread = false; 133 134 // If we have no thread_id then this is the first message of a new thread. 135 if ( empty( $this->thread_id ) ) { 136 $this->thread_id = (int) $wpdb->get_var( "SELECT MAX(thread_id) FROM {$bp->messages->table_name_messages}" ) + 1; 137 $new_thread = true; 138 } 139 140 // First insert the message into the messages table. 141 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 ) ) ) { 142 return false; 143 } 144 145 $this->id = $wpdb->insert_id; 146 147 $recipient_ids = array(); 148 149 if ( $new_thread ) { 150 // Add an recipient entry for all recipients. 151 foreach ( (array) $this->recipients as $recipient ) { 152 $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 ) ); 153 $recipient_ids[] = $recipient->user_id; 154 } 155 156 // Add a sender recipient entry if the sender is not in the list of recipients. 157 if ( ! in_array( $this->sender_id, $recipient_ids ) ) { 158 $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, sender_only ) VALUES ( %d, %d, 1 )", $this->sender_id, $this->thread_id ) ); 159 } 160 } else { 161 // Update the unread count for all recipients. 162 $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = unread_count + 1, sender_only = 0, is_deleted = 0 WHERE thread_id = %d AND user_id != %d", $this->thread_id, $this->sender_id ) ); 163 } 164 165 messages_remove_callback_values(); 166 167 /** 168 * Fires after the current message item has been saved. 169 * 170 * @since 1.0.0 171 * 172 * @param BP_Messages_Message $this Current instance of the message item being saved. Passed by reference. 173 */ 174 do_action_ref_array( 'messages_message_after_save', array( &$this ) ); 175 176 return $this->id; 177 } 178 179 /** 180 * Get a list of recipients for a message. 181 * 182 * @return object $value List of recipients for a message. 183 */ 184 public function get_recipients() { 185 global $wpdb; 186 187 $bp = buddypress(); 188 189 return $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) ); 190 } 191 192 /** Static Functions **************************************************/ 193 194 /** 195 * Get list of recipient IDs from their usernames. 196 * 197 * @param array $recipient_usernames Usernames of recipients. 198 * 199 * @return bool|array $recipient_ids Array of Recepient IDs. 200 */ 201 public static function get_recipient_ids( $recipient_usernames ) { 202 $recipient_ids = false; 203 204 if ( ! $recipient_usernames ) { 205 return $recipient_ids; 206 } 207 208 if ( is_array( $recipient_usernames ) ) { 209 $rec_un_count = count( $recipient_usernames ); 210 211 for ( $i = 0, $count = $rec_un_count; $i < $count; ++ $i ) { 212 if ( $rid = bp_core_get_userid( trim( $recipient_usernames[ $i ] ) ) ) { 213 $recipient_ids[] = $rid; 214 } 215 } 216 } 217 218 /** 219 * Filters the array of recipients IDs. 220 * 221 * @since 2.8.0 222 * 223 * @param array $recipient_ids Array of recipients IDs that were retrieved based on submitted usernames. 224 * @param array $recipient_usernames Array of recipients usernames that were submitted by a user. 225 */ 226 return apply_filters( 'messages_message_get_recipient_ids', $recipient_ids, $recipient_usernames ); 227 } 228 229 /** 230 * Get the ID of the message last sent by the logged-in user for a given thread. 231 * 232 * @param int $thread_id ID of the thread. 233 * 234 * @return int|null ID of the message if found, otherwise null. 235 */ 236 public static function get_last_sent_for_user( $thread_id ) { 237 global $wpdb; 238 239 $bp = buddypress(); 240 241 $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 ) ); 242 243 return is_numeric( $query ) ? (int) $query : $query; 244 } 245 246 /** 247 * Check whether a user is the sender of a message. 248 * 249 * @param int $user_id ID of the user. 250 * @param int $message_id ID of the message. 251 * 252 * @return int|null Returns the ID of the message if the user is the 253 * sender, otherwise null. 254 */ 255 public static function is_user_sender( $user_id, $message_id ) { 256 global $wpdb; 257 258 $bp = buddypress(); 259 260 $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 ) ); 261 262 return is_numeric( $query ) ? (int) $query : $query; 263 } 264 265 /** 266 * Get the ID of the sender of a message. 267 * 268 * @param int $message_id ID of the message. 269 * 270 * @return int|null The ID of the sender if found, otherwise null. 271 */ 272 public static function get_message_sender( $message_id ) { 273 global $wpdb; 274 275 $bp = buddypress(); 276 277 $query = $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) ); 278 279 return is_numeric( $query ) ? (int) $query : $query; 280 } 281 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Mar 1 01:01:37 2021 | Cross-referenced by PHPXref 0.7.1 |