<?php
/**
 * Plugin Name:       Playard — Sports Facility Booking System for Courts, Fields, Pitches, Venues and More
 * Plugin URI:        https://github.com/devmasum52/playard
 * Description:       Sports facility booking plugin. Let customers book fields, courts, and pitches directly from your WordPress website — no SaaS, full control.
 * Version:           1.0.3
 * Author:            Masum Ahmad
 * Author URI:        https://masumahmad.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       playard
 * Domain Path:       /languages
 * Requires at least: 6.0
 * Requires PHP:      8.0
 */

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

define( 'PLAYARD_VERSION',  '1.0.3' );
define( 'PLAYARD_PATH',     plugin_dir_path( __FILE__ ) );
define( 'PLAYARD_URL',      plugin_dir_url( __FILE__ ) );
define( 'PLAYARD_MIN_PHP',  '8.0' );
define( 'PLAYARD_MIN_WP',   '6.0' );

// Notice functions must be defined BEFORE the early returns so they are
// always available when WordPress fires admin_notices, even on PHP < 8.0.
function playard_php_version_notice() {
	echo '<div class="notice notice-error"><p>' .
		sprintf(
			/* translators: 1: required PHP version, 2: current PHP version */
			esc_html__( 'Playard requires PHP %1$s or higher. You are running PHP %2$s. Please upgrade PHP to activate Playard.', 'playard' ),
			esc_html( PLAYARD_MIN_PHP ),
			esc_html( PHP_VERSION )
		) . '</p></div>';
}

function playard_wp_version_notice() {
	echo '<div class="notice notice-error"><p>' .
		sprintf(
			/* translators: 1: required WordPress version, 2: current WordPress version */
			esc_html__( 'Playard requires WordPress %1$s or higher. You are running WordPress %2$s. Please upgrade WordPress to activate Playard.', 'playard' ),
			esc_html( PLAYARD_MIN_WP ),
			esc_html( get_bloginfo( 'version' ) )
		) . '</p></div>';
}

if ( version_compare( PHP_VERSION, PLAYARD_MIN_PHP, '<' ) ) {
	add_action( 'admin_notices', 'playard_php_version_notice' );
	// Deactivate to prevent fatal errors from PHP 8.0+ syntax in included files.
	add_action( 'admin_init', function () {
		deactivate_plugins( plugin_basename( __FILE__ ) );
	} );
	return;
}

if ( version_compare( get_bloginfo( 'version' ), PLAYARD_MIN_WP, '<' ) ) {
	add_action( 'admin_notices', 'playard_wp_version_notice' );
	add_action( 'admin_init', function () {
		deactivate_plugins( plugin_basename( __FILE__ ) );
	} );
	return;
}

require_once PLAYARD_PATH . 'includes/functions.php';
require_once PLAYARD_PATH . 'includes/class-playard-helpers.php';
require_once PLAYARD_PATH . 'includes/class-playard-install.php';
require_once PLAYARD_PATH . 'includes/class-playard.php';

add_filter( 'cron_schedules', [ 'Playard_Install', 'register_cron_intervals' ] );

register_activation_hook( __FILE__, [ 'Playard_Install', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'Playard_Install', 'deactivate' ] );

add_action( 'plugins_loaded', 'playard_init' );

function playard_init(): void {
	Playard_Install::maybe_upgrade();
	Playard::instance();
}
