BP_Messages_Message

Single message class.

Defined (1)

The class is defined in the following location(s).

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