[TikiWiki-commits] [Git][tikiwiki/tiki][29.x] [BP][FIX] Cookie Consent: Fix cookie consent categories handling
"UshindiG \(@GedeonTS\) via TikiWiki-cvs" <[email protected]>
| Newsgroups | gmane.comp.cms.tiki.cvs |
|---|---|
| Message-ID | <69b40240613b0_3b1f947f455667@gitlab-sidekiq-low-urgency-cpu-bound-v2-576b4bd5d6-gw6q2.mail> |
UshindiG pushed to branch 29.x at Tiki Wiki CMS Groupware / Tiki
Commits:
58a42104 by UshindiG at 2026-03-13T14:17:51+02:00
[BP][FIX] Cookie Consent: Fix cookie consent categories handling
---
* [BP][FIX] Cookie Consent: Fix cookie consent categories handling
---
* [FIX] tiki-cookie-handler.js: replace hardcoded essential category string with constant for improved maintainability
* [FIX] tiki-cookie-handler.js: improve code readability by using constant name exposed from php class CookieConsentLib to js
* [FIX] tiki-cookie-handler: update cookie consent handling to use constants from the CookieConsentLib
* [FIX] Correctly set essential category in cookie consent handling
* [FIX] Update cookie consent handling to dynamically set categories based on user selection
* [FIX] Cookie Consent: Fix cookie consent categories handling
See merge request tikiwiki/tiki!7953
(cherry picked from commit 90c59b527346cf7faf3dd2f5ced72dc308cfb2d6)
See merge request tikiwiki/tiki!9642
- - - - -
3 changed files:
- lib/setup/javascript.php
- src/js/jquery-tiki/tiki-cookie-handler.js
- templates/cookie_consent.tpl
Changes:
=====================================
lib/setup/javascript.php
=====================================
@@ -242,6 +242,7 @@ $jqueryTiki['cookie_consent_expires'] = $prefs['cookie_consent_expires'];
$jqueryTiki['cookie_consent_name'] = CookieConsentLib::COOKIE_CONSENT_NAME;
$jqueryTiki['cookie_consent_categories'] = json_encode(array_keys(CookieConsentLib::getCookieCategories()));
$jqueryTiki['cookie_consent_value'] = json_encode(CookieConsentLib::getConsentPreferences(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
+$jqueryTiki['BUILTIN_COOKIE_CATEGORY_ESSENTIAL'] = json_encode(CookieConsentLib::BUILTIN_COOKIE_CATEGORY_ESSENTIAL);
$jqueryTiki['wiki_url_scheme'] = $prefs['wiki_url_scheme'];
//set at 4 hours if empty
=====================================
src/js/jquery-tiki/tiki-cookie-handler.js
=====================================
@@ -1,17 +1,16 @@
window.CookieHandler = (() => {
+ const BUILTIN_COOKIE_CATEGORY_ESSENTIAL = JSON.parse(jqueryTiki.BUILTIN_COOKIE_CATEGORY_ESSENTIAL);
+
function setConsentCookies(actionType = "customized") {
const COOKIE_CONSENT_NAME = jqueryTiki.cookie_consent_name;
const COOKIE_CATEGORIES = JSON.parse(jqueryTiki.cookie_consent_categories);
- let getcookie_consent_value = JSON.parse(jqueryTiki.cookie_consent_value);
-
- if (getcookie_consent_value === null || getcookie_consent_value.length === 0) {
- getcookie_consent_value = {
- action: "customized",
- categories: {},
- consentGiven: false,
- };
- }
+ const getcookie_consent_value = {
+ action: "customized",
+ categories: {},
+ consentGiven: false,
+ };
+ const ACTION_TYPES = { CUSTOMIZED: "customized", ACCEPT_ALL: "acceptAll", DECLINE_UNNECESSARY: "declineUnnecessary" };
const COOKIE_CONSENT_VALUE = getcookie_consent_value;
const exp = new Date();
exp.setTime(exp.getTime() + 24 * 60 * 60 * 1000 * jqueryTiki.cookie_consent_expires);
@@ -38,12 +37,30 @@ window.CookieHandler = (() => {
}
});
- if (allChecked) {
- COOKIE_CONSENT_VALUE.action = "acceptAll";
- } else if (noneChecked) {
- COOKIE_CONSENT_VALUE.action = "declineUnnecessary";
- } else {
- COOKIE_CONSENT_VALUE.action = "customized";
+ $("#customConsentSectionBanner input[type='checkbox']").each(function () {
+ if (!$(this).prop("disabled")) {
+ if ($(this).is(":checked")) {
+ noneChecked = false;
+ } else {
+ allChecked = false;
+ }
+ }
+ });
+
+ if (actionType === ACTION_TYPES.ACCEPT_ALL) {
+ COOKIE_CONSENT_VALUE.action = ACTION_TYPES.ACCEPT_ALL;
+ COOKIE_CONSENT_VALUE.categories = {};
+ COOKIE_CATEGORIES.forEach((category) => {
+ COOKIE_CONSENT_VALUE.categories[category] = true;
+ });
+ } else if (actionType === ACTION_TYPES.CUSTOMIZED) {
+ COOKIE_CONSENT_VALUE.action = ACTION_TYPES.CUSTOMIZED;
+ } else if (actionType === ACTION_TYPES.DECLINE_UNNECESSARY) {
+ COOKIE_CONSENT_VALUE.action = ACTION_TYPES.DECLINE_UNNECESSARY;
+ COOKIE_CONSENT_VALUE.categories = {};
+ COOKIE_CATEGORIES.forEach((category) => {
+ COOKIE_CONSENT_VALUE.categories[category] = category === BUILTIN_COOKIE_CATEGORY_ESSENTIAL; // Only essential category is true
+ });
}
COOKIE_CONSENT_VALUE.consentGiven = true;
@@ -69,7 +86,17 @@ window.CookieHandler = (() => {
let allNonEssentialUnchecked = true;
$("#customConsentSection input[type='checkbox']").each(function () {
- if ($(this).prop("disabled") || $(this).attr("name").includes("cookie_consent_essential")) {
+ if ($(this).prop("disabled") || $(this).attr("name").includes(BUILTIN_COOKIE_CATEGORY_ESSENTIAL)) {
+ return;
+ }
+ if ($(this).is(":checked")) {
+ allNonEssentialUnchecked = false;
+ } else {
+ allNonEssentialChecked = false;
+ }
+ });
+ $("#customConsentSectionBanner input[type='checkbox']").each(function () {
+ if ($(this).prop("disabled") || $(this).attr("name").includes(BUILTIN_COOKIE_CATEGORY_ESSENTIAL)) {
return;
}
if ($(this).is(":checked")) {
@@ -106,6 +133,9 @@ $(document).ready(() => {
$("#customConsentSection input[type='checkbox']").on("change", function () {
CookieHandler.updateTopLevelAction();
});
+ $("#customConsentSectionBanner input[type='checkbox']").on("change", function () {
+ CookieHandler.updateTopLevelAction();
+ });
$("#cookie_consent_preference").on("click", () => {
CookieHandler.setConsentCookies("acceptAll");
@@ -152,6 +182,11 @@ $(document).ready(() => {
$(this).prop("checked", true);
}
});
+ $('#customConsentSectionBanner input[type="checkbox"]').each(function () {
+ if (!$(this).prop("disabled")) {
+ $(this).prop("checked", true);
+ }
+ });
}
CookieHandler.updateTopLevelAction();
});
@@ -164,6 +199,11 @@ $(document).ready(() => {
$(this).prop("checked", false);
}
});
+ $('#customConsentSectionBanner input[type="checkbox"]').each(function () {
+ if (!$(this).prop("disabled")) {
+ $(this).prop("checked", false);
+ }
+ });
}
CookieHandler.updateTopLevelAction();
});
=====================================
templates/cookie_consent.tpl
=====================================
@@ -26,14 +26,14 @@
</div>
</div>
- <div class="row mb-4">
+ <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}
<div class="form-check form-switch d-flex align-items-center flex-shrink-0">
- <input class="form-check-input me-2" type="checkbox" id="cookie_{$category}"
+ <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}>
- <label class="form-check-label me-2 fw-semibold" for="cookie_{$category}">
+ <label class="form-check-label me-2 fw-semibold" for="toggle{$category|capitalize}">
{tr}{$data.name}{/tr}
</label>
<button type="button" class="btn btn-link p-0 text-decoration-none text-dark"
@@ -49,12 +49,12 @@
<div class="row">
<div class="col-12 d-flex flex-wrap gap-2 justify-content-center justify-content-md-end">
{if $prefs.cookie_consent_disable eq 'n'}
- <button type="button" class="btn btn-outline-danger"
+ <button type="submit" class="btn btn-outline-danger"
id="cookie_decline_unnecessary_button">
{tr}Refuse unnecessary{/tr}
</button>
{/if}
- <button type="button" class="btn btn-secondary"
+ <button type="submit" class="btn btn-secondary"
id="cookie_save_button">
{tr}Save preferences{/tr}
</button>
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/58a42104c4d3f662dd4e366a065234d4f2de8ce3
--
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/58a42104c4d3f662dd4e366a065234d4f2de8ce3
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