| @@ -8,204 +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 | - 'orderby' => 'relevance', | |
| 57 | - 'tax_query' => $tax_query | |
| 58 | - ]; | |
| 53 | + $term = get_term_by( 'slug', $search_cat ); | |
| 59 | 54 | |
| 60 | - if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) { | |
| 61 | - $args['suppress_filters'] = false; | |
| 62 | - $args['lang'] = ICL_LANGUAGE_CODE; | |
| 63 | - } | |
| 55 | + $post_status = ['publish']; | |
| 64 | 56 | |
| 65 | - $search_results = $this->query->get_posts( $args ); | |
| 57 | + if( current_user_can( 'read_private_docs' ) ) { | |
| 58 | + array_push($post_status, 'private'); | |
| 59 | + } | |
| 66 | 60 | |
| 67 | - $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 | + ]; | |
| 68 | 71 | |
| 69 | - ob_start(); | |
| 70 | - betterdocs()->views->get( 'shortcode-parts/search-results', [ | |
| 71 | - 'search_results' => $search_results, | |
| 72 | - 'search_input' => $search_input | |
| 73 | - ] ); | |
| 72 | + if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) { | |
| 73 | + $args['suppress_filters'] = false; | |
| 74 | + $args['lang'] = ICL_LANGUAGE_CODE; | |
| 75 | + } | |
| 74 | 76 | |
| 75 | - $_output = ob_get_clean(); | |
| 77 | + $search_results = $this->query->get_posts( $args ); | |
| 76 | 78 | |
| 77 | - $_input_not_found = ''; | |
| 78 | - if ( ! $search_results->have_posts() ) { | |
| 79 | - $_input_not_found = $search_input; | |
| 80 | - } | |
| 79 | + $response = []; | |
| 81 | 80 | |
| 82 | - $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 | + ); | |
| 83 | 89 | |
| 84 | - if ( $_output && strlen( $search_input ) >= 3 ) { | |
| 85 | - $this->insert( $search_input, $_input_not_found ); | |
| 86 | - } | |
| 90 | + $_output = ob_get_clean(); | |
| 87 | 91 | |
| 88 | - wp_reset_query(); | |
| 89 | - wp_reset_postdata(); | |
| 92 | + $_input_not_found = ''; | |
| 93 | + if ( ! $search_results->have_posts() ) { | |
| 94 | + $_input_not_found = $search_input; | |
| 95 | + } | |
| 90 | 96 | |
| 91 | - wp_send_json_success( $response ); | |
| 92 | - } | |
| 97 | + $response['post_lists'] = $_output; | |
| 93 | 98 | |
| 94 | - private function insert( $search_input, $input_not_found ) { | |
| 95 | - global $wpdb; | |
| 96 | - $search = $wpdb->get_results( | |
| 97 | - $wpdb->prepare( | |
| 98 | - "SELECT * | |
| 99 | - FROM {$wpdb->prefix}betterdocs_search_keyword | |
| 100 | - WHERE keyword = %s", | |
| 101 | - $search_input | |
| 102 | - ) | |
| 103 | - ); | |
| 99 | + if ( $_output && strlen( $search_input ) >= 3 ) { | |
| 100 | + betterdocs()->query->insert_search_keyword( $search_input, $_input_not_found ); | |
| 101 | + } | |
| 104 | 102 | |
| 105 | - if ( ! empty( $search ) ) { | |
| 106 | - $search_log = $wpdb->get_results( | |
| 107 | - $wpdb->prepare( | |
| 108 | - "SELECT * | |
| 109 | - FROM {$wpdb->prefix}betterdocs_search_log | |
| 110 | - WHERE created_at = %s AND keyword_id = %d", | |
| 111 | - date( 'Y-m-d' ), $search[0]->id | |
| 112 | - ) | |
| 113 | - ); | |
| 103 | + wp_reset_postdata(); | |
| 114 | 104 | |
| 115 | - if ( ! empty( $search_log ) ) { | |
| 116 | - if ( ! empty( $input_not_found ) ) { | |
| 117 | - $tbl_field = 'not_found_count'; | |
| 118 | - $count = $search_log[0]->not_found_count + 1; | |
| 119 | - } else { | |
| 120 | - $tbl_field = 'count'; | |
| 121 | - $count = $search_log[0]->count + 1; | |
| 122 | - } | |
| 123 | - $wpdb->query( | |
| 124 | - $wpdb->prepare( | |
| 125 | - "UPDATE {$wpdb->prefix}betterdocs_search_log | |
| 126 | - SET " . $tbl_field . " = " . $count . " | |
| 127 | - WHERE created_at = %s AND keyword_id = %d", | |
| 128 | - $search_log[0]->created_at, $search_log[0]->keyword_id | |
| 129 | - ) | |
| 130 | - ); | |
| 131 | - } else { | |
| 132 | - if ( ! empty( $input_not_found ) ) { | |
| 133 | - $count = 0; | |
| 134 | - $not_found_count = 1; | |
| 135 | - } else { | |
| 136 | - $count = 1; | |
| 137 | - $not_found_count = 0; | |
| 138 | - } | |
| 139 | - $wpdb->query( | |
| 140 | - $wpdb->prepare( | |
| 141 | - "INSERT INTO {$wpdb->prefix}betterdocs_search_log | |
| 142 | - ( keyword_id, count, not_found_count, created_at ) | |
| 143 | - VALUES ( %d, %d, %d, %s )", | |
| 144 | - [ | |
| 145 | - $search[0]->id, | |
| 146 | - $count, | |
| 147 | - $not_found_count, | |
| 148 | - date( 'Y-m-d' ) | |
| 149 | - ] | |
| 150 | - ) | |
| 151 | - ); | |
| 152 | - } | |
| 153 | - } else { | |
| 154 | - $insert = $wpdb->query( | |
| 155 | - $wpdb->prepare( | |
| 156 | - "INSERT INTO {$wpdb->prefix}betterdocs_search_keyword | |
| 157 | - ( keyword ) | |
| 158 | - VALUES ( %s )", | |
| 159 | - [ | |
| 160 | - $search_input | |
| 161 | - ] | |
| 162 | - ) | |
| 163 | - ); | |
| 105 | + wp_send_json_success( $response ); | |
| 106 | + } | |
| 164 | 107 | |
| 165 | - if ( $insert ) { | |
| 166 | - if ( ! empty( $input_not_found ) ) { | |
| 167 | - $count = 0; | |
| 168 | - $not_found_count = 1; | |
| 169 | - } else { | |
| 170 | - $count = 1; | |
| 171 | - $not_found_count = 0; | |
| 172 | - } | |
| 173 | - $wpdb->query( | |
| 174 | - $wpdb->prepare( | |
| 175 | - "INSERT INTO {$wpdb->prefix}betterdocs_search_log | |
| 176 | - ( keyword_id, count, not_found_count, created_at ) | |
| 177 | - VALUES ( %d, %d, %d, %s )", | |
| 178 | - [ | |
| 179 | - $wpdb->insert_id, | |
| 180 | - $count, | |
| 181 | - $not_found_count, | |
| 182 | - date( 'Y-m-d' ) | |
| 183 | - ] | |
| 184 | - ) | |
| 185 | - ); | |
| 186 | - } | |
| 187 | - } | |
| 188 | - } | |
| 108 | + public function get_name() { | |
| 109 | + return 'betterdocs_search_form'; | |
| 110 | + } | |
| 189 | 111 | |
| 190 | - public function get_name() { | |
| 191 | - return 'betterdocs_search_form'; | |
| 192 | - } | |
| 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 | + } | |
| 193 | 128 | |
| 194 | - /** | |
| 195 | - * Summary of default_attributes | |
| 196 | - * @return array | |
| 197 | - */ | |
| 198 | - public function default_attributes() { | |
| 199 | - return apply_filters( 'betterdocs_search_form_attr', [ | |
| 200 | - 'placeholder' => __( 'Search', 'betterdocs' ), | |
| 201 | - 'heading' => '', | |
| 202 | - 'subheading' => '', | |
| 203 | - 'heading_tag' => 'h2', | |
| 204 | - 'subheading_tag' => 'h3' | |
| 205 | - ] ); | |
| 206 | - } | |
| 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 | + ); | |
| 207 | 137 | |
| 208 | - public function render( $atts, $content = null ) { | |
| 209 | - $this->views( 'shortcodes/search' ); | |
| 210 | - } | |
| 138 | + $this->views( 'shortcodes/search' ); | |
| 139 | + } | |
| 211 | 140 | } |