From fa284b41db606e7aa89fe707a47883a49ee75174 Mon Sep 17 00:00:00 2001 From: Michael Engel Date: Thu, 16 Jan 2025 17:37:16 +0100 Subject: [PATCH] Improving some helper functions and Kirby3 support (#25) * optimizes getting the cookie via PHP * increase composer version * adjust readme --- CookieBanner.php | 15 +++++---------- README.md | 6 +++++- composer.json | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/CookieBanner.php b/CookieBanner.php index a22f094..273e60b 100644 --- a/CookieBanner.php +++ b/CookieBanner.php @@ -3,21 +3,19 @@ namespace Michnhokn; use Kirby\Cms\Plugin; -use Kirby\Cms\PluginAsset; -use Kirby\Cms\PluginAssets; use Kirby\Data\Json; -use Kirby\Exception\Exception; use Kirby\Exception\NotFoundException; use Kirby\Http\Cookie; use Kirby\Toolkit\A; class CookieBanner { - public const COOKIE_NAME = 'cookie_status'; + public const string COOKIE_NAME = 'cookie_status'; private static function getCookie(): ?string { - $cookie = Cookie::get(self::COOKIE_NAME); + kirby()->response()->usesCookie(self::COOKIE_NAME); + $cookie = $_COOKIE[self::COOKIE_NAME] ?? null; if (!is_string($cookie) or empty($cookie)) { return null; } @@ -26,10 +24,7 @@ private static function getCookie(): ?string public static function isFeatureAllowed(string $featureName): bool { - if (($cookie = self::getCookie()) === null) { - return false; - } - return str_contains($featureName, $cookie); + return in_array($featureName, self::allowedFeatures()); } public static function allowedFeatures(): array @@ -41,7 +36,7 @@ public static function allowedFeatures(): array return array_values( array_unique( array_filter( - explode(',', $cookie) + array_map('trim', explode(',', $cookie)) ) ) ); diff --git a/README.md b/README.md index d4b1394..eadf8f5 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ return [ ```` php false, // Displays the consent modal on initial load. Default: true 'showOnFirst' => false, @@ -164,6 +164,10 @@ return [ > This plugin is provided free of charge & published under the permissive MIT License. If you use it in a commercial > project, please consider to [buy me a beer 🍺](https://buymeacoff.ee/michnhokn) +## Compatability +Starting with version `1.1.0` of this plugin, it is only compatible with Kirby 4. +If you need to install this plugin for a Kirby 3 instance, please use version `1.0.9`. + ## License [MIT](./LICENSE) License © 2020-PRESENT [Michael Engel](https://github.com/michnhokn) diff --git a/composer.json b/composer.json index ab802c9..4758f44 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "michnhokn/kirby-cookie-banner", "description": "Add a cookie modal to your Kirby3 website", "type": "kirby-plugin", - "version": "1.1.0", + "version": "1.2.0", "license": "MIT", "authors": [ {