| 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 |
|
| 22 |
if(is_tax()){ |
| 23 |
$handlers[] = 'betterdocs-encyclopedia'; |
| 24 |
$handlers[] = 'betterdocs-single'; |
| 25 |
$handlers[] = 'betterdocs-glossaries'; |
| 26 |
} |
| 27 |
return $handlers; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_script_depends() { |
| 31 |
$handlers = ['betterdocs-search']; |
| 32 |
|
| 33 |
if(is_tax()){ |
| 34 |
$handlers[] = 'betterdocs-glossaries'; |
| 35 |
} |
| 36 |
return $handlers; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_search_results() { |
| 40 |
global $wpdb; |
| 41 |
$search_input = isset( $_POST['search_input'] ) ? sanitize_text_field( $_POST['search_input'] ) : ''; |
| 42 |
$search_cat = isset( $_POST['search_cat'] ) ? wp_strip_all_tags( $_POST['search_cat'] ) : ''; |
| 43 |
$lang = isset( $_POST['lang'] ) ? wp_strip_all_tags( $_POST['lang'] ) : ''; |
| 44 |
$search_input = preg_replace( '/[^A-Za-z0-9_\- ][]]/', '', strtolower( $search_input ) ); |
| 45 |
|
| 46 |
$tax_query = []; |
| 47 |
if ( $search_cat ) { |
| 48 |
$tax_query = [ |
| 49 |
[ |
| 50 |
'taxonomy' => 'doc_category', |
| 51 |
'field' => 'slug', |
| 52 |
'terms' => $search_cat, |
| 53 |
'operator' => 'AND', |
| 54 |
'include_children' => true |
| 55 |
] |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
$term = get_term_by( 'slug', $search_cat ); |
| 60 |
|
| 61 |
$args = [ |
| 62 |
'term_id' => isset( $term->term_id ) ? $term->term_id : 0, |
| 63 |
'post_type' => 'docs', |
| 64 |
'post_status' => 'publish', |
| 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( 'shortcode-parts/search-results', [ |
| 83 |
'search_results' => $search_results, |
| 84 |
'search_input' => $search_input |
| 85 |
] ); |
| 86 |
|
| 87 |
$_output = ob_get_clean(); |
| 88 |
|
| 89 |
$_input_not_found = ''; |
| 90 |
if ( ! $search_results->have_posts() ) { |
| 91 |
$_input_not_found = $search_input; |
| 92 |
} |
| 93 |
|
| 94 |
$response['post_lists'] = $_output; |
| 95 |
|
| 96 |
if ( $_output && strlen( $search_input ) >= 3 ) { |
| 97 |
$this->insert( $search_input, $_input_not_found ); |
| 98 |
} |
| 99 |
|
| 100 |
wp_reset_query(); |
| 101 |
wp_reset_postdata(); |
| 102 |
|
| 103 |
wp_send_json_success( $response ); |
| 104 |
} |
| 105 |
|
| 106 |
private function insert( $search_input, $input_not_found ) { |
| 107 |
global $wpdb; |
| 108 |
$search = $wpdb->get_results( |
| 109 |
$wpdb->prepare( |
| 110 |
"SELECT * |
| 111 |
FROM {$wpdb->prefix}betterdocs_search_keyword |
| 112 |
WHERE keyword = %s", |
| 113 |
$search_input |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
if ( ! empty( $search ) ) { |
| 118 |
$search_log = $wpdb->get_results( |
| 119 |
$wpdb->prepare( |
| 120 |
"SELECT * |
| 121 |
FROM {$wpdb->prefix}betterdocs_search_log |
| 122 |
WHERE created_at = %s AND keyword_id = %d", |
| 123 |
date( 'Y-m-d' ), $search[0]->id |
| 124 |
) |
| 125 |
); |
| 126 |
|
| 127 |
if ( ! empty( $search_log ) ) { |
| 128 |
if ( ! empty( $input_not_found ) ) { |
| 129 |
$tbl_field = 'not_found_count'; |
| 130 |
$count = $search_log[0]->not_found_count + 1; |
| 131 |
} else { |
| 132 |
$tbl_field = 'count'; |
| 133 |
$count = $search_log[0]->count + 1; |
| 134 |
} |
| 135 |
$wpdb->query( |
| 136 |
$wpdb->prepare( |
| 137 |
"UPDATE {$wpdb->prefix}betterdocs_search_log |
| 138 |
SET " . $tbl_field . " = " . $count . " |
| 139 |
WHERE created_at = %s AND keyword_id = %d", |
| 140 |
$search_log[0]->created_at, $search_log[0]->keyword_id |
| 141 |
) |
| 142 |
); |
| 143 |
} else { |
| 144 |
if ( ! empty( $input_not_found ) ) { |
| 145 |
$count = 0; |
| 146 |
$not_found_count = 1; |
| 147 |
} else { |
| 148 |
$count = 1; |
| 149 |
$not_found_count = 0; |
| 150 |
} |
| 151 |
$wpdb->query( |
| 152 |
$wpdb->prepare( |
| 153 |
"INSERT INTO {$wpdb->prefix}betterdocs_search_log |
| 154 |
( keyword_id, count, not_found_count, created_at ) |
| 155 |
VALUES ( %d, %d, %d, %s )", |
| 156 |
[ |
| 157 |
$search[0]->id, |
| 158 |
$count, |
| 159 |
$not_found_count, |
| 160 |
date( 'Y-m-d' ) |
| 161 |
] |
| 162 |
) |
| 163 |
); |
| 164 |
} |
| 165 |
} else { |
| 166 |
$insert = $wpdb->query( |
| 167 |
$wpdb->prepare( |
| 168 |
"INSERT INTO {$wpdb->prefix}betterdocs_search_keyword |
| 169 |
( keyword ) |
| 170 |
VALUES ( %s )", |
| 171 |
[ |
| 172 |
$search_input |
| 173 |
] |
| 174 |
) |
| 175 |
); |
| 176 |
|
| 177 |
if ( $insert ) { |
| 178 |
if ( ! empty( $input_not_found ) ) { |
| 179 |
$count = 0; |
| 180 |
$not_found_count = 1; |
| 181 |
} else { |
| 182 |
$count = 1; |
| 183 |
$not_found_count = 0; |
| 184 |
} |
| 185 |
$wpdb->query( |
| 186 |
$wpdb->prepare( |
| 187 |
"INSERT INTO {$wpdb->prefix}betterdocs_search_log |
| 188 |
( keyword_id, count, not_found_count, created_at ) |
| 189 |
VALUES ( %d, %d, %d, %s )", |
| 190 |
[ |
| 191 |
$wpdb->insert_id, |
| 192 |
$count, |
| 193 |
$not_found_count, |
| 194 |
date( 'Y-m-d' ) |
| 195 |
] |
| 196 |
) |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public function get_name() { |
| 203 |
return 'betterdocs_search_form'; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Summary of default_attributes |
| 208 |
* @return array |
| 209 |
*/ |
| 210 |
public function default_attributes() { |
| 211 |
return apply_filters( 'betterdocs_search_form_attr', [ |
| 212 |
'placeholder' => __( 'Search', 'betterdocs' ), |
| 213 |
'heading' => '', |
| 214 |
'subheading' => '', |
| 215 |
'heading_tag' => 'h2', |
| 216 |
'subheading_tag' => 'h3' |
| 217 |
] ); |
| 218 |
} |
| 219 |
|
| 220 |
public function render( $atts, $content = null ) { |
| 221 |
betterdocs()->assets->localize( 'betterdocs-search', 'betterdocsSearchConfigTwo', [ |
| 222 |
'is_post_type_archive' => is_post_type_archive( 'docs' ), |
| 223 |
] ); |
| 224 |
|
| 225 |
$this->views( 'shortcodes/search' ); |
| 226 |
} |
| 227 |
} |
| 228 |
|