/includes/filters.php

  1. <?php  
  2. // Exit if accessed directly 
  3. if ( ! defined( 'ABSPATH' ) ) exit; 
  4.  
  5. /** 
  6. * Get the default items to search though, if nothing has been selected in settings. 
  7. *  
  8. * @since 1.0.0 
  9. * @param mixed $value 
  10. * @return mixed 
  11. */ 
  12. function bb_global_search_default_items_to_search( $value ) { 
  13. if( empty( $value ) ) { 
  14. /** 
  15. * Setting > what to search? 
  16. * If admin has not selected anything yet( right after activating the plugin maybe),  
  17. * lets make sure search results do return someting at least. 
  18. * So, by default, we'll search though blog posts and members. 
  19. */ 
  20. $value = array( 'posts', 'members' ); 
  21.  
  22. /** 
  23. * If member search is turned on, but none of wp_user table fields or xprofile fields are selected,  
  24. * we'll force username and nicename fields 
  25. */ 
  26. if( in_array( 'members', $value ) ) { 
  27. // Is any wp_user table colum or xprofile field selected? 
  28. $field_selected = false; 
  29. foreach( $value as $item_to_search ) { 
  30. if( strpos( $item_to_search, 'member_field_' )===0 || strpos( $item_to_search, 'xprofile_field_' )===0 ) { 
  31. $field_selected = true; 
  32. break; 
  33.  
  34. //if not, lets add username and nicename to default items to search 
  35. if( !$field_selected ) { 
  36. $value[] = 'member_field_user_login'; 
  37. $value[] = 'member_field_user_nicename'; 
  38.  
  39. return $value; 
  40. add_filter( 'buddyboss_global_search_option_items-to-search', 'bb_global_search_default_items_to_search' ); 
  41.  
  42. /** 
  43. * Remove 'messages' and 'notifications' from search, if user is not logged In 
  44. *  
  45. * @since 1.0.0 
  46. * @param mixed $value 
  47. * @return mixed 
  48. */ 
  49. function bboss_global_search_remove_search_types_for_guests( $search_types ) { 
  50. if( !is_admin() && !empty( $search_types ) && !is_user_logged_in() ) { 
  51. $items_to_remove = array( 'messages', 'notifications' ); 
  52. $filtered_search_types = array(); 
  53. foreach( $search_types as $search_type ) { 
  54. if( !in_array( $search_type, $items_to_remove ) ) { 
  55. $filtered_search_types[] = $search_type; 
  56.  
  57. $search_types = $filtered_search_types; 
  58. return $search_types; 
  59. add_filter( 'buddyboss_global_search_option_items-to-search', 'bboss_global_search_remove_search_types_for_guests', 9 ); 
  60.  
  61.  
  62. add_filter( 'template_include', 'buddyboss_global_search_override_wp_native_results', 999 ); //don't leave any chance!. 
  63.  
  64. /** 
  65. * Force native wp search section to load page template so we can hook stuff into it. 
  66. * 
  67. * @since 1.0.0 
  68. * @param mixed $value 
  69. * @return mixed 
  70. **/ 
  71.  
  72. function buddyboss_global_search_override_wp_native_results($template) { 
  73.  
  74. if ( is_search() ) { //if search page. 
  75.  
  76.  
  77. $live_template = locate_template( array( 'buddyboss-global-search.php' , 'page.php', 'single.php', 'index.php' ) ); 
  78.  
  79. if ( '' != $live_template ) { 
  80. return $live_template; 
  81.  
  82.  
  83. return $template; 
  84.  
  85.  
  86. /** 
  87. * Load dummy post for wp native search result. magic starts here. 
  88. * @since 1.0.0  
  89. * @param mixed $value 
  90. * @return mixed 
  91. **/ 
  92.  
  93. add_filter( 'template_include', 'buddyboss_global_search_result_page_dummy_post_load', 999 ); //don't leave any chance!. 
  94.  
  95. function buddyboss_global_search_result_page_dummy_post_load($template) { 
  96. global $wp_query; 
  97.  
  98. if(!is_search()) { //cancel if not search page. 
  99. return $template;  
  100.  
  101. $dummy = array( 
  102. 'ID' => 0,  
  103. 'post_status' => 'public',  
  104. 'post_author' => 0,  
  105. 'post_parent' => 0,  
  106. 'post_type' => 'page',  
  107. 'post_date' => 0,  
  108. 'post_date_gmt' => 0,  
  109. 'post_modified' => 0,  
  110. 'post_modified_gmt' => 0,  
  111. 'post_content' => '',  
  112. 'post_title' => __('Search Results', "buddypress-global-search"),  
  113. 'post_excerpt' => '',  
  114. 'post_content_filtered' => '',  
  115. 'post_mime_type' => '',  
  116. 'post_password' => '',  
  117. 'post_name' => '',  
  118. 'guid' => '',  
  119. 'menu_order' => 0,  
  120. 'pinged' => '',  
  121. 'to_ping' => '',  
  122. 'ping_status' => '',  
  123. 'comment_status' => 'closed',  
  124. 'comment_count' => 0,  
  125. 'filter' => 'raw',  
  126. 'is_404' => false,  
  127. 'is_page' => false,  
  128. 'is_single' => false,  
  129. 'is_archive' => false,  
  130. 'is_tax' => false,  
  131. 'is_search' => true,  
  132. ); 
  133. // Set the $post global 
  134. $post = new WP_Post( (object) $dummy ); 
  135.  
  136. // Copy the new post global into the main $wp_query 
  137. $wp_query->post = $post; 
  138. $wp_query->posts = array( $post ); 
  139. $wp_query->post_count = 1; 
  140. $wp_query->max_num_pages = 0; 
  141.  
  142. return $template; 
  143.  
  144. /** 
  145. * Force native wp search page not to look any data into db to save query and performance 
  146. * @since 1.0.0 
  147. * @param mixed $value 
  148. * @return mixed 
  149. **/ 
  150.  
  151. add_filter('pre_get_posts', 'buddyboss_global_search_clear_native_search_query'); 
  152.  
  153. function buddyboss_global_search_clear_native_search_query($query) { 
  154.  
  155. if ($query->is_search && !is_admin() ) { 
  156.  
  157. remove_filter('pre_get_posts', 'buddyboss_global_search_clear_native_search_query'); //only do first time 
  158.  
  159.  
  160. return $query; 
.