Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Catch ajax requests
- add_action( 'wp_ajax_kleo_ajax_search', 'kleo_ajax_search' );
- add_action( 'wp_ajax_nopriv_kleo_ajax_search', 'kleo_ajax_search' );
- 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' => array('publish','inherit'),
- 'post_password' => '',
- 'suppress_filters' => false,
- 's' => $_REQUEST['s'],
- );
- if ( isset( $_REQUEST['context'] ) && '' != $_REQUEST['context'] ) {
- $context = explode( ',', $_REQUEST['context'] );
- $defaults['post_type'] = $context;
- }
- //Remove forum since it is handled with a different function
- if ( ! empty( $defaults['post_type'] ) && is_array( $defaults['post_type'] ) ) {
- foreach ( $defaults['post_type'] as $ptk => $ptv ) {
- if ( 'forum' == $ptv ) {
- unset( $defaults['post_type'][ $ptk ] );
- break;
- }
- }
- }
- if ( empty( $defaults['post_type'] ) ) {
- $posts = null;
- } else {
- $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_type_str = array();
- $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 ) {
- $post_type_str[$post->post_type] = $post->post_type;
- $count ++;
- if ( $count > 4 ) {
- continue;
- }
- $format = get_post_format( $post->ID );
- if( $post->post_type == 'attachment') {
- $img_url = wp_get_attachment_thumb_url( $post->ID );
- $image = '<img src="'.aq_resize( $img_url, 44, 44, true, true, true ).'" class="kleo-rounded"/>';
- } else {
- 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 = wp_strip_all_tags($excerpt); //added to remove gogole adsense code from search 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>';
- }
- if ( ! empty( $post_type_str ) ) {
- if ( count( $post_type_str ) > 1 ) {
- $search_str_posts = '&post_type[]=' . implode( ',', $post_type_str );
- } else {
- $search_str_posts = '&post_type=' . implode( ',', $post_type_str );
- }
- } else {
- $search_str_posts = '';
- }
- $output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . $search_str_posts . "'>" . __( '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();
- }
- }
Add Comment
Please, Sign In to add comment