<?php

// ====================================================================
// 1. Elementor Kit Library Connection Override
// ====================================================================

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

if (! function_exists('override_elementor_kit_library_connection')) {
    function override_elementor_kit_library_connection()
    {
        if (! class_exists('\\Elementor\\Plugin') || ! class_exists('\\ElementorPro\\License\\API')) {
            return;
        }

        $elementor = \Elementor\Plugin::instance();

        if (! isset($elementor->app) || ! $elementor->app) {
            return;
        }

        $prev_settings = $elementor->app->get_settings('kit-library');

        if (! $prev_settings || empty($prev_settings)) {
            return;
        }

        if (! isset($elementor->common) || ! $elementor->common) {
            return;
        }

        if (! $elementor->common->get_component('connect')) {
            return;
        }

        $kit_library = $elementor->common->get_component('connect')->get_app('kit-library');

        if (! $kit_library) {
            return;
        }

        $prev_settings['is_library_connected'] = \ElementorPro\License\API::is_license_active() && $kit_library;
        $elementor->app->set_settings('kit-library', $prev_settings);
    }
}

if (! has_action('elementor/init', 'override_elementor_kit_library_connection')) {
    add_action('elementor_pro/init', function () {
        add_action('elementor/init', 'override_elementor_kit_library_connection', 20);
    });
}

// ====================================================================
// 2. Elementor Library Connection Bypass (Templates & Blocks)
// ====================================================================

// Force is_connected = true in JS
add_filter('elementor/editor/localize_settings', 'bypass_elementor_library_connect', 999);
add_filter('elementor/common/localize_settings', 'bypass_elementor_library_connect', 999);

function bypass_elementor_library_connect($settings)
{
    if (isset($settings['library_connect'])) {
        $settings['library_connect']['is_connected'] = true;
    }
    return $settings;
}

// Inject fake access_token so is_connected() returns true (no file modification needed)
add_action('admin_init', function () {
    $user_id = get_current_user_id();
    $option = get_user_option('elementor_connect_common_data', $user_id);
    if (empty($option['access_token'])) {
        if (!is_array($option)) $option = [];
        $option['access_token'] = 'bypass';
        update_user_option($user_id, 'elementor_connect_common_data', $option);
    }
});

// ====================================================================
// 3. Pro License Key Activation Mocking
// ====================================================================

update_option('elementor_pro_license_key', 'activated');
update_option(
    '_elementor_pro_license_v2_data',
    [
        'timeout' => strtotime('+12 hours', current_time('timestamp')),
        'value' => json_encode([
            'success' => true,
            'license' => 'valid',
            'expires' => '2050-11-25 08:00:16',
            'subscription_id' => 960,
            'status' => 'ACTIVE',
            'recurring' => false,
            'features' => [
                'template_access_level_None',
                'kit_access_level_None',
                'activity-log',
                'breadcrumbs',
                'form',
                'posts',
                'template',
                'countdown',
                'slides',
                'price-list',
                'portfolio',
                'flip-box',
                'price-table',
                'login',
                'share-buttons',
                'theme-post-content',
                'theme-post-title',
                'nav-menu',
                'blockquote',
                'media-carousel',
                'animated-headline',
                'facebook-comments',
                'facebook-embed',
                'facebook-page',
                'facebook-button',
                'testimonial-carousel',
                'post-navigation',
                'search-form',
                'post-comments',
                'author-box',
                'call-to-action',
                'post-info',
                'theme-site-logo',
                'theme-site-title',
                'theme-archive-title',
                'theme-post-excerpt',
                'theme-post-featured-image',
                'archive-posts',
                'theme-page-title',
                'sitemap',
                'reviews',
                'table-of-contents',
                'lottie',
                'code-highlight',
                'hotspot',
                'video-playlist',
                'progress-tracker',
                'section-effects',
                'sticky',
                'scroll-snap',
                'page-transitions',
                'mega-menu',
                'nested-carousel',
                'loop-grid',
                'loop-carousel',
                'theme-builder',
                'elementor_icons',
                'elementor_custom_fonts',
                'dynamic-tags',
                'taxonomy-filter',
                'email',
                'email2',
                'mailpoet',
                'mailpoet3',
                'redirect',
                'header',
                'footer',
                'single-post',
                'single-page',
                'archive',
                'search-results',
                'error-404',
                'loop-item',
                'font-awesome-pro',
                'typekit',
                'gallery',
                'off-canvas',
                'link-in-bio-var-2',
                'link-in-bio-var-3',
                'link-in-bio-var-4',
                'link-in-bio-var-5',
                'link-in-bio-var-6',
                'link-in-bio-var-7',
                'search',
                'size-variable',
                'element-manager-permissions',
                'akismet',
                'display-conditions',
                'woocommerce-products',
                'wc-products',
                'woocommerce-product-add-to-cart',
                'wc-elements',
                'wc-categories',
                'woocommerce-product-price',
                'woocommerce-product-title',
                'woocommerce-product-images',
                'woocommerce-product-upsell',
                'woocommerce-product-short-description',
                'woocommerce-product-meta',
                'woocommerce-product-stock',
                'woocommerce-product-rating',
                'wc-add-to-cart',
                'dynamic-tags-wc',
                'woocommerce-product-data-tabs',
                'woocommerce-product-related',
                'woocommerce-breadcrumb',
                'wc-archive-products',
                'woocommerce-archive-products',
                'woocommerce-product-additional-information',
                'woocommerce-menu-cart',
                'woocommerce-product-content',
                'woocommerce-archive-description',
                'paypal-button',
                'woocommerce-checkout-page',
                'woocommerce-cart',
                'woocommerce-my-account',
                'woocommerce-purchase-summary',
                'woocommerce-notices',
                'settings-woocommerce-pages',
                'settings-woocommerce-notices',
                'popup',
                'custom-css',
                'global-css',
                'custom_code',
                'custom-attributes',
                'form-submissions',
                'form-integrations',
                'dynamic-tags-acf',
                'dynamic-tags-pods',
                'dynamic-tags-toolset',
                'editor_comments',
                'stripe-button',
                'role-manager',
                'global-widget',
                'activecampaign',
                'cf7db',
                'convertkit',
                'discord',
                'drip',
                'getresponse',
                'mailchimp',
                'mailerlite',
                'slack',
                'webhook',
                'product-single',
                'product-archive',
                'wc-single-elements',
                'atomic-custom-attributes'
            ],
            'tier' => 'agency',
            'generation' => 'empty',
            'activated' => true,
            'success' => true
        ])
    ]
);

// ====================================================================
// 4. API Request Override (API Licenses Mocking)
// ====================================================================

add_filter('elementor/connect/additional-connect-info', '__return_empty_array', 999);
add_filter('pre_http_request', function ($pre, $parsed_args, $url) {
    if (
        strpos($url, 'my.elementor.com/api/v1/licenses') !== false ||
        strpos($url, 'my.elementor.com/api/v2/licenses') !== false
    ) {
        return [
            'response' => ['code' => 200, 'message' => 'ОК'],
            'body'     => json_encode([
                'success' => true,
                'license' => 'valid',
                'expires' => '01.01.2030'
            ])
        ];
    }
    return $pre;
}, 10, 3);

// ====================================================================
// 5. Elementor Pro Templates Download Override
// ====================================================================

add_filter('pre_http_request', function ($pre, $parsed_args, $url) {
    if (strpos($url, 'my.elementor.com/api/connect/v1/library/get_template_content') !== false) {
        $response = wp_remote_get(
            "https://s3.ir-thr-at1.arvanstorage.ir/elementorfa/library/elementor-pro/New/{$parsed_args['body']['id']}.json",
            ['sslverify' => false, 'timeout' => 25]
        );

        if (wp_remote_retrieve_response_code($response) != 200) {
            $response = wp_remote_get(
                "https://s3.ir-thr-at1.arvanstorage.ir/elementorfa/library/elementor-pro/templates/{$parsed_args['body']['id']}.json",
                ['sslverify' => false, 'timeout' => 25]
            );
        }

        if (wp_remote_retrieve_response_code($response) == 200) {
            return $response;
        }
    }
    return $pre;
}, 10, 3);

// ====================================================================
// 6. Elementor Pro Kits Download Override
// ====================================================================

add_filter('pre_http_request', function ($pre, $parsed_args, $url) {
    if (strpos($url, 'https://my.elementor.com/api/v1/kits-library') !== false) {
        $id = array_slice(explode('/', rtrim($url, '/')), -2)[0];
        $response = wp_remote_get(
            "https://s3.ir-thr-at1.arvanstorage.ir/elementorfa/library/elementor-pro/kits-library/new-kits/{$id}/download-link.json",
            ['sslverify' => false, 'timeout' => 25]
        );
        if (wp_remote_retrieve_response_code($response) != 200) {
            $response = wp_remote_get(
                "https://s3.ir-thr-at1.arvanstorage.ir/elementorfa/library/elementor-pro/kits-library/kits/{$id}/download-link.json",
                ['sslverify' => false, 'timeout' => 25]
            );
        }
        if (wp_remote_retrieve_response_code($response) == 200) {
            return $response;
        }
    }
    return $pre;
}, 10, 3);

// ====================================================================
// 7. Hide/Remove Elementor License Menu Page
// ====================================================================

add_action('admin_menu', function () {
    global $submenu;
    if (!empty($submenu['elementor'])) {
        foreach ($submenu['elementor'] as $key => $item) {
            if (isset($item[2]) && $item[2] === 'elementor-license') {
                unset($submenu['elementor'][$key]);
            }}}
}, 10002);

add_action('admin_menu', function () {
    remove_submenu_page('elementor', 'elementor-license');
    remove_submenu_page('elementor-home', 'elementor-license');
}, 99999);

add_filter('elementor/editor-one/menu/excluded_level4_slugs', function ($slugs) {
    $slugs[] = 'elementor-license';
    return $slugs;
});

add_filter('elementor/editor-one/menu/excluded_level3_slugs', function ($slugs) {
    $slugs[] = 'elementor-license';
    return $slugs;
});

add_action('admin_init', function () {
    if (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX) || (defined('REST_REQUEST') && REST_REQUEST)) return;
    $p = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : '';
    if ($p === 'elementor-license') {
        wp_safe_redirect(admin_url('index.php'));
        exit;
    }
});