BP_Search::ajax_search()
Description
Source
File: bp-search/classes/class-bp-search.php
public function ajax_search() {
check_ajax_referer( 'bp_search_ajax', 'nonce' );
if ( isset( $_POST["view"] ) && $_POST["view"] == "content" ) {
$_GET["s"] = $_POST["s"];
if ( ! empty( $_POST["subset"] ) ) {
$_GET["subset"] = $_POST["subset"];
}
if ( ! empty( $_POST["list"] ) ) {
$_GET["list"] = $_POST["list"];
}
$content = "";
BP_Search::instance()->prepare_search_page();
$content = bp_search_buffer_template_part( 'results-page-content', '', false );
echo $content;
die();
}
$args = array(
'search_term' => $_REQUEST['search_term'],
//How many results should be displyed in autosuggest?
//@todo: give a settings field for this value
'ajax_per_page' => $_REQUEST['per_page'],
'count_total' => true,
'template_type' => 'ajax',
);
if ( isset( $_REQUEST['forum_search_term'] ) ) {
$args['forum_search'] = true;
}
$this->do_search( $args );
$search_results = array();
if ( isset( $this->search_results['all']['items'] ) && ! empty( $this->search_results['all']['items'] ) ) {
/* ++++++++++++++++++++++++++++++++++
group items of same type together
++++++++++++++++++++++++++++++++++ */
$types = array();
foreach ( $this->search_results['all']['items'] as $item_id => $item ) {
$type = $item['type'];
if ( empty( $types ) || ! in_array( $type, $types ) ) {
$types[] = $type;
}
}
$new_items = array();
foreach ( $types as $type ) {
$first_html_changed = false;
foreach ( $this->search_results['all']['items'] as $item_id => $item ) {
if ( $item['type'] != $type ) {
continue;
}
//add group/type title in first one
/*
if( !$first_html_changed ){
//this filter can be used to change display of 'posts' to 'Blog Posts' etc..
$label = apply_filters( 'bp_search_label_search_type', $type );
//$item['html'] = "<div class='results-group results-group-{$type}'><span class='results-group-title'>{$label}</span></div>" . $item['html'];
$first_html_changed = true;
}
*/
$new_items[ $item_id ] = $item;
}
}
$this->search_results['all']['items'] = $new_items;
/* _______________________________ */
$url = $this->search_page_search_url( false );
if ( true === $this->search_args['forum_search'] ) {
$url = $url;
} else {
$url = esc_url( add_query_arg( array( 'view' => 'content', 'no_frame' => '1', 'bp_search' => 1 ), $url ) );
}
$type_mem = "";
foreach ( $this->search_results['all']['items'] as $item_id => $item ) {
$new_row = array( 'value' => $item['html'] );
$type_label = apply_filters( 'bp_search_label_search_type', $item['type'] );
$new_row['type'] = $item['type'];
$new_row['type_label'] = "";
$new_row['value'] = $item['html'];
if ( isset( $item['title'] ) ) {
$new_row['label'] = $item['title'];
}
// if ( $type_mem != $new_row['type'] ) {
// $type_mem = $new_row['type'];
// $cat_row = $new_row;
// $cat_row["type"] = $item['type'];
// $cat_row['type_label'] = $type_label;
// $category_search_url = esc_url( add_query_arg( array( 'subset' => $item['type'] ), $url ) );
// $html = "<span><a href='" . esc_url( $category_search_url ) . "'>" . $type_label . "</a></span>";
// $cat_row["value"] = apply_filters( 'buddypress_gs_autocomplete_category', $html, $item['type'], $url, $type_label );
// $search_results[] = $cat_row;
// }
$search_results[] = $new_row;
}
// Show "View All" link
if ( absint( $this->search_results['all']['total_match_count'] ) > absint( bp_search_get_form_option( 'bp_search_number_of_results', 5 ) ) ) {
$all_results_row = array(
"value" => "<div class='bp-search-ajax-item allresults'><a href='" . esc_url( $url ) . "'>" . __( 'View all', 'buddyboss' ) . "</a></div>",
"type" => 'view_all_type',
"type_label" => ''
);
$search_results[] = $all_results_row;
}
} else {
//@todo give a settings screen for this field
$search_results[] = array(
'value' => '<div class="bp-search-ajax-item noresult">' . sprintf( __( "Nothing found for '%s'", "buddyboss" ), stripslashes( $this->search_args['search_term'] ) ) . '</div>',
'label' => $this->search_args['search_term']
);
}
die( json_encode( $search_results ) );
}
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.