From 707c5be0661fb3a6db2ce6f59c77572747d51759 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 14 Apr 2026 15:48:14 +0200 Subject: [PATCH] Initial release of KGV Cookie plugin v1.0.0 --- assets/css/kgv-cookie.css | 82 +++++++++++++++ assets/js/kgv-cookie.js | 102 +++++++++++++++++++ kgv-cookie.php | 209 ++++++++++++++++++++++++++++++++++++++ readme.txt | 19 ++++ 4 files changed, 412 insertions(+) create mode 100644 assets/css/kgv-cookie.css create mode 100644 assets/js/kgv-cookie.js create mode 100644 kgv-cookie.php create mode 100644 readme.txt diff --git a/assets/css/kgv-cookie.css b/assets/css/kgv-cookie.css new file mode 100644 index 0000000..1ae1b23 --- /dev/null +++ b/assets/css/kgv-cookie.css @@ -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; + } +} diff --git a/assets/js/kgv-cookie.js b/assets/js/kgv-cookie.js new file mode 100644 index 0000000..e170105 --- /dev/null +++ b/assets/js/kgv-cookie.js @@ -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); +})(); diff --git a/kgv-cookie.php b/kgv-cookie.php new file mode 100644 index 0000000..6677d99 --- /dev/null +++ b/kgv-cookie.php @@ -0,0 +1,209 @@ + '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(); + ?> +
+

KGV Cookie

+
+ + + + + + + + + + + + + + + + + + + + + + + + +
+
+ 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']; + ?> + + 'Cookie-Einstellungen', + ), + $atts, + 'kgv_cookie_settings_link' + ); + + return ''; + } +} + +register_activation_hook( __FILE__, array( 'KGV_Cookie_Plugin', 'activate' ) ); +KGV_Cookie_Plugin::instance(); diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..97f4892 --- /dev/null +++ b/readme.txt @@ -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`