Advertisement
cipher87

avia_ajax_search

Mar 3rd, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.94 KB | None | 0 0
  1. if( ! function_exists( 'avia_ajax_search' ) )
  2. {
  3.     //now hook into wordpress ajax function to catch any ajax requests
  4.     add_action( 'wp_ajax_avia_ajax_search', 'avia_ajax_search' );
  5.     add_action( 'wp_ajax_nopriv_avia_ajax_search', 'avia_ajax_search' );
  6.  
  7.     function avia_ajax_search()
  8.     {
  9.        
  10.         unset( $_REQUEST['action'] );
  11.        
  12.         if( empty( $_REQUEST['s'] ) )
  13.         {
  14.             $_REQUEST['s'] = array_shift( array_values( $_REQUEST ) );
  15.         }
  16.        
  17.         if( empty( $_REQUEST['s'] ) )
  18.         {
  19.             die();
  20.         }
  21.  
  22.         $defaults = array(
  23.                         'numberposts'       => 5,
  24.                         'post_type'         => 'any',
  25.                         'post_status'       => 'publish',
  26.                         'post_password'     => '',
  27.                         'suppress_filters'  => false,
  28.                         'results_hide_fields' => ''
  29.                     );
  30.  
  31.         /**
  32.          * WP Filter for the contents of the search query variable
  33.          *
  34.          * @param string
  35.          * @return string
  36.          */
  37.         $_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s'] );
  38.  
  39.         $search_parameters  = array_merge( $defaults, $_REQUEST );
  40.  
  41.         if ( $search_parameters['results_hide_fields'] !== '' )
  42.         {
  43.             $search_parameters['results_hide_fields'] = explode( ',', $_REQUEST['results_hide_fields'] );
  44.         }
  45.         else
  46.         {
  47.             $search_parameters['results_hide_fields'] = array();
  48.         }
  49.  
  50.         /**
  51.          * @used_by             Avia_Custom_Pages       10
  52.          *
  53.          * @param array
  54.          * @return array
  55.          */
  56.         $search_query = apply_filters( 'avf_ajax_search_query', http_build_query( $search_parameters ) );
  57.        
  58.         /**
  59.          * @used_by         Avia_Relevanssi         10
  60.          *
  61.          * @param string $function_name
  62.          * @param array $search_query
  63.          * @param array $search_parameters
  64.          * @param array $defaults
  65.          * @return string
  66.          */
  67.         $query_function = apply_filters( 'avf_ajax_search_function', 'get_posts', $search_query, $search_parameters, $defaults );
  68.        
  69.         $posts = ( ( $query_function == 'get_posts') || ! function_exists( $query_function ) ) ? get_posts( $search_query ) : $query_function( $search_query, $search_parameters, $defaults );
  70.    
  71.         $search_messages = array(
  72.                 'no_criteria_matched' => __( 'Sorry, no posts matched your criteria', 'avia_framework' ),
  73.                 'another_search_term' => __( 'Please try another search term', 'avia_framework' ),
  74.                 'time_format'         => get_option( 'date_format' ),
  75.                 'all_results_query'   => http_build_query( $_REQUEST ),
  76.                 'all_results_link'    => home_url( '?' . http_build_query( $_REQUEST ) ),
  77.                 'view_all_results'    => __( 'View all results', 'avia_framework' )
  78.             );
  79.        
  80.         $search_messages = apply_filters( 'avf_ajax_search_messages', $search_messages, $search_query );
  81.        
  82.         if( empty( $posts ) )
  83.         {
  84.             $output  = "<span class='av_ajax_search_entry ajax_not_found'>";
  85.             $output .=      "<span class='av_ajax_search_image " . av_icon_string('info') . "'>";
  86.             $output .=      '</span>';
  87.             $output .=      "<span class='av_ajax_search_content'>";
  88.             $output .=          "<span class='av_ajax_search_title'>";
  89.             $output .=              $search_messages['no_criteria_matched'];
  90.             $output .=          '</span>';
  91.             $output .=          "<span class='ajax_search_excerpt'>";
  92.             $output .=              $search_messages['another_search_term'];
  93.             $output .=          '</span>';
  94.             $output .=      '</span>';
  95.             $output .= '</span>';
  96.            
  97.             echo $output;
  98.             die();
  99.         }
  100.  
  101.         //if we got posts resort them by post type
  102.         $output = '';
  103.         $sorted = array();
  104.         $post_type_obj = array();
  105.        
  106.         foreach( $posts as $post )
  107.         {
  108.             $sorted[ $post->post_type][] = $post;
  109.             if( empty( $post_type_obj[ $post->post_type ] ) )
  110.             {
  111.                 $post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type );
  112.             }
  113.         }
  114.  
  115.         //now we got everything we need to preapre the output
  116.         foreach( $sorted as $key => $post_type )
  117.         {
  118.             // check if post titles are in the hidden fields list
  119.             if ( ! in_array( 'post_titles', $search_parameters['results_hide_fields'] ) )
  120.             {
  121.                 if( isset( $post_type_obj[ $key ]->labels->name ) )
  122.                 {
  123.                     $label = apply_filters( 'avf_ajax_search_label_names', $post_type_obj[ $key ]->labels->name );
  124.                     $output .= "<h4>{$label}</h4>";
  125.                 }
  126.                 else
  127.                 {
  128.                     $output .= '<hr />';
  129.                 }
  130.             }
  131.  
  132.             foreach( $post_type as $post )
  133.             {
  134.                 $image = '';
  135.                 $extra_class = '';
  136.  
  137.                 // check if image is in the hidden fields list
  138.                 if ( ! in_array( 'image', $search_parameters['results_hide_fields'] ) )
  139.                 {
  140.                     $image = get_the_post_thumbnail( $post->ID, 'thumbnail' );
  141.                     $extra_class = $image ? 'with_image' : '';
  142.                     $post_type = $image ? '' : ( get_post_format( $post->ID ) != '' ? get_post_format( $post->ID ) : 'standard' );
  143.                     $iconfont = $image ? '' : av_icon_string( $post_type );
  144.  
  145.                 }
  146.  
  147.                 $excerpt     = '';
  148.  
  149.                 // check if post meta fields are in the hidden fields list
  150.                 if ( ! in_array( 'meta', $search_parameters['results_hide_fields'] ) )
  151.                 {
  152.                     if( ! empty($post->post_excerpt ) )
  153.                     {
  154.                         $excerpt =  apply_filters( 'avf_ajax_search_excerpt', avia_backend_truncate( $post->post_excerpt, 70, ' ', '...', true, '', true ) );
  155.                     }
  156.                     else
  157.                     {
  158.                         $excerpt = apply_filters( 'avf_ajax_search_no_excerpt', get_the_time( $search_messages['time_format'], $post->ID ), $post );
  159.                     }
  160.                 }
  161.  
  162.                 $link = apply_filters( 'av_custom_url', get_permalink( $post->ID ), $post );
  163.  
  164.                 $output .= "<a class ='av_ajax_search_entry {$extra_class}' href='{$link}'>";
  165.  
  166.                 if ( $image !== '' || $iconfont )
  167.                 {
  168.                     $output .= "<span class='av_ajax_search_image' {$iconfont}>";
  169.                     $output .=      $image;
  170.                     $output .= '</span>';
  171.                 }
  172.                 $output .= "<span class='av_ajax_search_content'>";
  173.                 $output .=      "<span class='av_ajax_search_title'>";
  174.                 $output .=          get_the_title($post->ID);
  175.                 $output .=      '</span>';
  176.                
  177.                 if( $excerpt !== '' )
  178.                 {
  179.                     $output .=      "<span class='ajax_search_excerpt'>";
  180.                     $output .=          $excerpt;
  181.                     $output .=      '</span>';
  182.                 }
  183.                 $output .=  '</span>';
  184.                 $output .= '</a>';
  185.             }
  186.         }
  187.  
  188.         $output .= "<a class='av_ajax_search_entry av_ajax_search_entry_view_all' href='{$search_messages['all_results_link']}'>{$search_messages['view_all_results']}</a>";
  189.  
  190.         echo $output;
  191.         die();
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement