<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Analytics
{
    private static $config = null;

    public const VER = '1.0.0';

    public const CONSENT_COOKIE_NAME = 'puffergo_cookie_consent';

    public static function init()
    {
        if (! is_admin()) {
            add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_analytics_scripts'), 1);
        }
    }

    private static function get_config()
    {
        if (self::$config === null) {
            self::$config = array('ga4_enabled' => get_option('puffergo_ga4_enabled', false), 'ga4_id' => get_option('puffergo_ga4_id', ''), 'baidu_enabled' => get_option('puffergo_baidu_enabled', false), 'baidu_id' => get_option('puffergo_baidu_id', ''), 'fb_pixel_enabled' => get_option('puffergo_fb_pixel_enabled', false), 'fb_pixel_id' => get_option('puffergo_fb_pixel_id', ''), );
        }

return self::$config;
    }

    public static function enqueue_analytics_scripts()
    {
        $config = self::get_config();
        $ga4_active = $config['ga4_enabled'] && ! empty($config['ga4_id']);
        $fb_active = $config['fb_pixel_enabled'] && ! empty($config['fb_pixel_id']);

        if ($ga4_active || $fb_active) {
            self::enqueue_consent_banner();
        }

if ($ga4_active) {
            self::enqueue_ga4($config['ga4_id']);
        }

if ($fb_active) {
            self::enqueue_facebook_pixel($config['fb_pixel_id']);
        }

if ($config['baidu_enabled'] && ! empty($config['baidu_id'])) {
            self::enqueue_baidu($config['baidu_id']);
        }
    }

    private static function enqueue_consent_banner()
    {
        wp_enqueue_style('puffergo-cookie-consent', plugins_url('assets/css/puffergo-cookie-consent.css', PUFFERGO_PLUGIN_FILE), array(), self::VER);
        wp_enqueue_script('puffergo-cookie-consent', plugins_url('assets/js/puffergo-cookie-consent.js', PUFFERGO_PLUGIN_FILE), array(), self::VER, true);
        wp_localize_script('puffergo-cookie-consent', 'puffergoCookieConsent', array('cookieName' => self::CONSENT_COOKIE_NAME, 'strings' => array('message' => __('This site uses cookies for analytics and advertising. Accept to allow, or reject to opt out.', 'puffergo'), 'accept' => __('Accept', 'puffergo'), 'reject' => __('Reject', 'puffergo'), 'ariaLabel' => __('Cookie consent', 'puffergo'), ), ));
    }

    private static function enqueue_ga4($ga4_id)
    {
        wp_enqueue_script('puffergo-ga4', 'https://www.googletagmanager.com/gtag/js?id=' . esc_attr($ga4_id), array(), self::VER, false);
        wp_script_add_data('puffergo-ga4', 'async', true);
        $inline_script = sprintf("window.dataLayer = window.dataLayer || [];\n" . "function gtag(){dataLayer.push(arguments);}\n" . "gtag('consent', 'default', {\n" . "  'ad_storage': 'denied',\n" . "  'ad_user_data': 'denied',\n" . "  'ad_personalization': 'denied',\n" . "  'analytics_storage': 'denied',\n" . "  'wait_for_update': 500\n" . "});\n" . "gtag('js', new Date());\n" . "gtag('config', '%s');", esc_js($ga4_id));
        wp_add_inline_script('puffergo-ga4', $inline_script);
    }

    private static function enqueue_facebook_pixel($pixel_id)
    {
        $inline_script = sprintf("!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?" . "n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;" . "n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;" . "t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window," . "document,'script','https://connect.facebook.net/en_US/fbevents.js');\n" . "fbq('init', '%s');\n" . "fbq('track', 'PageView');", esc_js($pixel_id));
        add_action('wp_footer', function () use ($inline_script) { printf('<script type="text/plain" data-puffergo-consent="marketing">%s</script>' . "\n" . '<noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=%s&ev=PageView&noscript=1" alt="" /></noscript>' . "\n", $inline_script, esc_attr($pixel_id)); }, 20);
    }

    private static function enqueue_baidu($baidu_id)
    {
        wp_register_script('puffergo-baidu', false, array(), self::VER, false);
        wp_enqueue_script('puffergo-baidu');
        $inline_script = sprintf("var _hmt = _hmt || [];\n(function() {\n  var hm = document.createElement(\"script\");\n  hm.src = \"https://hm.baidu.com/hm.js?%s\";\n  var s = document.getElementsByTagName(\"script\")[0];\n  s.parentNode.insertBefore(hm, s);\n})();", esc_js($baidu_id));
        wp_add_inline_script('puffergo-baidu', $inline_script);
    }
}
