1'use strict'; 2/** 3 * Set consent_type as passed by localize_script, and trigger a hook so geo ip scripts can alter it as needed. 4 * It's set on document ready, so the consent management plugin can first add an event listener. 5 * 6 * 7 * Edit: chronologically it seems difficult to create a sort of filter for the consent type. 8 * Let's change it so cookiebanners are just required to set it, if it's not available, we use a default, as defined here. 9 * 10 * This way, if a consent management plugin does not define a consenttype, the one passed here will be used, and it will still work. 11 * 12 * 13 */ 14 15window.wp_fallback_consent_type = consent_api.consent_type; 16window.waitfor_consent_hook = consent_api.waitfor_consent_hook; 17 18/** 19 * Check if a specific service has consent 20 * @param {string} service 21 */ 22function wp_has_service_consent(service) { 23 //Check if it's in the consented services cookie 24 let consented_services_json = consent_api_get_cookie(consent_api.cookie_prefix + '_' + 'consented_services'); 25 let consented_services; 26 try { 27 consented_services = JSON.parse(consented_services_json); 28 } catch (e) { 29 consented_services = {}; 30 } 31 32 if ( !consented_services.hasOwnProperty( service ) ){ 33 //default to the category 34 const category = wp_get_service_category(service); 35 return wp_has_consent( category ); 36 } else { 37 return consented_services[service]; 38 } 39} 40/** 41 * Check if a specific service is denied 42 * @param {string} service 43 */ 44function wp_is_service_denied( service ) { 45 //Check if it's in the consented services cookie 46 let consented_services_json = consent_api_get_cookie(consent_api.cookie_prefix + '_' + 'consented_services'); 47 let consented_services; 48 try { 49 consented_services = JSON.parse(consented_services_json); 50 } catch (e) { 51 consented_services = {}; 52 } 53 54 if ( !consented_services.hasOwnProperty( service ) ){ 55 return false; 56 } else { 57 return !consented_services[service]; 58 } 59} 60 61/** 62 * Set consent for a specific service 63 * @param {string} service 64 * @param {boolean} consented 65 */ 66function wp_set_service_consent( service, consented ){ 67 let consented_services_json = consent_api_get_cookie(consent_api.cookie_prefix + '_' + 'consented_services'); 68 let consented_services; 69 try { 70 consented_services = JSON.parse(consented_services_json); 71 } catch (e) { 72 consented_services = {}; 73 } 74 consented_services[service] = consented; 75 consent_api_set_cookie(consent_api.cookie_prefix + '_consented_services', JSON.stringify(consented_services)); 76 77 let details = {}; 78 details.service = service; 79 details.value = consented; 80 let event = new CustomEvent('wp_consent_api_status_change_service', { detail: details }); 81 document.dispatchEvent(event); 82} 83 84 85/** 86 * Check if a user has given consent for a specific category. 87 * 88 * @param {string} category The item to check consent against. 89 */ 90function wp_has_consent(category) { 91 let has_consent = false; 92 let consent_type; 93 if ( typeof (window.wp_consent_type) !== "undefined" ){ 94 consent_type = window.wp_consent_type; 95 } else { 96 consent_type = window.wp_fallback_consent_type 97 } 98 99 let cookie_value = consent_api_get_cookie(consent_api.cookie_prefix + '_' + category); 100 if ( !consent_type ) { 101 //if consent_type is not set, there's no consent management, we should return true to activate all cookies 102 has_consent = true; 103 } else if (consent_type.indexOf('optout') !== -1 && cookie_value === '') { 104 //if it's opt out and no cookie is set we should also return true 105 has_consent = true; 106 } else { 107 //all other situations, return only true if value is allow 108 has_consent = (cookie_value === 'allow'); 109 } 110 111 return has_consent; 112} 113 114/** 115 * Retrieve the category of a registered service 116 * 117 * @param {string} service 118 * @returns {string} 119 */ 120function wp_get_service_category( service ) { 121 // Check if services array exists and is valid 122 if (!consent_api.services || !Array.isArray(consent_api.services)) { 123 return 'marketing'; 124 } 125 126 let services = consent_api.services; 127 for (let i = 0; i < services.length; i++) { 128 if (services[i] && 129 services[i].name === service && 130 services[i].category) { 131 return services[i].category; 132 } 133 } 134 return 'marketing'; 135} 136 137/** 138 * Set cookie by consent type. 139 * 140 * @param {string} name The cookie name to set. 141 * @param {string} value The cookie value to set. 142 */ 143function consent_api_set_cookie(name, value) { 144 let secure = ";secure"; 145 let days = consent_api.cookie_expiration; 146 let date = new Date(); 147 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 148 let expires = ";expires=" + date.toGMTString(); 149 150 if (window.location.protocol !== "https:") secure = ''; 151 152 document.cookie = name + "=" + value + secure + expires + ";path=/"; 153} 154
155/** 156 * Retrieve a cookie by name. 157 * 158 * @param {string} name The name of the cookie to get data from. 159 */ 160function consent_api_get_cookie(name) { 161 name = name + "="; 162 var cookies = window.document.cookie.split(';'); 163 164 for (var i = 0; i < cookies.length; i++) { 165 var cookie = cookies[i].trim(); 166 if ( cookie.indexOf(name) === 0 ) 167 return cookie.substring(name.length, cookie.length); 168 } 169 170 //If we get to this point, that means the cookie wasn't found, we return an empty string. 171 return ""; 172} 173 174/** 175 * Set a new consent category value. 176 * 177 * @param {string} category The consent category to update. 178 * @param {string} value The value to update the consent category to. 179 */ 180function wp_set_consent(category, value) { 181 let event; 182 if (value !== 'allow' && value !== 'deny') return; 183 var previous_value = consent_api_get_cookie(consent_api.cookie_prefix + '_' + category); 184 consent_api_set_cookie(consent_api.cookie_prefix + '_' + category, value); 185 186 //do not trigger a change event if nothing has changed. 187 if ( previous_value === value ) return; 188 189 var changedConsentCategory = []; 190 changedConsentCategory[category] = value; 191 try { 192 // For modern browsers except IE: 193 event = new CustomEvent('wp_listen_for_consent_change', {detail: changedConsentCategory}); 194 } catch (err) { 195 // If IE 11 (or 10 or 9...?) 196 event = document.createEvent('Event'); 197 event.initEvent('wp_listen_for_consent_change', true, true); 198 event.detail = changedConsentCategory; 199 } 200 // Dispatch/Trigger/Fire the event 201 document.dispatchEvent(event); 202} 203 204 205
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.