[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] Cookies: tabs state to be stored and loaded from cookies, enhance...
"Merci Jacob \(@mercihabam\) via TikiWiki-cvs" <[email protected]>
| Newsgroups | gmane.comp.cms.tiki.cvs |
|---|---|
| Message-ID | <69833494bf155_3b4cd7b4497771@gitlab-sidekiq-low-urgency-cpu-bound-v2-65d8676567-h22x5.mail> |
Merci Jacob pushed to branch master at Tiki Wiki CMS Groupware / Tiki
Commits:
f214bd8e by Merci Jacob at 2026-02-04T11:50:37+00:00
[FIX] Cookies: tabs state to be stored and loaded from cookies, enhance cookies handlers to account for when the consent feature is disabled, and ensure that the consent value is synced when the consent has been given
---
* as of now the cookie format seems good across all Tiki sites, so no need to keep using `cookie_consent_last_update_date`
* set consent given data under cookie_consent_user_pref
* throw error in either case when attempting to set a cookie without a category
* ensure that cookie_consent_date setting is user scoped is kept only programatic along with the setting 'cookie_consent_last_update_date'
* [ENH] Add way to automatically reset cookie consent when an update has been detected
* [FIX] tabs state to be stored and loaded from cookies, enhance cookies handlers to account for when the consent feature is disabled, and ensure that the consent value is synced when the consent has been given
See merge request tikiwiki/tiki!8345
- - - - -
5 changed files:
- lib/CookieConsent/CookieConsentLib.php
- lib/jquery_tiki/tiki-jquery.js
- lib/setup/javascript.php
- lib/tiki-js.js
- src/js/jquery-tiki/tiki-cookie-handler.js
Changes:
=====================================
lib/CookieConsent/CookieConsentLib.php
=====================================
@@ -70,18 +70,32 @@ class CookieConsentLib
return $consentPreferences;
}
// If the user is logged in, try to load their stored preference
+ if ($user) {
+ $consentPreferences = self::getUserPreference();
+ if (! empty($consentPreferences)) {
+ // Sync the cookie with the user preference
+ self::setConsentPreferences($consentPreferences);
+ return $consentPreferences;
+ }
+ }
+
+ return $consentPreferences;
+ }
+
+ private static function getUserPreference()
+ {
+ global $tikilib, $user;
+
if ($user) {
$userPreferences = $tikilib->get_user_preference($user, 'cookie_consent_user_pref', '');
if ($userPreferences) {
$consentPreferences = json_decode($userPreferences, true);
if (is_array($consentPreferences)) {
- // Sync the cookie with the user preference
- self::setConsentPreferences($consentPreferences);
return $consentPreferences;
}
}
}
- return $consentPreferences;
+ return [];
}
/**
=====================================
lib/jquery_tiki/tiki-jquery.js
=====================================
@@ -3421,7 +3421,7 @@ $(function () {
let tabShown = false,
notShown = [];
- if (document.location.search.match(/cookietab=/)) {
+ if (document.location.search.match(/contenttab=/)) {
tabShown = true;
} else if (document.location.hash && $tabs.length) {
$tabs.tab('show');
@@ -3434,9 +3434,8 @@ $(function () {
// class "active" set serverside from $cookietab var
let $tab = $('a[data-bs-toggle=tab].active', this);
- if (hrefFromCookie && $tab.length === 0) {
- $tab = $('a[data-bs-toggle=tab][href="' + hrefFromCookie + '"]');
- } else {
+ if (hrefFromCookie) {
+ $tab = $('a[data-bs-toggle=tab][href="' + hrefFromCookie + '"]', this);
$tab.tab('show');
tabShown = true;
return; // active was set serverside, job done
@@ -3468,7 +3467,7 @@ $(function () {
if ($(this).parents(".tab-content").length === 0) {
document.location.hash = $(e.target).attr("href");
}
- setCookieBrowser($(this).parents(".tabs").first().data("name"), $(e.target).attr("href"), "tabs");
+ setCookie($(this).parents(".tabs").first().data("name"), $(e.target).attr("href"), "tabs", "session", window.tikiCookieConstants.BUILTIN_COOKIE_CATEGORY_FUNCTIONAL);
}).on("click", function () {
const scroll = $window.scrollTop(); // prevent window jumping to tabs on click
$(this).tab('show');
=====================================
lib/setup/javascript.php
=====================================
@@ -238,6 +238,7 @@ $jqueryTiki['tiki_same_day_time_only'] = $prefs['tiki_same_day_time_only'];
$jqueryTiki['jquery_timeago'] = $prefs['jquery_timeago'] === 'y';
$jqueryTiki['short_date_format'] = $prefs['short_date_format'];
$jqueryTiki['short_time_format'] = $prefs['short_time_format'];
+$jqueryTiki['cookie_consent_enabled'] = $prefs['cookie_consent_feature'] === 'y' ? true : false;
$jqueryTiki['cookie_consent_dom_id'] = $prefs['cookie_consent_dom_id'];
$jqueryTiki['cookie_consent_mode'] = $prefs['cookie_consent_mode'];
$jqueryTiki['cookie_consent_expires'] = $prefs['cookie_consent_expires'];
=====================================
lib/tiki-js.js
=====================================
@@ -552,7 +552,12 @@ function setCookie(name, value, section, expires, cookieCategory, path, domain,
if (getCookie(name, section) == value) {
return true;
}
- if (cookieCategory) {
+
+ if (!cookieCategory) {
+ throw new Error("No cookie category provided.");
+ }
+
+ if (jqueryTiki.cookie_consent_enabled) {
let consentCookie = jqueryTiki.cookie_consent_value ?? getCookie("cookie_consent");
let consentData = JSON.parse(decodeURIComponent(consentCookie));
// Ensure the consent cookie object exists
@@ -571,9 +576,8 @@ function setCookie(name, value, section, expires, cookieCategory, path, domain,
if (consentData.categories[cookieCategory] !== true) {
return false; // Consent was NOT given, cookie was NOT set
}
- } else {
- throw new Error("No cookie category provided.");
}
+
// Set a default expiration date if none is provided (one year from now)
if (!expires) {
expires = new Date();
=====================================
src/js/jquery-tiki/tiki-cookie-handler.js
=====================================
@@ -64,8 +64,11 @@ window.CookieHandler = (() => {
}
COOKIE_CONSENT_VALUE.consentGiven = true;
+
// Store the entire consent object (including action and categories) in a single cookie
- setCookieBrowser(COOKIE_CONSENT_NAME, encodeURIComponent(JSON.stringify(COOKIE_CONSENT_VALUE)), "", exp);
+ const cookieConsentValue = encodeURIComponent(JSON.stringify(COOKIE_CONSENT_VALUE));
+ jqueryTiki.cookie_consent_value = cookieConsentValue;
+ setCookieBrowser(COOKIE_CONSENT_NAME, cookieConsentValue, "", exp);
$(document).trigger("cookies.consent.agree");
}
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/f214bd8e8ebefa9e82c28fd0a75093820b0c2cca
--
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/f214bd8e8ebefa9e82c28fd0a75093820b0c2cca
You're receiving this email because of your account on gitlab.com.
_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs