✏️ 正在编辑: product-ajax-search.php
路径:
/home/h359620/public_html/wp-content/themes/datis/inc/includes/product-ajax-search.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php if (!defined('ABSPATH')) { exit; } if (!class_exists('Datis_Product_Ajax_Search_Handler')) { class Datis_Product_Ajax_Search_Handler { /** * @var Datis_Product_Ajax_Search_Handler|null */ private static $instance = null; private function __construct() { $this->init_hooks(); } private function init_hooks() { // AJAX handlers (logged-in + guest) add_action('wp_ajax_datis_product_ajax_search', [$this, 'handle_ajax']); add_action('wp_ajax_nopriv_datis_product_ajax_search', [$this, 'handle_ajax']); // Localize script add_action('wp_enqueue_scripts', [$this, 'localize_script'], 20); add_action('elementor/frontend/before_enqueue_scripts', [$this, 'localize_script'], 20); add_action('elementor/editor/before_enqueue_scripts', [$this, 'localize_script'], 20); } public function localize_script() { // اگر اسکریپت ثبت/انکیو نشده بود هیچ کاری نکن if (!wp_script_is('datis-product-ajax-search', 'registered') && !wp_script_is('datis-product-ajax-search', 'enqueued')) { return; } wp_localize_script( 'datis-product-ajax-search', 'DatisProductSearch', [ 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('datis_product_ajax_search'), 'i18n' => [ 'results_label' => __('Search results', 'datis'), 'start_typing' => __('Start typing to search…', 'datis'), 'no_results' => __('No results found.', 'datis'), 'ajax_error' => __('Search failed. Please try again.', 'datis'), ], ] ); } private function highlight_term($text, $term) { $term = trim(wp_strip_all_tags($term)); if ($term === '') { return esc_html($text); } $safe = esc_html($text); $pattern = '/' . preg_quote($term, '/') . '/iu'; return preg_replace($pattern, '<em>$0</em>', $safe); } public function handle_ajax() { if (!function_exists('WC')) { wp_send_json_error(['message' => __('WooCommerce is not active.', 'datis')]); } // Security if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'datis_product_ajax_search')) { wp_send_json_error(['message' => __('Security check failed.', 'datis')]); } $term = isset($_POST['term']) ? sanitize_text_field(wp_unslash($_POST['term'])) : ''; $tab = isset($_POST['tab']) ? sanitize_key(wp_unslash($_POST['tab'])) : 'all'; $limit = isset($_POST['limit']) ? (int) $_POST['limit'] : 8; $term = trim($term); $limit = max(1, min(50, $limit)); if ($term === '') { wp_send_json_success([ 'count' => 0, 'html' => '', 'all_url' => wc_get_page_permalink('shop'), ]); } $args = [ 'post_type' => 'product', 'post_status' => 'publish', 's' => $term, 'posts_per_page' => $limit, 'ignore_sticky_posts' => true, 'no_found_rows' => false, ]; // Tab filtering/sorting if ($tab === 'newest') { $args['orderby'] = 'date'; $args['order'] = 'DESC'; } elseif ($tab === 'best') { $args['meta_key'] = 'total_sales'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; } elseif ($tab === 'sale') { $sale_ids = wc_get_product_ids_on_sale(); if (empty($sale_ids)) { $sale_ids = [0]; } $args['post__in'] = $sale_ids; $args['orderby'] = 'date'; $args['order'] = 'DESC'; } $q = new WP_Query($args); $items_html = ''; if ($q->have_posts()) { while ($q->have_posts()) { $q->the_post(); $product_id = get_the_ID(); $product = wc_get_product($product_id); if (!$product) { continue; } $title = get_the_title($product_id); $url = get_permalink($product_id); $badge = ''; if ($product->is_featured()) { $badge = __('Suggested', 'datis'); } elseif ($product->is_on_sale()) { $badge = __('On sale', 'datis'); } $title_html = $this->highlight_term($title, $term); $items_html .= '<a href="' . esc_url($url) . '" class="result-item">'; if ($badge !== '') { $items_html .= '<span class="result-badge">' . esc_html($badge) . '</span>'; } $items_html .= '<span class="result-title">' . $title_html . '</span>'; $items_html .= '</a>'; } wp_reset_postdata(); } $count = (int) $q->found_posts; $base = wc_get_page_permalink('shop'); $all_url = add_query_arg( [ 's' => $term, 'post_type' => 'product', ], $base ); if ($tab === 'newest') { $all_url = add_query_arg(['orderby' => 'date'], $all_url); } elseif ($tab === 'best') { $all_url = add_query_arg(['orderby' => 'popularity'], $all_url); } elseif ($tab === 'sale') { $all_url = add_query_arg(['on_sale' => 1], $all_url); } wp_send_json_success([ 'count' => $count, 'html' => $items_html, 'all_url' => esc_url_raw($all_url), ]); } public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } } // Boot Datis_Product_Ajax_Search_Handler::get_instance(); }
💾 保存文件
← 返回文件管理器