Skip to content

Commit

Permalink
CookieConsent 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMaaarc committed Feb 14, 2024
1 parent 0edd90d commit 3f075c4
Show file tree
Hide file tree
Showing 25 changed files with 392 additions and 587 deletions.
335 changes: 115 additions & 220 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/index.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions assets/index.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions assets/index.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions assets/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import "vanilla-cookieconsent/src/cookieconsent.css"
import "vanilla-cookieconsent/src/cookieconsent.js"
import "vanilla-cookieconsent/dist/cookieconsent.css"
import * as CookieConsent from "vanilla-cookieconsent"

console.log(CookieConsent)
window.CookieConsent = CookieConsent
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Kirby cookieconsent plugin",
"license": "MIT",
"type": "kirby-plugin",
"version": "2.0.0",
"version": "3.0.0",
"authors": [
{
"name": "Marc Stampfli",
Expand Down
Binary file removed docs/cover.jpg
Binary file not shown.
Binary file added docs/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 69 additions & 39 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,75 @@
@include_once __DIR__ . '/vendor/autoload.php';

use Kirby\Http\Response;
use Zephir\Cookieconsent\Helpers;

$languages = require_once(__DIR__ . '/languages/lang.php');
use Zephir\Cookieconsent\Configuration;

Kirby::plugin('zephir/cookieconsent', [
'snippets' => [
'cookieconsentJs' => __DIR__ . '/snippets/cookieconsent_js.php',
'cookieconsentCss' => __DIR__ . '/snippets/cookieconsent_css.php'
],
'routes' => function ($kirby) {
return [
[
'pattern' => 'cookieconsent.js',
'language' => '*',
'action' => function () {
return new Response(
Helpers::getInitJs(),
'application/javascript',
200,
[
'Cache-Control' => 'public, max-age=3600, must-revalidate'
]
);
}
]
];
},
'options' => [
'type' => 'simple',
'activeBlocks' => [
'necessary' => true,
'functionality' => false,
'experience' => false,
'measurement' => true,
'marketing' => false
],
'extend' => [],
'defaultLocale' => 'de',
'cdn' => false
],
'translations' => $languages
'snippets' => [
'cookieconsentJs' => __DIR__ . '/snippets/cookieconsent_js.php',
'cookieconsentCss' => __DIR__ . '/snippets/cookieconsent_css.php'
],
'routes' => function ($kirby) {
return [
[
'pattern' => 'cookieconsent.js',
'language' => '*',
'action' => function () {
return new Response(
Configuration::getInitJs(),
'application/javascript',
200,
[
'Cache-Control' => 'public, max-age=3600, must-revalidate'
]
);
}
]
];
},
'options' => [
'cdn' => false,

// CookieConsent options
// Root options
'revision' => 1,
'root' => 'document.body',
'autoClearCookies' => true, // Only works when the categories has an autoClear array
'autoShow' => true,
'hideFromBots' => true,
'disablePageInteraction' => false,
'lazyHtmlGeneration' => true,

// Block options
'guiOptions' => [
'consentModal' => [
'layout' => 'box',
'position' => 'bottom right',
'flipButtons' => false,
'equalWeightButtons' => true
],
'preferencesModal' => [
'layout' => 'box',
// 'position' => 'left',
'flipButtons' => false,
'equalWeightButtons' => true
]
],
'categories' => [
'necessary' => [
'enabled' => true,
'readOnly' => true
],
'measurement' => [],
'functionality' => [],
'experience' => [],
'marketing' => []
],

// Language options
'translations' => [
'de' => require_once(__DIR__ . '/translations/de.php'),
'en' => require_once(__DIR__ . '/translations/en.php'),
'fr' => require_once(__DIR__ . '/translations/fr.php')
]
]
]);
31 changes: 0 additions & 31 deletions languages/de.php

This file was deleted.

31 changes: 0 additions & 31 deletions languages/en.php

This file was deleted.

31 changes: 0 additions & 31 deletions languages/fr.php

This file was deleted.

7 changes: 0 additions & 7 deletions languages/lang.php

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"parcel": "^2.1.1"
},
"dependencies": {
"vanilla-cookieconsent": "^2.9.2"
"vanilla-cookieconsent": "^3.0.0"
}
}
2 changes: 1 addition & 1 deletion snippets/cookieconsent_css.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php if (option('zephir.cookieconsent.cdn')): ?>
<?= css('https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@v2.9.2/dist/cookieconsent.css') ?>
<?= css('https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@v3.0.0/dist/cookieconsent.css') ?>
<?php else: ?>
<?= css('/media/plugins/zephir/cookieconsent/index.css') ?>
<?php endif; ?>
2 changes: 1 addition & 1 deletion snippets/cookieconsent_js.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php if (option('zephir.cookieconsent.cdn')): ?>
<?= js('https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@v2.9.2/dist/cookieconsent.js', ['defer' => true]) ?>
<?= js('https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@v3.0.0/dist/cookieconsent.umd.js', ['defer' => true]) ?>
<?php else: ?>
<?= js('/media/plugins/zephir/cookieconsent/index.js', ['defer' => true]) ?>
<?php endif; ?>
Expand Down
37 changes: 37 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Zephir\Cookieconsent;

class Configuration
{
public static function getInitJs()
{
return "
CookieConsent.run(" . json_encode(self::get()) . ");
";
}

public static function get()
{
return [
'categories' => kirby()->option('zephir.cookieconsent.categories'),
'guiOptions' => kirby()->option('zephir.cookieconsent.guiOptions'),
'revision' => kirby()->option('zephir.cookieconsent.revision'),
'root' => kirby()->option('zephir.cookieconsent.root'),
'autoClearCookies' => kirby()->option('zephir.cookieconsent.autoClearCookies'), // Only works when the categories has an autoClear array
'autoShow' => kirby()->option('zephir.cookieconsent.autoShow'),
'hideFromBots' => kirby()->option('zephir.cookieconsent.hideFromBots'),
'disablePageInteraction' => kirby()->option('zephir.cookieconsent.disablePageInteraction'),
'lazyHtmlGeneration' => kirby()->option('zephir.cookieconsent.lazyHtmlGeneration'),
'language' => [
'default' => 'kirby',
'autoDetect' => false,
'rtl' => kirby()->language()->direction() === 'rtl',
'translations' => [
'kirby' => Helpers::getTranslations()
]
]
];
}

}
36 changes: 7 additions & 29 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,16 @@

class Helpers
{

public static function getCookieconsentTranslation($key)
public static function getTranslations()
{
return t($key, null, option('languages') ? null : option('zephir.cookieconsent.defaultLocale'));
}
$translations = kirby()->option('zephir.cookieconsent.translations');
$locale = kirby()->language()->code();

public static function getInitJs()
{
$options = Options::get(kirby()->option('zephir.cookieconsent.type'));
return "
var cc = initCookieConsent();
cc.run(Object.assign(
" . json_encode($options) . ",
{
onAccept: function (cookie) {
const event = new CustomEvent('cookieConsentAccept', { detail: { cookie } });
window.dispatchEvent(event);
},
onChange: function (cookie, changed_categories) {
const event = new CustomEvent('cookieConsentChanged', { detail: { cookie, changed_categories } });
window.dispatchEvent(event);
},
onFirstAction: function (user_preferences, cookie) {
const event = new CustomEvent('cookieConsentFirstAction', { detail: { cookie, user_preferences } });
window.dispatchEvent(event);
}
}
));
if (empty($translations[$locale])) {
return $translations[array_key_first($translations)];
}

const event = new CustomEvent('cookieConsentInitialized', { detail: { cc } });
window.dispatchEvent(event);
";
return $translations[$locale];
}

}
Loading

0 comments on commit 3f075c4

Please sign in to comment.