PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.0.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.0.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Shortcodes / SearchForm.php

SearchForm.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.0.0, at includes/Shortcodes/SearchForm.php

203 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Shortcodes;
4
5 use WPDeveloper\BetterDocs\Core\Query;
6 use WPDeveloper\BetterDocs\Utils\Helper;
7 use WPDeveloper\BetterDocs\Core\Settings;
8 use WPDeveloper\BetterDocs\Core\Shortcode;
9 use WPDeveloper\BetterDocs\Admin\Customizer\Defaults;
10
11 class SearchForm extends Shortcode {
12 public function __construct( Settings $settings, Query $query, Helper $helper, Defaults $defaults ) {
13 parent::__construct( $settings, $query, $helper, $defaults );
14
15 add_action( 'wp_ajax_nopriv_betterdocs_get_search_result', [$this, 'get_search_results'] );
16 add_action( 'wp_ajax_betterdocs_get_search_result', [$this, 'get_search_results'] );
17 }
18
19 public function get_style_depends() {
20 return ['betterdocs-search'];
21 }
22
23 public function get_script_depends() {
24 return ['betterdocs-search'];
25 }
26
27 public function get_search_results() {
28 global $wpdb;
29 $search_input = isset( $_POST['search_input'] ) ? sanitize_text_field( $_POST['search_input'] ) : '';
30 $search_cat = isset( $_POST['search_cat'] ) ? wp_strip_all_tags( $_POST['search_cat'] ) : '';
31 $search_input = preg_replace( '/[^A-Za-z0-9_\- ][]]/', '', strtolower( $search_input ) );
32
33 $tax_query = [];
34 if ( $search_cat ) {
35 $tax_query = [
36 [
37 'taxonomy' => 'doc_category',
38 'field' => 'slug',
39 'terms' => $search_cat,
40 'operator' => 'AND',
41 'include_children' => true
42 ]
43 ];
44 }
45
46 $term = get_term_by( 'slug', $search_cat );
47
48 $search_results = $this->query->get_posts( [
49 'term_id' => isset( $term->term_id ) ? $term->term_id : 0,
50 'post_type' => 'docs',
51 'post_status' => 'publish',
52 'posts_per_page' => -1,
53 'suppress_filters' => true,
54 's' => $search_input,
55 'tax_query' => $tax_query,
56 ] );
57
58 $response = [];
59
60 ob_start();
61 betterdocs()->views->get( 'shortcode-parts/search-results', [
62 'search_results' => $search_results,
63 'search_input' => $search_input
64 ] );
65
66 $_output = ob_get_clean();
67
68 $_input_not_found = '';
69 if ( ! $search_results->have_posts() ) {
70 $_input_not_found = $search_input;
71 }
72
73 $response['post_lists'] = $_output;
74
75 if ( $_output && strlen( $search_input ) >= 3 ) {
76 $this->insert( $search_input, $_input_not_found );
77 }
78
79 wp_reset_query();
80 wp_reset_postdata();
81
82 wp_send_json_success( $response );
83 }
84
85 private function insert( $search_input, $input_not_found ) {
86 global $wpdb;
87 $search = $wpdb->get_results(
88 $wpdb->prepare(
89 "SELECT *
90 FROM {$wpdb->prefix}betterdocs_search_keyword
91 WHERE keyword = %s",
92 $search_input
93 )
94 );
95
96 if ( ! empty( $search ) ) {
97 $search_log = $wpdb->get_results(
98 $wpdb->prepare(
99 "SELECT *
100 FROM {$wpdb->prefix}betterdocs_search_log
101 WHERE created_at = %s AND keyword_id = %d",
102 date( 'Y-m-d' ), $search[0]->id
103 )
104 );
105
106 if ( ! empty( $search_log ) ) {
107 if ( ! empty( $input_not_found ) ) {
108 $tbl_field = 'not_found_count';
109 $count = $search_log[0]->not_found_count + 1;
110 } else {
111 $tbl_field = 'count';
112 $count = $search_log[0]->count + 1;
113 }
114 $wpdb->query(
115 $wpdb->prepare(
116 "UPDATE {$wpdb->prefix}betterdocs_search_log
117 SET " . $tbl_field . " = " . $count . "
118 WHERE created_at = %s AND keyword_id = %d",
119 $search_log[0]->created_at, $search_log[0]->keyword_id
120 )
121 );
122 } else {
123 if ( ! empty( $input_not_found ) ) {
124 $count = 0;
125 $not_found_count = 1;
126 } else {
127 $count = 1;
128 $not_found_count = 0;
129 }
130 $wpdb->query(
131 $wpdb->prepare(
132 "INSERT INTO {$wpdb->prefix}betterdocs_search_log
133 ( keyword_id, count, not_found_count, created_at )
134 VALUES ( %d, %d, %d, %s )",
135 [
136 $search[0]->id,
137 $count,
138 $not_found_count,
139 date( 'Y-m-d' )
140 ]
141 )
142 );
143 }
144 } else {
145 $insert = $wpdb->query(
146 $wpdb->prepare(
147 "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword
148 ( keyword )
149 VALUES ( %s )",
150 [
151 $search_input
152 ]
153 )
154 );
155
156 if ( $insert ) {
157 if ( ! empty( $input_not_found ) ) {
158 $count = 0;
159 $not_found_count = 1;
160 } else {
161 $count = 1;
162 $not_found_count = 0;
163 }
164 $wpdb->query(
165 $wpdb->prepare(
166 "INSERT INTO {$wpdb->prefix}betterdocs_search_log
167 ( keyword_id, count, not_found_count, created_at )
168 VALUES ( %d, %d, %d, %s )",
169 [
170 $wpdb->insert_id,
171 $count,
172 $not_found_count,
173 date( 'Y-m-d' )
174 ]
175 )
176 );
177 }
178 }
179 }
180
181 public function get_name() {
182 return 'betterdocs_search_form';
183 }
184
185 /**
186 * Summary of default_attributes
187 * @return array
188 */
189 public function default_attributes() {
190 return apply_filters( 'betterdocs_search_form_attr', [
191 'placeholder' => __( 'Search', 'betterdocs' ),
192 'heading' => '',
193 'subheading' => '',
194 'heading_tag' => 'h2',
195 'subheading_tag' => 'h3'
196 ] );
197 }
198
199 public function render( $atts, $content = null ) {
200 $this->views( 'shortcodes/search' );
201 }
202 }
203