BBoss_Global_Search_Posts

BuddyPress Global Search - search posts **************************************.

Defined (1)

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

/includes/search-types/class.BBoss_Global_Search_Posts.php  
  1. class BBoss_Global_Search_Posts extends BBoss_Global_Search_Type { 
  2. private $type = 'posts'; 
  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. * @see buddyboss_global_search() 
  9. * @return object BBoss_Global_Search_Posts 
  10. */ 
  11. public static function instance() { 
  12. // Store the instance locally to avoid private static replication 
  13. static $instance = null; 
  14.  
  15. // Only run these methods if they haven't been run previously 
  16. if (null === $instance) { 
  17. $instance = new BBoss_Global_Search_Posts(); 
  18.  
  19. add_action( 'bboss_global_search_settings_item_posts', array( $instance, 'print_search_options' ) ); 
  20.  
  21. // Always return the instance 
  22. return $instance; 
  23.  
  24. /** 
  25. * A dummy constructor to prevent this class from being loaded more than once. 
  26. * @since 1.0.0 
  27. */ 
  28. private function __construct() { /** Do nothing here */ 
  29.  
  30. public function sql( $search_term, $only_totalrow_count=false ) { 
  31. /** an example UNION query :-  
  32. ----------------------------------------------------- 
  33. SELECT  
  34. wp_posts.id , 'posts' as type, wp_posts.post_title LIKE '%ho%' AS relevance, wp_posts.post_date as entry_date  
  35. FROM  
  36. wp_posts  
  37. WHERE  
  38. 1=1  
  39. AND ( 
  40. (wp_posts.post_title LIKE '%ho%')  
  41. OR (wp_posts.post_content LIKE '%ho%') 
  42. )  
  43. AND wp_posts.post_type IN ('post', 'page', 'attachment')  
  44. AND ( 
  45. wp_posts.post_status = 'publish'  
  46. OR wp_posts.post_author = 1  
  47. AND wp_posts.post_status = 'private' 
  48. )  
  49. ---------------------------------------------------- 
  50. */ 
  51. global $wpdb; 
  52. $query_placeholder = array();  
  53.  
  54. $sql = " SELECT "; 
  55.  
  56. if( $only_totalrow_count ) { 
  57. $sql .= " COUNT( DISTINCT id ) "; 
  58. } else { 
  59. $sql .= " DISTINCT id , 'posts' as type, post_title LIKE '%%%s%%' AS relevance, post_date as entry_date "; 
  60. $query_placeholder[] = $search_term; 
  61.  
  62.  
  63. /** ++++++++++++++++++++++++++++++++ 
  64. * wp_posts table fields 
  65. +++++++++++++++++++++++++++++++ */ 
  66. $items_to_search = buddyboss_global_search()->option('items-to-search'); 
  67. $post_fields = array(); 
  68. foreach( $items_to_search as $item ) { 
  69. //should start with member_field_ prefix 
  70. //see print_search_options 
  71. if( strpos( $item, 'post_field_' )===0 ) { 
  72. $post_field = str_replace( 'post_field_', '', $item ); 
  73. $post_fields[$post_field] = true; 
  74.  
  75. $sql .= " FROM {$wpdb->posts} WHERE 1=1 AND ( (post_title LIKE '%%%s%%') OR ( (post_content LIKE '%%%s%%') AND (post_content NOT LIKE '%%[%%%s%%]%%') )"; 
  76.  
  77. if ( ! empty( $post_fields['post_meta'] ) ) { 
  78. $sql .= " OR ( ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%%%s%%' ) )"; 
  79. $query_placeholder[] = $search_term; 
  80.  
  81. $sql .= ") AND post_type IN ('post', 'page') AND post_status = 'publish' "; 
  82.  
  83. $query_placeholder[] = $search_term; 
  84. $query_placeholder[] = $search_term; 
  85. $query_placeholder[] = $search_term; 
  86. $sql = $wpdb->prepare( $sql, $query_placeholder ); 
  87.  
  88. return apply_filters(  
  89. 'BBoss_Global_Search_Posts_sql',  
  90. $sql,  
  91. array(  
  92. 'search_term' => $search_term,  
  93. 'only_totalrow_count' => $only_totalrow_count,  
  94. )  
  95. ); 
  96.  
  97. protected function generate_html( $template_type='' ) { 
  98. $post_ids = array(); 
  99. foreach( $this->search_results['items'] as $item_id=>$item_html ) { 
  100. $post_ids[] = $item_id; 
  101.  
  102. //now we have all the posts 
  103. //lets do a wp_query and generate html for all posts 
  104. $qry = new WP_Query( array( 'post_type' =>array( 'post', 'page' ), 'post__in'=>$post_ids ) ); 
  105. if( $qry->have_posts() ) { 
  106. while( $qry->have_posts() ) { 
  107. $qry->the_post();  
  108. $result = array( 
  109. 'id' => get_the_ID(),  
  110. 'type' => $this->type,  
  111. 'title' => get_the_title(),  
  112. 'html' => buddyboss_global_search_buffer_template_part( 'loop/post', $template_type, false ),  
  113. ); 
  114.  
  115. $this->search_results['items'][get_the_ID()] = $result; 
  116. wp_reset_postdata(); 
  117.  
  118.  
  119. /** 
  120. * What fields members should be searched on? 
  121. * Prints options to search through username, email, nicename/displayname. 
  122. * Prints xprofile fields, if xprofile component is active. 
  123. * @since 1.1.0 
  124. */ 
  125. function print_search_options( $items_to_search ) { 
  126. echo "<div class='wp-posts-fields' style='margin: 10px 0 10px 30px'>"; 
  127. //echo "<p class='wp-post-part-name' style='margin: 5px 0'><strong>" . __('Account', 'buddypress-global-search') . "</strong></p>"; 
  128.  
  129. $fields = array( 
  130. 'post_meta' => __( 'Post Meta', 'buddypress-global-search' ),  
  131. ); 
  132.  
  133. foreach( $fields as $field=>$label ) { 
  134. $item = 'post_field_' . $field; 
  135. $checked = !empty( $items_to_search ) && in_array( $item, $items_to_search ) ? ' checked' : ''; 
  136. echo "<label><input type='checkbox' value='{$item}' name='buddyboss_global_search_plugin_options[items-to-search][]' {$checked}>{$label}</label><br>"; 
  137.  
  138. echo "</div><!-- .wp-user-fields -->"; 
  139.