if ( ! function_exists( 'kleo_ajax_search' ) ) {
function kleo_ajax_search() {
//if "s" input is missing exit
if ( empty( $_REQUEST['s'] ) && empty( $_REQUEST['bbp_search'] ) ) {
die();
}
if ( ! empty( $_REQUEST['bbp_search'] ) ) {
$search_string = $_REQUEST['bbp_search'];
} else {
$search_string = $_REQUEST['s'];
}
$output = "";
$context = "any";
$defaults = array(
'numberposts' => 4,
'posts_per_page' => 20,
'post_type' => 'any',
'post_status' => 'publish',
'post_password' => '',
'suppress_filters' => false,
's' => $_REQUEST['s']
);
if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] != '' ) {
$context = explode( ",", $_REQUEST['context'] );
$defaults['post_type'] = $context;
}
if ( ! empty( $defaults['post_type'] ) && is_array( $defaults['post_type'] ) ) {
foreach ( $defaults['post_type'] as $ptk => $ptv ) {
if ( $ptv == 'forum' ) {
unset( $defaults['post_type'][ $ptk ] );
break;
}
}
}
$defaults = apply_filters( 'kleo_ajax_query_args', $defaults );
$the_query = new WP_Query( $defaults );
$posts = $the_query->get_posts();
$members = array();
$members['total'] = 0;
$groups = array();
$groups['total'] = 0;
$forums = false;
if ( function_exists( 'bp_is_active' ) && ( $context == "any" || in_array( "members", $context ) ) ) {
$members = bp_core_get_users( array(
'search_terms' => $search_string,
'per_page' => $defaults['numberposts'],
'populate_extras' => false
) );
}
if ( function_exists( 'bp_is_active' ) && bp_is_active( "groups" ) && ( $context == "any" || in_array( "groups", $context ) ) ) {
$groups = groups_get_groups( array(
'search_terms' => $search_string,
'per_page' => $defaults['numberposts'],
'populate_extras' => false
) );
}
if ( class_exists( 'bbPress' ) && ( $context == "any" || in_array( "forum", $context ) ) ) {
$forums = kleo_bbp_get_replies( $search_string );
}
//if there are no posts, groups nor members
if ( empty( $posts ) && $members['total'] == 0 && $groups['total'] == 0 && ! $forums ) {
$output = "
";
$output .= "
";
$output .= " ";
$output .= __( "Sorry, we haven't found anything based on your criteria.", 'kleo_framework' );
$output .= "
";
$output .= __( "Please try searching by different terms.", 'kleo_framework' );
$output .= "
";
$output .= "
";
echo $output;
die();
}
/* Forums topics search */
if ( ! empty( $forums ) ) {
$output .= '';
$output .= '
' . __( "Members", 'kleo_framework' ) . '
';
foreach ( (array) $members['users'] as $member ) {
$image = '
 ) . ')
';
if ( $update = bp_get_user_meta( $member->ID, 'bp_latest_update', true ) ) {
$latest_activity = char_trim( trim( strip_tags( bp_create_excerpt( $update['content'], 50, "..." ) ) ) );
} else {
$latest_activity = '';
}
$output .= "
";
$output .= "
$image
";
$output .= "
";
$output .= "
";
$output .= $member->display_name;
$output .= "";
$output .= "
";
$output .= $latest_activity;
$output .= "";
$output .= "
";
$output .= "
";
}
$output .= "
" . __( 'View member results', 'kleo_framework' ) . "";
$output .= "
";
}
//if there are groups
if ( $groups['total'] != 0 ) {
$output .= '';
if ( isset( $post_type_obj[ $ptype ]->labels->name ) ) {
$output .= "
" . $post_type_obj[ $ptype ]->labels->name . "
";
} else {
$output .= "
";
}
$count = 0;
foreach ( $post_type as $post ) {
$count ++;
if ( $count > 4 ) {
continue;
}
$format = get_post_format( $post->ID );
if ( $img_url = kleo_get_post_thumbnail_url( $post->ID ) ) {
$image = aq_resize( $img_url, 44, 44, true, true, true );
if ( ! $image ) {
$image = $img_url;
}
$image = '

';
} else {
if ( $format == 'video' ) {
$image = "
";
} elseif ( $format == 'image' || $format == 'gallery' ) {
$image = "
";
} else {
$image = "
";
}
}
$excerpt = "";
if ( ! empty( $post->post_content ) ) {
$excerpt = $post->post_content;
$excerpt = preg_replace( "/\[(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", '', $excerpt );
$excerpt = char_trim( trim( strip_tags( $excerpt ) ), 40, "..." );
}
$link = apply_filters( 'kleo_custom_url', get_permalink( $post->ID ) );
$classes = "format-" . $format;
$output .= "
";
$output .= "
$image
";
$output .= "
";
$output .= "
";
$output .= get_the_title( $post->ID );
$output .= "";
$output .= "
";
$output .= $excerpt;
$output .= "";
$output .= "
";
$output .= "
";
}
$output .= '
';
}
$output .= "