<?php

/**
 * Plugin Name: Spellmatic
 * Plugin URI:        https://spellmatic.com
 * Description:       Full-site spell check, grammar correction, and AI-powered proofreading for WordPress. Scan every post, page, product, and builder layout in one click — then fix errors individually or in bulk.
 * Version:           1.3.9
 * Requires at least: 6.0
 * Requires PHP:      8.0
 * Author:            Spellmatic
 * Author URI:        https://spellmatic.com/about
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       spellmatic
 * Domain Path:       /languages
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// ─── Freemius SDK ─────────────────────────────────────────────────────────────
if ( function_exists( 'spellmatic_fs' ) ) {
    // Freemius version auto-switching.
    spellmatic_fs()->set_basename( false, __FILE__ );
} else {
    /**
     * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
     * `function_exists` CALL ABOVE TO PROPERLY WORK.
     */
    if ( !function_exists( 'spellmatic_fs' ) ) {
        // Create a helper function for easy SDK access.
        function spellmatic_fs() {
            global $spellmatic_fs;
            if ( !isset( $spellmatic_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $spellmatic_fs = fs_dynamic_init( array(
                    'id'               => '26325',
                    'slug'             => 'spellmatic',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_8ec31ec4ee09eb5ecb673243dd113',
                    'is_premium'       => false,
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'       => 'spellmatic',
                        'first-path' => 'admin.php?page=spellmatic',
                        'support'    => false,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $spellmatic_fs;
        }

        // Init Freemius.
        spellmatic_fs();
        // Signal that SDK was initiated.
        do_action( 'spellmatic_fs_loaded' );
        // ─── Freemius uninstall hook ──────────────────────────────────────────
        // Freemius overrides WordPress's standard uninstall.php mechanism via
        // register_uninstall_hook(). Fire our cleanup through Freemius's
        // 'after_uninstall' action instead.
        spellmatic_fs()->add_action( 'after_uninstall', 'spellmatic_cleanup_on_uninstall' );
    }
    // ─── Plugin logic starts here ─────────────────────────────────────────────
    // ─── Constants ────────────────────────────────────────────────────────────
    define( 'SPELLMATIC_VERSION', '1.3.9' );
    define( 'SPELLMATIC_FILE', __FILE__ );
    define( 'SPELLMATIC_DIR', plugin_dir_path( __FILE__ ) );
    define( 'SPELLMATIC_URL', plugin_dir_url( __FILE__ ) );
    define( 'SPELLMATIC_BASENAME', plugin_basename( __FILE__ ) );
    function spellmatic_cleanup_on_uninstall() {
        global $wpdb;
        // Always clean up: cron hooks & transients.
        wp_clear_scheduled_hook( 'spellmatic_digest' );
        delete_transient( 'spellmatic_current_scan' );
        // Only drop tables + options if the site owner opted in.
        $settings = get_option( 'spellmatic_settings', [] );
        $delete_data = !empty( $settings['delete_data_on_uninstall'] );
        if ( $delete_data ) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Intentional table cleanup on uninstall.
            $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}spellmatic_results" );
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Intentional table cleanup on uninstall.
            $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}spellmatic_scans" );
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Intentional table cleanup on uninstall.
            $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}spellmatic_fix_history" );
            $options = [
                'spellmatic_version',
                'spellmatic_settings',
                'spellmatic_onboarding_complete',
                'spellmatic_backup_dismissed',
                'spellmatic_byok',
                'spellmatic_or_model',
                'spellmatic_scan_credits',
                'spellmatic_dictionary'
            ];
            foreach ( $options as $option ) {
                delete_option( $option );
            }
        }
    }

    // ─── License hooks (after SDK + classes load) ─────────────────────────────
    add_action( 'plugins_loaded', function () {
        if ( class_exists( 'Spellmatic_License' ) ) {
            Spellmatic_License::register_fs_hooks();
        }
    }, 5 );
    // priority 5 — before Core boots at 10
    // ─── Autoload core classes ────────────────────────────────────────────────
    $spellmatic_classes = [
        'Spellmatic_License'          => SPELLMATIC_DIR . 'includes/class-license.php',
        'Spellmatic_Dictionary'       => SPELLMATIC_DIR . 'includes/class-dictionary.php',
        'Spellmatic_Scheduler'        => SPELLMATIC_DIR . 'includes/class-scheduler.php',
        'Spellmatic_Core'             => SPELLMATIC_DIR . 'includes/class-core.php',
        'Spellmatic_ContentExtractor' => SPELLMATIC_DIR . 'includes/class-content-extractor.php',
        'Spellmatic_Scanner'          => SPELLMATIC_DIR . 'includes/class-scanner.php',
        'Spellmatic_Harper'           => SPELLMATIC_DIR . 'includes/class-harper.php',
        'Spellmatic_Fixer'            => SPELLMATIC_DIR . 'includes/class-fixer.php',
        'Spellmatic_BYOK'             => SPELLMATIC_DIR . 'includes/class-byok.php',
        'Spellmatic_Dashboard'        => SPELLMATIC_DIR . 'admin/class-dashboard.php',
    ];
    foreach ( $spellmatic_classes as $spellmatic_class => $spellmatic_file ) {
        if ( file_exists( $spellmatic_file ) ) {
            require_once $spellmatic_file;
        }
    }
    // ─── Activation / Deactivation hooks ──────────────────────────────────────
    register_activation_hook( __FILE__, ['Spellmatic_Core', 'activate'] );
    register_deactivation_hook( __FILE__, ['Spellmatic_Core', 'deactivate'] );
    // ─── Boot ─────────────────────────────────────────────────────────────────
    add_action( 'plugins_loaded', function () {
        // Run DB migrations on version upgrade (activation hook only fires on first install).
        $stored_version = get_option( 'spellmatic_version', '0.0.0' );
        if ( version_compare( $stored_version, SPELLMATIC_VERSION, '<' ) ) {
            Spellmatic_Core::create_tables();
            update_option( 'spellmatic_version', SPELLMATIC_VERSION );
        }
        Spellmatic_Core::get_instance();
    }, 10 );
}