PageSourceSearch

https://www.cibsejournal.com/wp-content/plugins/cpl-user-cookie-pr…/cpl-user-cookie-preferences.js?ver=1.0.14

js cibsejournal.com collected 2026-09-24 09:55:51 UTC 12,650 bytes, 319 lines download raw bytes

1
2document.addEventListener("DOMContentLoaded", function() {
3  
4  const settings = (typeof cplUserCookiePreferencesSettings !== 'undefined') ? cplUserCookiePreferencesSettings : {};
5  const version = settings.cookieVersion || 1;
6  const COOKIE_NAME = 'cpl_user_cookie_preferences_v' + version;
7  
8  const COOKIE_EXPIRATION_DAYS = settings.cookieExpiry ? parseInt(settings.cookieExpiry, 10) : 180;
9
10  const catFuncEnabled = (settings.catFunctionalEnabled === '1');
11  const catAnaEnabled  = (settings.catAnalyticsEnabled === '1');
12  const catMarkEnabled = (settings.catMarketingEnabled === '1');
13
14  function getCookie(name) {
15    const value = `; ${document.cookie}`;
16    const parts = value.split(`; ${name}=`);
17    if (parts.length === 2) return parts.pop().split(';').shift();
18    return null;
19  }
20
21  function setCookie(name, value, days) {
22    const d = new Date();
23    d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
24    document.cookie = `${name}=${encodeURIComponent(JSON.stringify(value))};expires=${d.toUTCString()};path=/;SameSite=Lax`;
25  }
26
27  function updateConsentLayer(prefs) {
28    const effectiveFunctional = catFuncEnabled ? !!prefs.functional : false;
29    const effectiveAnalytics  = catAnaEnabled  ? !!prefs.analytics  : false;
30    const effectiveMarketing  = catMarkEnabled ? !!prefs.marketing  : false;
31
32    if (typeof gtag === 'function') {
33      gtag('consent', 'update', {
34        'functionality_storage': effectiveFunctional ? 'granted' : 'denied',
35        'personalization_storage': effectiveFunctional ? 'granted' : 'denied',
36        'analytics_storage': effectiveAnalytics ? 'granted' : 'denied',
37        'ad_storage': effectiveMarketing ? 'granted' : 'denied',
38        'ad_user_data': effectiveMarketing ? 'granted' : 'denied',
39        'ad_personalization': effectiveMarketing ? 'granted' : 'denied',
40        'security_storage': 'granted' 
41      });
42    }
43
44    window.dataLayer = window.dataLayer || [];
45    window.dataLayer.push({
46      'event': 'cookie_consent_update',
47      'consent_functional': effectiveFunctional,
48      'consent_analytics': effectiveAnalytics,
49      'consent_marketing': effectiveMarketing
50    });
51  }
52
53  function sendAnalyticsChoice(sanitizedPrefs, source, isUpdate) {
54    if (!settings.ajaxUrl || !settings.nonce) return;
55
56    let choiceType = 'custom_selection';
57    const isFunc = sanitizedPrefs.functional;
58    const isAna  = sanitizedPrefs.analytics;
59    const isMark = sanitizedPrefs.marketing;
60
61    // Check all active categories
62    const activeFunc = catFuncEnabled ? isFunc : true;
63    const activeAna  = catAnaEnabled  ? isAna  : true;
64    const activeMark = catMarkEnabled ? isMark : true;
65
66    if (activeFunc && activeAna && activeMark) {
67        choiceType = 'full_opt_in';
68    } else if (!isFunc && !isAna && !isMark) {
69        choiceType = 'full_opt_out';
70    } else if (isFunc && !isAna && !isMark) {
71        choiceType = 'functional_only';
72    } else if (!isFunc && isAna && !isMark) {
73        choiceType = 'analytics_only';
74    } else if (!isFunc && !isAna && isMark) {
75        choiceType = 'marketing_only';
76    }
77
78    const device = /Mobi|Android|iPhone|iPad/i.test(navigator.userAgent) ? 'mobile' : 'desktop';
79
80    fetch(settings.ajaxUrl, {
81      method: 'POST',
82      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
83      body: new URLSearchParams({
84        action: 'cpl_track_consent_choice',
85        choice: choiceType,
86        device: device,
87        source: source || 'banner',
88        user_status: isUpdate ? 'update' : 'initial',
89        nonce: settings.nonce
90      })
91    }).catch(function(err) {
92      console.error("Consent tracking failed", err);
93    });
94  }
95
96  function saveAndApply(prefs, source) {
97    // Check if user already had a saved consent cookie before saving the new one
98    const isUpdate = !!getCookie(COOKIE_NAME);
99
100    const sanitizedPrefs = {
101      functional: catFuncEnabled ? !!prefs.functional : false,
102      analytics:  catAnaEnabled  ? !!prefs.analytics  : false,
103      marketing:  catMarkEnabled ? !!prefs.marketing  : false
104    };
105
106    setCookie(COOKIE_NAME, sanitizedPrefs, COOKIE_EXPIRATION_DAYS);
107    updateConsentLayer(sanitizedPrefs);
108    sendAnalyticsChoice(sanitizedPrefs, source, isUpdate);
109    
110    const overlay = document.getElementById('cpl-user-cookie-preferences-overlay');
111    const banner = document.getElementById('cpl-user-cookie-preferences-banner');
112    const floatingBtn = document.getElementById('cpl-floating-btn');
113
114    if (overlay) overlay.style.display = 'none';
115    if (banner) banner.style.display = 'none';
116    
117    // Show floating button once consent selection has been made
118    if (floatingBtn) floatingBtn.style.display = 'flex';
119  }
120  
121  function forceExpandToggles() {
122    const toggles = document.querySelector('.cpl-user-cookie-preferences-toggles');
123    const btnAllowSelection = document.getElementById('cmp-allow-selection');
124    const btnAllowAll = document.getElementById('cmp-allow-all');
125    if (toggles && toggles.classList.contains('cpl-toggles-collapsed')) {
126        toggles.classList.remove('cpl-toggles-collapsed');
127        if (typeof cplUserCookiePreferencesSettings !== 'undefined') {
128            if (btnAllowSelection) {
129                btnAllowSelection.textContent = cplUserCookiePreferencesSettings.allowSelLabel;
130                btnAllowSelection.dataset.state = 'save';
131            }
132            if (btnAllowAll) {
133                btnAllowAll.textContent = cplUserCookiePreferencesSettings.allowAllLabel;
134            }
135        }
136    }
137  }
138
139  function initCookiePreferences() {
140    const savedCookie = getCookie(COOKIE_NAME);
141    const closeBtn = document.getElementById('cmp-close-modal');
142    const floatingBtn = document.getElementById('cpl-floating-btn');
143
144    if (savedCookie) {
145      try {
146        const prefs = JSON.parse(decodeURIComponent(savedCookie));
147        updateConsentLayer(prefs);
148
149        // Pre-fill toggles for users editing preferences if categories exist
150        const elFunc = document.getElementById('cmp-functional');
151        const elAna  = document.getElementById('cmp-analytics');
152        const elMark = document.getElementById('cmp-marketing');
153
154        if (elFunc) elFunc.checked = catFuncEnabled && !!prefs.functional;
155        if (elAna)  elAna.checked  = catAnaEnabled  && !!prefs.analytics;
156        if (elMark) elMark.checked = catMarkEnabled && !!prefs.marketing;
157
158        // User HAS made a selection: show floating button
159        if (floatingBtn) floatingBtn.style.display = 'flex';
160      } catch(e) {
161        console.error("Consent cookie parsing failed", e);
162      }
163    } else {
164      // User HAS NOT made a selection: hide floating button
165      if (floatingBtn) floatingBtn.style.display = 'none';
166
167      const mode = settings.displayMode || 'banner';
168      if (mode === 'banner') {
169        const banner = document.getElementById('cpl-user-cookie-preferences-banner');
170        if(banner) banner.style.display = 'block';
171      } else {
172        const overlay = document.getElementById('cpl-user-cookie-preferences-overlay');
173        if(overlay) overlay.style.display = 'flex';
174        
175        // Handle close button visibility based on admin setting
176        if (settings.modalCloseEnabled === '1' && closeBtn) {
177            closeBtn.style.display = 'block';
178        } else if (closeBtn) {
179            closeBtn.style.display = 'none';
180        }
181        
182        // Handle initially hiding cookie options in modal mode
183        if (settings.hideOptions === '1') {
184            const toggles = document.querySelector('.cpl-user-cookie-preferences-toggles');
185            const btnAllowSelection = document.getElementById('cmp-allow-selection');
186            const btnAllowAll = document.getElementById('cmp-allow-all');
187            
188            if (toggles && btnAllowSelection) {
189                if (settings.hideOptionsAnimate === '0') {
190                    toggles.classList.add('cpl-no-transition');
191                }
192                
193                toggles.classList.add('cpl-toggles-collapsed');
194                btnAllowSelection.textContent = settings.cookieSettingsLabel;
195                btnAllowSelection.dataset.state = 'expand';
196                
197                if (btnAllowAll) {
198                    btnAllowAll.textContent = settings.acceptAllOverrideLabel;
199                }
200            }
201        }
202      }
203    }
204  }
205
206  const btnAllowAll = document.getElementById('cmp-allow-all');
207  if (btnAllowAll) {
208    btnAllowAll.addEventListener('click', function() {
209      const elFunc = document.getElementById('cmp-functional');
210      const elAna  = document.getElementById('cmp-analytics');
211      const elMark = document.getElementById('cmp-marketing');
212
213      if (elFunc) elFunc.checked = true;
214      if (elAna)  elAna.checked  = true;
215      if (elMark) elMark.checked = true;
216
217      saveAndApply({
218        functional: catFuncEnabled,
219        analytics:  catAnaEnabled,
220        marketing:  catMarkEnabled
221      }, 'modal');
222    });
223  }
224
225  const btnAllowSelection = document.getElementById('cmp-allow-selection');
226  if (btnAllowSelection) {
227    btnAllowSelection.addEventListener('click', function() {
228      if (this.dataset.state === 'expand') {
229          forceExpandToggles();
230          return;
231      }
232
233      const elFunc = document.getElementById('cmp-functional');
234      const elAna  = document.getElementById('cmp-analytics');
235      const elMark = document.getElementById('cmp-marketing');
236      
237      saveAndApply({
238        functional: (catFuncEnabled && elFunc) ? elFunc.checked : false,
239        analytics:  (catAnaEnabled  && elAna)  ? elAna.checked  : false,
240        marketing:  (catMarkEnabled && elMark) ? elMark.checked : false
241      }, 'modal');
242    });
243  }
244
245  const bannerAllowAll = document.getElementById('cmp-banner-allow-all');
246  if (bannerAllowAll) {
247    bannerAllowAll.addEventListener('click', function() {
248      saveAndApply({
249        functional: catFuncEnabled,
250        analytics:  catAnaEnabled,
251        marketing:  catMarkEnabled
252      }, 'banner');
253    });
254  }
255
256  const bannerPrefs = document.getElementById('cmp-banner-prefs');
257  const closeBtn = document.getElementById('cmp-close-modal');
258  const floatingBtn = document.getElementById('cpl-floating-btn');
259
260  if (bannerPrefs) {
261    bannerPrefs.addEventListener('click', function() {
262      document.getElementById('cpl-user-cookie-preferences-banner').style.display = 'none';
263      document.getElementById('cpl-user-cookie-preferences-overlay').style.display = 'flex';
264      if (closeBtn) closeBtn.style.display = 'block';
265      
266      const toggles = document.querySelector('.cpl-user-cookie-preferences-toggles');
267      if(toggles) toggles.classList.remove('cpl-no-transition');
268      forceExpandToggles(); 
269    });
270  }
271
272  if (floatingBtn) {
273    floatingBtn.addEventListener('click', function() {
274      const banner = document.getElementById('cpl-user-cookie-preferences-banner');
275      if (banner) banner.style.display = 'none';
276      document.getElementById('cpl-user-cookie-preferences-overlay').style.display = 'flex';
277      if (closeBtn) closeBtn.style.display = 'block';
278      
279      const toggles = document.querySelector('.cpl-user-cookie-preferences-toggles');
280      if(toggles) toggles.classList.remove('cpl-no-transition');
281      forceExpandToggles(); 
282    });
283  }
284
285  // Handle global manual links and shortcodes
286  document.addEventListener('click', function(e) {
287      if (e.target && e.target.closest('.cpl-open-cookie-preferences')) {
288          e.preventDefault();
289          const banner = document.getElementById('cpl-user-cookie-preferences-banner');
290          if (banner) banner.style.display = 'none';
291          
292          const overlay = document.getElementById('cpl-user-cookie-preferences-overlay');
293          if (overlay) overlay.style.display = 'flex';
294          
295          const closeBtn = document.getElementById('cmp-close-modal');
296          if (closeBtn) closeBtn.style.display = 'block';
297          
298          const toggles = document.querySelector('.cpl-user-cookie-preferences-toggles');
299          if(toggles) toggles.classList.remove('cpl-no-transition');
300          forceExpandToggles(); 
301      }
302  });
303
304  if (closeBtn) {
305    closeBtn.addEventListener('click', function() {
306      document.getElementById('cpl-user-cookie-preferences-overlay').style.display = 'none';
307      
308      if (!getCookie(COOKIE_NAME)) {
309          const banner = document.getElementById('cpl-user-cookie-preferences-banner');
310          const mode = settings.displayMode || 'banner';
311          if (mode === 'banner' && banner) {
312              banner.style.display = 'block';
313          }
314      }
315    });
316  }
317
318  initCookiePreferences();
319});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.