From a1b0e04ed74b7c35014e1948a0b68d322fb9fef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Tue, 29 Apr 2025 14:23:38 -0300 Subject: [PATCH 1/2] add support for onetrust --- assets/scripts/onetrust.js | 73 ++++++++++++++++++++++++++++++++++++++ dist/onetrust.asset.php | 1 + dist/onetrust.js | 1 + src/Admin.php | 13 +++++++ src/Frontend.php | 4 +++ src/Plugin.php | 10 ++++++ webpack.mix.js | 1 + 7 files changed, 103 insertions(+) create mode 100644 assets/scripts/onetrust.js create mode 100644 dist/onetrust.asset.php create mode 100644 dist/onetrust.js diff --git a/assets/scripts/onetrust.js b/assets/scripts/onetrust.js new file mode 100644 index 0000000..b9e86a3 --- /dev/null +++ b/assets/scripts/onetrust.js @@ -0,0 +1,73 @@ +/* eslint no-unused-vars:"off" */ +import { + AD_STORAGE_CONSENT, + ANALYTICS_STORAGE_CONSENT, + EVENT_CONSENT, + NECESSARY_STORAGE_CONSENT, + PREFERENCES_STORAGE_CONSENT, + } from './api'; + +/** + * @param {TCData} tcData + * @returns {ConsentData} + */ +function getOneTrustMappedConsentData(activeGroups) { + const hasNecessaryConsent = activeGroups.includes('C0001'); + const hasStatisticsConsent = activeGroups.includes('C0002'); // performance + const hasPreferencesConsent = activeGroups.includes('C0003'); // functional + const hasMarketingConsent = activeGroups.includes('C0004'); // targeting + + return { + consents: { + [NECESSARY_STORAGE_CONSENT]: hasNecessaryConsent, + [PREFERENCES_STORAGE_CONSENT]: hasPreferencesConsent, + [AD_STORAGE_CONSENT]: hasMarketingConsent, + [ANALYTICS_STORAGE_CONSENT]: hasStatisticsConsent, + }, + version: undefined, + } +} + +function getConsentData() { + return window.gdsCmp?.onetrustMappedConsents; +} + +function runEvent() { + window.dispatchEvent(new CustomEvent(EVENT_CONSENT)); + for (const [consent, value] of Object.entries(getConsentData().consents)) { + if (value) { + window.dispatchEvent(new CustomEvent(`${EVENT_CONSENT}.${consent}`)); + } + } +} + +window.gdsCmp = { + ...(window.gdsCmp || {}), + onetrustMappedConsents: { + version: undefined, + consents: {}, + }, + getConsentData, + show() { + window.Optanon?.ToggleInfoDisplay?.(); + }, + hide() { + window.Optanon?.Close?.(); + }, + withdraw() { + window.Optanon?.RejectAll?.(); + }, +} + +const originalOptanonWrapper = window.OptanonWrapper; +window.OptanonWrapper = function() { + if (typeof originalOptanonWrapper === 'function') { + originalOptanonWrapper(); + } + + if (typeof OptanonActiveGroups !== 'undefined') { + const activeGroups = OnetrustActiveGroups.split(',').filter(Boolean); + window.gdsCmp.onetrustMappedConsents = getOneTrustMappedConsentData(activeGroups); + runEvent(); + } +} diff --git a/dist/onetrust.asset.php b/dist/onetrust.asset.php new file mode 100644 index 0000000..07edcd2 --- /dev/null +++ b/dist/onetrust.asset.php @@ -0,0 +1 @@ + array(), 'version' => 'cd870bee1f597845'); diff --git a/dist/onetrust.js b/dist/onetrust.js new file mode 100644 index 0000000..01c6637 --- /dev/null +++ b/dist/onetrust.js @@ -0,0 +1 @@ +(()=>{"use strict";function t(n){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(n)}function n(n){var e=function(n,e){if("object"!=t(n)||!n)return n;var o=n[Symbol.toPrimitive];if(void 0!==o){var r=o.call(n,e||"default");if("object"!=t(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(n)}(n,"string");return"symbol"==t(e)?e:e+""}function e(t,e,o){return(e=n(e))in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}const o="gds-cmp.consent",r="necessary",i="preferences",s="marketing",c="statistics";function u(t,n){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);n&&(o=o.filter((function(n){return Object.getOwnPropertyDescriptor(t,n).enumerable}))),e.push.apply(e,o)}return e}function p(t){for(var n=1;nsettings_title . '_onetrust', + __('OneTrust', $this->text_domain), + [$this, 'renderCheckbox'], + $this->settings_title, + $this->settings_title . '_general', + [ + 'key' => 'onetrust', + 'label' => __('Add integration for OneTrust API', $this->text_domain), + ] + ); + add_settings_section( $this->settings_title . '_data_posts', __('Posts Data', $this->text_domain), @@ -483,6 +495,7 @@ public function getDefaultSettings(): array 'banner_off' => 0, 'embeds_require_consent' => 0, 'tcfapi' => 0, + 'onetrust' => 0, 'incl_post_type' => 1, 'incl_categories' => 1, 'incl_tags' => 1, diff --git a/src/Frontend.php b/src/Frontend.php index d08afe6..1953971 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -81,6 +81,10 @@ public function enqueueAssets(): void if ($this->settings['tcfapi'] ?? false) { wp_enqueue_script("{$this->name}/tcfapi/js"); } + + if ($this->settings['onetrust'] ?? false) { + wp_enqueue_script("{$this->name}/onetrust/js"); + } } public function consentManager(): void diff --git a/src/Plugin.php b/src/Plugin.php index 9b71e66..1fc921b 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -130,6 +130,16 @@ public function registerAssets(): void ); // Support async loading if a plugin implements it. wp_script_add_data("{$this->name}/tcfapi/js", 'async', true); + + wp_register_script( + "{$this->name}/onetrust/js", + "{$this->url}/dist/onetrust.js", + [], + filemtime($this->path . '/dist/onetrust.js'), + version_compare($GLOBALS['wp_version'], '6.3') >= 0 ? ['strategy' => 'async'] : false, + ); + // Support async loading if a plugin implements it. + wp_script_add_data("{$this->name}/onetrust/js", 'async', true); } public function blockEditorAssets(): void diff --git a/webpack.mix.js b/webpack.mix.js index f621c0c..f398bff 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -5,4 +5,5 @@ mix.sass('assets/styles/main.scss', 'dist/') .js('assets/scripts/main.js', 'dist/') .js('assets/scripts/inline.js', 'dist/') .js('assets/scripts/tcfapi.js', 'dist/') + .js('assets/scripts/onetrust.js', 'dist/') .blocks('assets/scripts/editor.js', 'dist/'); From 916e3bd6a70a6da810f1723ca390c9ae35097ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Tue, 29 Apr 2025 14:33:42 -0300 Subject: [PATCH 2/2] Change youtube and google maps consent to marketing by default --- src/Integrations/Embeds.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Integrations/Embeds.php b/src/Integrations/Embeds.php index 153c525..518e090 100644 --- a/src/Integrations/Embeds.php +++ b/src/Integrations/Embeds.php @@ -47,8 +47,8 @@ function (array $matches) use ($description, $button) { [$tag, $element, $attributes, $innerContent] = $matches; $consents = match (true) { - str_contains($tag, 'youtube') => [Consent::MARKETING, Consent::STATISTICS], - str_contains($tag, 'google.com/maps') => [Consent::MARKETING, Consent::STATISTICS], + str_contains($tag, 'youtube') => [Consent::MARKETING], + str_contains($tag, 'google.com/maps') => [Consent::MARKETING], default => [], };