Files

128 lines
4.2 KiB
PHTML

<?php
/**
* @package Plumrocket_GlobalPrivacyControl
* @copyright Copyright (c) 2025 Plumrocket Inc.
* @license https://plumrocket.com/license End-user License Agreement
*/
?>
<?php
/** @var \Magento\Framework\View\Element\Template $block */
/** @var \Plumrocket\Base\ViewModel\CspNonce $cspNonceViewModel */
$cspNonceViewModel = $block->getData('cspNonceViewModel');
?>
<script <?= /* @noEscape */ $cspNonceViewModel->getNonce() ?>>
var prGlobalPrivacyControlApi = {
config: <?= /* @noEscape */ $block->getSerializedConfig() ?>,
isApplicableForUser: false,
isConfigured: false,
configure: function () {
const setConfigurationResult = (isApplicable) => {
this.isApplicableForUser = isApplicable;
this.isConfigured = true;
if (isApplicable) {
this.signalHandlers.processPendingHandlers();
}
document.dispatchEvent(new Event('pr-global-privacy-control-api:configured'));
};
if (! this.config.canProcessSignal) {
setConfigurationResult(false);
return;
}
if (this.cache.get('isApplicableForUser')) {
setConfigurationResult(this.cache.get('isApplicableForUser'));
return;
}
if (! navigator.globalPrivacyControl) {
setConfigurationResult(false);
return;
}
if (! this.config.isGeotargetingEnabled) {
this.cache.set('isApplicableForUser', true);
setConfigurationResult(true);
return;
}
this.geotargeting.isApplicable()
.then(result => {
this.cache.set('isApplicableForUser', result);
setConfigurationResult(result);
})
.catch(() => setConfigurationResult(true));
},
shouldApply: function () {
if (! this.isConfigured) {
return false;
}
return this.isApplicableForUser;
},
};
prGlobalPrivacyControlApi.geotargeting = {
isApplicable: async function () {
return fetch(prGlobalPrivacyControlApi.config.geotargetingApiEndpoint, {
method: 'POST',
credentials: 'include'
}).then(r => r.json());
}
};
prGlobalPrivacyControlApi.signalHandlers = {
handlers: [],
addHandler: function (callback) {
let handler = {callback: callback, executed: false}
this.handlers.push(handler);
if (prGlobalPrivacyControlApi.isConfigured && prGlobalPrivacyControlApi.shouldApply()) {
handler.callback();
handler.executed = true;
}
},
processPendingHandlers: function () {
if (! prGlobalPrivacyControlApi.shouldApply()) {
return;
}
// Set cookie to indicate signal was handled
if (! prCookieService.get('pr-gpc-signal-handled')) {
prCookieService.set('pr-gpc-signal-handled', true, {expires: null});
}
this.handlers.forEach(function (handler) {
if (false === handler.executed) {
handler.callback();
handler.executed = true;
}
}.bind(this));
}
};
prGlobalPrivacyControlApi.cache = {
storageKey: 'pr_global_privacy_control_api_cache',
get: function (key) {
try {
let raw = sessionStorage.getItem(this.storageKey);
let cacheData = JSON.parse(raw);
return cacheData.hasOwnProperty(key) ? cacheData[key] : null;
} catch (e) {
return null;
}
},
set: function (key, value) {
try {
let cacheData = JSON.parse(sessionStorage.getItem(this.storageKey) || '{}');
cacheData[key] = value;
sessionStorage.setItem(this.storageKey, JSON.stringify(cacheData));
} catch (e) {}
},
};
prGlobalPrivacyControlApi.configure();
</script>