Skip to content

Commit

Permalink
resolved issue with search tracking cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Locke committed Feb 9, 2024
1 parent ebd4413 commit 6d1be24
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
28 changes: 3 additions & 25 deletions supersearch-frontend.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php


function supersearch_enqueue_scripts() {
wp_enqueue_script('supersearch-autocomplete', plugin_dir_url(__FILE__) . 'supersearch.js', [], '1.3.0', true);
wp_enqueue_script('supersearch-autocomplete', plugin_dir_url(__FILE__) . 'supersearch.js', [], '1.3.1', true);
wp_enqueue_script('featherlight', plugin_dir_url(__FILE__) . 'featherlight.min.js', [], '1.0', true);
}
add_action('wp_enqueue_scripts', 'supersearch_enqueue_scripts');
Expand Down Expand Up @@ -31,28 +29,8 @@ function supersearch_input_shortcode() {
<input type="hidden" id="supersearch-key" value="<?php echo get_option('supersearch_public_key');?>">
<input type="hidden" id="mobile_top_offset" value="<?php echo get_option('supersearch_mobile_top_offset');?>">
<input type="hidden" id="desktop_top_offset" value="<?php echo get_option('supersearch_desktop_top_offset');?>">
<input type="hidden" id="supersearch-id" value="<?php echo $_COOKIE['_super_search_id'] ?? ''; ?>" >
<input type="hidden" id="supersearch-id" value="" >
<?php
return ob_get_clean(); // Return the buffered content
}
add_shortcode('supersearch', 'supersearch_input_shortcode');

function supersearch_wp_cookie() {
if (!isset($_COOKIE['_super_search_id']) && !headers_sent()) {
$trackingId = generateTrackingId();
$expiration_time = time() + (90 * 24 * 60 * 60);
setcookie('_super_search_id', $trackingId, $expiration_time, COOKIEPATH, COOKIE_DOMAIN);
}
}
add_action( 'init', 'supersearch_wp_cookie' );

function generateTrackingId() {
return sprintf(
'%04x-%04x-%04x-%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}

add_shortcode('supersearch', 'supersearch_input_shortcode');
23 changes: 23 additions & 0 deletions supersearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ document.addEventListener('DOMContentLoaded', function () {
$.featherlight.close();
}
});
document.getElementById('supersearch-id').value = getOrCreateCookie();
});

function search_query(searchInput) {
Expand Down Expand Up @@ -67,4 +68,26 @@ function repositionFeatherlight(instance) {
var mobile_top_offset = parseInt(document.getElementById('desktop_top_offset').value);
instance.css('top', searchInputPosition.top + mobile_top_offset + 'px');
}
}

function getOrCreateCookie() {
function generateGUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function getCookieValue() {
var matches = document.cookie.match('(^|;)\\s*_super_search_id\\s*=\\s*([^;]+)');
return matches ? decodeURIComponent(matches[2]) : null;
}
var cookieValue = getCookieValue();
if (!cookieValue) {
cookieValue = generateGUID();
var date = new Date();
date.setTime(date.getTime() + (90 * 24 * 60 * 60 * 1000)); // Set expiration date 90 days from now
var expires = "expires=" + date.toUTCString();
document.cookie = "_super_search_id=" + encodeURIComponent(cookieValue) + ";" + expires + ";path=/";
}
return cookieValue;
}
2 changes: 1 addition & 1 deletion supersearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Plugin Name: SuperSearch
* Description: SuperSearch provides a hyperfast search solution for your WordPress site.
* Version: 1.0.6
* Version: 1.0.7
* Author: https://www.hi-orbit.com
*/

Expand Down

0 comments on commit 6d1be24

Please sign in to comment.