<?php
/**
 * CAPTCHA Render Partial
 *
 * @package GuardDog
 * @since 1.8.0
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
// These are local variables in a template file, not global variables
// The Plugin Checker incorrectly flags local template variables as globals
// These variables are passed from the calling class and scoped to this template file
// They do not pollute the global namespace

// Extract variables passed from the class
$provider = $provider ?? 'none';
$config = $config ?? [];

// phpcs:disable WordPress.Security.NonceVerification.Recommended
// Nonce verification is not required in this context:
// 1. This is a view partial that only reads GET parameters for display purposes
// 2. The GET parameter is used only to display error messages, not to process form data
// 3. All input is properly sanitized before use
// 4. No privileged actions or data modifications occur based on this GET parameter
$captcha_error = isset($_GET['captcha_error']) ? sanitize_text_field(wp_unslash($_GET['captcha_error'])) : '';
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
?>

<div class="guard-dog-captcha-container">
    <?php switch ($provider): 
        case 'recaptcha_v2': ?>
            <div class="g-recaptcha" 
                 data-sitekey="<?php echo esc_attr($config['site_key']); ?>" 
                 data-theme="<?php echo esc_attr($config['theme']); ?>" 
                 data-size="<?php echo esc_attr($config['size']); ?>"></div>
            <?php if ($captcha_error): ?>
                <div class="guard-dog-captcha-error"><?php echo esc_html($config['error_message']); ?></div>
            <?php endif; ?>
            <?php break; ?>
            
        <?php case 'recaptcha_v3': ?>
            <?php if ($captcha_error): ?>
                <div class="guard-dog-captcha-error"><?php echo esc_html($config['error_message']); ?></div>
            <?php endif; ?>
            <p class="guard-dog-recaptcha-notice">
                <?php esc_html_e('This site is protected by reCAPTCHA and the Google', 'guard-dog'); ?>
                <a href="https://policies.google.com/privacy" target="_blank"><?php esc_html_e('Privacy Policy', 'guard-dog'); ?></a>
                <?php esc_html_e('and', 'guard-dog'); ?>
                <a href="https://policies.google.com/terms" target="_blank"><?php esc_html_e('Terms of Service', 'guard-dog'); ?></a>
                <?php esc_html_e('apply.', 'guard-dog'); ?>
            </p>
            <?php break; ?>
            
        <?php case 'hcaptcha': ?>
            <div class="h-captcha" 
                 data-sitekey="<?php echo esc_attr($config['site_key']); ?>" 
                 data-theme="<?php echo esc_attr($config['theme']); ?>" 
                 data-size="<?php echo esc_attr($config['size']); ?>"></div>
            <?php if ($captcha_error): ?>
                <div class="guard-dog-captcha-error"><?php echo esc_html($config['error_message']); ?></div>
            <?php endif; ?>
            <?php break; ?>
            
        <?php case 'turnstile': ?>
            <div class="cf-turnstile" 
                 data-sitekey="<?php echo esc_attr($config['site_key']); ?>" 
                 data-theme="<?php echo esc_attr($config['theme']); ?>"></div>
            <?php if ($captcha_error): ?>
                <div class="guard-dog-captcha-error"><?php echo esc_html($config['error_message']); ?></div>
            <?php endif; ?>
            <?php break; ?>
            
    <?php endswitch; ?>
</div> 