BBoss_Global_Search_Messages

BuddyPress Global Search - search messages **************************************.

Defined (1)

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

/includes/search-types/class.BBoss_Global_Search_Messages.php  
  1. class BBoss_Global_Search_Messages extends BBoss_Global_Search_Type { 
  2. private $type = 'messages'; 
  3.  
  4. /** 
  5. * Insures that only one instance of Class exists in memory at any 
  6. * one time. Also prevents needing to define globals all over the place. 
  7. * @since 1.0.0 
  8. * @return object BBoss_Global_Search_Messages 
  9. */ 
  10. public static function instance() { 
  11. // Store the instance locally to avoid private static replication 
  12. static $instance = null; 
  13.  
  14. // Only run these methods if they haven't been run previously 
  15. if (null === $instance) { 
  16. $instance = new BBoss_Global_Search_Messages(); 
  17.  
  18. // Always return the instance 
  19. return $instance; 
  20.  
  21. /** 
  22. * A dummy constructor to prevent this class from being loaded more than once. 
  23. * @since 1.0.0 
  24. */ 
  25. private function __construct() { /** Do nothing here */ 
  26.  
  27. function sql( $search_term, $only_totalrow_count=false ) { 
  28. /** FOR INBOX SEARCH ---------- 
  29. SELECT  
  30. m.thread_id, MAX(m.date_sent) AS date_sent  
  31. FROM  
  32. wp_bp_messages_recipients r, wp_bp_messages_messages m  
  33. WHERE  
  34. m.thread_id = r.thread_id  
  35. AND r.is_deleted = 0  
  36. AND ( subject LIKE '%leo%' OR message LIKE '%leo%' )  
  37. AND r.user_id = 1  
  38. AND r.sender_only = 0  
  39. GROUP BY  
  40. m.thread_id  
  41. ORDER BY date_sent DESC LIMIT 0, 10 
  42. */ 
  43. /** FOR SENTBOX SEARCH -------- 
  44. SELECT  
  45. m.thread_id, MAX(m.date_sent) AS date_sent  
  46. FROM  
  47. wp_bp_messages_recipients r, wp_bp_messages_messages m  
  48. WHERE  
  49. m.thread_id = r.thread_id  
  50. AND r.is_deleted = 0  
  51. AND ( subject LIKE '%leo%' OR message LIKE '%leo%' )  
  52. AND m.sender_id = r.user_id  
  53. AND m.sender_id = 1  
  54. GROUP  
  55. BY m.thread_id  
  56. ORDER BY  
  57. date_sent DESC LIMIT 0, 10 
  58. */ 
  59. /** COMBINED --------------- 
  60. SELECT  
  61. m.thread_id, MAX(m.date_sent) AS date_sent  
  62. FROM  
  63. wp_bp_messages_recipients r, wp_bp_messages_messages m  
  64. WHERE  
  65. m.thread_id = r.thread_id  
  66. AND r.is_deleted = 0  
  67. AND ( subject LIKE '%leo%' OR message LIKE '%leo%' )  
  68. AND ( 
  69. ( r.user_id = 1 AND r.sender_only = 0 ) 
  70. OR 
  71. ( m.sender_id = 1 AND m.sender_id = r.user_id ) 
  72. )  
  73. GROUP  
  74. BY m.thread_id  
  75. ORDER BY  
  76. date_sent DESC LIMIT 0, 10 
  77. */ 
  78. /** a better version  
  79.  
  80. SELECT DISTINCT m.id , 'messages' as type, m.message LIKE '%Aliquam%' AS relevance, m.date_sent AS entry_date  
  81. FROM  
  82. wp_bp_messages_messages m LEFT JOIN wp_bp_messages_recipients r ON m.thread_id = r.thread_id  
  83. WHERE  
  84. r.is_deleted = 0  
  85. AND ( m.subject LIKE '%Aliquam%' OR m.message LIKE '%Aliquam%' )  
  86. AND ( 
  87. ( r.user_id = 1 AND r.sender_only = 0 )  
  88. OR  
  89. ( m.sender_id = 1 AND m.sender_id = r.user_id )  
  90. )  
  91. */ 
  92. global $wpdb, $bp; 
  93. $sql = " SELECT "; 
  94.  
  95. $query_placeholder = array(); 
  96. if( $only_totalrow_count ) { 
  97. $sql .= " COUNT( DISTINCT m.id ) "; 
  98. } else { 
  99. $sql .= " DISTINCT m.id , 'messages' as type, m.message LIKE '%%%s%%' AS relevance, m.date_sent AS entry_date "; 
  100. $query_placeholder[] = $search_term; 
  101.  
  102. $sql .= " FROM  
  103. {$bp->messages->table_name_messages} m LEFT JOIN {$bp->messages->table_name_recipients} r ON m.thread_id = r.thread_id  
  104. WHERE  
  105. r.is_deleted = 0  
  106. AND ( m.subject LIKE '%%%s%%' OR m.message LIKE '%%%s%%' )  
  107. AND ( 
  108. ( r.user_id = %d AND r.sender_only = 0 )  
  109. OR  
  110. ( m.sender_id = %d AND m.sender_id = r.user_id )  
  111. )  
  112. "; 
  113.  
  114. $query_placeholder[] = $search_term; 
  115. $query_placeholder[] = $search_term; 
  116. $query_placeholder[] = get_current_user_id(); 
  117. $query_placeholder[] = get_current_user_id(); 
  118.  
  119. $sql = $wpdb->prepare( $sql, $query_placeholder ); 
  120.  
  121. return apply_filters(  
  122. 'BBoss_Global_Search_Messages_sql',  
  123. $sql,  
  124. array(  
  125. 'search_term' => $search_term,  
  126. 'only_totalrow_count' => $only_totalrow_count,  
  127. )  
  128. ); 
  129.  
  130. protected function generate_html( $template_type='' ) { 
  131. $message_ids = array(); 
  132. foreach( $this->search_results['items'] as $message_id=>$item_html ) { 
  133. $message_ids[] = $message_id; 
  134.  
  135. //$message_ids = implode( ', ', $message_ids ); 
  136. global $wpdb, $bp; 
  137. $messages_sql = "SELECT m.* " 
  138. . " FROM {$bp->messages->table_name_messages} m " 
  139. . " WHERE m.id IN ( " . implode( ', ', $message_ids ) . " ) "; 
  140. $messages = $wpdb->get_results( $messages_sql ); 
  141.  
  142. $recepients_sql = "SELECT r.thread_id, GROUP_CONCAT( DISTINCT r.user_id ) AS 'recepient_ids' " 
  143. . " FROM {$bp->messages->table_name_recipients} r JOIN {$bp->messages->table_name_messages} m ON m.thread_id = r.thread_id " 
  144. . " WHERE m.id IN ( " . implode( ', ', $message_ids ) . " ) " 
  145. . " GROUP BY r.thread_id "; 
  146. $threads_recepients = $wpdb->get_results( $recepients_sql ); 
  147.  
  148. foreach( $threads_recepients as $thread_recepients ) { 
  149. foreach( $messages as $message ) { 
  150. if( $message->thread_id==$thread_recepients->thread_id ) { 
  151. $message->recepients = explode( ', ', $thread_recepients->recepient_ids ); 
  152.  
  153. foreach( $messages as $message ) { 
  154. global $current_message; 
  155. $current_message = $message; 
  156.  
  157. $result = array( 
  158. 'id' => $message->id,  
  159. 'type' => $this->type,  
  160. 'title' => $this->search_term,  
  161. 'html' => buddyboss_global_search_buffer_template_part( 'loop/message', $template_type, false ),  
  162. ); 
  163.  
  164. $this->search_results['items'][$message->id] = $result; 
  165.  
  166.