1 2/** 3 * Global variable to store Ketch allowed consent 4 */ 5window.KetchAllowedConsent = []; 6window.KetchPrevAllowedConsent = []; 7 8const childMap = { 9 C0002: [ 'C0002_1','C0002_2','C0002_3','C0002_4','C0002_5' ], 10 C0004: [ 'C0004_1','C0004_2','C0004_3','C0004_4','C0004_5','C0004_6','C0004_7','C0004_8' ], 11} 12 13function stampOriginalPurposes() { 14 document.querySelectorAll('script[type="text/plain"][data-purposes]') 15 .forEach(el => { 16 if (! el.dataset.purposesOriginal) { 17 el.dataset.purposesOriginal = el.getAttribute('data-purposes') 18 } 19 }) 20} 21 22/** 23 * On click of "tmbi-cookie-preference-menu" class, open the Ketch consent preference center 24 * And also add event listener for Ketch consent event 25 */ 26document.addEventListener( 'DOMContentLoaded', () => { 27 stampOriginalPurposes() 28 tccKetchClickPreferenceCenter(); 29 tccKetchEventListen(); 30} ); 31 32/** 33 * Update every placeholder script's data-purposes to the allowed list 34 * @param {string[]} allowed 35 */ 36function mapDataPurposes( allowed ) { 37 document.querySelectorAll( 'script[type="text/plain"][data-purposes]' ) 38 .forEach( ( el ) => { 39 // Fall back to the current data-purposes if the element was not stamped 40 // yet (e.g. injected after DOMContentLoaded ran stampOriginalPurposes()). 41 // Without this guard an unstamped node throws and aborts consent handling 42 // for every remaining gated script, so nothing un-gates. 43 if ( ! el.dataset.purposesOriginal ) { 44 el.dataset.purposesOriginal = el.getAttribute( 'data-purposes' ) || '' 45 } 46 const parents = el.dataset.purposesOriginal.split( ' ' ) 47 const result = [] 48 49 parents.forEach( ( parent ) => { 50 if ( allowed.includes( parent ) ) { 51 result.push( parent ) 52 const kids = childMap[ parent ] || [] 53 kids.forEach( ( k ) => { 54 if ( allowed.includes( k )) { 55 result.push( k ) 56 } 57 } ) 58 } 59 } ) 60 61 el.setAttribute( 'data-purposes', result.join( ' ' ) ) 62 } ) 63} 64 65/** 66 * Filter to set Ketch allowed consent 67 */ 68function tccKetchAllowedConsent ( consent ) { 69 return window.KetchAllowedConsent; 70} 71wp.hooks.addFilter( 'tccAllowedConsent', 'tcc', tccKetchAllowedConsent ); 72 73/** 74 * Callback function for Ketch consent event 75 * @param {Array} consent Consent data will be like { purposes: { 'C0001': true, 'C0002': false, 'C0004': true } } 76 * 77 * @return {void} 78 */ 79function tccKetchConsentInit ( consent ) { 80 console.log( 'TCC V2 :: KETCH CONSENT :: ', consent ); 81 82 let allowedConsent = Object.keys( consent.purposes ).filter( purpose => consent.purposes[ purpose ] ); 83 window.KetchAllowedConsent = allowedConsent; 84 85 if ( window.KetchPrevAllowedConsent.length === 0 ) { 86 window.KetchPrevAllowedConsent = [ ...( window.KetchAllowedConsent || [] ) ]; 87 } 88 89 mapDataPurposes( allowedConsent ); 90 91 wp.hooks.doAction( 'tccConsentInitialized', window.KetchAllowedConsent ); 92} 93 94/** 95 * Callback function for Ketch user consent updated event 96 * @param {Array} consent Consent data will be like { purposes: { 'C0001': true, 'C0002': false, 'C0004': true } } 97 * 98 * @return {void} 99 */ 100function tccKetchUserConsentUpdated ( consent ) { 101 console.log( 'TCC V2 :: KETCH CONSENT UPDATED :: ', window.KetchPrevAllowedConsent, window.KetchAllowedConsent ); 102 wp.hooks.doAction( 'tccConsentUpdated', window.KetchPrevAllowedConsent, window.KetchAllowedConsent ); 103 window.KetchPrevAllowedConsent = [ ...( window.KetchAllowedConsent || [] ) ]; 104 105 let allowedConsent = Object.keys( consent.purposes ).filter( purpose => consent.purposes[ purpose ] ); 106 107 mapDataPurposes( allowedConsent ); 108} 109 110/** 111 * Updates the text and appearance of the cookie preference center link. 112 * 113 * @param {string} regionInfo - The region identifier supplied by Ketch. 114 * @return {void} This function does not return a value. It modifies the DOM directly by updating the specified 115 * preference center element. 116 */ 117function tccKetchChangePreferenceCenterText( regionInfo ) { 118 const preferenceCenterEl = document.querySelector( '.tmbi-cookie-preference-menu a' ); 119 if ( !preferenceCenterEl ) { 120 return; 121 } 122 123 console.log( 'TCC V2 :: KETCH REGION INFO :: ' , regionInfo ); 124 125 // All regions now use the same label. 126 const labelText = 'Your Privacy Choices'; 127 128 // Clear existing children. 129 preferenceCenterEl.innerHTML = labelText; 130 131 // Create the <img>. 132 const privacyLogo = document.createElement( 'img' ); 133 134 privacyLogo.src = window.tmbi_cc_ketch_helper_privacy_logo_url; 135 privacyLogo.alt = 'Privacy Options Logo'; 136 privacyLogo.width = 24; 137 privacyLogo.height = 24; 138 privacyLogo.loading = 'lazy'; 139 privacyLogo.decoding = 'async'; 140 privacyLogo.style.verticalAlign = 'middle'; 141 if ( window.innerWidth < 1024 ) { 142 privacyLogo.style.width = '34px'; 143 } else { 144 privacyLogo.style.width = '24px'; 145 } 146 privacyLogo.style.marginRight = '8px'; 147 148 // Append logo and text. 149 preferenceCenterEl.parentNode.prepend( privacyLogo ); 150} 151 152/** 153 * Method to bind click event for preference center link 154 */ 155function tccKetchClickPreferenceCenter () { 156 const openPreferenceCenter = function () { 157 if ( typeof ketch !== 'undefined' ) { 158 ketch( 'showExperience', { 159 preferenceOptions: { 160 tab: 'consentsTab' 161 } 162 } ); 163 } 164 }; 165 166 document.querySelectorAll( '.tmbi-cookie-preference-menu, .tmbi-cookie-preference-popup' ) 167 .forEach( ( link ) => { 168 link.addEventListener( 'click', openPreferenceCenter ); 169 } ); 170} 171 172/** 173 * Listen Ketch consent event 174 */ 175function tccKetchEventListen () { 176 if ( typeof ketch !== 'undefined' ) { 177 ketch( 'on', 'consent', tccKetchConsentInit ); 178 ketch( 'on', 'regionInfo', tccKetchChangePreferenceCenterText ); 179 ketch( 'on', 'userConsentUpdated', tccKetchUserConsentUpdated ); 180 } 181}
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.