<?php
/**
 * Plugin Name:       BitForgeNet Guest Tickets and Door Check-in
 * Plugin URI:        https://bitforgenet.dev/plugins/bitforgenet-guest-tickets/
 * Description:       Professional event, registration, ticketing and door check-in system. Custom events with organizers, venues and line-ups, per-event registration, manual or automated payment validation, unique QR tickets rendered onto designer templates, PDF delivery by email, and live access control at the gate.
 * Version:           1.0.9
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Author:            BitForge Network
 * Author URI:        https://bitforgenet.dev/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       bitforgenet-guest-tickets
 * Domain Path:       /languages
 *
 * @package BitForgeNet_Guest_Tickets
 */

defined( 'ABSPATH' ) || exit;

define( 'BFNET_VERSION', '1.0.9' );
define( 'BFNET_DB_VERSION', '1.4.0' );
define( 'BFNET_FILE', __FILE__ );
define( 'BFNET_PATH', plugin_dir_path( __FILE__ ) );
define( 'BFNET_URL', plugin_dir_url( __FILE__ ) );
define( 'BFNET_BASENAME', plugin_basename( __FILE__ ) );
define( 'BFNET_MIN_PHP', '7.4' );
define( 'BFNET_MIN_WP', '5.8' );

/**
 * Abort with an admin notice when the environment is unsupported.
 *
 * @return bool True when the environment is compatible.
 */
function bfnet_environment_ok() {
	global $wp_version;

	$errors = array();

	if ( version_compare( PHP_VERSION, BFNET_MIN_PHP, '<' ) ) {
		/* translators: 1: required PHP version, 2: current PHP version. */
		$errors[] = sprintf( 'BitForgeNet Guest Tickets requires PHP %1$s or higher. This server runs PHP %2$s.', BFNET_MIN_PHP, PHP_VERSION );
	}
	if ( isset( $wp_version ) && version_compare( $wp_version, BFNET_MIN_WP, '<' ) ) {
		/* translators: 1: required WP version, 2: current WP version. */
		$errors[] = sprintf( 'BitForgeNet Guest Tickets requires WordPress %1$s or higher. This site runs WordPress %2$s.', BFNET_MIN_WP, $wp_version );
	}
	if ( ! extension_loaded( 'gd' ) || ! function_exists( 'imagecreatetruecolor' ) ) {
		$errors[] = 'BitForgeNet Guest Tickets requires the PHP GD extension to render QR codes and tickets. Please ask your host to enable it.';
	}

	if ( empty( $errors ) ) {
		return true;
	}

	add_action(
		'admin_notices',
		static function () use ( $errors ) {
			echo '<div class="notice notice-error"><p><strong>BitForgeNet Guest Tickets</strong></p><ul style="list-style:disc;margin-left:20px">';
			foreach ( $errors as $error ) {
				echo '<li>' . esc_html( $error ) . '</li>';
			}
			echo '</ul></div>';
		}
	);

	return false;
}

/**
 * PSR-ish autoloader for BFNET_* classes.
 *
 * @param string $class_name Fully qualified class name.
 * @return void
 */
function bfnet_autoload( $class_name ) {
	if ( 0 !== strpos( $class_name, 'BFNET_' ) ) {
		return;
	}

	$file = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';

	$dirs = array(
		BFNET_PATH . 'includes/',
		BFNET_PATH . 'includes/models/',
		BFNET_PATH . 'includes/services/',
		BFNET_PATH . 'admin/',
		BFNET_PATH . 'public/',
	);

	foreach ( $dirs as $dir ) {
		$path = $dir . $file;
		if ( is_readable( $path ) ) {
			require_once $path;
			return;
		}
	}
}
spl_autoload_register( 'bfnet_autoload' );

register_activation_hook( __FILE__, array( 'BFNET_Install', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'BFNET_Install', 'deactivate' ) );

/**
 * Boot the plugin.
 *
 * @return void
 */
function bfnet_boot() {
	if ( ! bfnet_environment_ok() ) {
		return;
	}
	BFNET_Plugin::instance();
}
add_action( 'plugins_loaded', 'bfnet_boot', 5 );

/**
 * Convenience accessor for the plugin container.
 *
 * @return BFNET_Plugin
 */
function bfnet_events() {
	return BFNET_Plugin::instance();
}
