Files
att-consent/includes/class-att-consent.php
Steve Hanlon 6bf9f553de 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>
2026-02-16 05:45:42 +00:00

180 lines
4.5 KiB
PHP

<?php
/**
* Main plugin class.
*
* @package ATT_Consent
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ATT_Consent {
/**
* Singleton instance.
*
* @var ATT_Consent|null
*/
private static $instance = null;
/**
* Default settings.
*
* @var array
*/
private static $defaults = array(
// General.
'tracking_mode' => 'gtag',
'ga4_measurement_id' => '',
'gtm_container_id' => '',
'consent_mode' => 'advanced',
'banner_position' => 'bottom',
'consent_expiry' => 365,
// Appearance.
'banner_bg_color' => '#1a1a2e',
'banner_text_color' => '#ffffff',
'btn_accept_bg' => '#4CAF50',
'btn_accept_text' => '#ffffff',
'btn_reject_bg' => '#555555',
'btn_reject_text' => '#ffffff',
'btn_preferences_bg' => 'transparent',
'btn_preferences_text' => '#ffffff',
// Text / Labels.
'banner_heading' => 'We value your privacy',
'banner_message' => 'We use cookies to enhance your browsing experience, serve personalised content, and analyse our traffic. By clicking "Accept All", you consent to our use of cookies.',
'btn_accept_label' => 'Accept All',
'btn_reject_label' => 'Reject All',
'btn_preferences_label' => 'Manage Preferences',
'btn_save_label' => 'Save Preferences',
// Category descriptions.
'cat_necessary_desc' => 'These cookies are essential for the website to function and cannot be switched off.',
'cat_functional_desc' => 'These cookies enable personalised features and functionality.',
'cat_analytics_desc' => 'These cookies help us understand how visitors interact with our website.',
'cat_marketing_desc' => 'These cookies are used to deliver advertisements relevant to you.',
// Advanced.
'url_passthrough' => true,
'ads_data_redaction' => true,
'wait_for_update' => 500,
);
/**
* Get singleton instance.
*
* @return ATT_Consent
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
private function __construct() {
add_action( 'init', array( $this, 'load_textdomain' ) );
$this->load_dependencies();
}
/**
* Load plugin text domain.
*/
public function load_textdomain() {
load_plugin_textdomain( 'att-consent', false, dirname( ATT_CC_PLUGIN_BASENAME ) . '/languages' );
}
/**
* Load required files.
*/
private function load_dependencies() {
require_once ATT_CC_PLUGIN_DIR . 'includes/class-scripts-manager.php';
require_once ATT_CC_PLUGIN_DIR . 'includes/class-consent-api.php';
if ( is_admin() ) {
require_once ATT_CC_PLUGIN_DIR . 'includes/class-admin.php';
new ATT_Consent_Admin();
}
if ( ! is_admin() || wp_doing_ajax() ) {
require_once ATT_CC_PLUGIN_DIR . 'includes/class-frontend.php';
new ATT_Consent_Frontend();
}
new ATT_Consent_API();
}
/**
* Plugin activation.
*/
public static function activate() {
self::create_scripts_table();
// Set defaults if no settings exist yet.
if ( false === get_option( 'att_consent_settings' ) ) {
update_option( 'att_consent_settings', self::$defaults );
}
}
/**
* Plugin deactivation.
*/
public static function deactivate() {
// No-op. Cleanup happens in uninstall.php.
}
/**
* Create the custom scripts table.
*/
private static function create_scripts_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'att_cc_scripts';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
name varchar(200) NOT NULL DEFAULT '',
snippet longtext NOT NULL,
category varchar(50) NOT NULL DEFAULT 'analytics',
placement varchar(20) NOT NULL DEFAULT 'head',
status varchar(20) NOT NULL DEFAULT 'active',
priority int(11) NOT NULL DEFAULT 10,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY category (category),
KEY status (status)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
update_option( 'att_cc_db_version', ATT_CC_VERSION );
}
/**
* Get plugin settings merged with defaults.
*
* @return array
*/
public static function get_settings() {
$settings = get_option( 'att_consent_settings', array() );
return wp_parse_args( $settings, self::$defaults );
}
/**
* Get default settings.
*
* @return array
*/
public static function get_defaults() {
return self::$defaults;
}
}