Files
Bookshelf/bd-bookshelf-plugin/public/js/bd-bookshelf-gdpr.js
2026-10-18 19:01:01 -07:00

71 lines
2.0 KiB
JavaScript

// Copyright 2026 Umbra Tools (umbratools.com)
// SPDX-License-Identifier: Apache-2.0
/**
* BD Bookshelf - GDPR Consent
*
* @package BD_Bookshelf
*/
(function($) {
'use strict';
$(document).ready(function() {
// Check if consent already given
if (bdBookshelfHasConsent()) {
return;
}
// Show banner
$('#bd-bookshelf-gdpr-banner').show();
// Handle accept button
$('#bd-bookshelf-gdpr-accept').on('click', function() {
$.ajax({
url: bdBookshelfGdpr.ajaxurl,
type: 'POST',
data: {
action: 'bd_bookshelf_accept_consent',
nonce: bdBookshelfGdpr.nonce
},
success: function(response) {
if (response.success) {
// Set cookie
bdBookshelfSetCookie('bd_bookshelf_consent', '1', bdBookshelfGdpr.cookieDays);
// Hide banner
$('#bd-bookshelf-gdpr-banner').fadeOut(300);
}
}
});
});
});
function bdBookshelfHasConsent() {
return bdBookshelfGetCookie('bd_bookshelf_consent') === '1';
}
function bdBookshelfSetCookie(name, value, days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
function bdBookshelfGetCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
})(jQuery);