Server IP : 162.213.251.208 / Your IP : 3.14.134.31 Web Server : LiteSpeed System : Linux business55.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : jmoroovq ( 1890) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/jmoroovq/expressmedicalbillingservices.com/wp-content/plugins/bdthemes-element-pack/modules/search/ |
Upload File : |
<?php namespace ElementPack\Modules\Search; use ElementPack\Base\Element_Pack_Module_Base; if ( !defined('ABSPATH') ) exit; // Exit if accessed directly class Module extends Element_Pack_Module_Base { public function __construct() { parent::__construct(); $this->add_actions(); } public function get_name() { return 'search'; } public function get_widgets() { $widgets = [ 'Search', ]; return $widgets; } /** * @param array $term_ids * @return array */ private function mapGroupControlQuery($term_ids = []) { $terms = get_terms( [ 'term_taxonomy_id' => $term_ids, 'hide_empty' => false, ] ); $tax_terms_map = []; foreach ( $terms as $term ) { $taxonomy = $term->taxonomy; $tax_terms_map[$taxonomy][] = $term->term_id; } return $tax_terms_map; } public function element_pack_ajax_search() { global $post; $result = array('results' => array()); $search_input = isset($_REQUEST['s']) ? $_REQUEST['s'] : ''; $settings = $_POST['settings']; if ( strlen($search_input) >= 3 ) { $query_args = [ 'post_type' => sanitize_text_field($settings['post_type']), 's' => sanitize_text_field($search_input), 'posts_per_page' => ($settings['per_page']) ? sanitize_text_field($settings['per_page']) : 5, 'post_status' => 'publish', ]; /** * Set Authors */ $include_users = []; $exclude_users = []; if ( !empty($settings['include_author_ids']) ) { if ( in_array('authors', $settings['include_by']) ) { $include_users = wp_parse_id_list($settings['include_author_ids']); } } if ( !empty($settings['exclude_author_ids']) ) { if ( in_array('authors', $settings['exclude_by']) ) { $exclude_users = wp_parse_id_list($settings['exclude_author_ids']); $include_users = array_diff($include_users, $exclude_users); } } if ( !empty($include_users) ) { $query_args['author__in'] = $include_users; } if ( !empty($exclude_users) ) { $query_args['author__not_in'] = $exclude_users;; } /** * Set Taxonomy */ $include_terms = []; $exclude_terms = []; $terms_query = []; if ( !empty($settings['include_term_ids']) ) { if ( in_array('terms', $settings['include_by']) ) { $include_terms = wp_parse_id_list($settings['include_term_ids']); } } if ( !empty($settings['exclude_term_ids']) ) { if ( in_array('terms', $settings['exclude_by']) ) { $exclude_terms = wp_parse_id_list($settings['exclude_term_ids']); $include_terms = array_diff($include_terms, $exclude_terms); } } if ( !empty($include_terms) ) { $tax_terms_map = $this->mapGroupControlQuery($include_terms); foreach ( $tax_terms_map as $tax => $terms ) { $terms_query[] = [ 'taxonomy' => $tax, 'field' => 'term_id', 'terms' => $terms, 'operator' => 'IN', ]; } } if ( !empty($exclude_terms) ) { $tax_terms_map = $this->mapGroupControlQuery($exclude_terms); foreach ( $tax_terms_map as $tax => $terms ) { $terms_query[] = [ 'taxonomy' => $tax, 'field' => 'term_id', 'terms' => $terms, 'operator' => 'NOT IN', ]; } } if ( !empty($terms_query) ) { $query_args['tax_query'] = $terms_query; $query_args['tax_query']['relation'] = 'AND'; } $query = new \WP_Query($query_args); while ( $query->have_posts() ) { $query->the_post(); $content = !empty($post->post_excerpt) ? strip_tags(strip_shortcodes($post->post_excerpt)) : strip_tags(strip_shortcodes($post->post_content)); if ( strlen($content) > 180 ) { $content = substr($content, 0, 179) . '...'; } $result['results'][] = array( 'title' => get_the_title(), 'text' => $content, 'url' => get_permalink($post->ID), ); } } die(json_encode($result)); } protected function add_actions() { // TODO AJAX SEARCH add_action('wp_ajax_element_pack_search', [$this, 'element_pack_ajax_search']); add_action('wp_ajax_nopriv_element_pack_search', [$this, 'element_pack_ajax_search']); } }