<?php

/**
 * Plugin Name: Tendforge Statement Forge for EAA
 * Plugin URI:  https://tendforge.com/en/tendforge-statement-forge-for-eaa/
 * Description: A zero-config wizard that generates the EU/EAA accessibility statement page and a feedback mechanism. It produces a self-declared disclosure document — it never claims your site is compliant and it is not an accessibility overlay.
 * Version:     1.0.14
 * 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-statement-forge-for-eaa
 * Domain Path: /languages
 * Requires at least: 6.5
 * Requires PHP: 7.4
 */
defined( 'ABSPATH' ) || exit;
if ( function_exists( 'tsffe_fs' ) ) {
    // Another Tendforge Statement Forge for EAA build is already loaded — tell Freemius this file is the
    // premium basename so it can auto-deactivate the free copy (prevents redeclare).
    tsffe_fs()->set_basename( false, __FILE__ );
} else {
    if ( !defined( 'TSFFE_VERSION' ) ) {
        define( 'TSFFE_VERSION', '1.0.14' );
        define( 'TSFFE_PLUGIN_FILE', __FILE__ );
        define( 'TSFFE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
        define( 'TSFFE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    }
    // ── Freemius SDK ──────────────────────────────────────────────────────────
    if ( !function_exists( 'tsffe_fs' ) ) {
        function tsffe_fs() {
            global $tsffe_fs;
            if ( !isset( $tsffe_fs ) ) {
                require_once TSFFE_PLUGIN_DIR . 'vendor/freemius/start.php';
                $tsffe_fs = fs_dynamic_init( array(
                    'id'               => '33569',
                    'slug'             => 'tendforge-statement-forge-for-eaa',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_765c111980b631438afe939dda12a',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'anonymous_mode'   => true,
                    'menu'             => array(
                        'slug'       => 'tsffe-statement-forge',
                        'first-path' => 'plugins.php',
                        'support'    => false,
                        'contact'    => false,
                        'pricing'    => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $tsffe_fs;
        }

        tsffe_fs();
        do_action( 'tsffe_fs_loaded' );
    }
    register_deactivation_hook( __FILE__, function () {
        wp_clear_scheduled_hook( 'tsffep_review_reminder' );
    } );
    // 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;
    // the published statement page content is intentionally left intact.
    if ( function_exists( 'tsffe_fs' ) ) {
        tsffe_fs()->add_action( 'after_uninstall', function () {
            $tsffe_cleanup = function () {
                delete_option( 'tsffe_statement' );
                delete_option( 'tsffe_page_id' );
                delete_option( 'tsffe_footer_link' );
                // Pro options.
                delete_option( 'tsffep_agency' );
                delete_option( 'tsffep_annex_v' );
                delete_option( 'tsffep_annex_v_page_id' );
                delete_option( 'tsffep_history' );
                delete_option( 'tsffep_reminder_settings' );
                delete_option( 'tsffep_statement_lang' );
                wp_clear_scheduled_hook( 'tsffep_review_reminder' );
            };
            if ( is_multisite() ) {
                foreach ( get_sites( array(
                    'fields' => 'ids',
                    'number' => 0,
                ) ) as $tsffe_site_id ) {
                    switch_to_blog( $tsffe_site_id );
                    $tsffe_cleanup();
                    restore_current_blog();
                }
            } else {
                $tsffe_cleanup();
            }
            // The Pro agency defaults can also be stored network-wide on multisite,
            // where delete_option() cannot reach them.
            delete_site_option( 'tsffep_agency' );
        } );
    }
    add_action( 'init', function () {
        load_plugin_textdomain( 'tendforge-statement-forge-for-eaa', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
    } );
    // "Settings" link on the Plugins screen. (Freemius adds Upgrade / Activate License.)
    add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) {
        if ( !(function_exists( 'tsffe_fs' ) && tsffe_fs()->can_use_premium_code()) ) {
            $links['tsffe_go_pro'] = '<a href="' . esc_url( 'https://checkout.freemius.com/plugin/33569/plan/55220/' ) . '" target="_blank" rel="noopener" style="color:#15803d;font-weight:600;">' . esc_html__( 'Go Pro', 'tendforge-statement-forge-for-eaa' ) . '</a>';
        }
        $links = array(
            'tsffe_settings' => '<a href="' . esc_url( admin_url( 'admin.php?page=tsffe-statement-forge' ) ) . '">' . esc_html__( 'Settings', 'tendforge-statement-forge-for-eaa' ) . '</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 () {
        require_once TSFFE_PLUGIN_DIR . 'includes/class-tsffe-bodies.php';
        require_once TSFFE_PLUGIN_DIR . 'includes/class-tsffe-data.php';
        require_once TSFFE_PLUGIN_DIR . 'includes/class-tsffe-render.php';
        require_once TSFFE_PLUGIN_DIR . 'includes/class-tsffe-page.php';
        require_once TSFFE_PLUGIN_DIR . 'includes/class-tsffe-block.php';
        require_once TSFFE_PLUGIN_DIR . 'includes/class-tsffe-wizard.php';
        require_once TSFFE_PLUGIN_DIR . 'includes/class-tsffe-ajax.php';
        TSFFE_Wizard::init();
        TSFFE_Ajax::init();
        TSFFE_Page::init();
        TSFFE_Block::init();
        // ── Pro features (loaded only for licensed users; stripped from the free build) ──
        $tsffe_use_premium = function_exists( 'tsffe_fs' ) && tsffe_fs()->can_use_premium_code() || defined( 'TSFFE_DEV_PREMIUM' ) && TSFFE_DEV_PREMIUM;
        if ( $tsffe_use_premium ) {
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-annex-v.php';
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-i18n.php';
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-history.php';
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-reminders.php';
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-export.php';
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-meta.php';
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-agency.php';
            require_once TSFFE_PLUGIN_DIR . 'includes/premium/class-tsffep-ui.php';
            TSFFEP_AnnexV::init();
            TSFFEP_I18n::init();
            TSFFEP_History::init();
            TSFFEP_Reminders::init();
            TSFFEP_Export::init();
            TSFFEP_Meta::init();
            TSFFEP_Agency::init();
            TSFFEP_UI::init();
        }
    }, 20 );
}