Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = "<div class='kleo_ajax_entry ajax_not_found'>";
- $output .= "<div class='ajax_search_content'>";
- $output .= "<i class='icon icon-attention-circled'></i> ";
- $output .= __("Sorry, we haven't found anything based on your criteria.", 'kleo_framework');
- $output .= "<br>";
- $output .= __("Please try searching by different terms.", 'kleo_framework');
- $output .= "</div>";
- $output .= "</div>";
- echo $output;
- die();
- }
- //if there are members
- if ( $members['total'] != 0 ) {
- $output .= '<div class="kleo-ajax-part kleo-ajax-type-members">';
- $output .= '<h4><span>' . __("Members", 'kleo_framework') . '</span></h4>';
- foreach ( (array) $members['users'] as $member ) {
- $image = '<img src="' . bp_core_fetch_avatar(array('item_id' => $member-> ID, 'width' => 25, 'height' => 25, 'html' => false)) . '" class="kleo-rounded" alt="">';
- 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 .= "<div class ='kleo_ajax_entry'>";
- $output .= "<div class='ajax_search_image'>$image</div>";
- $output .= "<div class='ajax_search_content'>";
- $output .= "<a href='" . bp_core_get_user_domain( $member->ID ) . "' class='search_title'>";
- $output .= $member->display_name;
- $output .= "</a>";
- $output .= "<span class='search_excerpt'>";
- $output .= $latest_activity;
- $output .= "</span>";
- $output .= "</div>";
- $output .= "</div>";
- }
- $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_members_directory_permalink() . "?s=" . $search_string ) . "'>" . __('View member results','kleo_framework') . "</a>";
- $output .= "</div>";
- }
- //if there are groups
- if ( $groups['total'] != 0 ) {
- $output .= '<div class="kleo-ajax-part kleo-ajax-type-groups">';
- $output .= '<h4><span>' . __("Groups", 'kleo_framework') . '</span></h4>';
- foreach ( (array) $groups['groups'] as $group ) {
- $image = '<img src="' . bp_core_fetch_avatar(array('item_id' => $group->id, 'object'=>'group', 'width' => 25, 'height' => 25, 'html' => false)) . '" class="kleo-rounded" alt="">';
- $output .= "<div class ='kleo_ajax_entry'>";
- $output .= "<div class='ajax_search_image'>$image</div>";
- $output .= "<div class='ajax_search_content'>";
- $output .= "<a href='" . bp_get_group_permalink( $group ) . "' class='search_title'>";
- $output .= $group->name;
- $output .= "</a>";
- $output .= "</div>";
- $output .= "</div>";
- }
- $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_groups_directory_permalink() . "?s=" . $search_string ) . "'>" . __('View group results','kleo_framework') . "</a>";
- $output .= "</div>";
- }
- //if there are posts
- if( ! empty( $posts ) ) {
- $post_types = array();
- $post_type_obj = array();
- foreach ( $posts as $post ) {
- $post_types[$post->post_type][] = $post;
- if (empty($post_type_obj[$post->post_type])) {
- $post_type_obj[$post->post_type] = get_post_type_object($post->post_type);
- }
- }
- if(isset($post_types['gd_place'])) {
- $save_post_type = array( 'gd_place' => $post_types['gd_place']);
- unset ($post_types['gd_place']);
- $post_types = $save_post_type + $post_types;
- }
- foreach ($post_types as $ptype => $post_type) {
- $output .= '<div class="kleo-ajax-part kleo-ajax-type-' . esc_attr( $post_type_obj[$ptype]->name ) . '">';
- if (isset($post_type_obj[$ptype]->labels->name)) {
- $output .= "<h4><span>" . $post_type_obj[$ptype]->labels->name . "</span></h4>";
- } else {
- $output .= "<hr>";
- }
- $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 = '<img src="'. $image .'" class="kleo-rounded">';
- } else {
- if ($format == 'video') {
- $image = "<i class='icon icon-video'></i>";
- } elseif ($format == 'image' || $format == 'gallery') {
- $image = "<i class='icon icon-picture'></i>";
- } else {
- $image = "<i class='icon icon-link'></i>";
- }
- }
- $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 .= "<div class ='kleo_ajax_entry $classes'>";
- $output .= "<div class='ajax_search_image'>$image</div>";
- $output .= "<div class='ajax_search_content'>";
- $output .= "<a href='$link' class='search_title'>";
- $output .= get_the_title($post->ID);
- $output .= "</a>";
- $output .= "<span class='search_excerpt'>";
- $output .= $excerpt;
- $output .= "</span>";
- $output .= "</div>";
- $output .= "</div>";
- }
- $output .= '</div>';
- }
- $output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . "'>" . __('View all results', 'kleo_framework') . "</a>";
- }
- /* Forums topics search */
- if( ! empty( $forums ) ) {
- $output .= '<div class="kleo-ajax-part kleo-ajax-type-forums">';
- $output .= '<h4><span>' . __("Forums", 'kleo_framework') . '</span></h4>';
- $i = 0;
- foreach ( $forums as $fk => $forum ) {
- $i++;
- if ($i <= 4 ) {
- $image = "<i class='icon icon-chat-1'></i>";
- $output .= "<div class ='kleo_ajax_entry'>";
- $output .= "<div class='ajax_search_image'>$image</div>";
- $output .= "<div class='ajax_search_content'>";
- $output .= "<a href='" . $forum['url'] . "' class='search_title'>";
- $output .= $forum['name'];
- $output .= "</a>";
- //$output .= "<span class='search_excerpt'>";
- //$output .= $latest_activity;
- //$output .= "</span>";
- $output .= "</div>";
- $output .= "</div>";
- }
- }
- $output .= "<a class='ajax_view_all' href='" . esc_url( bbp_get_search_url() . "?bbp_search=" . $search_string ) . "'>" . __('View forum results','kleo_framework') . "</a>";
- $output .= "</div>";
- }
- echo $output;
- die();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement