BBoss_Global_Search_ACF

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

Defined (1)

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

/includes/plugins/search-acf/class.BBoss_Global_Search_ACF.php  
  1. class BBoss_Global_Search_ACF extends BBoss_Global_Search_Type { 
  2.  
  3. private $acf_field_group; 
  4.  
  5. /** 
  6. * A real constructor. Since we do want multiple copies of this class. 
  7. * The idea is to have one object for each searchable custom post type. 
  8. * @since 1.0.0 
  9. */ 
  10. public function __construct( $acf_field_group ) { 
  11. $this->acf_field_group = $acf_field_group; 
  12.  
  13. public function sql( $search_term, $only_totalrow_count = false ) {  
  14.  
  15. $rule = get_post_meta( $this->acf_field_group, 'rule', true ); 
  16.  
  17. $param = $rule['param']; 
  18.  
  19. $param_post = strpos($param, 'post'); 
  20. $param_page = strpos($param, 'page'); 
  21.  
  22. //Return if field is not for user, post, page screen. 
  23. if ( $param != 'ef_user' && $param_post != 0 && $param_page != 0 ) { 
  24. return; 
  25.  
  26. global $wpdb; 
  27. $query_placeholder1 = array(); 
  28. $query_placeholder2 = array(); 
  29.  
  30. //Getting ACF meta_key 
  31. $acf_meta_objects = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key LIKE %s", $this->acf_field_group, 'field_%'), ARRAY_A); 
  32.  
  33. if ( ! empty( $acf_meta_objects ) ) { 
  34. $count_acf_meta_objects = count($acf_meta_objects); 
  35.  
  36. foreach ( $acf_meta_objects as $key => $acf_meta_obj ) { 
  37. $acf_meta_obj = maybe_unserialize( $acf_meta_obj[ 'meta_value' ] ); 
  38. $acf_meta_key = $acf_meta_obj[ 'name' ]; 
  39.  
  40. $metasql .= " ( ({$wpdb->prefix}postmeta.meta_key = '$acf_meta_key' )  
  41. AND ({$wpdb->prefix}postmeta.meta_value LIKE '%%%s%%') 
  42. )"; 
  43.  
  44. $usersql .= " ( ({$wpdb->prefix}usermeta.meta_key = '$acf_meta_key' )  
  45. AND ({$wpdb->prefix}usermeta.meta_value LIKE '%%%s%%') 
  46. )"; 
  47.  
  48. if ( ( $count_acf_meta_objects - 1 ) != $key ) {  
  49. $metasql .= "OR"; 
  50. $usersql .= "OR"; 
  51.  
  52.  
  53. if ( $param_post === 0 ) { 
  54. //Posts query begins 
  55. $sql = " SELECT "; 
  56.  
  57. if ( $only_totalrow_count ) { 
  58. $sql .= " COUNT( DISTINCT id ) "; 
  59. } else { 
  60. $sql .= " DISTINCT id, 'posts' as type, post_title LIKE '%%%s%%' AS relevance, post_date as entry_date "; 
  61.  
  62. $sql .= " FROM  
  63. {$wpdb->prefix}posts INNER JOIN {$wpdb->prefix}postmeta ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id  
  64. WHERE  
  65. 1=1  
  66. AND {$wpdb->prefix}posts.post_type IN ('post', 'page') 
  67. AND {$wpdb->prefix}posts.post_status = 'publish' 
  68. AND ( 
  69. $metasql 
  70. ) "; 
  71.  
  72. $query_placeholder1[] = $search_term; 
  73.  
  74. for( $i = 1; $i <= $count_acf_meta_objects; $i++ ) { 
  75. $query_placeholder1[] = $search_term; 
  76. //Post query ends 
  77. $sql = $wpdb->prepare( $sql, $query_placeholder1 ); 
  78.  
  79.  
  80. if ( $param == 'ef_user' ) { 
  81. //User query begins 
  82. $user_sql = " SELECT "; 
  83.  
  84. if ( $only_totalrow_count ) { 
  85. $user_sql .= " COUNT( DISTINCT id ) "; 
  86. } else { 
  87. $user_sql .= " DISTINCT id, 'members' as type, display_name LIKE '%%%s%%' AS relevance, user_registered as entry_date "; 
  88.  
  89. $user_sql .= " FROM  
  90. {$wpdb->prefix}users INNER JOIN {$wpdb->prefix}usermeta ON {$wpdb->prefix}users.ID = {$wpdb->prefix}usermeta.user_id  
  91. WHERE  
  92. 1=1 
  93. AND ( 
  94. $usersql 
  95. ) "; 
  96.  
  97. $query_placeholder2[] = $search_term; 
  98.  
  99. for( $i = 1; $i <= $count_acf_meta_objects; $i++ ) { 
  100. $query_placeholder2[] = $search_term; 
  101. $sql = $wpdb->prepare( $user_sql, $query_placeholder2 ); 
  102.  
  103. return apply_filters(  
  104. 'BBoss_Global_Search_ACF_sql',  
  105. $sql,  
  106. array(  
  107. 'search_term' => $search_term,  
  108. 'only_totalrow_count' => $only_totalrow_count,  
  109. )  
  110. ); 
  111.  
  112. protected function generate_html( $template_type = '' ) { 
  113.  
  114. $post_ids = array(); 
  115. foreach( $this->search_results['items'] as $item_id=>$item_html ) { 
  116. $post_ids[] = $item_id; 
  117.  
  118. //now we have all the posts 
  119. //lets do a wp_query and generate html for all posts 
  120. $query = new WP_Query( array( 'post_type' =>array( 'post', 'page' ), 'post__in'=>$post_ids ) ); 
  121. if( $query->have_posts() ) { 
  122. while( $query->have_posts() ) { 
  123. $query->the_post();  
  124. $result = array( 
  125. 'id' => get_the_ID(),  
  126. 'type' => $this->type,  
  127. 'title' => get_the_title(),  
  128. 'html' => buddyboss_global_search_buffer_template_part( 'loop/post', $template_type, false ),  
  129. ); 
  130.  
  131. $this->search_results['items'][get_the_ID()] = $result; 
  132. wp_reset_postdata(); 
  133.