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