<?php
/**
 * Plugin Name: Custom Attributes Manager
 * Description: Manage custom HTML attributes for accessibility.
 * Version: 1.0.1
 * Author: Kesz1 Technologies LLC
 * Text Domain: custom-attributes-manager
 * Domain Path: /languages
 * License:     GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

    if ( ! defined( 'ABSPATH' ) ) {
       exit; // Exit if accessed directly
    }
    
    // Create a helper function for easy SDK access.
    function camanager_fs() {
        global $camanager_fs;

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

            $camanager_fs = fs_dynamic_init( array(
                'id'                  => '21362',
                'slug'                => 'custom-attributes-manager',
                'type'                => 'plugin',
                'public_key'          => 'pk_6515a3e43f270928895ca8dfa13fb',
                'is_premium'          => false,
                'is_org_compliant'    => true,
                'has_premium_version' => true,
                'has_addons'          => false,
                'has_paid_plans'      => false,
                'menu'                => array(
                    'first-path'     => 'plugins.php',
                    'contact'        => false,
                    'support'        => false,
                ),
            ) );
        }

        return $camanager_fs;
    }

    // Init Freemius.
    camanager_fs();
    // Signal that SDK was initiated.
    do_action( 'camanager_fs_loaded' );


// Define constants
define('CAMANAGER_PATH', plugin_dir_path(__FILE__));
define('CAMANAGER_URL', plugin_dir_url(__FILE__));
define('CAMANAGER_VERSION', '1.0.0');


// Includes
require_once CAMANAGER_PATH . 'includes/class-camanager-admin.php';
require_once CAMANAGER_PATH . 'includes/class-camanager-frontend.php';

// Init classes
new CAMANAGER_Admin();
new CAMANAGER_Frontend();

// 1) Define the uninstall routine IN THIS FILE.
final class CAMANAGER_Uninstaller {
    public static function run() {
        // Cleanup for the current site
        self::cleanup_site( get_current_blog_id() );

        // Multisite cleanup if plugin is network-activated
        if ( is_multisite() ) {
            global $wpdb;

            /**
             * Direct DB query justified:
             * WordPress doesn't provide an API to list all blogs.
             * This query runs only during plugin uninstall for cleanup.
             */
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
            $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );

            foreach ( $blog_ids as $blog_id ) {
                switch_to_blog( (int) $blog_id );
                self::cleanup_site( $blog_id );
                restore_current_blog();
            }
        }
    }

    private static function cleanup_site( $blog_id ) {
        // Delete plugin options and transient
        delete_option( 'camanager_attributes' );
        delete_option( 'camanager_settings' );
        delete_transient( 'camanager_some_cache' );

        // Clear scheduled events
        wp_clear_scheduled_hook( 'camanager_daily_event' );

        global $wpdb;
        $table = esc_sql( $wpdb->prefix . 'camanager_records' );

        /**
         * Direct database query for uninstall cleanup.
         * Safe because:
         * - `$table` is sanitized via esc_sql()
         * - This runs only during uninstall (no user input)
         * - `$wpdb->prepare()` cannot be used for table names
         * - No caching is relevant for schema changes
         */
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
        $wpdb->query( "DROP TABLE IF EXISTS `$table`" );

    }
}

// 2) Register the uninstall hook pointing to a static method in THIS FILE.
register_uninstall_hook( __FILE__, [ 'CAMANAGER_Uninstaller', 'run' ] );
