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; } $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-exclamation-sign'></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='" . 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='" . 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); } } 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 = char_trim(trim(strip_tags(strip_shortcodes($post->post_content))), 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='" . 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='" . bbp_get_search_url() . "?bbp_search=" . $search_string . "'>" . __('View forum results', 'kleo_framework') . "</a>"; $output .= "</div>"; } echo $output; die; }
function kleo_ajax_search() { //if "s" input is missing exit if (empty($_REQUEST['s'])) { die; } $output = ""; $defaults = array('numberposts' => 4, 'post_type' => 'any', 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false); $defaults = apply_filters('kleo_ajax_query_args', $defaults); $query = array_merge($defaults, $_REQUEST); $query = http_build_query($query); $posts = get_posts($query); //if there are no posts if (empty($posts)) { $output = "<div class='kleo_ajax_entry ajax_not_found'>"; $output .= "<div class='ajax_search_content'>"; $output .= "<i class='icon icon-exclamation-sign'></i> "; $output .= __("Sorry, no pages matched 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 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); } } foreach ($post_types as $ptype => $post_type) { if (isset($post_type_obj[$ptype]->labels->name)) { $output .= "<h4>" . $post_type_obj[$ptype]->labels->name . "</h4>"; } else { $output .= "<hr>"; } foreach ($post_type as $post) { $format = get_post_format($post->ID); if (get_the_post_thumbnail($post->ID, 'thumbnail')) { $image = get_the_post_thumbnail($post->ID, 'thumbnail'); } 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 = "<br>" . char_trim(trim(strip_tags(strip_shortcodes($post->post_content))), 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 .= "<a class='ajax_view_all' href='" . home_url('?s=' . $_REQUEST['s']) . "'>" . __('View all results', 'kleo_framework') . "</a>"; echo $output; die; }