<?php
/**
 * Plugin Name:       HJCyberX Checkout for Paddle Lite
 * Plugin URI:        https://plugins.hjcyberx.com/plugins/hjcyberx-checkout-paddle#features
 * Description:       A free WooCommerce Payment Gateway for Paddle Billing (v2 API). Upgrade to Pro for Vault Access, Auto-Fetch, and Subscriptions!
 * Version:           1.0.2
 * Requires at least: 6.0
 * Requires PHP:      8.0
 * Author:            hatimjavaid
 * Author URI:        https://hjcyberx.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       hjcyberx-checkout-for-paddle-lite
 * Domain Path:       /languages
 * WC requires at least: 7.0
 * WC tested up to:   9.0
 *
 * @package HJCyberX Checkout for Paddle Lite
 */

// ──────────────────────────────────────────────.
// Prevent direct access.
// ──────────────────────────────────────────────.
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// ──────────────────────────────────────────────.
// Plugin constants.
// ──────────────────────────────────────────────.
define( 'PADDLEKONNECT_LITE_VERSION', '1.0.2' );
define( 'PADDLEKONNECT_LITE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PADDLEKONNECT_LITE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'PADDLEKONNECT_LITE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

// ──────────────────────────────────────────────.
// HPOS Compatibility.
// ──────────────────────────────────────────────.
add_action(
	'before_woocommerce_init',
	function () {
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
				'custom_order_tables',
				__FILE__,
				true
			);
		}
	}
);

// ──────────────────────────────────────────────.
// PHP version guard.
// ──────────────────────────────────────────────.
if ( version_compare( PHP_VERSION, '8.0', '<' ) ) {
	add_action(
		'admin_notices',
		function () {
			printf(
				'<div class="notice notice-error"><p>%s</p></div>',
				esc_html__(
					'HJCyberX Checkout for Paddle Lite requires PHP 8.0 or higher. Please upgrade your PHP version.',
					'hjcyberx-checkout-for-paddle-lite'
				)
			);
		}
	);
	return;
}

// ──────────────────────────────────────────────.
// WooCommerce dependency guard.
// Runs after all plugins have loaded to check.
// whether WooCommerce is active.
// ──────────────────────────────────────────────.
add_action(
	'plugins_loaded',
	function () {

		if ( ! class_exists( 'WooCommerce' ) ) {
			add_action(
				'admin_notices',
				function () {
					printf(
						'<div class="notice notice-error"><p>%s</p></div>',
						esc_html__(
							'HJCyberX Checkout for Paddle Lite requires WooCommerce to be installed and active.',
							'hjcyberx-checkout-for-paddle-lite'
						)
					);
				}
			);
			return;
		}

		// WooCommerce is present — bootstrap the plugin.
		require_once PADDLEKONNECT_LITE_PLUGIN_DIR . 'includes/class-paddlekonnect-lite-loader.php';
		PaddleKonnect_Lite_Loader::init();
	},
	10
);

// ──────────────────────────────────────────────.
// Activation hook.
// ──────────────────────────────────────────────.
register_activation_hook(
	__FILE__,
	function () {
		flush_rewrite_rules();
	}
);

// ──────────────────────────────────────────────.
// Deactivation hook.
// ──────────────────────────────────────────────.
register_deactivation_hook(
	__FILE__,
	function () {
		flush_rewrite_rules();
	}
);

// ──────────────────────────────────────────────.
// Plugin Action Links.
// ──────────────────────────────────────────────.
add_filter(
	'plugin_action_links_' . PADDLEKONNECT_LITE_PLUGIN_BASENAME,
	function ( $links ) {
		$custom_link = '<a href="https://hjcyberx.com/" target="_blank" style="color: #0073aa; font-weight: bold;">Visit Website</a>';
		array_unshift( $links, $custom_link );
		return $links;
	}
);
