BBoss_Global_Search_Members

BuddyPress Global Search - search members **************************************.

Defined (1)

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

/includes/search-types/class.BBoss_Global_Search_Members.php  
  1. class BBoss_Global_Search_Members extends BBoss_Global_Search_Type { 
  2. private $type = 'members'; 
  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_Members 
  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_Members(); 
  17.  
  18. add_action( 'bboss_global_search_settings_item_members', array( $instance, 'print_search_options' ) ); 
  19.  
  20. // Always return the instance 
  21. return $instance; 
  22.  
  23. /** 
  24. * A dummy constructor to prevent this class from being loaded more than once. 
  25. * @since 1.0.0 
  26. */ 
  27. private function __construct() { /** Do nothing here */ 
  28.  
  29. /** 
  30. * Generates sql for members search. 
  31. *  
  32. * @todo: if Mr.X has set privacy of xprofile field 'location' data as 'private' 
  33. * then, location of Mr.X shouldn't be checked in searched. 
  34. *  
  35. * @since 1.0.0 
  36. * @param string $search_term 
  37. * @param boolean $only_totalrow_count 
  38. *  
  39. * @return string sql query 
  40. */ 
  41. public function sql( $search_term, $only_totalrow_count=false ) { 
  42. global $wpdb, $bp; 
  43. $query_placeholder = array();  
  44. $items_to_search = buddyboss_global_search()->option('items-to-search'); 
  45.  
  46. $COLUMNS = " SELECT "; 
  47.  
  48. if( $only_totalrow_count ) { 
  49. $COLUMNS .= " COUNT( DISTINCT u.id ) "; 
  50. } else { 
  51. $COLUMNS .= " DISTINCT u.id, 'members' as type, u.display_name LIKE '%%%s%%' AS relevance, a.date_recorded as entry_date "; 
  52. $query_placeholder[] = $search_term; 
  53.  
  54. $FROM = " {$wpdb->users} u JOIN {$bp->members->table_name_last_activity} a ON a.user_id=u.id "; 
  55.  
  56. $WHERE = array(); 
  57. $WHERE[] = "1=1"; 
  58. $where_fields = array(); 
  59.  
  60. /** ++++++++++++++++++++++++++++++++ 
  61. * wp_users table fields 
  62. +++++++++++++++++++++++++++++++ */ 
  63. $user_fields = array(); 
  64. foreach( $items_to_search as $item ) { 
  65. //should start with member_field_ prefix 
  66. //see print_search_options 
  67. if( strpos( $item, 'member_field_' )===0 ) { 
  68. $user_fields[] = str_replace( 'member_field_', '', $item ); 
  69.  
  70. if( !empty( $user_fields ) ) { 
  71. $conditions_wp_user_table = array(); 
  72. foreach ( $user_fields as $user_field ) { 
  73.  
  74. if ( 'user_meta' === $user_field ) { 
  75. //Search in user meta table for terms 
  76. $conditions_wp_user_table[] = " ID IN ( SELECT user_id FROM {$wpdb->usermeta} WHERE meta_value LIKE '%%%s%%' ) "; 
  77. $query_placeholder[] = $search_term; 
  78. } else { 
  79. $conditions_wp_user_table[] = $user_field . " LIKE '%%%s%%' "; 
  80. $query_placeholder[] = $search_term; 
  81.  
  82.  
  83.  
  84. $clause_wp_user_table = "u.id IN ( SELECT ID FROM {$wpdb->users} WHERE ( "; 
  85. $clause_wp_user_table .= implode( ' OR ', $conditions_wp_user_table ); 
  86. $clause_wp_user_table .= " ) ) "; 
  87.  
  88. $where_fields[] = $clause_wp_user_table; 
  89. /** _____________________________ */ 
  90.  
  91. /** ++++++++++++++++++++++++++++++++ 
  92. * xprofile fields 
  93. +++++++++++++++++++++++++++++++ */ 
  94. //get all selected xprofile fields 
  95. if( function_exists( 'bp_is_active' ) && bp_is_active( 'xprofile' ) ) { 
  96. $groups = bp_xprofile_get_groups( array( 
  97. 'fetch_fields' => true 
  98. ) ); 
  99.  
  100. if ( !empty( $groups ) ) { 
  101. $selected_xprofile_fields = array( 
  102. 'word_search' => array(0), //Search for whole word in field of type checkbox and radio 
  103. 'char_search' => array(0), //Search for character in field of type textbox, textarea and etc 
  104. ); 
  105.  
  106. $word_search_field_type = array( 'radio', 'checkbox' ); 
  107.  
  108. foreach ( $groups as $group ) { 
  109. if ( !empty( $group->fields ) ) { 
  110. foreach ( $group->fields as $field ) { 
  111. $item = 'xprofile_field_' . $field->id; 
  112. if( !empty( $items_to_search ) && in_array( $item, $items_to_search ) ) { 
  113.  
  114. if( in_array( $field->type, $word_search_field_type ) ) { 
  115. $selected_xprofile_fields['word_search'][] = $field->id; 
  116. } else { 
  117. $selected_xprofile_fields['char_search'][] = $field->id; 
  118.  
  119. if( !empty( $selected_xprofile_fields ) ) { 
  120. //u.id IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '%%%s%%' ) 
  121. $clause_xprofile_table = "u.id IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE ( value LIKE '%%%s%%' AND field_id IN ( "; 
  122. $clause_xprofile_table .= implode( ', ', $selected_xprofile_fields['char_search'] ); 
  123. $clause_xprofile_table .= ") ) OR ( value REGEXP '[[:<:]]{$search_term}[[:>:]]' AND field_id IN ( "; 
  124. $clause_xprofile_table .= implode( ', ', $selected_xprofile_fields['word_search'] ); 
  125. $clause_xprofile_table .= ") ) ) "; 
  126.  
  127. $where_fields[] = $clause_xprofile_table; 
  128. $query_placeholder[] = $search_term; 
  129. /** _____________________________ */ 
  130.  
  131. if( !empty( $where_fields ) ) 
  132. $WHERE[] = implode ( ' OR ', $where_fields ); 
  133.  
  134. // other conditions 
  135. $WHERE[] = " a.component = 'members' "; 
  136. $WHERE[] = " a.type = 'last_activity' "; 
  137.  
  138. $sql = $COLUMNS . ' FROM ' . $FROM . ' WHERE ' . implode( ' AND ', $WHERE ); 
  139. if( !$only_totalrow_count ) { 
  140. $sql .= " GROUP BY u.id "; 
  141.  
  142. $sql = $wpdb->prepare( $sql, $query_placeholder ); 
  143.  
  144. return apply_filters(  
  145. 'BBoss_Global_Search_Members_sql',  
  146. $sql,  
  147. array(  
  148. 'search_term' => $search_term,  
  149. 'only_totalrow_count' => $only_totalrow_count,  
  150. )  
  151. ); 
  152.  
  153. protected function generate_html( $template_type='' ) { 
  154. $group_ids = array(); 
  155. foreach( $this->search_results['items'] as $item_id => $item ) { 
  156. $group_ids[] = $item_id; 
  157.  
  158. //now we have all the posts 
  159. //lets do a groups loop 
  160. if( bp_has_members( array( 'search_terms' => '', 'include'=>$group_ids, 'per_page'=>count($group_ids) ) ) ) { 
  161. while ( bp_members() ) { 
  162. bp_the_member(); 
  163.  
  164. $result_item = array( 
  165. 'id' => bp_get_member_user_id(),  
  166. 'type' => $this->type,  
  167. 'title' => bp_get_member_name(),  
  168. 'html' => buddyboss_global_search_buffer_template_part( 'loop/member', $template_type, false ),  
  169. ); 
  170.  
  171. $this->search_results['items'][bp_get_member_user_id()] = $result_item; 
  172.  
  173. /** 
  174. * What fields members should be searched on? 
  175. * Prints options to search through username, email, nicename/displayname. 
  176. * Prints xprofile fields, if xprofile component is active. 
  177. *  
  178. * @since 1.1.0 
  179. */ 
  180. function print_search_options( $items_to_search ) { 
  181. echo "<div class='wp-user-fields' style='margin: 10px 0 0 30px'>"; 
  182. echo "<p class='xprofile-group-name' style='margin: 5px 0'><strong>" . __('Account', 'buddypress-global-search') . "</strong></p>"; 
  183.  
  184. $fields = array( 
  185. 'user_login' => __( 'Username/Login', 'buddypress-global-search' ),  
  186. 'display_name' => __( 'Display Name', 'buddypress-global-search' ),  
  187. 'user_email' => __( 'Email', 'buddypress-global-search' ),  
  188. 'user_meta' => __( 'User Meta', 'buddypress-global-search' ) 
  189. ); 
  190. foreach( $fields as $field=>$label ) { 
  191. $item = 'member_field_' . $field; 
  192. $checked = !empty( $items_to_search ) && in_array( $item, $items_to_search ) ? ' checked' : ''; 
  193. echo "<label><input type='checkbox' value='{$item}' name='buddyboss_global_search_plugin_options[items-to-search][]' {$checked}>{$label}</label><br>"; 
  194.  
  195. echo "</div><!-- .wp-user-fields -->"; 
  196.  
  197. if( !function_exists( 'bp_is_active' ) || !bp_is_active( 'xprofile' ) ) 
  198. return; 
  199.  
  200. $groups = bp_xprofile_get_groups( array( 
  201. 'fetch_fields' => true 
  202. ) ); 
  203.  
  204. if ( !empty( $groups ) ) { 
  205. echo "<div class='xprofile-fields' style='margin: 0 0 10px 30px'>"; 
  206. foreach ( $groups as $group ) { 
  207. echo "<p class='xprofile-group-name' style='margin: 5px 0'><strong>" . $group->name . "</strong></p>"; 
  208.  
  209. if ( !empty( $group->fields ) ) { 
  210. foreach ( $group->fields as $field ) { 
  211. //lets save these as xprofile_field_{field_id} 
  212. $item = 'xprofile_field_' . $field->id; 
  213. $checked = !empty( $items_to_search ) && in_array( $item, $items_to_search ) ? ' checked' : ''; 
  214. echo "<label><input type='checkbox' value='{$item}' name='buddyboss_global_search_plugin_options[items-to-search][]' {$checked}>{$field->name}</label><br>"; 
  215. echo "</div><!-- .xprofile-fields -->";