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