Skip to content

Commit

Permalink
Tracker doesn't use jQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaydsouza committed Jan 17, 2024
1 parent ac526cd commit 0d3dea2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
21 changes: 18 additions & 3 deletions includes/class-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ public static function enqueue_scripts() {
*/
$ajax_bsearch_tracker = apply_filters( 'bsearch_tracker_script_args', $ajax_bsearch_tracker );

wp_enqueue_script( 'bsearch_tracker', plugins_url( 'includes/js/better-search-tracker.min.js', BETTER_SEARCH_PLUGIN_FILE ), array( 'jquery' ), BETTER_SEARCH_VERSION, true );
wp_enqueue_script(
'bsearch_tracker',
plugins_url( 'includes/js/better-search-tracker.min.js', BETTER_SEARCH_PLUGIN_FILE ),
array(),
BETTER_SEARCH_VERSION,
true
);

wp_localize_script( 'bsearch_tracker', 'ajax_bsearch_tracker', $ajax_bsearch_tracker );

Expand Down Expand Up @@ -151,8 +157,17 @@ public static function parse_request( $wp ) {

$str = self::update_count( $search_query );

header( 'content-type: application/x-javascript' );
wp_send_json( $str );
// If the debug parameter is set then we output $str else we send a No Content header.
if ( array_key_exists( 'bsearch_debug', $wp->query_vars ) && 1 === absint( $wp->query_vars['bsearch_debug'] ) ) {
header( 'content-type: application/x-javascript' );
wp_send_json( $str );
} else {
header( 'HTTP/1.0 204 No Content' );
header( 'Cache-Control: max-age=15, s-maxage=0' );
}

// Stop anything else from loading as it is not needed.
exit;

} else {
return;
Expand Down
29 changes: 21 additions & 8 deletions includes/js/better-search-tracker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
jQuery( document ).ready(
function(r) {
var a = {
action: "bsearch_tracker",
document.addEventListener('DOMContentLoaded', function() {
fetch(ajax_bsearch_tracker.ajax_url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
},
body: new URLSearchParams({
action: 'bsearch_tracker',
bsearch_search_query: ajax_bsearch_tracker.bsearch_search_query
};
jQuery.post( ajax_bsearch_tracker.ajax_url, a )
}
);
}).toString()
})
.then(function(response) {
return response.json();
})
.then(function(data) {
// handle the response data
})
.catch(function(error) {
console.error('Error:', error);
});
});
2 changes: 1 addition & 1 deletion includes/js/better-search-tracker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d3dea2

Please sign in to comment.