✏️ 正在编辑: ajax-search.php
路径:
/home/h359620/public_html/wp-content/themes/datis/inc/includes/ajax-search.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php if (! defined('ABSPATH')) { exit; } if (! class_exists('Datis_Ajax_Search_Handler')) { class Datis_Ajax_Search_Handler { /** * @var Datis_Ajax_Search_Handler */ private static $instance = null; /** * Private constructor – use get_instance(). */ private function __construct() { $this->init_hooks(); } /** * Register hooks. */ private function init_hooks() { // AJAX handlers (logged-in + guest). add_action('wp_ajax_datis_ajax_search', array($this, 'handle_ajax')); add_action('wp_ajax_nopriv_datis_ajax_search', array($this, 'handle_ajax')); // Localize script for frontend (Elementor + normal). add_action('wp_enqueue_scripts', array($this, 'localize_script'), 20); add_action('elementor/frontend/before_enqueue_scripts', array($this, 'localize_script'), 20); add_action('elementor/editor/before_enqueue_scripts', array($this, 'localize_script'), 20); } /** * Localize the JS file "datis-ajax-search" with settings and i18n strings. * Make sure the script handle "datis-ajax-search" is registered/enqueued somewhere * (for example inside your widget). */ public function localize_script() { // If script is not registered/enqueued, do nothing. if (! wp_script_is('datis-ajax-search', 'registered') && ! wp_script_is('datis-ajax-search', 'enqueued')) { return; } wp_localize_script( 'datis-ajax-search', 'DatisAjaxSearch', array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('datis_ajax_search'), 'i18n' => array( 'searching' => __('Searching…', 'datis'), 'error' => __('An error occurred.', 'datis'), 'server_error' => __('Server connection error.', 'datis'), 'start_typing' => __('Start typing to search.', 'datis'), 'no_results' => __('No results were found.', 'datis'), ), ) ); } /** * Handle the AJAX search request. */ public function handle_ajax() { // Security. if (! isset($_POST['nonce'])) { wp_send_json_error( array( 'message' => __('Security check failed.', 'datis'), ) ); } if (! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'datis_ajax_search')) { wp_send_json_error( array( 'message' => __('Security check failed.', 'datis'), ) ); } // داخل handle_ajax() $keyword = isset($_POST['keyword']) ? sanitize_text_field(wp_unslash($_POST['keyword'])) : ''; $per_page = isset($_POST['per_page']) ? (int) $_POST['per_page'] : 10; // NEW: get post types as array $post_types_raw = isset($_POST['post_types']) ? (array) $_POST['post_types'] : ['post']; $post_types = []; foreach ($post_types_raw as $pt) { $pt = sanitize_key($pt); if (! empty($pt)) { $post_types[] = $pt; } } if (empty($post_types)) { $post_types = ['post']; } if (empty($keyword)) { wp_send_json_success( array( 'html' => '<p class="search-empty-text">' . esc_html__('Start typing to search.', 'datis') . '</p>', ) ); } $args = array( 'post_type' => $post_types, 'post_status' => 'publish', 's' => $keyword, 'posts_per_page' => $per_page, ); $query = new \WP_Query($args); ob_start(); if ($query->have_posts()) { echo '<ul class="search-results-list-inner">'; while ($query->have_posts()) { $query->the_post(); $title = get_the_title(); $permalink = get_permalink(); echo '<li class="search-result-item">'; echo '<a class="search-result-link" href="' . esc_url($permalink) . '">'; echo '<div class="search-result-texts">'; echo '<div class="search-result-title">' . esc_html($title) . '</div>'; echo '</div>'; if (has_post_thumbnail()) { echo '<div class="search-result-thumb">'; echo get_the_post_thumbnail( get_the_ID(), 'thumbnail', array( 'class' => 'search-result-thumb-img', 'alt' => esc_attr($title), ) ); echo '</div>'; } echo '</a>'; echo '</li>'; } echo '</ul>'; wp_reset_postdata(); } else { echo '<p class="search-empty-text">' . esc_html__('No results were found.', 'datis') . '</p>'; } $html = ob_get_clean(); wp_send_json_success( array( 'html' => $html, ) ); } /** * Get singleton instance. * * @return Datis_Ajax_Search_Handler */ public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } } // Boot. Datis_Ajax_Search_Handler::get_instance(); }
💾 保存文件
← 返回文件管理器