<?php
/**
 * Plugin Name: Calio Addon for Elementor - Widgets, Slider & Extensions
 * Description: Modular Elementor addon with creative widgets, WooCommerce elements, dashboard tools, and lightweight extension controls.
 * Version: 1.11
 * Requires at least: 6.7
 * Requires PHP: 8.2
 * Requires Plugins: elementor
 * Author: Sermedias
 * Author URI: https://profiles.wordpress.org/sermedias/
 * Text Domain: calio-addon
 * Domain Path: /languages
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 * Elementor requires at least: 3.20.0
 */

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

if ( ! function_exists( 'calio_fs' ) ) {
    // Create a helper function for easy SDK access.
    function calio_fs() {
        global $calio_fs;

        if ( ! isset( $calio_fs ) ) {
            // Include Freemius SDK.
            require_once plugin_dir_path(__FILE__) . 'vendor/freemius/start.php';

            $calio_fs = fs_dynamic_init( array(
                'id'                  => '26250',
                'slug'                => 'calio',
                'premium_slug'        => 'calio-addon-elementor-pro',
                'type'                => 'plugin',
                'public_key'          => 'pk_cc53b5a644a5b9e7611d00b0ca45f',
                'is_premium'          => false,
                'premium_suffix'      => '1 Web Site',
                // If your plugin is a serviceware, set this option to false.
                'has_premium_version' => true,
                'parallel_activation' => array(
                    'enabled'                  => true,
                    'premium_version_basename' => 'calio-addon-elementor-pro/calio-addon-elementor-pro.php',
                ),
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'is_org_compliant'    => true,
                'menu'                => array(
                    'slug'           => 'calio_addon_options',
                    'support'        => true,
                    'account'        => false,
                    'contact'        => false,
                ),
            ) );
        }

        return $calio_fs;
    }

    // Init Freemius.
    calio_fs();

    // Keep Freemius debug output out of public/editor markup unless explicitly enabled.
    if (!defined('CALIO_ALLOW_FREEMIUS_DEBUG') || CALIO_ALLOW_FREEMIUS_DEBUG !== true) {
        $calio_disable_freemius_echo = static function (): void {
            if (class_exists('FS_Logger')) {
                remove_action('admin_footer', 'FS_Logger::dump', 100);
                remove_action('wp_footer', 'FS_Logger::dump', 100);
            }
        };

        add_action('admin_footer', $calio_disable_freemius_echo, 1);
        add_action('wp_footer', $calio_disable_freemius_echo, 1);
        add_action('init', $calio_disable_freemius_echo, 99);
    }

    // Signal that SDK was initiated.
    do_action( 'calio_fs_loaded' );
}

define('CALIO_VERSION', '1.11');
define('CALIO_FILE', __FILE__);
define('CALIO_BASENAME', plugin_basename(__FILE__));
define('CALIO_PATH', plugin_dir_path(__FILE__));
define('CALIO_URL', plugin_dir_url(__FILE__));
define('CALIO_TEXT_DOMAIN', 'calio-addon');
define('CALIO_SLUG', 'calio-addon');
define('CALIO_MENU_SLUG', 'calio_addon_options');

require_once CALIO_PATH . 'autoload.php';

// Activation checks keep unsupported environments from leaving the plugin half-enabled.
register_activation_hook(CALIO_FILE, static function (): void {
    if (!class_exists('Calio\\Plugin') && is_readable(CALIO_PATH . 'src/Plugin.php')) {
        require_once CALIO_PATH . 'src/Plugin.php';
    }

    if (class_exists('Calio\\Plugin')) {
        \Calio\Plugin::on_activation();
    }
});

// Runtime boot is deferred until all plugins are loaded so Elementor can initialize first.
add_action('plugins_loaded', static function (): void {
    if (!class_exists('Calio\\Plugin')) {
        return;
    }

    \Calio\Plugin::instance()->boot();
}, 20);

// Keep Freemius account/pricing submenus hidden since license + upgrade flows are surfaced in Calio's dashboard.
add_action('admin_menu', static function (): void {
    if (!is_admin()) {
        return;
    }

    $parentSlugs = ['calio_addon_options', 'calio'];

    foreach ($parentSlugs as $parentSlug) {
        remove_submenu_page($parentSlug, 'calio-account');
        remove_submenu_page($parentSlug, 'calio_account');
        remove_submenu_page($parentSlug, 'calio-pricing');
        remove_submenu_page($parentSlug, 'calio_pricing');
        remove_submenu_page($parentSlug, 'calio-upgrade');
        remove_submenu_page($parentSlug, 'calio_upgrade');
    }

    global $submenu;

    foreach ($parentSlugs as $parentSlug) {
        if (empty($submenu[$parentSlug]) || !is_array($submenu[$parentSlug])) {
            continue;
        }

        foreach ($submenu[$parentSlug] as $item) {
            $menuSlug = isset($item[2]) ? (string) $item[2] : '';

            if ($menuSlug === '') {
                continue;
            }

            if (stripos($menuSlug, 'calio') === false) {
                continue;
            }

            $hideKeys = ['account', 'pricing', 'upgrade', 'plans'];
            foreach ($hideKeys as $needle) {
                if (stripos($menuSlug, $needle) !== false) {
                    remove_submenu_page($parentSlug, $menuSlug);
                    break;
                }
            }
        }
    }
}, 999);
