Initial release of KGV Cookie plugin v1.0.0
This commit is contained in:
82
assets/css/kgv-cookie.css
Normal file
82
assets/css/kgv-cookie.css
Normal file
@@ -0,0 +1,82 @@
|
||||
.kgv-cookie-banner {
|
||||
position: fixed;
|
||||
left: 1rem;
|
||||
right: 1rem;
|
||||
z-index: 99999;
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
background: #13315c;
|
||||
color: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 1rem 1.25rem;
|
||||
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
|
||||
opacity: 0;
|
||||
transform: translateY(12px);
|
||||
transition: opacity 0.25s ease, transform 0.25s ease;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner--bottom {
|
||||
bottom: 1rem;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner--top {
|
||||
top: 1rem;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner.is-visible {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__text p {
|
||||
margin: 0.25rem 0;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__text a {
|
||||
color: #f7d488;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn,
|
||||
.kgv-cookie-reopen {
|
||||
border: 1px solid rgba(255, 255, 255, 0.5);
|
||||
background: transparent;
|
||||
color: #fff;
|
||||
border-radius: 8px;
|
||||
padding: 0.55rem 0.85rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn--primary {
|
||||
border-color: #f7d488;
|
||||
background: #f7d488;
|
||||
color: #13315c;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn:hover,
|
||||
.kgv-cookie-reopen:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
@media (max-width: 680px) {
|
||||
.kgv-cookie-banner {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.kgv-cookie-banner__actions {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.kgv-cookie-btn {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
102
assets/js/kgv-cookie.js
Normal file
102
assets/js/kgv-cookie.js
Normal file
@@ -0,0 +1,102 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var config = window.kgvCookieConfig || {};
|
||||
var cookieName = config.cookieName || 'kgv_cookie_consent';
|
||||
|
||||
function setCookie(name, value, days) {
|
||||
var expires = '';
|
||||
if (days) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||
expires = '; expires=' + date.toUTCString();
|
||||
}
|
||||
document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/; SameSite=Lax';
|
||||
}
|
||||
|
||||
function getCookie(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, c.length);
|
||||
}
|
||||
if (c.indexOf(nameEQ) === 0) {
|
||||
return decodeURIComponent(c.substring(nameEQ.length, c.length));
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function publishConsent(value) {
|
||||
window.KGVCookieConsent = value;
|
||||
window.dispatchEvent(new CustomEvent('kgvCookieConsentChanged', { detail: { value: value } }));
|
||||
}
|
||||
|
||||
function hasConsent(type) {
|
||||
var current = getCookie(cookieName);
|
||||
if (!current) {
|
||||
return false;
|
||||
}
|
||||
if (current === 'all') {
|
||||
return true;
|
||||
}
|
||||
return type === 'essential' && current === 'essential';
|
||||
}
|
||||
|
||||
function init() {
|
||||
var banner = document.getElementById('kgv-cookie-banner');
|
||||
if (!banner) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.KGVCookie = {
|
||||
getConsent: function () {
|
||||
return getCookie(cookieName);
|
||||
},
|
||||
hasConsent: hasConsent,
|
||||
reopen: function () {
|
||||
banner.hidden = false;
|
||||
banner.classList.add('is-visible');
|
||||
}
|
||||
};
|
||||
|
||||
var current = getCookie(cookieName);
|
||||
if (!current) {
|
||||
banner.hidden = false;
|
||||
banner.classList.add('is-visible');
|
||||
} else {
|
||||
publishConsent(current);
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
var trigger = event.target.closest('[data-kgv-cookie]');
|
||||
if (!trigger) {
|
||||
return;
|
||||
}
|
||||
|
||||
var action = trigger.getAttribute('data-kgv-cookie');
|
||||
if (action === 'accept-all') {
|
||||
setCookie(cookieName, 'all', 365);
|
||||
publishConsent('all');
|
||||
banner.classList.remove('is-visible');
|
||||
banner.hidden = true;
|
||||
}
|
||||
|
||||
if (action === 'essential-only') {
|
||||
setCookie(cookieName, 'essential', 365);
|
||||
publishConsent('essential');
|
||||
banner.classList.remove('is-visible');
|
||||
banner.hidden = true;
|
||||
}
|
||||
|
||||
if (action === 'reopen') {
|
||||
banner.hidden = false;
|
||||
banner.classList.add('is-visible');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
})();
|
||||
209
kgv-cookie.php
Normal file
209
kgv-cookie.php
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: KGV Cookie
|
||||
* Plugin URI: https://apex-project.de/
|
||||
* Description: Einfaches Cookie-Banner mit Einwilligungsverwaltung fuer KGV-Webseiten.
|
||||
* Version: 1.0.0
|
||||
* Author: Ronny Grobel
|
||||
* Text Domain: kgv-cookie
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
final class KGV_Cookie_Plugin {
|
||||
const VERSION = '1.0.0';
|
||||
const OPTION_KEY = 'kgv_cookie_settings';
|
||||
const COOKIE_NAME = 'kgv_cookie_consent';
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
public static function instance() {
|
||||
if ( null === self::$instance ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
|
||||
add_action( 'admin_init', array( $this, 'register_settings' ) );
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
|
||||
add_action( 'wp_footer', array( $this, 'render_banner' ) );
|
||||
add_shortcode( 'kgv_cookie_settings_link', array( $this, 'render_reopen_link' ) );
|
||||
}
|
||||
|
||||
public static function activate() {
|
||||
if ( false === get_option( self::OPTION_KEY ) ) {
|
||||
add_option( self::OPTION_KEY, self::default_settings() );
|
||||
}
|
||||
}
|
||||
|
||||
private static function default_settings() {
|
||||
return array(
|
||||
'banner_text' => 'Wir verwenden Cookies, um die Webseite sicher zu betreiben und Inhalte zu verbessern.',
|
||||
'privacy_url' => '',
|
||||
'accept_label' => 'Alle akzeptieren',
|
||||
'essential_label' => 'Nur notwendige',
|
||||
'position' => 'bottom',
|
||||
);
|
||||
}
|
||||
|
||||
private function get_settings() {
|
||||
$settings = get_option( self::OPTION_KEY, array() );
|
||||
if ( ! is_array( $settings ) ) {
|
||||
$settings = array();
|
||||
}
|
||||
|
||||
return wp_parse_args( $settings, self::default_settings() );
|
||||
}
|
||||
|
||||
public function add_settings_page() {
|
||||
add_options_page(
|
||||
'KGV Cookie',
|
||||
'KGV Cookie',
|
||||
'manage_options',
|
||||
'kgv-cookie',
|
||||
array( $this, 'render_settings_page' )
|
||||
);
|
||||
}
|
||||
|
||||
public function register_settings() {
|
||||
register_setting(
|
||||
'kgv_cookie_settings_group',
|
||||
self::OPTION_KEY,
|
||||
array( $this, 'sanitize_settings' )
|
||||
);
|
||||
}
|
||||
|
||||
public function sanitize_settings( $input ) {
|
||||
$defaults = self::default_settings();
|
||||
$output = array();
|
||||
|
||||
$output['banner_text'] = isset( $input['banner_text'] ) ? sanitize_textarea_field( $input['banner_text'] ) : $defaults['banner_text'];
|
||||
$output['privacy_url'] = isset( $input['privacy_url'] ) ? esc_url_raw( $input['privacy_url'] ) : '';
|
||||
$output['accept_label'] = isset( $input['accept_label'] ) ? sanitize_text_field( $input['accept_label'] ) : $defaults['accept_label'];
|
||||
$output['essential_label'] = isset( $input['essential_label'] ) ? sanitize_text_field( $input['essential_label'] ) : $defaults['essential_label'];
|
||||
$output['position'] = isset( $input['position'] ) && in_array( $input['position'], array( 'top', 'bottom' ), true ) ? $input['position'] : $defaults['position'];
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public function render_settings_page() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$settings = $this->get_settings();
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>KGV Cookie</h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php settings_fields( 'kgv_cookie_settings_group' ); ?>
|
||||
<table class="form-table" role="presentation">
|
||||
<tr>
|
||||
<th scope="row"><label for="kgv-cookie-banner-text">Banner-Text</label></th>
|
||||
<td>
|
||||
<textarea id="kgv-cookie-banner-text" name="<?php echo esc_attr( self::OPTION_KEY ); ?>[banner_text]" class="large-text" rows="3"><?php echo esc_textarea( $settings['banner_text'] ); ?></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="kgv-cookie-privacy-url">Datenschutz-URL</label></th>
|
||||
<td>
|
||||
<input id="kgv-cookie-privacy-url" type="url" name="<?php echo esc_attr( self::OPTION_KEY ); ?>[privacy_url]" value="<?php echo esc_attr( $settings['privacy_url'] ); ?>" class="regular-text" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="kgv-cookie-accept-label">Button: Alle akzeptieren</label></th>
|
||||
<td>
|
||||
<input id="kgv-cookie-accept-label" type="text" name="<?php echo esc_attr( self::OPTION_KEY ); ?>[accept_label]" value="<?php echo esc_attr( $settings['accept_label'] ); ?>" class="regular-text" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="kgv-cookie-essential-label">Button: Nur notwendige</label></th>
|
||||
<td>
|
||||
<input id="kgv-cookie-essential-label" type="text" name="<?php echo esc_attr( self::OPTION_KEY ); ?>[essential_label]" value="<?php echo esc_attr( $settings['essential_label'] ); ?>" class="regular-text" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">Position</th>
|
||||
<td>
|
||||
<label><input type="radio" name="<?php echo esc_attr( self::OPTION_KEY ); ?>[position]" value="bottom" <?php checked( $settings['position'], 'bottom' ); ?> /> Unten</label><br />
|
||||
<label><input type="radio" name="<?php echo esc_attr( self::OPTION_KEY ); ?>[position]" value="top" <?php checked( $settings['position'], 'top' ); ?> /> Oben</label>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<?php submit_button(); ?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function enqueue_assets() {
|
||||
$settings = $this->get_settings();
|
||||
|
||||
wp_enqueue_style(
|
||||
'kgv-cookie-style',
|
||||
plugins_url( 'assets/css/kgv-cookie.css', __FILE__ ),
|
||||
array(),
|
||||
self::VERSION
|
||||
);
|
||||
|
||||
wp_enqueue_script(
|
||||
'kgv-cookie-script',
|
||||
plugins_url( 'assets/js/kgv-cookie.js', __FILE__ ),
|
||||
array(),
|
||||
self::VERSION,
|
||||
true
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
'kgv-cookie-script',
|
||||
'kgvCookieConfig',
|
||||
array(
|
||||
'cookieName' => self::COOKIE_NAME,
|
||||
'position' => $settings['position'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function render_banner() {
|
||||
if ( is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$settings = $this->get_settings();
|
||||
$privacy_url = $settings['privacy_url'];
|
||||
?>
|
||||
<div id="kgv-cookie-banner" class="kgv-cookie-banner kgv-cookie-banner--<?php echo esc_attr( $settings['position'] ); ?>" hidden>
|
||||
<div class="kgv-cookie-banner__text">
|
||||
<p><?php echo esc_html( $settings['banner_text'] ); ?></p>
|
||||
<?php if ( '' !== $privacy_url ) : ?>
|
||||
<p><a href="<?php echo esc_url( $privacy_url ); ?>">Datenschutzerklaerung</a></p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="kgv-cookie-banner__actions">
|
||||
<button type="button" class="kgv-cookie-btn kgv-cookie-btn--primary" data-kgv-cookie="accept-all"><?php echo esc_html( $settings['accept_label'] ); ?></button>
|
||||
<button type="button" class="kgv-cookie-btn" data-kgv-cookie="essential-only"><?php echo esc_html( $settings['essential_label'] ); ?></button>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function render_reopen_link( $atts ) {
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'label' => 'Cookie-Einstellungen',
|
||||
),
|
||||
$atts,
|
||||
'kgv_cookie_settings_link'
|
||||
);
|
||||
|
||||
return '<button type="button" class="kgv-cookie-reopen" data-kgv-cookie="reopen">' . esc_html( $atts['label'] ) . '</button>';
|
||||
}
|
||||
}
|
||||
|
||||
register_activation_hook( __FILE__, array( 'KGV_Cookie_Plugin', 'activate' ) );
|
||||
KGV_Cookie_Plugin::instance();
|
||||
19
readme.txt
Normal file
19
readme.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
KGV Cookie - WordPress Plugin
|
||||
|
||||
Beschreibung:
|
||||
KGV Cookie zeigt ein Cookie-Banner auf der Website an und speichert die Einwilligung des Besuchers.
|
||||
|
||||
Features:
|
||||
- Banner mit zwei Auswahloptionen: Alle akzeptieren / Nur notwendige
|
||||
- Speicherung der Entscheidung im Cookie `kgv_cookie_consent`
|
||||
- Einstellungsseite unter Einstellungen > KGV Cookie
|
||||
- Shortcode zum erneuten Oeffnen: [kgv_cookie_settings_link]
|
||||
|
||||
Installation:
|
||||
1. Plugin in `wp-content/plugins/KGV-Cookie/` ablegen
|
||||
2. In WordPress aktivieren
|
||||
3. Banner-Text und Datenschutz-URL in den Plugin-Einstellungen setzen
|
||||
|
||||
Hinweis fuer Entwickler:
|
||||
- JavaScript API steht als `window.KGVCookie` bereit.
|
||||
- Event: `kgvCookieConsentChanged`
|
||||
Reference in New Issue
Block a user