[TikiWiki-commits] [Git][tikiwiki/tiki][30.x] 2 commits: [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 <6a747eeb23bb7_3819f3d46728@gitlab-sidekiq-low-urgency-cpu-bound-v2-c84d8dfcb-ddn79.mail>

Jonny Bradley pushed to branch 30.x at Tiki Wiki CMS Groupware / Tiki


Commits:
439a1cee by Jonny Bradley at 2026-08-06T12:59:26+01: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

(cherry picked from commit 697e6c7054cac09ece4789627c10d0ce5679c55e)

- - - - -
fdec42d3 by Jonny Bradley at 2026-08-06T13:23:24+01:00
[FIX] cookies: Increase specificity for `box-cookiesettings` module positioning - only needed in 30.x (due to CSS loading order differences i think)

- - - - -


5 changed files:

- lib/CookieConsent/CookieConsentLib.php
- lib/wiki-plugins/wikiplugin_cookieconsent.php
- src/js/jquery-tiki/tiki-cookie-handler.js
- templates/cookie_consent.tpl
- themes/base_files/scss/_tiki-miscellaneous_global.scss


Changes:

=====================================
lib/CookieConsent/CookieConsentLib.php
=====================================
@@ -46,6 +46,43 @@ class CookieConsentLib
         ];
     }
 
+    private static function getDisabledCookieCategoryKeys(): array
+    {
+        $disabledCategoriesInfo = \TikiLib::lib('prefs')->getPreference('cookie_consent_disable_builtin_categories');
+        return $disabledCategoriesInfo['value'];
+    }
+
+    /**
+     * Stub function to minimize code churn.  In the future this will return all consent categories that were requested
+     * by tiki code for the current request.
+     *
+     * Right now, it will return the categories not included in the temporary cookie_consent_disable_builtin_categories prefs
+     *
+     * @return array Cookie categories requested by tiki code in the current request.
+     */
+    public static function getRequestedCookieCategories(): array
+    {
+
+        $requestedCategories = self::getCookieCategories();
+        $currentCategories = self::getConsentPreferences()['categories'];
+
+        $disabledCategories = self::getDisabledCookieCategoryKeys();
+        if ($disabledCategories) {
+            //Strip out the disabled categories
+            foreach ($disabledCategories as $disabledCategoryKey) {
+                if (isset($requestedCategories[$disabledCategoryKey])) {
+                    unset($requestedCategories[$disabledCategoryKey]);
+                } else {
+                    \Feedback::error(tr("Category %0 from pref cookie_consent_disable_builtin_categories not found in: %1", $disabledCategoryKey, implode(', ', array_keys($requestedCategories))));
+                }
+            }
+        }
+        foreach ($requestedCategories as $key => & $value) {
+            $value['consent'] = $currentCategories[$key];
+        }
+        return $requestedCategories;
+    }
+
     /**
      * Initialize the consent preferences based on existing cookies.
      *


=====================================
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::checkAllowedCookieCategory(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>


=====================================
themes/base_files/scss/_tiki-miscellaneous_global.scss
=====================================
@@ -215,33 +215,33 @@ form[name=editpageform] .mb-3 .tab-content .alert-warning {
 }
 
 // cookie settings module
-.box-cookiesettings {
+.modules .module.box-cookiesettings {
     position: fixed;
 }
 
-.box-cookiesettings.none {
+.modules .module.box-cookiesettings.none {
     position: unset;
 }
 
-.box-cookiesettings.topleft {
+.modules .box-cookiesettings.topleft {
     left: 0;
     top: 0;
     border-bottom-right-radius: $border-radius;
 }
 
-.box-cookiesettings.topright {
+.modules .module.box-cookiesettings.topright {
     right: 0;
     top: 0;
     border-bottom-left-radius: $border-radius;
 }
 
-.box-cookiesettings.bottomleft {
+.modules .module.box-cookiesettings.bottomleft {
     left: 0;
     bottom: 0;
     border-top-right-radius: $border-radius;
 }
 
-.box-cookiesettings.bottomright {
+.modules .module.box-cookiesettings.bottomright {
     right: 0;
     bottom: 0;
     border-top-left-radius: $border-radius;



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/compare/f34bac7607e63bbce9b9a72ffcfd1ff4e7d31cda...fdec42d37a40f14e616ca8b22dea4df5e1f055bb

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/compare/f34bac7607e63bbce9b9a72ffcfd1ff4e7d31cda...fdec42d37a40f14e616ca8b22dea4df5e1f055bb
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.