$ad_id, 'type' => isset( $data['type'] ) ? $data['type'] : 'impression', 'date' => current_time( 'Y-m-d' ), ]; } return $data; } /** * Inject TCF 2.0 consent listener. * This is the FREE version - basic TCF 2.0 support. */ public static function inject_consent_listener() { if ( ! self::is_enabled( 'gdpr_consent_mode' ) ) { return; } $settings = self::get_settings(); $cookie_name = isset( $settings['consent_cookie_name'] ) ? (string) $settings['consent_cookie_name'] : ''; $cookie_value = isset( $settings['consent_cookie_value'] ) ? (string) $settings['consent_cookie_value'] : ''; $cookie_regex = ! empty( $cookie_name ) ? preg_quote( $cookie_name, '/' ) : ''; $script = sprintf( "(function() { 'use strict'; var cookieName = %s; var cookieValue = %s; var cookieRegex = %s; window.advajraConsent = { hasConsent: false, pending: [], init: function() { var self = this; if (self.checkExistingConsent()) { self.hasConsent = true; return; } if (window.__tcfapi) { window.__tcfapi('addEventListener', 2, function(tcData, success) { if (success && (tcData.eventStatus === 'useractioncomplete' || tcData.eventStatus === 'tcloaded')) { if (tcData.purpose && tcData.purpose.consents) { if (tcData.purpose.consents[1] || tcData.purpose.consents[3]) { self.grantConsent(); } } } }); } if (cookieName) { self.watchCookie(cookieName, cookieValue); } }, checkExistingConsent: function() { var cookie; if (!cookieName) { return false; } cookie = document.cookie.match(new RegExp(cookieRegex + '=([^;]+)')); if (cookie) { return cookieValue ? cookie[1].indexOf(cookieValue) !== -1 : true; } return false; }, watchCookie: function(name, value) { var self = this; var check = function() { var cookie = document.cookie.match(new RegExp(name + '=([^;]+)')); if (cookie) { if (!value || cookie[1].indexOf(value) !== -1) { self.grantConsent(); return true; } } return false; }; var interval = setInterval(function() { if (check() || self.hasConsent) { clearInterval(interval); } }, 500); setTimeout(function() { clearInterval(interval); }, 30000); }, grantConsent: function() { if (this.hasConsent) return; this.hasConsent = true; if (this.pending.length && window.advajraTrack) { this.pending.forEach(function(event) { window.advajraTrack(event.adId, event.type); }); this.pending = []; } document.dispatchEvent(new CustomEvent('advajra_consent_granted')); }, queue: function(adId, type) { this.pending.push({ adId: adId, type: type }); }, check: function() { return this.hasConsent; } }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function() { window.advajraConsent.init(); }); } else { window.advajraConsent.init(); } })();", wp_json_encode( $cookie_name ), wp_json_encode( $cookie_value ), wp_json_encode( $cookie_regex ) ); wp_add_inline_script( 'advajra-tracking', $script, 'before' ); } /** * Register REST routes. */ public static function register_rest_routes() { register_rest_route( 'advajra/v1', '/privacy-status', [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_privacy_status' ], 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, ] ); } /** * Get privacy status for admin UI. * * @return \WP_REST_Response */ public static function get_privacy_status() { $settings = self::get_settings(); $response = [ 'consent_mode_enabled' => self::is_enabled( 'gdpr_consent_mode' ), 'privacy_safe_enabled' => self::is_enabled( 'privacy_safe_mode' ), 'manual_config' => [ 'cookie_name' => isset( $settings['consent_cookie_name'] ) ? $settings['consent_cookie_name'] : '', 'cookie_value' => isset( $settings['consent_cookie_value'] ) ? $settings['consent_cookie_value'] : '', ], 'detected_cmp' => null, 'is_pro' => false, ]; $response = apply_filters( 'advajra_privacy_status', $response ); return rest_ensure_response( $response ); } }