[TikiWiki-commits] [Git][tikiwiki/tiki][master] [ENH][FIX] cookies: Add cookie category in Plugin CookieConsent and fix change prefs

"Jonny Bradley \(@jonnybradley\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <6a74732fc3a03_3919f35c6645a@gitlab-sidekiq-low-urgency-cpu-bound-v2-c84d8dfcb-9mvnc.mail>

Jonny Bradley pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
697e6c70 by Jonny Bradley at 2026-08-06T11:25:37+00:00
[ENH][FIX] cookies: Add cookie category in Plugin CookieConsent and fix change prefs
---
* [FIX] cookies: Show currently selected categories in the consent preferences form, and reload the page without the `cookie_consent` url param once saved (maybe it should reload the first time cookies are saved also?)

* [ENH] cookies: Add a parameter to specify the cookie category in Plugin CookieConsent. Also split the content if it contains `{ELSE]` to allow showing optional content if consent is not given

See merge request tikiwiki/tiki!10737

- - - - -


4 changed files:

- lib/CookieConsent/CookieConsentLib.php
- lib/wiki-plugins/wikiplugin_cookieconsent.php
- src/js/jquery-tiki/tiki-cookie-handler.js
- templates/cookie_consent.tpl


Changes:

=====================================
lib/CookieConsent/CookieConsentLib.php
=====================================
@@ -66,6 +66,7 @@ class CookieConsentLib
     {
 
         $requestedCategories = self::getCookieCategories();
+        $currentCategories = self::getConsentPreferences()['categories'];
 
         $disabledCategories = self::getDisabledCookieCategoryKeys();
         if ($disabledCategories) {
@@ -78,6 +79,9 @@ class CookieConsentLib
                 }
             }
         }
+        foreach ($requestedCategories as $key => & $value) {
+            $value['consent'] = $currentCategories[$key];
+        }
         return $requestedCategories;
     }
 


=====================================
lib/wiki-plugins/wikiplugin_cookieconsent.php
=====================================
@@ -13,7 +13,8 @@ function wikiplugin_cookieconsent_info()
         'documentation' => 'PluginCookieConsent',
         'description' => tra('Display content based on whether cookie consent has been granted by the user.'),
         'prefs' => ['wikiplugin_cookieconsent', 'cookie_consent_feature'],
-        'body' => tra('Wiki syntax containing the content that can be hidden or shown.'),
+        'body' => tr('Wiki syntax containing the content that can be hidden or shown. The body may contain %0{ELSE}%1.
+            Text after the marker will be displayed if consent has not been granted.', '<code>', '</code>'),
         'filter' => 'wikicontent',
         'introduced' => 10,
         'iconname' => 'information',
@@ -43,6 +44,20 @@ function wikiplugin_cookieconsent_info()
                 'default' => 'wp-cookie-consent-required',
                 'filter' => 'text',
             ],
+            'cookie_category_needed' => [
+                'required' => false,
+                'name' => tra('Cookie category needed'),
+                'description' => tra('Category of cookies needed to be consented to for this content to be displayed. Defaults to "Essential"'),
+                'options' => [
+                    ['text' => tra('Essential'), 'value' => CookieConsentLib::BUILTIN_COOKIE_CATEGORY_ESSENTIAL],
+                    ['text' => tra('Functional'), 'value' => CookieConsentLib::BUILTIN_COOKIE_CATEGORY_FUNCTIONAL],
+                    ['text' => tra('Analytics'), 'value' => CookieConsentLib::BUILTIN_COOKIE_CATEGORY_ANALYTICS],
+                    ['text' => tra('Marketing'), 'value' => CookieConsentLib::BUILTIN_COOKIE_CATEGORY_MARKETING],
+                ],
+                'since' => '30.0',
+                'default' => CookieConsentLib::BUILTIN_COOKIE_CATEGORY_ESSENTIAL,
+                'filter' => 'text',
+            ],
         ]
     ];
 }
@@ -57,9 +72,16 @@ function wikiplugin_cookieconsent($body, $params)
 
     $class = $params['element_class'];
 
-    if (! CookieConsentLib::isCategoryAllowed(CookieConsentLib::BUILTIN_COOKIE_CATEGORY_ESSENTIAL)) {
-        $body = '';
+    $parts = explode('{ELSE}', $body);
+    if (! CookieConsentLib::isCategoryAllowed($params['cookie_category_needed'])) {
+        if (count($parts) > 1) {
+            $body = $parts[1];
+        } else {
+            $body = '';
+        }
         $class .= ($class ? ' ' : '') . $params['no_consent_class'];
+    } else {
+        $body = $parts[0];
     }
 
     $tag1 = $tag2 = '';


=====================================
src/js/jquery-tiki/tiki-cookie-handler.js
=====================================
@@ -70,6 +70,10 @@ window.CookieHandler = (() => {
         jqueryTiki.cookie_consent_value = cookieConsentValue;
         setCookieBrowser(COOKIE_CONSENT_NAME, cookieConsentValue, "", exp);
         $(document).trigger("cookies.consent.agree");
+
+        if (document.location.href.match(/[?&]cookie_consent/)) {
+            document.location.replace(document.location.href.replace(/[?&]cookie_consent/g, ""));
+        }
     }
 
     function capitalize(str) {


=====================================
templates/cookie_consent.tpl
=====================================
@@ -25,11 +25,15 @@
 
                     <div id="customConsentSectionBanner" class="row mb-4">
                         <div class="col-12 d-flex flex-wrap gap-3 gap-md-4">
-                            {foreach from=$cookie_categories key=category item=data}
+                            {foreach $cookie_categories as $category => $data}
                                 <div class="form-check form-switch d-flex align-items-center flex-shrink-0">
                                     <input class="form-check-input me-2" type="checkbox" id="toggle{$category|capitalize}"
-                                           name="cookie_consent_{$category}"
-                                           {if $category == 'essential'}checked disabled aria-disabled="true"{/if}>
+                                        name="cookie_consent_{$category}"
+                                        {if $category == 'essential'}
+                                            checked disabled aria-disabled="true"
+                                        {elseif $data.consent}
+                                            checked
+                                        {/if}>
                                     <label class="form-check-label me-2 fw-semibold" for="toggle{$category|capitalize}">
                                         {tr}{$data.name}{/tr}
                                     </label>



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/697e6c7054cac09ece4789627c10d0ce5679c55e

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/697e6c7054cac09ece4789627c10d0ce5679c55e
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.