Advertisement
rdusnr

Untitled

Nov 16th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.88 KB | None | 0 0
  1.  
  2. if(!function_exists('kleo_ajax_search'))
  3. {
  4.     function kleo_ajax_search()
  5.     {
  6.         //if "s" input is missing exit
  7.         if( empty( $_REQUEST['s'] ) && empty( $_REQUEST['bbp_search'] ) ) die();
  8.  
  9.         if( ! empty( $_REQUEST['bbp_search'] ) ) {
  10.             $search_string = $_REQUEST['bbp_search'];
  11.         } else {
  12.             $search_string = $_REQUEST['s'];
  13.         }
  14.  
  15.         $output = "";
  16.         $context = "any";
  17.         $defaults = array(
  18.             'numberposts' => 4,
  19.             'posts_per_page' => 20,
  20.             'post_type' => 'any',
  21.             'post_status' => 'publish',
  22.             'post_password' => '',
  23.             'suppress_filters' => false,
  24.             's' => $_REQUEST['s']
  25.         );
  26.  
  27.         if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] != '' ) {
  28.             $context = explode( ",", $_REQUEST['context'] );
  29.             $defaults['post_type'] = $context;
  30.         }
  31.         if ( ! empty( $defaults['post_type'] ) && is_array( $defaults['post_type'] ) ) {
  32.             foreach ( $defaults['post_type'] as $ptk => $ptv ) {
  33.                 if ( $ptv == 'forum' ) {
  34.                     unset( $defaults['post_type'][$ptk] );
  35.                     break;
  36.                 }
  37.             }
  38.         }
  39.  
  40.         $defaults =  apply_filters( 'kleo_ajax_query_args', $defaults);
  41.  
  42.         $the_query = new WP_Query( $defaults );
  43.         $posts = $the_query->get_posts();
  44.  
  45.         $members = array();
  46.         $members['total'] = 0;
  47.         $groups = array();
  48.         $groups['total'] = 0;
  49.         $forums = FALSE;
  50.  
  51.  
  52.         if ( function_exists( 'bp_is_active' ) && ( $context == "any" || in_array( "members", $context ) ) ) {
  53.             $members = bp_core_get_users(array('search_terms' => $search_string, 'per_page' => $defaults['numberposts'], 'populate_extras' => false));
  54.         }
  55.  
  56.         if ( function_exists( 'bp_is_active' ) && bp_is_active("groups") && ( $context == "any" || in_array( "groups", $context ) ) ) {
  57.             $groups = groups_get_groups(array('search_terms' => $search_string, 'per_page' => $defaults['numberposts'], 'populate_extras' => false));
  58.         }
  59.  
  60.         if ( class_exists( 'bbPress' ) && ( $context == "any" || in_array( "forum", $context ) ) ) {
  61.             $forums = kleo_bbp_get_replies( $search_string );
  62.         }
  63.  
  64.  
  65.         //if there are no posts, groups nor members
  66.         if( empty( $posts ) && $members['total'] == 0 && $groups['total'] == 0 && ! $forums  ) {
  67.             $output  = "<div class='kleo_ajax_entry ajax_not_found'>";
  68.             $output .= "<div class='ajax_search_content'>";
  69.             $output .= "<i class='icon icon-attention-circled'></i> ";
  70.             $output .= __("Sorry, we haven't found anything based on your criteria.", 'kleo_framework');
  71.             $output .= "<br>";
  72.             $output .= __("Please try searching by different terms.", 'kleo_framework');
  73.             $output .= "</div>";
  74.             $output .= "</div>";
  75.             echo $output;
  76.             die();
  77.         }
  78.  
  79.         //if there are members
  80.         if ( $members['total'] != 0 ) {
  81.  
  82.             $output .= '<div class="kleo-ajax-part kleo-ajax-type-members">';
  83.             $output .= '<h4><span>' . __("Members", 'kleo_framework') . '</span></h4>';
  84.             foreach ( (array) $members['users'] as $member ) {
  85.                 $image = '<img src="' . bp_core_fetch_avatar(array('item_id' => $member-> ID, 'width' => 25, 'height' => 25, 'html' => false)) . '" class="kleo-rounded" alt="">';
  86.                 if ( $update = bp_get_user_meta( $member-> ID, 'bp_latest_update', true ) ) {
  87.                     $latest_activity = char_trim( trim( strip_tags( bp_create_excerpt( $update['content'], 50,"..." ) ) ) );
  88.                 } else {
  89.                     $latest_activity = '';
  90.                 }
  91.                 $output .= "<div class ='kleo_ajax_entry'>";
  92.                 $output .= "<div class='ajax_search_image'>$image</div>";
  93.                 $output .= "<div class='ajax_search_content'>";
  94.                 $output .= "<a href='" . bp_core_get_user_domain( $member->ID ) . "' class='search_title'>";
  95.                 $output .= $member->display_name;
  96.                 $output .= "</a>";
  97.                 $output .= "<span class='search_excerpt'>";
  98.                 $output .= $latest_activity;
  99.                 $output .= "</span>";
  100.                 $output .= "</div>";
  101.                 $output .= "</div>";
  102.             }
  103.             $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_members_directory_permalink() . "?s=" . $search_string ) . "'>" . __('View member results','kleo_framework') . "</a>";
  104.             $output .= "</div>";
  105.         }
  106.  
  107.         //if there are groups
  108.         if ( $groups['total'] != 0 ) {
  109.  
  110.             $output .= '<div class="kleo-ajax-part kleo-ajax-type-groups">';
  111.             $output .= '<h4><span>' . __("Groups", 'kleo_framework') . '</span></h4>';
  112.             foreach ( (array) $groups['groups'] as $group ) {
  113.                 $image = '<img src="' . bp_core_fetch_avatar(array('item_id' => $group->id, 'object'=>'group', 'width' => 25, 'height' => 25, 'html' => false)) . '" class="kleo-rounded" alt="">';
  114.                 $output .= "<div class ='kleo_ajax_entry'>";
  115.                 $output .= "<div class='ajax_search_image'>$image</div>";
  116.                 $output .= "<div class='ajax_search_content'>";
  117.                 $output .= "<a href='" . bp_get_group_permalink( $group ) . "' class='search_title'>";
  118.                 $output .= $group->name;
  119.                 $output .= "</a>";
  120.                 $output .= "</div>";
  121.                 $output .= "</div>";
  122.             }
  123.             $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_groups_directory_permalink() . "?s=" . $search_string ) . "'>" . __('View group results','kleo_framework') . "</a>";
  124.             $output .= "</div>";
  125.         }
  126.  
  127.         //if there are posts
  128.         if( ! empty( $posts ) ) {
  129.             $post_types = array();
  130.             $post_type_obj = array();
  131.             foreach ( $posts as $post ) {
  132.                 $post_types[$post->post_type][] = $post;
  133.                 if (empty($post_type_obj[$post->post_type])) {
  134.                     $post_type_obj[$post->post_type] = get_post_type_object($post->post_type);
  135.                 }
  136.             }
  137.  
  138.             if(isset($post_types['gd_place'])) {
  139.                 $save_post_type = array( 'gd_place' => $post_types['gd_place']);
  140.                 unset ($post_types['gd_place']);
  141.                 $post_types = $save_post_type + $post_types;
  142.             }
  143.  
  144.             foreach ($post_types as $ptype => $post_type) {
  145.                 $output .= '<div class="kleo-ajax-part kleo-ajax-type-' . esc_attr( $post_type_obj[$ptype]->name ) . '">';
  146.                 if (isset($post_type_obj[$ptype]->labels->name)) {
  147.                     $output .= "<h4><span>" . $post_type_obj[$ptype]->labels->name . "</span></h4>";
  148.                 } else {
  149.                     $output .= "<hr>";
  150.                 }
  151.                 $count = 0;
  152.                 foreach ($post_type as $post) {
  153.  
  154.                     $count++;
  155.                     if ($count > 4) {
  156.                         continue;
  157.                     }
  158.                     $format = get_post_format( $post->ID );
  159.                     if ( $img_url = kleo_get_post_thumbnail_url( $post->ID ) ) {
  160.                         $image = aq_resize( $img_url, 44, 44, true, true, true );
  161.                         if( ! $image ) {
  162.                             $image = $img_url;
  163.                         }
  164.                         $image = '<img src="'. $image .'" class="kleo-rounded">';
  165.                     } else {
  166.                         if ($format == 'video') {
  167.                             $image = "<i class='icon icon-video'></i>";
  168.                         } elseif ($format == 'image' || $format == 'gallery') {
  169.                             $image = "<i class='icon icon-picture'></i>";
  170.                         } else {
  171.                             $image = "<i class='icon icon-link'></i>";
  172.                         }
  173.                     }
  174.  
  175.                     $excerpt = "";
  176.  
  177.                     if ( ! empty($post->post_content) ) {
  178.                         $excerpt = $post->post_content;
  179.                         $excerpt = preg_replace("/\[(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s",'', $excerpt);
  180.                         $excerpt = char_trim( trim(strip_tags($excerpt)), 40, "..." );
  181.                     }
  182.                     $link = apply_filters('kleo_custom_url', get_permalink($post->ID));
  183.                     $classes = "format-" . $format;
  184.                     $output .= "<div class ='kleo_ajax_entry $classes'>";
  185.                     $output .= "<div class='ajax_search_image'>$image</div>";
  186.                     $output .= "<div class='ajax_search_content'>";
  187.                     $output .= "<a href='$link' class='search_title'>";
  188.                     $output .= get_the_title($post->ID);
  189.                     $output .= "</a>";
  190.                     $output .= "<span class='search_excerpt'>";
  191.                     $output .= $excerpt;
  192.                     $output .= "</span>";
  193.                     $output .= "</div>";
  194.                     $output .= "</div>";
  195.                 }
  196.                 $output .= '</div>';
  197.             }
  198.  
  199.             $output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . "'>" . __('View all results', 'kleo_framework') . "</a>";
  200.         }
  201.  
  202.         /* Forums topics search */
  203.         if( ! empty( $forums ) ) {
  204.             $output .= '<div class="kleo-ajax-part kleo-ajax-type-forums">';
  205.             $output .= '<h4><span>' . __("Forums", 'kleo_framework') . '</span></h4>';
  206.  
  207.             $i = 0;
  208.             foreach ( $forums as $fk => $forum ) {
  209.  
  210.                 $i++;
  211.                 if ($i <= 4 ) {
  212.                     $image = "<i class='icon icon-chat-1'></i>";
  213.  
  214.                     $output .= "<div class ='kleo_ajax_entry'>";
  215.                     $output .= "<div class='ajax_search_image'>$image</div>";
  216.                     $output .= "<div class='ajax_search_content'>";
  217.                     $output .= "<a href='" . $forum['url'] . "' class='search_title'>";
  218.                     $output .= $forum['name'];
  219.                     $output .= "</a>";
  220.                     //$output .= "<span class='search_excerpt'>";
  221.                     //$output .= $latest_activity;
  222.                     //$output .= "</span>";
  223.                     $output .= "</div>";
  224.                     $output .= "</div>";
  225.                 }
  226.             }
  227.             $output .= "<a class='ajax_view_all' href='" . esc_url( bbp_get_search_url() . "?bbp_search=" . $search_string ) . "'>" . __('View forum results','kleo_framework') . "</a>";
  228.             $output .= "</div>";
  229.         }
  230.  
  231.  
  232.         echo $output;
  233.         die();
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement