Files
KGV-Cookie/kgv-cookie.php
2026-04-14 16:27:18 +02:00

215 lines
7.1 KiB
PHP

<?php
/**
* Plugin Name: KGV Cookie
* Plugin URI: https://apex-project.de/
* Description: Einfaches Cookie-Banner mit Einwilligungsverwaltung fuer KGV-Webseiten.
* Version: 1.0.5
* Author: Ronny Grobel
* Text Domain: kgv-cookie
* Author URI: https://apex-project.de/
* Plugin URI: https://apex-project.de/
* Update URI: https://git.apex-project.de/Wordpress_Plugins/KGV-Cookie
* Gitea Plugin URI: https://git.apex-project.de/Wordpress_Plugins/KGV-Cookie
* Requires Plugins: KGV-Updater
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class KGV_Cookie_Plugin {
const VERSION = '1.0.4';
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 ); ?>">Datenschutzerklärung</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();