Created
January 4, 2019 15:26
-
-
Save congthien/997af5be2afc93a85d8a92324877f784 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wpcoupon_coupon_ajax_search(){ | |
global $wpdb; | |
$data = array(); | |
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : ''; | |
$sql_tax = " = 'coupon_store' "; | |
$s = isset( $_REQUEST['ajax_sc'] ) ? (string) $_REQUEST['ajax_sc'] : ''; | |
$s = trim( $s ); | |
$_t = microtime( true ); | |
$lang = 'en'; | |
$join = ''; | |
$and = ''; | |
if ( class_exists( 'SitePress' ) ) { | |
$lang = ICL_LANGUAGE_CODE; | |
$join = "LEFT JOIN {$wpdb->prefix}icl_translations ON {$wpdb->prefix}icl_translations.`element_id` = {$wpdb->term_taxonomy}.term_id "; | |
$and = "AND {$wpdb->prefix}icl_translations.`language_code` = '$lang' | |
AND {$wpdb->prefix}icl_translations.`element_type` IN ('tax_coupon_store', 'tax_coupon_category' )"; | |
} | |
if ( strlen( $s ) > 0 ) { | |
$n = apply_filters('ajax_coupon_search_num_posts', 8); | |
$sql = "SELECT * FROM $wpdb->terms | |
JOIN $wpdb->term_taxonomy ON {$wpdb->terms}.`term_id` = {$wpdb->term_taxonomy}.term_id | |
$join | |
WHERE | |
{$wpdb->term_taxonomy}.`taxonomy` $sql_tax | |
AND {$wpdb->terms}.`name` LIKE %s | |
$and | |
GROUP BY {$wpdb->terms}.`term_id` | |
ORDER BY {$wpdb->terms}.`name` ASC | |
LIMIT 0, %d"; | |
$sql = $wpdb->prepare($sql, '%' . $wpdb->esc_like( $s ) . '%', $n); | |
foreach (( array )$wpdb->get_results($sql) as $k => $store) { | |
if( $store ->taxonomy == 'coupon_category' ) { | |
$image_id = get_term_meta( $store->term_id, '_wpc_cat_image_id', true ); | |
$thumb = false; | |
if ( $image_id > 0 ) { | |
$image= wp_get_attachment_image_src( $image_id, 'medium' ); | |
if ( $image ) { | |
$thumb = '<img src="'.esc_attr( $image[0] ).'" alt=" ">'; | |
} | |
} | |
if ( ! $thumb ) { | |
$icon = get_term_meta( $store->term_id, '_wpc_icon', true ); | |
if ( trim( $icon ) !== '' ){ | |
$thumb = '<i class="circular '.esc_attr( $icon ).'"></i>'; | |
} | |
} | |
$item = array( | |
"id" => $store->term_id, | |
"title" => $store->name, | |
"url" => get_term_link( $store ), | |
"image" => $thumb, | |
// "description" => sprintf(_n('%d Coupon', '%d Coupons', $n, 'wp-coupon'), $n) | |
); | |
} else { | |
wpcoupon_setup_store($store); | |
$item = array( | |
"id" => wpcoupon_store()->term_id, | |
"title" => wpcoupon_store()->name, | |
"url" => wpcoupon_store()->get_url(), | |
"home" => wpcoupon_store()->get_home_url( true ), | |
"image" => wpcoupon_store()->get_thumbnail(), | |
// "description" => sprintf(_n('%d Coupon', '%d Coupons', $n, 'wp-coupon'), $n) | |
); | |
} | |
//$n = wpcoupon_store()->count; | |
$data[] = $item; | |
} | |
} | |
$results = array( | |
'success' => true, | |
'results' => $data, | |
't' => microtime( true ) - $_t, | |
); | |
$results = apply_filters( 'wp_coupon_ajax_search_result', $results ); | |
wp_send_json( $results ); | |
die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment