<?php
/*
* Add your own functions here. You can also copy some of the theme functions into this file.
*/
if(!function_exists('avia_ajax_search'))
{
//now hook into wordpress ajax function to catch any ajax requests
add_action( 'wp_ajax_avia_ajax_search', 'avia_ajax_search' );
add_action( 'wp_ajax_nopriv_avia_ajax_search', 'avia_ajax_search' );
function avia_ajax_search()
{
global $avia_config;
unset($_REQUEST['action']);
if(empty($_REQUEST['s'])) $_REQUEST['s'] = array_shift(array_values($_REQUEST));
if(empty($_REQUEST['s'])) die();
$defaults = array('numberposts' => 5, 'post_type' => 'any', 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false, 'results_hide_fields' => '');
$_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s']);
$search_parameters = array_merge($defaults, $_REQUEST);
if ( $search_parameters['results_hide_fields'] !== '' ) {
$search_parameters['results_hide_fields'] = explode(',', $_REQUEST['results_hide_fields']);
}
else {
$search_parameters['results_hide_fields'] = array();
}
$search_query = apply_filters('avf_ajax_search_query', http_build_query($search_parameters));
$query_function = apply_filters('avf_ajax_search_function', 'get_posts', $search_query, $search_parameters, $defaults);
$posts = (($query_function == 'get_posts') || !function_exists($query_function)) ? get_posts($search_query) : $query_function($search_query, $search_parameters, $defaults);
$search_messages = array(
'no_criteria_matched' => __("Sorry, no posts matched your criteria", 'avia_framework'),
'another_search_term' => __("Please try another search term", 'avia_framework'),
'time_format' => get_option('date_format'),
'all_results_query' => http_build_query($_REQUEST),
'all_results_link' => home_url('?' . http_build_query($_REQUEST)),
'view_all_results' => __('View all results','avia_framework')
);
$search_messages = apply_filters('avf_ajax_search_messages', $search_messages, $search_query);
if(empty($posts))
{
$output = "<span class='av_ajax_search_entry ajax_not_found'>";
$output .= "<span class='av_ajax_search_image ".av_icon_string('info')."'>";
$output .= "</span>";
$output .= "<span class='av_ajax_search_content'>";
$output .= " <span class='av_ajax_search_title'>";
$output .= $search_messages['no_criteria_matched'];
$output .= " </span>";
$output .= " <span class='ajax_search_excerpt'>";
$output .= $search_messages['another_search_term'];
$output .= " </span>";
$output .= "</span>";
$output .= "</span>";
echo $output;
die();
}
//if we got posts resort them by post type
$output = "";
$sorted = array();
$post_type_obj = array();
foreach($posts as $post)
{
$sorted[$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);
}
}
//now we got everything we need to preapre the output
foreach($sorted as $key => $post_type)
{
// check if post titles are in the hidden fields list
if ( ! in_array('post_titles', $search_parameters['results_hide_fields'] ) )
{
if(isset($post_type_obj[$key]->labels->name))
{
$label = apply_filters('avf_ajax_search_label_names', $post_type_obj[$key]->labels->name);
$output .= "<h4>".$label."</h4>";
}
else
{
$output .= "<hr />";
}
}
foreach($post_type as $post) {
$image = "";
$extra_class = "";
// check if image is in the hidden fields list
if (!in_array('image', $search_parameters['results_hide_fields']))
{
$image = get_the_post_thumbnail($post->ID, 'thumbnail');
$extra_class = $image ? "with_image" : "";
$post_type = $image ? "" : get_post_format($post->ID) != "" ? get_post_format($post->ID) : "standard";
$iconfont = $image ? "" : av_icon_string($post_type);
}
$excerpt = "";
// check if post meta fields are in the hidden fields list
if ( ! in_array('meta', $search_parameters['results_hide_fields'] ) )
{
if(!empty($post->post_excerpt))
{
$excerpt = apply_filters( 'avf_ajax_search_excerpt', avia_backend_truncate($post->post_excerpt,70," ","...", true, '', true) );
}
else
{
$excerpt = apply_filters( 'avf_ajax_search_no_excerpt', get_the_time( $search_messages['time_format'], $post->ID ), $post );
}
}
$link = apply_filters('av_custom_url', get_permalink($post->ID), $post);
$output .= "<a class ='av_ajax_search_entry {$extra_class}' href='".$link."'>";
if ($image !== "" || $iconfont) {
$output .= "<span class='av_ajax_search_image' {$iconfont}>";
$output .= $image;
$output .= "</span>";
}
$output .= "<span class='av_ajax_search_content'>";
$output .= " <span class='av_ajax_search_title'>";
$output .= " <span class='wpcr3_rating_style1_average'>";
$output .= get_the_title($post->ID);
$output .= " </span>";
if ($excerpt !== '') {
$output .= " <span class='ajax_search_excerpt'>";
$output .= $excerpt;
$output .= " </span>";
}
$output .= "</span>";
$output .= "</a>";
}
}
$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>";
echo $output;
die();
}
}