Skip to content

Commit

Permalink
Improving some helper functions and Kirby3 support (#25)
Browse files Browse the repository at this point in the history
* optimizes getting the cookie via PHP
* increase composer version
* adjust readme
  • Loading branch information
michnhokn authored Jan 16, 2025
1 parent 2ebc4a5 commit fa284b4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
15 changes: 5 additions & 10 deletions CookieBanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -41,7 +36,7 @@ public static function allowedFeatures(): array
return array_values(
array_unique(
array_filter(
explode(',', $cookie)
array_map('trim', explode(',', $cookie))
)
)
);
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ return [

```` php
<?php snippet('cookie-modal', [
// Loads the custom assets of the plugin. Default: true
// Loads the custom assets of the plugin. Default: false
'assets' => false,
// Displays the consent modal on initial load. Default: true
'showOnFirst' => false,
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down

0 comments on commit fa284b4

Please sign in to comment.