<?php
if (!defined("ABSPATH")) exit;

// Pinery Flow — Ad Zone Management
// Register customizer settings and provide ad-zone rendering.
// Theme calls these via function_exists() for graceful degradation.

/**
 * Sanitize ad content — like wp_kses_post but also allows <script> tags
 * so AdSense and similar ad code works. Admin-only input; risk accepted.
 */
function pinery_flow_ad_sanitize($content) {
    $allowed = wp_kses_allowed_html('post');
    $allowed['script'] = [
        'src'         => true,
        'type'        => true,
        'async'       => true,
        'defer'       => true,
        'charset'     => true,
        'crossorigin' => true,
        'data-ad-client'             => true,
        'data-ad-slot'               => true,
        'data-ad-format'             => true,
        'data-ad-layout'             => true,
        'data-ad-layout-key'         => true,
        'data-full-width-responsive' => true,
        'data-adbreak-test'          => true,
        'data-language'              => true,
        'data-ad-region'             => true,
        'data-ad-channel'            => true,
    ];
    $allowed['ins'] = [
        'class'            => true,
        'style'            => true,
        'data-ad-client'   => true,
        'data-ad-slot'     => true,
        'data-ad-format'   => true,
        'data-ad-layout'   => true,
        'data-ad-layout-key' => true,
        'data-full-width-responsive' => true,
    ];
    return wp_kses($content, $allowed);
}

function pinery_flow_ad_zones() {
    return [
        'hero_below'    => __('Below Hero Section', 'pinery-flow'),
        'between_posts' => __('Between Post Cards', 'pinery-flow'),
        'footer_above'  => __('Above Footer', 'pinery-flow'),
    ];
}

function pinery_flow_ad_zone($zone_id) {
    $zones = pinery_flow_ad_zones();
    if (!isset($zones[$zone_id])) return;
    $content = get_theme_mod("pinery_ad_{$zone_id}", '');
    if (empty($content)) return;
    echo '<div class="ad-zone ad-zone--' . esc_attr($zone_id) . '">';
    echo do_shortcode(pinery_flow_ad_sanitize($content));
    echo '</div>';
}

function pinery_flow_register_ad_customizer_settings($wp_customize) {
    $wp_customize->add_section('pinery_ads', [
        'title'    => __('Ad Zones', 'pinery-flow'),
        'priority' => 50,
    ]);

    foreach (pinery_flow_ad_zones() as $id => $label) {
        $wp_customize->add_setting("pinery_ad_{$id}", [
            'default'           => '',
            'sanitize_callback' => 'pinery_flow_ad_sanitize',
        ]);
        $wp_customize->add_control("pinery_ad_{$id}", [
            'label'       => $label,
            'description' => __('HTML, shortcodes, and script tags supported.', 'pinery-flow'),
            'section'     => 'pinery_ads',
            'type'        => 'textarea',
        ]);
    }

    $wp_customize->add_setting('pinery_ad_between_every', [
        'default'           => 6,
        'sanitize_callback' => 'absint',
    ]);
    $wp_customize->add_control('pinery_ad_between_every', [
        'label'       => __('Insert ad every N posts', 'pinery-flow'),
        'section'     => 'pinery_ads',
        'type'        => 'number',
        'input_attrs' => ['min' => 1, 'max' => 50, 'step' => 1],
    ]);
}

function pinery_flow_maybe_between_ad($counter) {
    $every = get_theme_mod('pinery_ad_between_every', 6);
    if ($every > 0 && $counter % $every === 0) {
        pinery_flow_ad_zone('between_posts');
    }
}
