Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions assets/scripts/onetrust.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions dist/onetrust.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => 'cd870bee1f597845');
1 change: 1 addition & 0 deletions dist/onetrust.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ public function registerSettings()
]
);

add_settings_field(
$this->settings_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),
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Integrations/Embeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [],
};

Expand Down
10 changes: 10 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/');