'no', 'mode' => 'full', 'eea_only' => 'yes', 'default_state' => 'denied', 'title' => '', 'message' => '', 'light_message' => '', 'layout' => 'bar', 'policy_version' => 1, 'desc_necessary' => '', 'desc_functional' => '', 'desc_analytics' => '', 'desc_marketing' => '', 'reopen_enabled' => 'yes', 'reopen_label' => '', 'reopen_pos_desktop' => 'left', 'reopen_x_desktop' => 86, 'reopen_bottom_desktop' => 20, 'reopen_pos_mobile' => 'left', 'reopen_x_mobile' => 86, 'reopen_bottom_mobile' => 20, 'cookie_scan' => 'no', 'cookie_scan_days' => 30, 'logo' => '', 'bar_bg' => '', 'bar_text_color' => '', 'bar_text_size' => '18px', 'bar_desc_color' => '', 'bar_desc_size' => '14px', 'bar_link_color' => '', 'btn_accept_bg' => '', 'btn_accept_fg' => '', 'btn_accept_border' => '', 'btn_accept_bg_hover' => '', 'btn_accept_fg_hover' => '', 'btn_accept_border_hover' => '', 'btn_decline_bg' => '', 'btn_decline_fg' => '', 'btn_decline_border' => '', 'btn_decline_bg_hover' => '', 'btn_decline_fg_hover' => '', 'btn_decline_border_hover' => '', 'btn_customize_bg' => '', 'btn_customize_fg' => '', 'btn_customize_border' => '', 'btn_customize_bg_hover' => '', 'btn_customize_fg_hover' => '', 'btn_customize_border_hover' => '', 'cookie_overrides' => array(), ), $o ) ) ); } const SHIPPED_BUTTONS = array( 'accept' => array( 'bg' => '#111827', 'fg' => '#ffffff', 'border' => '#111827', 'bg_hover' => '#374151', 'fg_hover' => '#ffffff', 'border_hover' => '#374151' ), 'decline' => array( 'bg' => '#ffffff', 'fg' => '#111827', 'border' => '#111827', 'bg_hover' => '#f3f4f6', 'fg_hover' => '#111827', 'border_hover' => '#111827' ), 'customize' => array( 'bg' => 'transparent', 'fg' => '#374151', 'border' => '#d1d5db', 'bg_hover' => '#f9fafb', 'fg_hover' => '#111827', 'border_hover' => '#9ca3af' ), ); const SHIPPED_BAR = array( 'bar_bg' => '#1f2937', 'bar_text_color' => '#ffffff', 'bar_desc_color' => '#d1d5db', 'bar_link_color' => '#fbbf24', ); private static function theme_bar( array $s ) { foreach ( self::SHIPPED_BAR as $key => $value ) { if ( isset( $s[ $key ] ) && 0 === strcasecmp( trim( (string) $s[ $key ] ), $value ) ) { $s[ $key ] = ''; } } return $s; } private static function theme_buttons( array $s ) { foreach ( self::SHIPPED_BUTTONS as $btn => $shipped ) { $picked = false; foreach ( $shipped as $field => $value ) { $v = strtolower( trim( (string) ( isset( $s[ 'btn_' . $btn . '_' . $field ] ) ? $s[ 'btn_' . $btn . '_' . $field ] : '' ) ) ); if ( '' !== $v && $value !== $v ) { $picked = true; break; } } foreach ( $shipped as $field => $value ) { $key = 'btn_' . $btn . '_' . $field; $v = trim( (string) ( isset( $s[ $key ] ) ? $s[ $key ] : '' ) ); $s[ $key ] = ( $picked && '' !== $v && strtolower( $v ) !== $value ) ? $s[ $key ] : ''; } } return $s; } private static function policy_version() { $v = (int) self::settings()['policy_version']; return $v > 0 ? $v : self::POLICY_VERSION; } public static function mode() { $m = (string) self::settings()['mode']; return in_array( $m, array( 'full', 'light', 'light_eea_full' ), true ) ? $m : 'full'; } private static function mode_here() { $m = self::mode(); if ( 'light_eea_full' === $m ) { return self::is_eea() ? 'full' : 'light'; } return $m; } private static function gpc() { return 'full' !== self::mode() && isset( $_SERVER['HTTP_SEC_GPC'] ) && '1' === trim( (string) wp_unslash( $_SERVER['HTTP_SEC_GPC'] ) ); } public static function allows( $category ) { if ( 'necessary' === $category ) { return true; } if ( ( 'marketing' === $category || 'personalize' === $category ) && self::gpc() ) { return false; } $state = self::cookie_state(); if ( null !== $state ) { return ! empty( $state[ $category ] ); } if ( 'personalize' === $category ) { return false; } if ( 'light' === self::mode_here() ) { return true; } $s = self::settings(); if ( 'yes' === $s['eea_only'] && self::is_eea() ) { return false; } return 'granted' === $s['default_state']; } public static function has_choice() { return null !== self::cookie_state(); } public static function banner_active() { if ( 'yes' === (string) self::settings()['enabled'] ) { return true; } if ( function_exists( 'rooster_setting' ) && 'theme' === (string) rooster_setting( 'general', 'consent_banner', 'theme' ) ) { return true; } return '' !== self::external_cmp_active(); } public static function print_rule() { $rule = class_exists( 'AIEO_Session_Tracker' ) && method_exists( 'AIEO_Session_Tracker', 'consent_required_rule' ) && AIEO_Session_Tracker::consent_required_rule(); $cfg = array( 'banner' => self::banner_active(), 'light' => 'yes' === (string) self::settings()['enabled'] && 'light' === self::mode(), 'rule' => $rule, 'def' => '1' === (string) get_option( 'aieo_session_tracking_default_consent', '1' ), ); $js = '(function(){var C=' . wp_json_encode( $cfg ) . ';' . 'function ck(n){var m=document.cookie.match(new RegExp("(?:^|; )"+n+"=([^;]*)"));if(!m){return null;}try{return decodeURIComponent(m[1]);}catch(e){return m[1];}}' . 'function rec(){var r=ck("rooster_consent");if(r===null){try{r=localStorage.getItem("rooster_consent");}catch(e){}}if(!r){return null;}try{var o=JSON.parse(r);return o&&typeof o==="object"?o:null;}catch(e){return null;}}' . 'window.aieoConsentFor=function(c){try{if("necessary"===c||(!C.banner&&!C.rule)){return true;}' . 'var r=rec();if(r){return !!r[c];}' . 'var a=ck("aieo_consent");if(a!==null){try{var j=JSON.parse(a);if(j&&typeof j==="object"&&c in j){return !!j[c];}}catch(e){}}' . 'var y=ck("cookieyes-consent");if(y!==null){var k="marketing"===c?"advertisement":c,o=null;try{o=JSON.parse(y);}catch(e){}if(o&&typeof o==="object"){return o[k]==="yes";}var m=y.match(new RegExp("(?:^|,)\\\\s*"+k+":([^,]*)"));return !!m&&m[1].trim()==="yes";}' . 'if(ck("cookie_notice_accepted")==="true"){return true;}' . 'var z=ck("cmplz_"+("analytics"===c?"statistics":c));if(z!==null){return z==="allow";}' . 'if(C.light){return !("marketing"===c&&navigator.globalPrivacyControl===true);}' . 'return C.banner?false:!!C.def;}catch(e){return false;}};' . 'window.aieoOnConsent=function(cb){var f=function(){try{cb();}catch(e){}};window.addEventListener("rooster:consent:changed",f);document.addEventListener("aieo:consent-resolved",f);};' . '})();'; echo '' . "\n"; } public static function is_enabled_here() { $s = self::settings(); if ( 'yes' !== $s['enabled'] ) { return false; } if ( 'full' !== self::mode() ) { return true; } if ( 'yes' === $s['eea_only'] && ! self::is_eea() ) { return false; } return true; } public static function external_cmp_active() { static $cached = null; if ( null !== $cached ) { return $cached; } $found = ''; if ( defined( 'CLI_PLUGIN_VERSION' ) || defined( 'CKY_PLUGIN_VERSION' ) ) { $found = 'CookieYes'; } elseif ( function_exists( 'cmplz_has_consent' ) ) { $found = 'Complianz'; } elseif ( class_exists( 'Cookie_Notice', false ) ) { $found = 'Cookie Notice'; } if ( '' === $found ) { $known = array( 'cookie-law-info' => 'CookieYes', 'cookie-yes' => 'CookieYes', 'cookie-notice' => 'Cookie Notice', 'complianz-gdpr' => 'Complianz', 'complianz-gdpr-premium' => 'Complianz', ); $active = (array) get_option( 'active_plugins', array() ); if ( is_multisite() ) { $active = array_merge( $active, array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) ); } foreach ( $active as $basename ) { $dir = strtok( (string) $basename, '/' ); if ( isset( $known[ $dir ] ) ) { $found = $known[ $dir ]; break; } } } $cached = (string) apply_filters( 'aieo_external_cmp_active', $found ); return $cached; } public static function has_cmp_conflict() { $s = self::settings(); return '' !== self::external_cmp_active() && 'yes' === $s['enabled']; } private static function shared_cookie_state() { if ( empty( $_COOKIE['rooster_consent'] ) ) { return null; } $raw = json_decode( stripslashes( sanitize_text_field( wp_unslash( $_COOKIE['rooster_consent'] ) ) ), true ); if ( ! is_array( $raw ) || empty( $raw['decidedAt'] ) ) { return null; } return array( 'functional' => ! empty( $raw['functional'] ), 'analytics' => ! empty( $raw['analytics'] ), 'marketing' => ! empty( $raw['marketing'] ), 'personalize' => false, ); } private static function cookie_state() { if ( empty( $_COOKIE[ self::COOKIE ] ) ) { return self::shared_cookie_state(); } $raw = json_decode( stripslashes( sanitize_text_field( wp_unslash( $_COOKIE[ self::COOKIE ] ) ) ), true ); if ( ! is_array( $raw ) ) { return null; } $stored_v = isset( $raw['v'] ) ? (int) $raw['v'] : 0; if ( $stored_v < self::policy_version() ) { return null; } return array( 'functional' => ! empty( $raw['functional'] ), 'analytics' => ! empty( $raw['analytics'] ), 'marketing' => ! empty( $raw['marketing'] ), 'personalize' => ! empty( $raw['personalize'] ), ); } private static function is_eea() { $cc = ''; foreach ( array( 'HTTP_CF_IPCOUNTRY', 'GEOIP_COUNTRY_CODE', 'HTTP_X_COUNTRY_CODE' ) as $h ) { if ( ! empty( $_SERVER[ $h ] ) ) { $cc = strtoupper( substr( sanitize_text_field( wp_unslash( $_SERVER[ $h ] ) ), 0, 2 ) ); break; } } if ( '' === $cc ) { return true; } $eea = array( 'AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES','SE','IS','LI','NO','GB','CH' ); return in_array( $cc, $eea, true ); } public static function ads_consent_state() { if ( self::has_choice() ) { return self::allows( 'marketing' ) ? 'granted' : 'denied'; } return 'unspecified'; } public static function ads_consent_for_order( $order ) { if ( is_a( $order, 'WC_Order' ) ) { $m = (string) $order->get_meta( '_aieo_ads_consent' ); if ( 'granted' === $m || 'denied' === $m || 'unspecified' === $m ) { return $m; } } return self::ads_consent_state(); } public function capture_ads_consent( $order ) { if ( is_numeric( $order ) && function_exists( 'wc_get_order' ) ) { $order = wc_get_order( (int) $order ); } if ( ! is_a( $order, 'WC_Order' ) ) { return; } if ( '' !== (string) $order->get_meta( '_aieo_ads_consent' ) ) { return; } $order->update_meta_data( '_aieo_ads_consent', self::ads_consent_state() ); if ( 'woocommerce_checkout_create_order' !== current_action() ) { $order->save(); } } public function register() { static $registered = false; if ( $registered ) { return; } $registered = true; add_action( 'admin_init', array( $this, 'maybe_install_log' ) ); add_action( 'admin_init', array( $this, 'maybe_install_cookies_table' ) ); add_action( 'wp_ajax_aieo_consent_log', array( $this, 'ajax_log' ) ); add_action( 'wp_ajax_nopriv_aieo_consent_log', array( $this, 'ajax_log' ) ); add_action( 'wp_ajax_aieo_consent_scan', array( $this, 'ajax_scan' ) ); add_action( 'wp_ajax_nopriv_aieo_consent_scan', array( $this, 'ajax_scan' ) ); add_filter( 'cron_schedules', array( __CLASS__, 'cron_schedules' ) ); add_action( self::CRON_RESCAN, array( __CLASS__, 'cron_rescan' ) ); add_action( 'init', array( __CLASS__, 'schedule_rescan' ), 20 ); add_action( 'admin_footer', array( __CLASS__, 'maybe_auto_rescan' ) ); add_action( 'wp_head', array( __CLASS__, 'forcescan_load_sdks' ), 0 ); add_action( 'woocommerce_checkout_create_order', array( $this, 'capture_ads_consent' ), 30, 1 ); add_action( 'woocommerce_store_api_checkout_order_processed', array( $this, 'capture_ads_consent' ), 30, 1 ); add_filter( 'rocket_delay_js_exclusions', array( $this, 'rocket_js_exclusions' ) ); add_filter( 'rocket_exclude_defer_js', array( $this, 'rocket_js_exclusions' ) ); add_filter( 'rocket_defer_inline_exclusions', array( $this, 'rocket_js_exclusions' ) ); add_filter( 'rocket_rucss_inline_atts_exclusions', array( $this, 'rocket_css_atts_exclusions' ) ); add_filter( 'rocket_rucss_inline_content_exclusions', array( $this, 'rocket_css_content_exclusions' ) ); add_filter( 'rocket_lrc_exclusions', array( $this, 'rocket_lrc_exclusions' ) ); add_shortcode( 'aieo_privacy_choices', array( __CLASS__, 'privacy_choices_shortcode' ) ); add_action( 'wp_head', array( __CLASS__, 'print_rule' ), 0 ); if ( 'yes' !== self::settings()['enabled'] ) { return; } if ( 'full' !== self::mode() ) { add_filter( 'rooster/footer/links', array( __CLASS__, 'footer_links' ), 10, 2 ); add_filter( 'render_block_core/navigation', array( __CLASS__, 'footer_menu_link' ), 10, 2 ); } add_action( 'wp_head', array( $this, 'consent_mode_default' ), 1 ); add_action( 'wp_footer', array( $this, 'render_banner' ), 5 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'rooster/third_party/handles', static function ( $handles ) { $handles = is_array( $handles ) ? $handles : array(); $handles['aieo-consent'] = array( 'trigger' => 'human' ); return $handles; } ); if ( 'yes' === self::settings()['reopen_enabled'] ) { add_action( 'wp_footer', array( $this, 'render_reopen' ), 6 ); } } const CHOICES_HASH = '#aieo-privacy-choices'; private static function choices_label() { return __( 'Your privacy choices', 'ai-eshop-optimizer' ); } public static function privacy_choices_shortcode() { if ( 'yes' !== self::settings()['enabled'] || 'full' === self::mode() ) { return ''; } return '' . esc_html( self::choices_label() ) . ''; } public static function footer_links( $links, $area ) { $links = (array) $links; if ( 'legal' === $area ) { $links[] = array( self::choices_label(), self::CHOICES_HASH ); } return $links; } public static function footer_menu_link( $html, $block ) { $ref = isset( $block['attrs']['ref'] ) ? (int) $block['attrs']['ref'] : 0; if ( ! $ref || 'footer-legal' !== get_post_field( 'post_name', $ref ) || false !== strpos( $html, self::CHOICES_HASH ) ) { return $html; } $at = strrpos( $html, '' ); if ( false === $at ) { return $html; } $item = ''; return substr( $html, 0, $at ) . $item . substr( $html, $at ); } public function rocket_js_exclusions( $excluded ) { $excluded = is_array( $excluded ) ? $excluded : array(); foreach ( array( 'aieoConsent', 'aieo-cmp', 'aieo-consent-mode', 'aieo-consent-rule', 'aieo-attribution-client', 'aieo-session-tracker', 'aieo-captcha', 'aieo-survey', 'aieo-controls', 'recaptcha', ) as $needle ) { $excluded[] = $needle; } return $excluded; } public function rocket_css_atts_exclusions( $exclusions ) { $exclusions = is_array( $exclusions ) ? $exclusions : array(); $exclusions[] = 'aieo-cmp-style'; return $exclusions; } public function rocket_css_content_exclusions( $exclusions ) { $exclusions = is_array( $exclusions ) ? $exclusions : array(); $exclusions[] = '.aieo-cmp'; return $exclusions; } public function rocket_lrc_exclusions( $exclusions ) { $exclusions = is_array( $exclusions ) ? $exclusions : array(); foreach ( array( 'id="aieo-consent-banner"', 'id="aieo-cmp-overlay"', 'id="aieo-cmp-reopen"', 'aieo-cmp', ) as $needle ) { $exclusions[] = $needle; } return $exclusions; } public static function log_table() { global $wpdb; return $wpdb->prefix . 'aieo_consent_log'; } const LOG_SCHEMA_OPTION = 'aieo_consent_log_schema'; const LOG_SCHEMA_VERSION = '2026-11-16a'; public function maybe_install_log() { self::install_log_schema(); } public static function install_log_schema() { global $wpdb; if ( get_option( self::LOG_SCHEMA_OPTION ) === self::LOG_SCHEMA_VERSION ) { return true; } if ( class_exists( 'AIEO_Schema_Lock' ) && ! AIEO_Schema_Lock::acquire( self::LOG_SCHEMA_OPTION ) ) { return false; } $t = self::log_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { $cc = function_exists( 'aieo_get_charset_collate' ) ? aieo_get_charset_collate() : $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS `{$t}` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `session_id` varchar(64) DEFAULT NULL, `user_id` bigint unsigned NOT NULL DEFAULT 0, `functional` tinyint(1) NOT NULL DEFAULT 0, `analytics` tinyint(1) NOT NULL DEFAULT 0, `marketing` tinyint(1) NOT NULL DEFAULT 0, `policy_version` smallint unsigned NOT NULL DEFAULT 0, `ip_hash` varchar(64) DEFAULT NULL, `ua_hash` varchar(64) DEFAULT NULL, `source` varchar(24) NOT NULL DEFAULT 'banner', `created_at` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `session_id` (`session_id`), KEY `created_at` (`created_at`), KEY `user_id` (`user_id`, `id`) ) {$cc};"; if ( function_exists( 'aieo_dbdelta' ) ) { aieo_dbdelta( $sql ); } else { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } } elseif ( ! self::log_has_user_key() ) { $wpdb->query( "ALTER TABLE `{$t}` ADD KEY `user_id` (`user_id`, `id`)" ); } if ( ! self::log_has_user_key() ) { return false; } update_option( self::LOG_SCHEMA_OPTION, self::LOG_SCHEMA_VERSION, false ); return true; } private static function log_has_user_key() { global $wpdb; return (bool) $wpdb->get_var( $wpdb->prepare( 'SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s AND INDEX_NAME = %s LIMIT 1', self::log_table(), 'user_id' ) ); } public static function keeps_choices() { return 'yes' === (string) self::settings()['enabled']; } public static function latest_for_user( int $user_id ) { $row = self::latest_row( 'user_id', $user_id ); return null === $row ? null : self::bits_of( $row ); } public static function latest_for_visitor( string $visitor ) { global $wpdb; $visitor = substr( trim( $visitor ), 0, 64 ); $guest = self::latest_row( 'session_id', $visitor, true, array( 0 ) ); if ( null === $guest ) { return null; } $bits = self::bits_of( $guest ); $after = $wpdb->get_row( $wpdb->prepare( 'SELECT MIN(`analytics`) AS `analytics`, MIN(`marketing`) AS `marketing`, COUNT(*) AS `n` FROM `' . self::log_table() . '` WHERE `session_id` = %s AND `user_id` <> 0 AND `id` > %d', $visitor, (int) $guest['id'] ), ARRAY_A ); if ( is_array( $after ) && (int) $after['n'] > 0 ) { $bits &= self::bits_of( $after ); } return $bits; } public static function latest_for_person( int $user_id, string $visitor ) { $visitor = substr( trim( $visitor ), 0, 64 ); if ( $user_id <= 0 ) { return '' === $visitor ? null : self::latest_for_visitor( $visitor ); } $account = self::latest_row( 'user_id', $user_id, false ); $browser = '' !== $visitor ? self::latest_row( 'session_id', $visitor, false, array( 0, $user_id ) ) : null; $current = static function ( $row ) { return (int) $row['policy_version'] >= self::policy_version(); }; if ( null === $account ) { return null !== $browser && $current( $browser ) ? self::bits_of( $browser ) : null; } if ( ! $current( $account ) ) { return null; } if ( null === $browser || (int) $browser['user_id'] === $user_id || (int) $browser['id'] < (int) $account['id'] ) { return self::bits_of( $account ); } return $current( $browser ) ? ( self::bits_of( $account ) & self::bits_of( $browser ) ) : null; } private static function latest_row( $column, $value, $policy_checked = true, $users = null ) { global $wpdb; if ( ( 'user_id' === $column ? (int) $value <= 0 : '' === (string) $value ) || ! self::keeps_choices() ) { return null; } static $exists = null; $t = self::log_table(); if ( null === $exists ) { $exists = (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t; } if ( ! $exists ) { return null; } $where = 'user_id' === $column ? $wpdb->prepare( '`user_id` = %d', (int) $value ) : $wpdb->prepare( '`session_id` = %s', (string) $value ); if ( 'session_id' === $column && is_array( $users ) && $users ) { $where .= ' AND `user_id` IN (' . implode( ',', array_map( 'absint', $users ) ) . ')'; } $row = $wpdb->get_row( "SELECT `id`, `user_id`, `analytics`, `marketing`, `policy_version` FROM `{$t}` WHERE {$where} ORDER BY `id` DESC LIMIT 1", ARRAY_A ); if ( ! is_array( $row ) || ( $policy_checked && (int) $row['policy_version'] < self::policy_version() ) ) { return null; } return $row; } private static function bits_of( array $row ) { $analytics = defined( 'AIEO_Signal_Envelope::ANALYTICS' ) ? (int) constant( 'AIEO_Signal_Envelope::ANALYTICS' ) : 1; $marketing = defined( 'AIEO_Signal_Envelope::MARKETING' ) ? (int) constant( 'AIEO_Signal_Envelope::MARKETING' ) : 2; return ( empty( $row['analytics'] ) ? 0 : $analytics ) | ( empty( $row['marketing'] ) ? 0 : $marketing ); } public function ajax_log() { global $wpdb; $t = self::log_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { wp_send_json_success(); } $sid = ''; foreach ( array( 'aieo_attr_sid', 'aieo_session_id' ) as $ck ) { if ( ! empty( $_COOKIE[ $ck ] ) ) { $raw = is_string( $_COOKIE[ $ck ] ) ? trim( wp_unslash( $_COOKIE[ $ck ] ) ) : ''; $sid = class_exists( 'AIEO_Signal_Envelope' ) ? AIEO_Signal_Envelope::visitor_id( $raw ) : ( 1 === preg_match( '/^[A-Za-z0-9_-]{1,64}$/D', $raw ) ? $raw : '' ); if ( '' !== $sid ) { break; } } } $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; $source = isset( $_POST['source'] ) ? sanitize_key( wp_unslash( $_POST['source'] ) ) : 'banner'; if ( '' === $source ) { $source = 'banner'; } $wpdb->insert( $t, array( 'session_id' => '' !== $sid ? $sid : null, 'user_id' => get_current_user_id(), 'functional' => ! empty( $_POST['functional'] ) ? 1 : 0, 'analytics' => ! empty( $_POST['analytics'] ) ? 1 : 0, 'marketing' => ! empty( $_POST['marketing'] ) ? 1 : 0, 'policy_version' => self::policy_version(), 'ip_hash' => '' !== $ip ? hash( 'sha256', $ip ) : null, 'ua_hash' => '' !== $ua ? hash( 'sha256', $ua ) : null, 'source' => substr( $source, 0, 24 ), 'created_at' => current_time( 'mysql', true ), ) ); wp_send_json_success(); } public static function cookies_table() { global $wpdb; return $wpdb->prefix . 'aieo_consent_cookies'; } public function maybe_install_cookies_table() { global $wpdb; $t = self::cookies_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t ) { return; } $cc = function_exists( 'aieo_get_charset_collate' ) ? aieo_get_charset_collate() : $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS `{$t}` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `name` varchar(191) NOT NULL, `service` varchar(120) DEFAULT NULL, `category` varchar(20) DEFAULT NULL, `description` varchar(255) DEFAULT NULL, `status` varchar(16) NOT NULL DEFAULT 'auto', `hits` bigint unsigned NOT NULL DEFAULT 0, `pre_consent` tinyint(1) NOT NULL DEFAULT 0, `first_seen` datetime DEFAULT NULL, `last_seen` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`), KEY `category` (`category`), KEY `status` (`status`) ) {$cc};"; if ( function_exists( 'aieo_dbdelta' ) ) { aieo_dbdelta( $sql ); } else { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } } public static function classify_cookie( $name ) { $name = (string) $name; if ( '' === $name ) { return null; } $overrides = self::settings()['cookie_overrides']; if ( is_array( $overrides ) && ! empty( $overrides ) ) { $best = null; $len = -1; foreach ( $overrides as $pattern => $meta ) { $pattern = (string) $pattern; if ( self::pattern_matches( $pattern, $name ) && strlen( $pattern ) > $len ) { $best = $meta; $len = strlen( $pattern ); } } if ( is_array( $best ) ) { return array( 'service' => isset( $best['service'] ) ? (string) $best['service'] : '', 'category' => isset( $best['category'] ) ? (string) $best['category'] : '', 'description' => '', ); } } if ( ! function_exists( 'aieo_consent_known_cookies' ) ) { require_once __DIR__ . '/data/known-cookies.php'; } $best = null; $score = -1; foreach ( aieo_consent_known_cookies() as $row ) { $pattern = (string) $row[0]; $match = (string) $row[1]; $hit = ( 'exact' === $match ) ? ( $name === $pattern ) : ( 0 === strpos( $name, $pattern ) ); if ( ! $hit ) { continue; } $s = ( strlen( $pattern ) * 2 ) + ( 'exact' === $match ? 1 : 0 ); if ( $s > $score ) { $score = $s; $best = $row; } } if ( is_array( $best ) ) { return array( 'service' => $best[2], 'category' => $best[3], 'description' => $best[4] ); } return null; } private static function pattern_matches( $pattern, $name ) { if ( '' === $pattern ) { return false; } if ( '*' === substr( $pattern, -1 ) ) { return 0 === strpos( $name, substr( $pattern, 0, -1 ) ); } return $name === $pattern; } public function ajax_scan() { $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, self::SCAN_NONCE ) ) { wp_send_json_error(); } global $wpdb; $t = self::cookies_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { wp_send_json_success(); } $names = isset( $_POST['names'] ) ? (array) wp_unslash( $_POST['names'] ) : array(); $services = isset( $_POST['services'] ) ? (array) wp_unslash( $_POST['services'] ) : array(); $pre = ! empty( $_POST['pre_consent'] ) ? 1 : 0; $now = current_time( 'mysql', true ); if ( ! function_exists( 'aieo_consent_service_hosts' ) ) { require_once __DIR__ . '/data/known-cookies.php'; } $host_map = aieo_consent_service_hosts(); $count = 0; foreach ( $names as $name ) { if ( $count >= 60 ) { break; } $name = sanitize_text_field( $name ); if ( '' === $name || ! preg_match( '/^[A-Za-z0-9_\-.]{1,191}$/', $name ) ) { continue; } self::upsert_cookie( $name, self::classify_cookie( $name ), $pre, $now ); $count++; } foreach ( $services as $host ) { if ( $count >= 60 ) { break; } $host = sanitize_text_field( $host ); if ( '' === $host || ! isset( $host_map[ $host ] ) ) { continue; } $m = $host_map[ $host ]; self::upsert_cookie( 'svc:' . $host, array( 'service' => $m['service'], 'category' => $m['category'], 'description' => isset( $m['description'] ) ? $m['description'] : '', ), $pre, $now ); $count++; } wp_send_json_success(); } const CRON_RESCAN = 'aieo_consent_rescan'; const OPT_RESCAN_DUE = 'aieo_consent_rescan_due'; public static function cron_schedules( $schedules ) { if ( ! isset( $schedules['weekly'] ) ) { $schedules['weekly'] = array( 'interval' => WEEK_IN_SECONDS, 'display' => did_action( 'init' ) ? __( 'Once Weekly', 'ai-eshop-optimizer' ) : 'Once Weekly', ); } return $schedules; } public static function schedule_rescan() { if ( wp_next_scheduled( self::CRON_RESCAN ) ) { return; } $when = time() + ( 6 * HOUR_IN_SECONDS ) + wp_rand( 0, 3600 ); $every = isset( wp_get_schedules()['weekly'] ) ? 'weekly' : 'daily'; wp_schedule_event( $when, $every, self::CRON_RESCAN ); } public static function forcescan_load_sdks() { if ( ! isset( $_GET['aieo_cmp_forcescan'] ) || '1' !== (string) $_GET['aieo_cmp_forcescan'] ) { return; } echo "\n"; } public static function cron_rescan() { update_option( self::OPT_RESCAN_DUE, 1, false ); } public static function maybe_auto_rescan() { if ( ! is_admin() || wp_doing_ajax() || ! get_option( self::OPT_RESCAN_DUE ) ) { return; } if ( ! current_user_can( 'manage_options' ) ) { return; } delete_option( self::OPT_RESCAN_DUE ); update_option( 'aieo_consent_last_rescan', current_time( 'mysql' ), false ); $urls = array( home_url( '/' ) ); foreach ( array( 'wc_get_cart_url', 'wc_get_checkout_url' ) as $fn ) { if ( function_exists( $fn ) ) { $u = $fn(); if ( $u ) { $urls[] = $u; } } } if ( function_exists( 'wc_get_page_permalink' ) ) { $shop = wc_get_page_permalink( 'shop' ); if ( $shop ) { $urls[] = $shop; } } $urls = array_values( array_unique( array_filter( $urls ) ) ); ?> query( $wpdb->prepare( "INSERT INTO `{$t}` (name, service, category, description, status, hits, pre_consent, first_seen, last_seen) VALUES (%s, %s, %s, %s, %s, 1, %d, %s, %s) ON DUPLICATE KEY UPDATE hits = hits + 1, last_seen = VALUES(last_seen), pre_consent = GREATEST(pre_consent, VALUES(pre_consent)), service = IF(status = 'reviewed', service, VALUES(service)), category = IF(status = 'reviewed', category, VALUES(category)), description = IF(status = 'reviewed', description, VALUES(description)), status = IF(status = 'reviewed', 'reviewed', VALUES(status))", $name, $service, $category, $description, $status, $pre, $now, $now ) ); } public static function get_inventory() { global $wpdb; $t = self::cookies_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return array(); } $rows = $wpdb->get_results( "SELECT * FROM `{$t}` ORDER BY category ASC, hits DESC, name ASC" ); $out = array( 'necessary' => array(), 'functional' => array(), 'analytics' => array(), 'marketing' => array(), 'unclassified' => array() ); foreach ( (array) $rows as $r ) { $cat = isset( $out[ $r->category ] ) ? $r->category : 'unclassified'; $out[ $cat ][] = $r; } return $out; } public static function review_cookie( $name, $category, $service ) { global $wpdb; $t = self::cookies_table(); $wpdb->update( $t, array( 'category' => $category, 'service' => '' !== $service ? $service : null, 'status' => 'reviewed', ), array( 'name' => $name ) ); $s = self::settings(); $ov = is_array( $s['cookie_overrides'] ) ? $s['cookie_overrides'] : array(); $ov[ $name ] = array( 'category' => $category, 'service' => $service ); $s['cookie_overrides'] = $ov; update_option( 'aieo_consent_settings', $s, false ); } public static function clear_inventory() { global $wpdb; $t = self::cookies_table(); $wpdb->query( "TRUNCATE TABLE `{$t}`" ); } public static function editor_preview() { $elementor = class_exists( 'Elementor\\Plugin', false ) && isset( \Elementor\Plugin::$instance->preview ) && is_object( \Elementor\Plugin::$instance->preview ) && \Elementor\Plugin::$instance->preview->is_preview_mode(); $preview = $elementor || is_customize_preview() || wp_is_json_request() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ); return (bool) apply_filters( 'aieo_editor_preview', $preview ); } public function consent_mode_default() { $s = self::settings(); $mode = self::mode(); $region_aware = ( 'yes' === $s['eea_only'] && 'granted' === $s['default_state'] ); ?> 20, 'height' => 20, 'class' => 'aieo-cmp-close__icon' ) ); if ( '' !== $icon ) { return $icon; } } return class_exists( 'AIEO_Icons' ) ? AIEO_Icons::svg( 'x', '20px' ) : ''; } private static function measure_local_image( $url ) { $none = array( 0, 0 ); $url = trim( (string) $url ); if ( '' === $url ) { return $none; } $path = (string) wp_parse_url( $url, PHP_URL_PATH ); if ( '' === $path ) { return $none; } $path = rawurldecode( $path ); $candidates = array(); $up = wp_get_upload_dir(); if ( ! empty( $up['basedir'] ) && ! empty( $up['baseurl'] ) ) { $up_path = (string) wp_parse_url( $up['baseurl'], PHP_URL_PATH ); if ( '' !== $up_path && 0 === strpos( $path, $up_path ) ) { $candidates[] = $up['basedir'] . substr( $path, strlen( $up_path ) ); } $pos = strpos( $path, '/uploads/' ); if ( false !== $pos ) { $candidates[] = $up['basedir'] . substr( $path, $pos + strlen( '/uploads' ) ); } } $home_path = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); $rel = ( '' !== $home_path && '/' !== $home_path && 0 === strpos( $path, $home_path ) ) ? substr( $path, strlen( $home_path ) ) : ltrim( $path, '/' ); if ( '' !== $rel ) { $candidates[] = trailingslashit( ABSPATH ) . $rel; } $root = realpath( ABSPATH ); foreach ( $candidates as $file ) { $real = realpath( $file ); if ( false === $real || ( $root && 0 !== strpos( $real, $root ) ) ) { continue; } $sz = @getimagesize( $real ); if ( is_array( $sz ) && ! empty( $sz[0] ) && ! empty( $sz[1] ) ) { return array( (int) $sz[0], (int) $sz[1] ); } } return $none; } private static function button_css_vars( $s ) { $buttons = array( 'accept', 'decline', 'customize' ); $fields = array( 'bg' => 'bg', 'fg' => 'fg', 'border' => 'border', 'bg_hover' => 'bg-h', 'fg_hover' => 'fg-h', 'border_hover' => 'border-h', ); $ink = static function ( $fill ) { return ( '' !== $fill && 'transparent' !== strtolower( $fill ) ) ? 'oklch(from ' . $fill . ' clamp(0,(0.62 - l) * 999,1) 0 0)' : ''; }; $parts = array(); foreach ( $buttons as $b ) { $v = array(); foreach ( $fields as $key => $css ) { $v[ $key ] = isset( $s[ 'btn_' . $b . '_' . $key ] ) ? self::color( $s[ 'btn_' . $b . '_' . $key ] ) : ''; } if ( '' === implode( '', $v ) ) { continue; } $v['bg_hover'] = '' !== $v['bg_hover'] ? $v['bg_hover'] : $v['bg']; $v['fg'] = '' !== $v['fg'] ? $v['fg'] : $ink( $v['bg'] ); $v['fg_hover'] = '' !== $v['fg_hover'] ? $v['fg_hover'] : ( $v['bg_hover'] === $v['bg'] ? $v['fg'] : $ink( $v['bg_hover'] ) ); $v['border'] = '' !== $v['border'] ? $v['border'] : $v['bg']; $v['border_hover'] = '' !== $v['border_hover'] ? $v['border_hover'] : ( '' !== $v['bg_hover'] ? $v['bg_hover'] : $v['border'] ); foreach ( $fields as $key => $css ) { if ( '' !== $v[ $key ] ) { $parts[] = '--aieo-cmp-' . $b . '-' . $css . ':' . $v[ $key ]; } } } return implode( ';', $parts ); } private static function len( $v ) { $v = trim( (string) $v ); return preg_match( '/^[0-9]{1,3}(\.[0-9]{1,2})?(px|rem|em|%)?$/', $v ) ? $v : ''; } private static function bar_css_vars( $s ) { $parts = array(); foreach ( array( 'bar_bg' => '--aieo-bar-bg', 'bar_text_color' => '--aieo-bar-text', 'bar_desc_color' => '--aieo-bar-desc', 'bar_link_color' => '--aieo-bar-link' ) as $k => $css ) { $val = isset( $s[ $k ] ) ? self::color( $s[ $k ] ) : ''; if ( '' !== $val ) { $parts[] = $css . ':' . $val; } } foreach ( array( 'bar_text_size' => '--aieo-bar-text-size', 'bar_desc_size' => '--aieo-bar-desc-size' ) as $k => $css ) { $val = isset( $s[ $k ] ) ? self::len( $s[ $k ] ) : ''; if ( '' !== $val ) { $parts[] = $css . ':' . $val; } } return empty( $parts ) ? '' : ';' . implode( ';', $parts ); } public static function partner_count() { global $wpdb; $t = self::cookies_table(); $has = function_exists( 'aieo_dmm_table_exists' ) ? aieo_dmm_table_exists( $t ) : ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t ); if ( ! $has ) { return 0; } return (int) $wpdb->get_var( "SELECT COUNT(DISTINCT service) FROM `{$t}` WHERE service IS NOT NULL AND service <> '' AND category IN ('analytics','marketing','functional')" ); } private static function bbc_modal_message( $partners = 0 ) { $intro = $partners > 0 ? sprintf( __( 'We use cookies and similar technologies, and work with %d selected service providers, to keep the site working properly, to remember your preferences and, with your consent, to collect statistics about how the site is used so that we can keep improving our content and services.', 'ai-eshop-optimizer' ), $partners ) : __( 'We use cookies and similar technologies, and work with selected service providers, to keep the site working properly, to remember your preferences and, with your consent, to collect statistics about how the site is used so that we can keep improving our content and services.', 'ai-eshop-optimizer' ); $ask = __( 'Please choose your preferences for:', 'ai-eshop-optimizer' ); $asks = array( __( 'Storing and/or accessing information on your device.', 'ai-eshop-optimizer' ), __( 'Collecting anonymous or pseudonymised statistics to understand how the site is used.', 'ai-eshop-optimizer' ), __( 'Measuring the effectiveness of our content and services.', 'ai-eshop-optimizer' ), __( 'Showing or measuring content and communications through third-party services, where such technologies are used and only with your consent.', 'ai-eshop-optimizer' ), ); $foot = __( 'You can change your preferences at any time using “Manage options”.', 'ai-eshop-optimizer' ); $out = '

' . esc_html( $intro ) . '

' . esc_html( $ask ) . '

' . esc_html( $foot ) . '

'; } private static function bbc_bar_message() { return '

' . esc_html__( 'We use cookies to keep the site working properly, to remember your preferences and, with your consent, to understand how the site is used.', 'ai-eshop-optimizer' ) . '

'; } public function enqueue_assets() { if ( is_admin() || self::editor_preview() ) { return; } $dir = dirname( __DIR__, 2 ) . '/'; $css = 'assets/css/aieo-consent.css'; $js = 'assets/js/aieo-consent.js'; if ( file_exists( $dir . $css ) ) { wp_enqueue_style( 'aieo-consent', plugins_url( $css, $dir . 'plugin.php' ), array(), (string) filemtime( $dir . $css ) ); } if ( file_exists( $dir . $js ) ) { wp_register_script( 'aieo-consent', plugins_url( $js, $dir . 'plugin.php' ), array(), (string) filemtime( $dir . $js ), array( 'in_footer' => true, 'strategy' => 'defer' ) ); wp_add_inline_script( 'aieo-consent', 'window.aieoCmpCfg = ' . wp_json_encode( $this->banner_config() ) . ';', 'before' ); wp_enqueue_script( 'aieo-consent' ); } } private function banner_config() { $s = self::settings(); return array( 'ver' => (int) self::policy_version(), 'name' => self::COOKIE, 'ajax' => admin_url( 'admin-ajax.php' ), 'scanNonce' => wp_create_nonce( self::SCAN_NONCE ), 'scanOn' => 'no' !== ( isset( $s['cookie_scan'] ) ? $s['cookie_scan'] : 'yes' ), 'scanTtl' => (int) max( 1, (int) ( isset( $s['cookie_scan_days'] ) ? $s['cookie_scan_days'] : 30 ) ) * 86400000, 'hosts' => $this->scan_hosts( $s ), 'mode' => self::mode(), 'eeaOnly' => 'yes' === $s['eea_only'], 'defGrant' => 'granted' === $s['default_state'], 'cfTrace' => ! empty( $_SERVER['HTTP_CF_RAY'] ) || ! empty( $_SERVER['HTTP_CF_IPCOUNTRY'] ), 'oneTap' => 'yes' === ( isset( $s['one_tap_enabled'] ) ? $s['one_tap_enabled'] : 'no' ), ); } private function scan_hosts( array $s ) { $scan_on = 'no' !== ( isset( $s['cookie_scan'] ) ? $s['cookie_scan'] : 'yes' ); $force = isset( $_GET['aieo_cmp_forcescan'] ) && '1' === (string) $_GET['aieo_cmp_forcescan']; if ( ! $scan_on && ! $force ) { return array(); } if ( ! function_exists( 'aieo_consent_service_hosts' ) ) { require_once __DIR__ . '/data/known-cookies.php'; } return array_keys( aieo_consent_service_hosts() ); } public function render_banner() { if ( self::editor_preview() ) { return; } $s = self::settings(); $layout = in_array( $s['layout'], array( 'bar', 'modal' ), true ) ? $s['layout'] : 'bar'; $mode = self::mode(); $auto_show = false; $is_modal = ( 'modal' === $layout ); $reopen_css = static function ( $pos, $x, $b ) { $x = (int) $x; $b = (int) $b; if ( 'right' === $pos ) { return "right:{$x}px;left:auto;transform:none;bottom:{$b}px;"; } if ( 'center' === $pos ) { return "left:50%;right:auto;transform:translateX(-50%);bottom:{$b}px;"; } return "left:{$x}px;right:auto;transform:none;bottom:{$b}px;"; }; $reopen_desktop_css = $reopen_css( isset( $s['reopen_pos_desktop'] ) ? $s['reopen_pos_desktop'] : 'left', isset( $s['reopen_x_desktop'] ) ? $s['reopen_x_desktop'] : 86, isset( $s['reopen_bottom_desktop'] ) ? $s['reopen_bottom_desktop'] : 20 ); $reopen_mobile_css = $reopen_css( isset( $s['reopen_pos_mobile'] ) ? $s['reopen_pos_mobile'] : 'left', isset( $s['reopen_x_mobile'] ) ? $s['reopen_x_mobile'] : 86, isset( $s['reopen_bottom_mobile'] ) ? $s['reopen_bottom_mobile'] : 20 ); $mbar_owns_mobile = defined( 'AIEO_RUNTIME_OWNS_DISPLAY' ) || ( class_exists( 'AIEO_Side_Cart_Display' ) && class_exists( 'AIEO_DMM_Settings' ) && method_exists( 'AIEO_DMM_Settings', 'is_module_enabled' ) && AIEO_DMM_Settings::is_module_enabled( 'side_cart' ) ); $title = ( '' !== $s['title'] ) ? $s['title'] : __( 'Let us know your cookie preferences', 'ai-eshop-optimizer' ); if ( '' !== $s['message'] ) { $msg = $s['message']; } else { $msg = $is_modal ? self::bbc_modal_message( self::partner_count() ) : self::bbc_bar_message(); } $logo = isset( $s['logo'] ) ? trim( (string) $s['logo'] ) : ''; $logo_wh = ''; $logo_style = ''; $logo_cls = 'aieo-cmp-logo'; if ( '' !== $logo ) { $logo_dim = get_transient( 'aieo_cmp_logo_dim_' . md5( $logo ) ); if ( ! is_array( $logo_dim ) ) { $logo_dim = self::measure_local_image( $logo ); set_transient( 'aieo_cmp_logo_dim_' . md5( $logo ), $logo_dim, DAY_IN_SECONDS ); } if ( ! empty( $logo_dim[0] ) && ! empty( $logo_dim[1] ) ) { $logo_wh = ' width="' . (int) $logo_dim[0] . '" height="' . (int) $logo_dim[1] . '"'; $logo_style = ' style="--aieo-cmp-logo-h:' . (int) $logo_dim[1] . 'px"'; } else { $logo_cls .= ' aieo-cmp-logo--unsized'; } } $logo_tint = '' !== $logo ? self::logo_tint_attrs( $logo ) : ''; $vars = self::button_css_vars( $s ) . self::bar_css_vars( $s ); unset( $logo_dim ); $ajax = admin_url( 'admin-ajax.php' ); $descs = array( 'necessary' => '' !== $s['desc_necessary'] ? $s['desc_necessary'] : __( 'Needed for the site to work safely and correctly, and cannot be switched off.', 'ai-eshop-optimizer' ), 'functional' => '' !== $s['desc_functional'] ? $s['desc_functional'] : __( 'Remember your preferences, such as language and other settings, to improve your experience.', 'ai-eshop-optimizer' ), 'analytics' => '' !== $s['desc_analytics'] ? $s['desc_analytics'] : __( 'Help us understand how the site is used, so we can improve our content, functionality and services.', 'ai-eshop-optimizer' ), 'marketing' => '' !== $s['desc_marketing'] ? $s['desc_marketing'] : __( 'Where such technologies are used, these measure how well our communications perform and/or show relevant content through third-party services, only with your consent.', 'ai-eshop-optimizer' ), ); $cat_labels = array( 'necessary' => __( 'Strictly necessary', 'ai-eshop-optimizer' ), 'functional' => __( 'Functional', 'ai-eshop-optimizer' ), 'analytics' => __( 'Analytics', 'ai-eshop-optimizer' ), 'marketing' => __( 'Marketing', 'ai-eshop-optimizer' ), ); $cat_quals = array( 'marketing' => __( 'where such technologies are used', 'ai-eshop-optimizer' ), ); $scan_on = 'no' !== ( isset( $s['cookie_scan'] ) ? $s['cookie_scan'] : 'yes' ); $force = isset( $_GET['aieo_cmp_forcescan'] ) && '1' === (string) $_GET['aieo_cmp_forcescan']; $hosts = array(); if ( $scan_on || $force ) { if ( ! function_exists( 'aieo_consent_service_hosts' ) ) { require_once __DIR__ . '/data/known-cookies.php'; } $hosts = array_keys( aieo_consent_service_hosts() ); } ?>