Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/bdthemes-element-pack/modules/search/module.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace ElementPack\Modules\Search;
4 +
5 + use ElementPack\Base\Element_Pack_Module_Base;
6 +
7 + if ( ! defined( 'ABSPATH' ) )
8 + exit; // Exit if accessed directly
9 +
10 + class Module extends Element_Pack_Module_Base {
11 +
12 + public function __construct() {
13 + parent::__construct();
14 + $this->add_actions();
15 + }
16 +
17 +
18 + public function get_name() {
19 + return 'search';
20 + }
21 +
22 + public function get_widgets() {
23 +
24 + $widgets = [
25 + 'Search',
26 + ];
27 +
28 + return $widgets;
29 + }
30 +
31 + /**
32 + * @param array $term_ids
33 + * @return array
34 + */
35 + private function mapGroupControlQuery( $term_ids = [] ) {
36 + $terms = get_terms(
37 + [
38 + 'term_taxonomy_id' => $term_ids,
39 + 'hide_empty' => false,
40 + ]
41 + );
42 +
43 + $tax_terms_map = [];
44 +
45 + foreach ( $terms as $term ) {
46 + $taxonomy = $term->taxonomy;
47 + $tax_terms_map[ $taxonomy ][] = $term->term_id;
48 + }
49 +
50 + return $tax_terms_map;
51 + }
52 +
53 + public function element_pack_ajax_search() {
54 + global $post;
55 +
56 + $result = array( 'results' => array() );
57 + $search_input = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : '';
58 + $settings = isset( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : [];
59 +
60 + if ( strlen( $search_input ) >= 3 ) {
61 +
62 + $query_args = [
63 + 'post_type' => isset( $settings['post_type'] ) ? $settings['post_type'] : 'post',
64 + 's' => sanitize_text_field( $search_input ),
65 + 'posts_per_page' => ( $settings['per_page'] ) ? sanitize_text_field( $settings['per_page'] ) : 5,
66 + 'post_status' => 'publish',
67 + ];
68 +
69 + /**
70 + * Set Authors
71 + */
72 + $include_users = [];
73 + $exclude_users = [];
74 + if ( ! empty( $settings['include_author_ids'] ) ) {
75 + if ( in_array( 'authors', $settings['include_by'] ) ) {
76 + $include_users = wp_parse_id_list( $settings['include_author_ids'] );
77 + }
78 + }
79 + if ( ! empty( $settings['exclude_author_ids'] ) ) {
80 + if ( in_array( 'authors', $settings['exclude_by'] ) ) {
81 + $exclude_users = wp_parse_id_list( $settings['exclude_author_ids'] );
82 + $include_users = array_diff( $include_users, $exclude_users );
83 + }
84 + }
85 + if ( ! empty( $include_users ) ) {
86 + $query_args['author__in'] = $include_users;
87 + }
88 +
89 + if ( ! empty( $exclude_users ) ) {
90 + $query_args['author__not_in'] = $exclude_users;
91 + ;
92 + }
93 +
94 + /**
95 + * Set Taxonomy
96 + */
97 +
98 + $include_terms = [];
99 + $exclude_terms = [];
100 + $terms_query = [];
101 +
102 + if ( ! empty( $settings['include_term_ids'] ) ) {
103 + if ( in_array( 'terms', $settings['include_by'] ) ) {
104 + $include_terms = wp_parse_id_list( $settings['include_term_ids'] );
105 + }
106 + }
107 + if ( ! empty( $settings['exclude_term_ids'] ) ) {
108 + if ( in_array( 'terms', $settings['exclude_by'] ) ) {
109 + $exclude_terms = wp_parse_id_list( $settings['exclude_term_ids'] );
110 + $include_terms = array_diff( $include_terms, $exclude_terms );
111 + }
112 + }
113 +
114 + if ( ! empty( $include_terms ) ) {
115 + $tax_terms_map = $this->mapGroupControlQuery( $include_terms );
116 + foreach ( $tax_terms_map as $tax => $terms ) {
117 + $terms_query[] = [
118 + 'taxonomy' => $tax,
119 + 'field' => 'term_id',
120 + 'terms' => $terms,
121 + 'operator' => 'IN',
122 + ];
123 + }
124 + }
125 +
126 + if ( ! empty( $exclude_terms ) ) {
127 + $tax_terms_map = $this->mapGroupControlQuery( $exclude_terms );
128 + foreach ( $tax_terms_map as $tax => $terms ) {
129 + $terms_query[] = [
130 + 'taxonomy' => $tax,
131 + 'field' => 'term_id',
132 + 'terms' => $terms,
133 + 'operator' => 'NOT IN',
134 + ];
135 + }
136 + }
137 +
138 + if ( ! empty( $terms_query ) ) {
139 + $query_args['tax_query'] = $terms_query;
140 + $query_args['tax_query']['relation'] = 'AND';
141 + }
142 +
143 + $query_posts = get_posts( $query_args );
144 + if ( ! empty( $query_posts ) ) {
145 + foreach ( $query_posts as $post ) {
146 + $content = ! empty( $post->post_excerpt ) ? strip_tags( strip_shortcodes( $post->post_excerpt ) ) : strip_tags( strip_shortcodes( $post->post_content ) );
147 + if ( strlen( $content ) > 180 ) {
148 + $content = substr( $content, 0, 179 ) . '...';
149 + }
150 + $result['results'][] = array(
151 + 'title' => get_the_title(),
152 + 'text' => $content,
153 + 'url' => get_permalink( $post->ID ),
154 + );
155 + }
156 + }
157 + }
158 +
159 + die( json_encode( $result ) );
160 + }
161 +
162 +
163 + protected function add_actions() {
164 +
165 + // TODO AJAX SEARCH
166 + add_action( 'wp_ajax_element_pack_search', [ $this, 'element_pack_ajax_search' ] );
167 + add_action( 'wp_ajax_nopriv_element_pack_search', [ $this, 'element_pack_ajax_search' ] );
168 + }
169 + }
170 +