<?php
/**
 * Plugin Name: Botcrawl Bot Blocker
 * Plugin URI:  https://sera.guru/products/botcrawl-bot-blocker/
 * Description: Professional bot detection, enforcement, live monitoring, and geo-analytics for WordPress. 7-day free trial - then $9.99/year.
 * Version:     2.6.2
 * Author:      Botcrawl
 * Author URI:  https://botcrawl.com/
 * License:     Proprietary
 * Text Domain: botcrawl-bot-blocker
 * Requires at least: 6.0
 * Requires PHP: 8.0
 */

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

// Prevent double-load if the original bot-blocker.php is also active.
if ( defined( 'BOT_BLOCKER_FILE' ) ) {
	return;
}

define( 'BOTCRAWL_BOT_BLOCKER_VERSION', '2.6.2' );
define( 'BOT_BLOCKER_VERSION',      '2.6.2' ); // kept for internal class compatibility
define( 'BOT_BLOCKER_FILE',         __FILE__ );
define( 'BOT_BLOCKER_DIR',          plugin_dir_path( __FILE__ ) );
define( 'BOT_BLOCKER_URL',          plugin_dir_url( __FILE__ ) );

// ── Core classes ──────────────────────────────────────────────────────────────
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-settings.php';
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-storage.php';
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-geo.php';
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-detector.php';
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-engine.php';
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-log-table.php';
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-admin.php';

// ── License & Pro ─────────────────────────────────────────────────────────────
require_once BOT_BLOCKER_DIR . 'includes/class-sera-license.php';
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-pro.php';

// ── Update checker ───────────────────────────────────────────────────────────
require_once BOT_BLOCKER_DIR . 'includes/class-botblocker-updater.php';

// ── Lifecycle hooks ───────────────────────────────────────────────────────────
register_activation_hook( __FILE__, function ( $network_wide ) {
	if ( $network_wide && is_multisite() ) {
		// Activate on every site in the network.
		$sites = get_sites( [ 'number' => 500, 'fields' => 'ids' ] );
		foreach ( $sites as $site_id ) {
			switch_to_blog( (int) $site_id );
			BotBlocker_Storage::activate();
			SeraLicense::maybe_start_trial();
			restore_current_blog();
		}
	} else {
		BotBlocker_Storage::activate();
		SeraLicense::maybe_start_trial();
	}
} );

// When a new site is added to the network, set up its tables automatically.
add_action( 'wp_initialize_site', function ( $new_site ) {
	if ( is_plugin_active_for_network( plugin_basename( BOT_BLOCKER_FILE ) ) ) {
		switch_to_blog( $new_site->blog_id );
		BotBlocker_Storage::activate();
		SeraLicense::maybe_start_trial();
		// Propagate network license key if set.
		$network_key = get_site_option( 'bot_blocker_network_license_key', '' );
		if ( $network_key !== '' ) {
			update_option( 'sera_bb_license_key', $network_key );
		}
		restore_current_blog();
	}
} );

register_deactivation_hook( __FILE__, [ 'BotBlocker_Storage', 'deactivate' ] );
register_uninstall_hook( __FILE__,    [ 'BotBlocker_Storage', 'uninstall' ] );

// ── Bootstrap ─────────────────────────────────────────────────────────────────
add_action( 'plugins_loaded', function () {
	load_plugin_textdomain(
		'botcrawl-bot-blocker',
		false,
		dirname( plugin_basename( BOT_BLOCKER_FILE ) ) . '/languages'
	);

	// License system initialises first - sets trial state and registers notices.
	SeraLicense::init();

	// Update checker - hooks into WP native update mechanism.
	BotBlocker_Updater::init();

	// Core admin UI always loads so the license prompt is always accessible.
	BotBlocker_Settings::init();
	BotBlocker_Admin::init();

	// Engine, storage, geo, and Pro features only run when the license is active
	// (trial period OR valid paid license key).
	if ( SeraLicense::is_active() ) {
		BotBlocker_Storage::init();
		BotBlocker_Geo::init();
		BotBlocker_Detector::init();
		BotBlocker_Engine::init();
		BotBlocker_Pro::init();
	}
} );
