<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link    https://chillichalli.com
 * @since   1.1.7
 * @package CardOracle
 *
 * @wordpress-plugin
 * Plugin Name:       Card Oracle
 * Plugin URI:        https://chillichalli.com/card-oracle
 * Description:       This plugin lets you create tarot and oracle readings using your own cards, spreads and interpretations.
 * Version:           1.3.1
 * Author:            ChilliChalli
 * Author URI:        https://chillichalli.com
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       card-oracle
 *
 */
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
    die;
}
// Load the free-mode safe stub used when the Freemius SDK cannot initialize.
require_once __DIR__ . '/includes/class-card-oracle-fs-safe-stub.php';
/**
 * Setup the Freemius SDK integration
 *
 * @since 0.22.0
 */
if ( function_exists( 'card_oracle_fs' ) ) {
    try {
        card_oracle_fs()->set_basename( false, __FILE__ );
    } catch ( Throwable $e ) {
        error_log( 'Card Oracle: Freemius set_basename failed: ' . $e->getMessage() );
    }
} else {
    if ( !function_exists( 'card_oracle_fs' ) ) {
        /**
         * Create a helper function for easy SDK access.
         *
         * Wraps the Freemius SDK initialization in a Throwable guard so that
         * an upstream Freemius failure (API 429, PHP fatal in the SDK, etc.)
         * does not lock administrators out of wp-admin. On failure the plugin
         * runs in safe-mode using Card_Oracle_FS_Safe_Stub.
         */
        function card_oracle_fs() {
            global $card_oracle_fs;
            if ( !isset( $card_oracle_fs ) ) {
                try {
                    // Include Freemius SDK.
                    include_once __DIR__ . '/vendor/autoload.php';
                    $card_oracle_fs = fs_dynamic_init( array(
                        'id'               => '6063',
                        'slug'             => 'card-oracle',
                        'type'             => 'plugin',
                        'public_key'       => 'pk_f85b93aca5d64925a5271e7ca7e01',
                        'is_premium'       => false,
                        'has_addons'       => false,
                        'has_paid_plans'   => true,
                        'has_affiliation'  => 'selected',
                        'menu'             => array(
                            'slug' => 'card-oracle-admin-menu',
                        ),
                        'is_live'          => true,
                        'is_org_compliant' => true,
                    ) );
                } catch ( Throwable $e ) {
                    error_log( 'Card Oracle: Freemius SDK init failed, falling back to safe stub: ' . $e->getMessage() );
                    $card_oracle_fs = new Card_Oracle_FS_Safe_Stub();
                }
                // Defensive: if init succeeded but produced something we can't use, swap to stub.
                if ( !is_object( $card_oracle_fs ) || !method_exists( $card_oracle_fs, 'can_use_premium_code__premium_only' ) ) {
                    error_log( 'Card Oracle: Freemius SDK returned unexpected object, falling back to safe stub.' );
                    $card_oracle_fs = new Card_Oracle_FS_Safe_Stub();
                }
            }
            return $card_oracle_fs;
        }

        // Init Freemius.
        card_oracle_fs();
        // Signal that SDK was initiated.
        do_action( 'card_oracle_fs_loaded' );
    }
    /**
     * The code that runs during plugin activation.
     * This action is documented in includes/class-card-oracle-activator.php
     */
    function activate_card_oracle() {
        include_once plugin_dir_path( __FILE__ ) . 'includes/class-card-oracle-activator.php';
        Card_Oracle_Activator::activate();
    }

    /**
     * The code that runs during plugin deactivation.
     * This action is documented in includes/class-card-oracle-deactivator.php
     */
    function deactivate_card_oracle() {
        include_once plugin_dir_path( __FILE__ ) . 'includes/class-card-oracle-deactivator.php';
        Card_Oracle_Deactivator::deactivate();
    }

    register_activation_hook( __FILE__, 'activate_card_oracle' );
    register_deactivation_hook( __FILE__, 'deactivate_card_oracle' );
    /**
     * The core plugin class that is used to define internationalization,
     * admin-specific hooks, and public-facing site hooks.
     */
    include plugin_dir_path( __FILE__ ) . 'includes/class-card-oracle.php';
    /**
     * Begins execution of the plugin.
     *
     * Since everything within the plugin is registered via hooks,
     * then kicking off the plugin from this point in the file does
     * not affect the page life cycle.
     *
     * @since 0.5.0
     */
    function run_card_oracle() {
        $plugin = new Card_Oracle();
        $plugin->run();
    }

    run_card_oracle();
}