<?php
/**
 * Plugin Name: Vunelix Financial Widgets
 * Plugin URI:  https://vunelix.com/widgets/overview
 * Description: Embeddable real-time financial widgets for WordPress — tickers, screeners, heatmaps, currency converters, and technical analysis charts for crypto, forex, stocks, and funds.
 * Version:     1.0.5
 * Author:      Vunelix
 * Author URI:  https://vunelix.com
 * License:     GPL-2.0-or-later
 * Text Domain: vunelix-financial-widgets
 */

defined('ABSPATH') || exit;

// Guard against double-load (e.g. plugin upgrade in same request, mu-plugin
// + regular plugin both present, or symlinked dev installs).
defined('VUNELIX_VERSION')    || define('VUNELIX_VERSION', '1.0.5');
defined('VUNELIX_PLUGIN_DIR') || define('VUNELIX_PLUGIN_DIR', plugin_dir_path(__FILE__));
defined('VUNELIX_PLUGIN_URL') || define('VUNELIX_PLUGIN_URL', plugin_dir_url(__FILE__));
defined('VUNELIX_CDN_URL')    || define('VUNELIX_CDN_URL', VUNELIX_PLUGIN_URL . 'assets/js/');
defined('VUNELIX_API_URL')    || define('VUNELIX_API_URL', 'https://vunelix.com/');

// Load files
require_once VUNELIX_PLUGIN_DIR . 'includes/widget-configs.php';
require_once VUNELIX_PLUGIN_DIR . 'includes/class-post-type.php';
require_once VUNELIX_PLUGIN_DIR . 'includes/class-shortcode.php';
require_once VUNELIX_PLUGIN_DIR . 'includes/class-admin.php';

// Activation: set redirect flag
register_activation_hook(__FILE__, function() {
    set_transient('vunelix_activation_redirect', true, 30);
});

/**
 * Init
 */
function vunelix_init() {
    Vunelix_Post_Type::init();
    Vunelix_Shortcode::init();
    if (is_admin()) {
        Vunelix_Admin::init();
    }
}
add_action('plugins_loaded', 'vunelix_init');

/**
 * Enqueue widget JS on frontend (only for widgets used on current page)
 *
 * Plugin-served JS is the same un-obfuscated source served from vunelix.com
 * but the script URL points to the local plugin path. The runtime needs to
 * know where to fetch widget data + lang JSON + CSS, so PHP injects three
 * window globals before any module loads.
 */
function vunelix_enqueue_scripts() {
    global $vunelix_widgets_used;
    if (empty($vunelix_widgets_used)) return;

    $api_url = trailingslashit(VUNELIX_API_URL);
    $js_url  = $api_url . 'assets/js/';
    $css_url = VUNELIX_PLUGIN_URL . 'assets/css/widget-runtime/';

    // Widget CSS bundles are shipped locally with the plugin. Lang JSON
    // still lives on vunelix.com (small, frequently updated, low risk).
    $bootstrap = sprintf(
        'window.VUNELIX_API_URL=%s;window.VUNELIX_CSS_URL=%s;window.JS_URL=%s;',
        wp_json_encode($api_url),
        wp_json_encode($css_url),
        wp_json_encode($js_url)
    );

    $first = true;
    foreach ($vunelix_widgets_used as $slug => $js_file) {
        $handle = 'vunelix-' . $slug;
        wp_enqueue_script($handle, VUNELIX_CDN_URL . $js_file, [], VUNELIX_VERSION, true);

        // Inject runtime URLs once, before the first widget script
        if ($first) {
            wp_add_inline_script($handle, $bootstrap, 'before');
            $first = false;
        }

        // Add type="module" attribute (ES module imports rely on it)
        add_filter('script_loader_tag', function($tag, $h) use ($handle) {
            if ($h === $handle) {
                return str_replace(' src=', ' type="module" src=', $tag);
            }
            return $tag;
        }, 10, 2);
    }
}
add_action('wp_footer', 'vunelix_enqueue_scripts', 5);
