Initial commit: ATT Consent plugin v1.0.0
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>
This commit is contained in:
51
includes/class-consent-api.php
Normal file
51
includes/class-consent-api.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user