<?php
/**
 * Plugin Name: Cryptoniq PRO
 * Plugin URI: https://cryptoniq.io
 * Description: Accept Bitcoin, Ethereum and 20+ cryptocurrencies directly to your wallets — no third-party signup, no withdrawal fees. Free version supports BTC/ETH; PRO unlocks more coins, stablecoins (USDT/USDC), and instant payment detection.
 * Version: 2.0.1
 * Author: DivEngine
 * Author URI: https://t.me/div_engine
 * Text Domain: cryptoniq-pro
 * Domain Path: /languages
 */

if ( ! defined( 'ABSPATH' ) ) exit();

// === Define constants ===
if ( ! defined( 'CRYPTONIQ_VER' ) ) define( 'CRYPTONIQ_VER', '2.0.1' );
if ( ! defined( 'CRYPTONIQ_DIR_URL' ) ) define( 'CRYPTONIQ_DIR_URL', plugin_dir_url( __FILE__ ) );
if ( ! defined( 'CRYPTONIQ_DIR_PATH' ) ) define( 'CRYPTONIQ_DIR_PATH', plugin_dir_path( __FILE__ ) );
if ( ! defined( 'CRYPTONIQ_PAY_ID' ) ) define( 'CRYPTONIQ_PAY_ID', 'cryptoniq' );
if ( ! defined( 'CRYPTONIQ_OPTION' ) ) define( 'CRYPTONIQ_OPTION', 'cryptoniq_option' );
if ( ! defined( 'CRYPTONIQ_API_URL' ) ) define( 'CRYPTONIQ_API_URL', 'https://api.cryptoniq.io/' );


// === Main Plugin Class ===
class Cryptoniq_Engine {
	private $files = array(
		'admin/index.php',
		'gateway.php',
		'functions.php',
		'ajax.php',
		'prices.php',
		'prefixes.php',
	);

	// Public-explorer polling checkers. Loaded only when no license key is
	// set — with an active license, the API server pushes webhooks instead
	// and these files never run.
	private $lite_files = array(
		'request.php',
		'coins/index.php',
	);

	function __construct() {
		// Delay localization until WordPress is ready
		add_action( 'init', array( $this, 'load_textdomain' ) );

		// Continue setup
		$this->include_files();
		add_action( 'init', array( $this, 'register_assets' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ), 9999 );
	}

	private function include_files() {
		foreach ( $this->files as $file ) {
			require_once( CRYPTONIQ_DIR_PATH . $file );
		}

		// Polling fallback only when there's no license — with a license,
		// webhooks handle detection. cryptoniq_is_pro() lives in
		// functions.php which is loaded above.
		if ( function_exists( 'cryptoniq_is_pro' ) && ! cryptoniq_is_pro() ) {
			foreach ( $this->lite_files as $file ) {
				require_once( CRYPTONIQ_DIR_PATH . $file );
			}
		}
	}

	public function load_textdomain() {
		load_plugin_textdomain( 'cryptoniq-pro', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
	}

	public function register_assets() {
		wp_register_style( 'cryptoniq-libs-ionicons',
			CRYPTONIQ_DIR_URL . 'assets/libs/ionicons/css/ionicons.css',
			array(), CRYPTONIQ_VER );

		wp_register_style( 'cryptoniq-engine',
			CRYPTONIQ_DIR_URL . 'assets/css/cryptoniq.engine.css',
			array(), CRYPTONIQ_VER );

		wp_register_script( 'cryptoniq-libs',
			CRYPTONIQ_DIR_URL . 'assets/js/cryptoniq.libs.js',
			array( 'jquery' ), CRYPTONIQ_VER, false );

		wp_register_script( 'cryptoniq-engine',
			CRYPTONIQ_DIR_URL . 'assets/js/cryptoniq.engine.js',
			array( 'jquery' ), CRYPTONIQ_VER, false );

		wp_localize_script( 'cryptoniq-engine', 'cryptoniq_paybox_ajax_data', array(
			'url'   => admin_url( 'admin-ajax.php' ),
			'nonce' => wp_create_nonce( 'cryptoniq_front_ajax_nonce' )
		));
	}

	public function enqueue_assets() {
		wp_enqueue_style( 'cryptoniq-libs-ionicons' );
		wp_enqueue_style( 'cryptoniq-engine' );
		wp_enqueue_script( 'cryptoniq-libs' );
		wp_enqueue_script( 'cryptoniq-engine' );
	}
}
new Cryptoniq_Engine();

// === CRON: Update Prices ===
function cryptoniq_cron_update_prices( $schedules ) {
	$schedules['cryptoniq_updater_30m'] = array(
		'interval' => 60 * 60,
		'display'  => esc_html__( 'Every 30 Minutes', 'cryptoniq-pro' )
	);
	return $schedules;
}
add_filter( 'cron_schedules', 'cryptoniq_cron_update_prices' );

function cryptoniq_price_options_updater() {
	$currencies = cryptoniq_get_currency_prices();
	if ( is_array( $currencies ) && isset( $currencies['EUR'] ) ) {
		update_option( 'cryptoniq_currency_prices', $currencies, 'yes' );
	}

	$prices = cryptoniq_get_coin_prices();
	if ( is_array( $prices ) && isset( $prices['BTC'] ) ) {
		update_option( 'cryptoniq_coin_prices', $prices, 'yes' );
	}
}
add_action( 'cryptoniq_cron_update_prices', 'cryptoniq_price_options_updater' );

/**
 * Do the stored price/currency rows look usable? — license-aware check.
 *
 *   No license: API returns just BTC + EUR, so that's the minimum.
 *   Licensed:   full dataset, so we expect a licensed-only key like
 *               ETH_ARB too. If it's missing, the cache predates the
 *               license activation and needs a refresh.
 */
function cryptoniq_prices_data_is_fresh() {
	$prices     = get_option( 'cryptoniq_coin_prices' );
	$currencies = get_option( 'cryptoniq_currency_prices' );

	if ( ! is_array( $prices ) || ! is_array( $currencies ) ) {
		return false;
	}
	if ( ! isset( $currencies['EUR'] ) || ! isset( $prices['BTC'] ) ) {
		return false;
	}
	if ( cryptoniq_is_pro() && ! isset( $prices['ETH_ARB'] ) ) {
		// License just activated → cache predates it, refresh to pull the
		// full coin set.
		return false;
	}
	return true;
}

// === Option Check ===
function cryptoniq_check_options() {
	if ( ! cryptoniq_prices_data_is_fresh() ) {
		cryptoniq_price_options_updater();
	}
}
add_action( 'init', 'cryptoniq_check_options', 5 );

// === Cron Activation / Deactivation ===
function cryptoniq_cron_activation() {
	if ( ! wp_next_scheduled( 'cryptoniq_cron_update_prices' ) ) {
		wp_schedule_event( time(), 'cryptoniq_updater_30m', 'cryptoniq_cron_update_prices' );
	}

	// Immediately fetch prices on activation if not present
	if ( ! cryptoniq_prices_data_is_fresh() ) {
		cryptoniq_price_options_updater();
	}
}
register_activation_hook( __FILE__, 'cryptoniq_cron_activation' );

function cryptoniq_cron_deactivation() {
	wp_clear_scheduled_hook( 'cryptoniq_cron_update_prices' );
}
register_deactivation_hook( __FILE__, 'cryptoniq_cron_deactivation' );
