Google Consent Mode v2 cookie consent plugin with session attribution preservation, custom script management, and gtag.js/GTM support. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
|
/**
|
|
* WP Consent API integration.
|
|
*
|
|
* Registers with the WP Consent API plugin (if active) so other plugins
|
|
* can query consent status via wp_has_consent().
|
|
*
|
|
* @package ATT_Consent
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
class ATT_Consent_API {
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct() {
|
|
// Register as a compliant plugin with WP Consent API.
|
|
add_filter( 'wp_consent_api_registered_' . ATT_CC_PLUGIN_BASENAME, '__return_true' );
|
|
|
|
// Map our categories to WP Consent API categories.
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_consent_api_bridge' ), 20 );
|
|
}
|
|
|
|
/**
|
|
* Enqueue a small bridge script that syncs ATT Consent with WP Consent API.
|
|
*/
|
|
public function enqueue_consent_api_bridge() {
|
|
// Only load if WP Consent API is active.
|
|
if ( ! function_exists( 'wp_has_consent' ) ) {
|
|
return;
|
|
}
|
|
|
|
$script = "
|
|
document.addEventListener('att_consent_update', function(e) {
|
|
var c = e.detail;
|
|
if (typeof wp_set_consent === 'function') {
|
|
wp_set_consent('functional', c.functional ? 'allow' : 'deny');
|
|
wp_set_consent('statistics', c.analytics ? 'allow' : 'deny');
|
|
wp_set_consent('marketing', c.marketing ? 'allow' : 'deny');
|
|
wp_set_consent('preferences', c.functional ? 'allow' : 'deny');
|
|
}
|
|
});
|
|
";
|
|
|
|
wp_add_inline_script( 'att-consent-manager', $script, 'after' );
|
|
}
|
|
}
|