<?php
/**
 * Plugin Name: Cookie Notice Pro
 * Description: Baner zgody na cookies (CookieNoticePro) dla luxmed.pagedev.pl.
 * Version:     1.0.0
 * Author:      pagedev.pl
 */

if (!defined('ABSPATH')) {
    exit;
}

// ───── KONFIGURACJA — ZMIEŃ NA SWÓJ ─────
const CNP_GTM_ID = 'GTM-PDM8GTPF';
// ────────────────────────────────────────

/**
 * Google Consent Mode v2 default=denied + GTM loader.
 * MUSI być w wp_head z PRIORYTETEM 1 — żeby:
 *   1) dataLayer + gtag istniały zanim cokolwiek innego się załaduje,
 *   2) consent default został ustawiony PRZED tym jak GTM odpali tagi.
 * Cookie Notice Pro (enableGoogleConsentMode: true w configu) wywołuje
 * gtag('consent','update',...) po wyborze użytkownika.
 */
add_action('wp_head', function () {
    $gtm_id = CNP_GTM_ID;
    ?>
    <!-- Google Consent Mode v2 — default denied (Cookie Notice Pro updates after user choice) -->
    <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('consent', 'default', {
      'ad_storage': 'denied',
      'ad_user_data': 'denied',
      'ad_personalization': 'denied',
      'analytics_storage': 'denied',
      'functionality_storage': 'denied',
      'personalization_storage': 'denied',
      'security_storage': 'granted',
      'wait_for_update': 500
    });
    gtag('set', 'ads_data_redaction', true);
    gtag('set', 'url_passthrough', true);
    </script>
    <!-- End Google Consent Mode v2 -->

    <!-- Google Tag Manager -->
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','<?php echo esc_js($gtm_id); ?>');</script>
    <!-- End Google Tag Manager -->
    <?php
}, 1);

/**
 * GTM noscript fallback — pierwszy strzał po <body>, drugi w stopce
 * (na wypadek motywów które nie wywołują wp_body_open).
 */
function cnp_render_gtm_noscript() {
    $gtm_id = CNP_GTM_ID;
    ?>
    <!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo esc_attr($gtm_id); ?>"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) -->
    <?php
}
add_action('wp_body_open', 'cnp_render_gtm_noscript');
add_action('wp_footer',    'cnp_render_gtm_noscript', 1);

add_action('wp_enqueue_scripts', function () {
    $base = plugin_dir_url(__FILE__) . 'assets/';
    $dir  = plugin_dir_path(__FILE__) . 'assets/';

    wp_enqueue_style(
        'cnp-style',
        $base . 'cookienoticepro.style.css',
        [],
        file_exists($dir . 'cookienoticepro.style.css') ? filemtime($dir . 'cookienoticepro.style.css') : '1.0.0'
    );

    wp_enqueue_script(
        'cnp-script',
        $base . 'cookienoticepro.script.js',
        ['jquery'],
        file_exists($dir . 'cookienoticepro.script.js') ? filemtime($dir . 'cookienoticepro.script.js') : '1.0.0',
        true
    );
}, 20);

add_action('wp_footer', function () {
    ?>
    <script>
    jQuery(function ($) {
        if (window.cookieNoticePro && typeof window.cookieNoticePro.init === 'function') {
            window.cookieNoticePro.init();
        }
    });
    </script>
    <?php
}, 99);
