<?php

/**
 * Plugin Name: Tendforge Widget Auditor for Elementor
 * Plugin URI:  https://tendforge.com/en/widget-auditor/
 * Description: Find out which Elementor addon widgets your site actually uses. Scan every page, spot unused widgets and entire unused addons, and cut the bloat safely.
 * Version:     1.2.3
 * Author:      Tendforge
 * Author URI:  https://tendforge.com
 * License:     GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: tendforge-widget-auditor-for-elementor
 * Domain Path: /languages
 * Requires at least: 6.5
 * Requires PHP: 7.4
 * Requires Plugins: elementor
 * Elementor tested up to: 3.35
 */
defined( 'ABSPATH' ) || exit;
if ( function_exists( 'tfwa_fs' ) ) {
    // Another Widget Auditor build is already loaded — tell Freemius this file is the
    // premium basename so it can auto-deactivate the free copy (prevents redeclare).
    tfwa_fs()->set_basename( false, __FILE__ );
} else {
    if ( !defined( 'TFWA_VERSION' ) ) {
        define( 'TFWA_VERSION', '1.2.3' );
        define( 'TFWA_PLUGIN_FILE', __FILE__ );
        define( 'TFWA_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
        define( 'TFWA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    }
    // ── Freemius SDK ──────────────────────────────────────────────────────────
    // Single-plugin freemium: this is the premium codebase; Freemius auto-builds
    // the free (wp.org) version by stripping /includes/premium/ (@fs_premium_only).
    if ( !function_exists( 'tfwa_fs' ) ) {
        function tfwa_fs() {
            global $tfwa_fs;
            if ( !isset( $tfwa_fs ) ) {
                require_once TFWA_PLUGIN_DIR . 'vendor/freemius/start.php';
                $tfwa_fs = fs_dynamic_init( array(
                    'id'               => '33554',
                    'slug'             => 'tendforge-widget-auditor-for-elementor',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_65ed004523893dd666135a679874e',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'anonymous_mode'   => true,
                    'menu'             => array(
                        'slug'       => 'tfwa-widget-auditor',
                        'first-path' => 'plugins.php',
                        'support'    => false,
                        'contact'    => false,
                        'pricing'    => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $tfwa_fs;
        }

        tfwa_fs();
        do_action( 'tfwa_fs_loaded' );
    }
    register_deactivation_hook( __FILE__, function () {
        wp_clear_scheduled_hook( 'tfwap_scheduled_scan' );
    } );
    // Data cleanup on uninstall — via Freemius (replaces the WP-native uninstall.php,
    // which Freemius's deployment does not allow). Only removes this plugin's own data.
    if ( function_exists( 'tfwa_fs' ) ) {
        tfwa_fs()->add_action( 'after_uninstall', function () {
            $tfwa_cleanup = function () {
                delete_option( 'tfwa_scan_results' );
                delete_option( 'tfwap_settings' );
                // The bulk-disable restore snapshot. NEVER delete elementor_disabled_elements:
                // that option belongs to Elementor and may hold the user's own manual choices.
                delete_option( 'tfwap_disabled_snapshot' );
                wp_clear_scheduled_hook( 'tfwap_scheduled_scan' );
            };
            if ( is_multisite() ) {
                foreach ( get_sites( array(
                    'fields' => 'ids',
                    'number' => 0,
                ) ) as $tfwa_site_id ) {
                    switch_to_blog( $tfwa_site_id );
                    $tfwa_cleanup();
                    restore_current_blog();
                }
            } else {
                $tfwa_cleanup();
            }
        } );
    }
    add_action( 'init', function () {
        // Bundled translations; wordpress.org language packs take precedence once available.
        load_plugin_textdomain( 'tendforge-widget-auditor-for-elementor', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
    } );
    // "Settings" link on the Plugins screen. (Freemius adds Upgrade / Activate License.)
    add_filter( 'plugin_action_links_' . plugin_basename( TFWA_PLUGIN_FILE ), function ( $links ) {
        if ( !(function_exists( 'tfwa_fs' ) && tfwa_fs()->can_use_premium_code()) ) {
            $links['tfwa_go_pro'] = '<a href="' . esc_url( 'https://checkout.freemius.com/plugin/33554/plan/55183/' ) . '" target="_blank" rel="noopener" style="color:#15803d;font-weight:600;">' . esc_html__( 'Go Pro', 'tendforge-widget-auditor-for-elementor' ) . '</a>';
        }
        $links = array(
            'tfwa_settings' => '<a href="' . esc_url( admin_url( 'admin.php?page=tfwa-widget-auditor' ) ) . '">' . esc_html__( 'Settings', 'tendforge-widget-auditor-for-elementor' ) . '</a>',
        ) + $links;
        return $links;
    } );
    // Remove Freemius's optional "Opt In" (reconnect) action link. In anonymous mode opt-in is
    // optional, and its connect screen collides with our own admin menu. Activate License stays.
    add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) {
        foreach ( $links as $k => $html ) {
            if ( is_string( $html ) && (false !== strpos( $html, 'fs_action=' ) || false !== strpos( $html, '-pricing' )) ) {
                unset($links[$k]);
            }
        }
        return $links;
    }, 10000 );
    add_action( 'plugins_loaded', function () {
        if ( !did_action( 'elementor/loaded' ) ) {
            add_action( 'admin_notices', function () {
                echo '<div class="notice notice-error"><p>' . esc_html__( 'Widget Auditor for Elementor requires Elementor to be installed and active.', 'tendforge-widget-auditor-for-elementor' ) . '</p></div>';
            } );
            return;
        }
        require_once TFWA_PLUGIN_DIR . 'includes/class-tfwa-widget-map.php';
        require_once TFWA_PLUGIN_DIR . 'includes/class-tfwa-footprint.php';
        require_once TFWA_PLUGIN_DIR . 'includes/class-tfwa-scanner.php';
        require_once TFWA_PLUGIN_DIR . 'includes/class-tfwa-admin-page.php';
        require_once TFWA_PLUGIN_DIR . 'includes/class-tfwa-ajax.php';
        TFWA_Admin_Page::init();
        TFWA_Ajax::init();
        // ── Pro features (loaded only for licensed users; stripped from the free build) ──
        $tfwa_use_premium = function_exists( 'tfwa_fs' ) && tfwa_fs()->can_use_premium_code() || defined( 'TFWA_DEV_PREMIUM' ) && TFWA_DEV_PREMIUM;
        // dev/CI bypass
        if ( $tfwa_use_premium ) {
            foreach ( array(
                'class-tfwap-actions.php',
                'class-tfwap-dependencies.php',
                'class-tfwap-export.php',
                'class-tfwap-schedule.php',
                'class-tfwap-ui.php'
            ) as $tfwa_pf ) {
                require_once TFWA_PLUGIN_DIR . 'includes/premium/' . $tfwa_pf;
            }
            TFWAP_Dependencies::init();
            TFWAP_Schedule::init();
            TFWAP_Export::init();
            TFWAP_UI::init();
        }
    }, 20 );
}