Initial import of the bdmurphy Bookshelf work: the bd-bookshelf theme, bd-bookshelf-plugin, bd-bookshelfhelper and bd-simpleemail plugins, plus the specification documents, mockups and release archives.
124 lines
3.3 KiB
PHP
124 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* GDPR Compliance class
|
|
*
|
|
* @package BD_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BDM_GDPR {
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct() {
|
|
add_action('wp_footer', [$this, 'render_consent_banner']);
|
|
add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
|
|
|
|
// AJAX handlers
|
|
add_action('wp_ajax_bd_bookshelf_accept_consent', [$this, 'ajax_accept_consent']);
|
|
add_action('wp_ajax_nopriv_bd_bookshelf_accept_consent', [$this, 'ajax_accept_consent']);
|
|
}
|
|
|
|
/**
|
|
* Enqueue GDPR scripts
|
|
*/
|
|
public function enqueue_scripts() {
|
|
if (is_admin()) {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_script(
|
|
'bd-bookshelf-gdpr',
|
|
BD_BOOKSHELF_URL . 'public/js/bd-bookshelf-gdpr.js',
|
|
[],
|
|
BD_BOOKSHELF_VERSION,
|
|
true
|
|
);
|
|
|
|
wp_localize_script('bd-bookshelf-gdpr', 'bdBookshelfGdpr', [
|
|
'ajaxurl' => admin_url('admin-ajax.php'),
|
|
'nonce' => wp_create_nonce('bd_bookshelf_gdpr_nonce'),
|
|
'cookieDays' => 30,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Render consent banner
|
|
*/
|
|
public function render_consent_banner() {
|
|
if (is_admin()) {
|
|
return;
|
|
}
|
|
|
|
if ($this->has_consent()) {
|
|
return;
|
|
}
|
|
|
|
$privacy_page_url = $this->get_privacy_page_url();
|
|
$privacy_text = get_option('bd_bookshelf_gdpr_text',
|
|
'We use cookies for affiliate tracking only. By clicking "Accept", you agree to our use of cookies as described in our <a href="' . esc_url($privacy_page_url) . '" target="_blank">Privacy Policy</a>.'
|
|
);
|
|
|
|
echo '<div id="bd-bookshelf-gdpr-banner" class="bd-bookshelf-gdpr-banner">';
|
|
echo '<div class="bd-bookshelf-gdpr-content">';
|
|
echo '<p>' . wp_kses_post($privacy_text) . '</p>';
|
|
echo '<button id="bd-bookshelf-gdpr-accept" class="bd-bookshelf-gdpr-accept">' . esc_html__('Accept', 'bd-bookshelf') . '</button>';
|
|
echo '</div>';
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Check if user has given consent
|
|
*/
|
|
public static function has_consent() {
|
|
if (isset($_COOKIE['bd_bookshelf_consent'])) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* AJAX handler for accepting consent
|
|
*/
|
|
public function ajax_accept_consent() {
|
|
check_ajax_referer('bd_bookshelf_gdpr_nonce', 'nonce');
|
|
|
|
// Set consent cookie (30 days)
|
|
setcookie('bd_bookshelf_consent', '1', time() + (30 * DAY_IN_SECONDS), '/');
|
|
|
|
wp_send_json_success(['message' => 'Consent accepted']);
|
|
}
|
|
|
|
/**
|
|
* Get privacy page URL
|
|
*/
|
|
private function get_privacy_page_url() {
|
|
$privacy_page_id = get_option('wp_page_for_privacy_policy');
|
|
|
|
if ($privacy_page_id) {
|
|
return get_permalink($privacy_page_id);
|
|
}
|
|
|
|
return '#';
|
|
}
|
|
|
|
/**
|
|
* Get GDPR consent banner text
|
|
*/
|
|
public static function get_consent_text() {
|
|
return get_option('bd_bookshelf_gdpr_text', '');
|
|
}
|
|
|
|
/**
|
|
* Update GDPR consent banner text
|
|
*/
|
|
public static function update_consent_text($text) {
|
|
update_option('bd_bookshelf_gdpr_text', sanitize_text_field($text));
|
|
}
|
|
}
|