Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
.idea
77 changes: 77 additions & 0 deletions assets/consent-api-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
jQuery(document).ready(function ($) {
var contentId = wpConsentExampleData.contentId;
var examplePluginContainers = $('#' + contentId + ' .category-container');

/**
* Listen for consent change events.
*/
console.log("load plugin example");
document.addEventListener("wp_listen_for_consent_change", function (e) {
console.log('listen for consent events');
var changedConsentCategory = e.detail;
console.log(changedConsentCategory);
examplePluginContainers.each(function () {
var container = $(this);
var consentCategory = container.data('consentcategory');
for (var key in changedConsentCategory) {
if (changedConsentCategory.hasOwnProperty(key)) {
if (key === consentCategory && changedConsentCategory[key] === 'allow') {
console.log("set " + consentCategory + " cookie on user actions");
activateConsent(consentCategory);
}
}
}
});
});

/**
* Do stuff as soon as the consent type is defined.
*/
$(document).on("wp_consent_type_defined", function (consentData) {
examplePluginContainers.each(function () {
var container = $(this);
var consentCategory = container.data('consentcategory');
activateMyCookies(consentCategory);
});
});

/**
* Function to activate consent for a specified category.
*
* @param {string} consentCategory - The consent category.
*/
function activateConsent(consentCategory) {
console.log("fire " + consentCategory);
$('#' + contentId + ' .' + consentCategory + '-content .no-consent-given').hide();
$('#' + contentId + ' .' + consentCategory + '-content .consent-given').show();
}

/**
* Function to handle consent-defined event.
*
* @param {string} consentCategory - The consent category.
*/
function activateMyCookies(consentCategory) {
if (wp_has_consent(consentCategory)) {
console.log("do " + consentCategory + " cookie stuff");
activateConsent(consentCategory);
} else {
console.log("no " + consentCategory + " cookies please");
}
}

// Check if we need to wait for the consent type to be set.
if (!window.waitfor_consent_hook) {
console.log("we don't have to wait for the consent type, we can check the consent level right away!");
examplePluginContainers.each(function () {
var container = $(this);
var consentCategory = container.data('consentcategory');
if (wp_has_consent(consentCategory)) {
activateConsent(consentCategory);
console.log("set " + consentCategory + " stuff now!");
} else {
console.log("No " + consentCategory + " stuff please!");
}
});
}
});
1 change: 1 addition & 0 deletions assets/consent-api-example.min.js

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

7 changes: 7 additions & 0 deletions assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.wp-consent-example .category-container .no-consent-given {
color: red;
}

.wp-consent-example .category-container .consent-given {
color: green;
}
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"require-dev": {
"szepeviktor/phpstan-wordpress": "^1.3",
"phpcompatibility/php-compatibility": "^9.3",
"designsecurity/progpilot": "^1.0",
"wp-coding-standards/wpcs": "^3.0",
"phpstan/extension-installer": "^1.3"
},
"scripts": {
"cs-report": "vendor/bin/phpcs --ignore=*/vendor/ --standard=WordPress --extensions=php ./ -s",
"php:lint:autofix": "vendor/bin/phpcbf --ignore=*/vendor/ --standard=WordPress --extensions=php ./",
"php:lint:report": "vendor/bin/phpcs --ignore=*/vendor/ --standard=WordPress --extensions=php ./ --report=summary; phpcs --ignore=*/vendor/ --standard=WordPress --extensions=php ./ --report=source",
"php-compatibility": "vendor/bin/phpcs -ps ./* --ignore=*/vendor/ --extensions=php --standard=PHPCompatibility --runtime-set testVersion 7.0-8.2",
"phpstan": "vendor/bin/phpstan analyse -c phpstan.neon",
"progpilot": "vendor/bin/progpilot --configuration configuration.yml",
"run-all-checks": [
"@composer cs-report",
"@composer php-compatibility",
"@composer phpstan",
"@composer progpilot"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
}
}
Loading