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

135 lines 3.6 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', 'betterdocs-pro' ];
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 $search_input = preg_replace( '/[^A-Za-z0-9_\- ][]]/', '', strtolower( $search_input ) );
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 $args = [
56 'term_id' => isset( $term->term_id ) ? $term->term_id : 0,
57 'post_type' => 'docs',
58 'post_status' => 'publish',
59 'posts_per_page' => -1,
60 'suppress_filters' => true,
61 's' => $search_input,
62 'orderby' => 'relevance',
63 'tax_query' => $tax_query
64 ];
65
66 if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
67 $args['suppress_filters'] = false;
68 $args['lang'] = ICL_LANGUAGE_CODE;
69 }
70
71 $search_results = $this->query->get_posts( $args );
72
73 $response = [];
74
75 ob_start();
76 betterdocs()->views->get(
77 'shortcode-parts/search-results',
78 [
79 'search_results' => $search_results,
80 'search_input' => $search_input
81 ]
82 );
83
84 $_output = ob_get_clean();
85
86 $_input_not_found = '';
87 if ( ! $search_results->have_posts() ) {
88 $_input_not_found = $search_input;
89 }
90
91 $response['post_lists'] = $_output;
92
93 if ( $_output && strlen( $search_input ) >= 3 ) {
94 betterdocs()->query->insert_search_keyword( $search_input, $_input_not_found );
95 }
96
97 wp_reset_postdata();
98
99 wp_send_json_success( $response );
100 }
101
102 public function get_name() {
103 return 'betterdocs_search_form';
104 }
105
106 /**
107 * Summary of default_attributes
108 * @return array
109 */
110 public function default_attributes() {
111 return apply_filters(
112 'betterdocs_search_form_attr',
113 [
114 'placeholder' => __( 'Search', 'betterdocs' ),
115 'heading' => '',
116 'subheading' => '',
117 'heading_tag' => 'h2',
118 'subheading_tag' => 'h3'
119 ]
120 );
121 }
122
123 public function render( $atts, $content = null ) {
124 betterdocs()->assets->localize(
125 'betterdocs-search',
126 'betterdocsSearchConfigTwo',
127 [
128 'is_post_type_archive' => is_post_type_archive( 'docs' ),
129 ]
130 );
131
132 $this->views( 'shortcodes/search' );
133 }
134 }
135