PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.3.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.3.3
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 4.3.3, at includes/Shortcodes/SearchForm.php

141 lines 3.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 $handlers = [ 'betterdocs-search' ];
21 return $handlers;
22 }
23
24 public function get_script_depends() {
25 $handlers = [ 'betterdocs-search'];
26
27 if ( is_tax() ) {
28 $handlers[] = 'betterdocs-glossaries';
29 }
30 return $handlers;
31 }
32
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 // Removed preg_replace that was stripping non-Latin characters - sanitize_text_field() already handles security
39
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 }
52
53 $term = get_term_by( 'slug', $search_cat );
54
55 $post_status = ['publish'];
56
57 if( current_user_can( 'read_private_docs' ) ) {
58 array_push($post_status, 'private');
59 }
60
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 ];
71
72 if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
73 $args['suppress_filters'] = false;
74 $args['lang'] = ICL_LANGUAGE_CODE;
75 }
76
77 $search_results = $this->query->get_posts( $args );
78
79 $response = [];
80
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 );
89
90 $_output = ob_get_clean();
91
92 $_input_not_found = '';
93 if ( ! $search_results->have_posts() ) {
94 $_input_not_found = $search_input;
95 }
96
97 $response['post_lists'] = $_output;
98
99 if ( $_output && strlen( $search_input ) >= 3 ) {
100 betterdocs()->query->insert_search_keyword( $search_input, $_input_not_found );
101 }
102
103 wp_reset_postdata();
104
105 wp_send_json_success( $response );
106 }
107
108 public function get_name() {
109 return 'betterdocs_search_form';
110 }
111
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 }
128
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 );
137
138 $this->views( 'shortcodes/search' );
139 }
140 }
141