<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. It loads the Composer autoloader, registers the activation and
 * deactivation hooks, and boots the core plugin class.
 *
 * @link              https://wordpress.org/plugins/easy-shipping-rate/
 * @since             1.0.0
 * @package           Esraw
 *
 * @wordpress-plugin
 * Plugin Name:       Easy Shipping Rate for WooCommerce
 * Plugin URI:        https://wordpress.org/plugins/easy-shipping-rate/
 * Description:       Create flexible, conditional shipping methods for WooCommerce based on cart total, weight, dimensions, products, user role, location and more.
 * Version:           1.3.0
 * Requires at least: 5.9
 * Requires PHP:      7.4
 * Author:            FLINIMI
 * Author URI:        https://profiles.wordpress.org/azouamauriac/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       easy-shipping-rate
 * Domain Path:       /src/languages
 * Requires Plugins:  woocommerce
 * WC requires at least: 5.9
 * WC tested up to:   10.8
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 */
define( 'ESRAW_VERSION', '1.3.0' );
define( 'ESRAW_FILE', __FILE__ );
define( 'ESRAW_PATH', plugin_dir_path( __FILE__ ) );
define( 'ESRAW_URL', plugin_dir_url( __FILE__ ) );

/**
 * Future gate for a Pro version. While false, no Account/Upgrade menu, no upsell
 * notice, and is_premium() always returns false. Freemius Insights boots
 * independently of this flag — the opt-in analytics run on the free plan regardless.
 */
if ( ! defined( 'ESRAW_PRO_ENABLED' ) ) {
	define( 'ESRAW_PRO_ENABLED', false );
}

/**
 * Recipient for the optional, consent-based lifecycle notifications
 * (install / activation / deactivation). This is the plugin developer's own
 * address; e-mails are only ever sent after the site administrator opts in.
 */
if ( ! defined( 'ESRAW_NOTIFY_EMAIL' ) ) {
	define( 'ESRAW_NOTIFY_EMAIL', 'azouamauriac@gmail.com' );
}

/**
 * Composer autoloader. It registers the plugin's PSR-4 classes (EasyShippingRate\*),
 * the namespaced helper functions (src/Support/functions.php) and the Freemius SDK
 * (vendor/freemius/wordpress-sdk/start.php) via Composer's "files" autoload.
 *
 * Bail with an admin notice rather than white-screening if dependencies were not
 * installed (someone cloned the repo without running "composer install").
 */
if ( ! file_exists( ESRAW_PATH . 'vendor/autoload.php' ) ) {
	add_action(
		'admin_notices',
		function () {
			echo '<div class="notice notice-error"><p>';
			echo esc_html__( 'Easy Shipping Rate for WooCommerce: dependencies are missing. Please contact the plugin support at azouamauriac@gmail.com', 'easy-shipping-rate' );
			echo '</p></div>';
		}
	);
	return;
}
require_once ESRAW_PATH . 'vendor/autoload.php';

if ( ! function_exists( 'esraw_fs' ) ) {
	/**
	 * Single Freemius SDK instance for the plugin.
	 *
	 * Boots in free-only mode (Insights): shows the opt-in analytics modal and
	 * records installs and deactivations, but adds no Account/Upgrade screens and
	 * exposes no Pro features. The id and public_key come from the Freemius
	 * dashboard (Settings -> Keys).
	 *
	 * @return Freemius|null
	 */
	function esraw_fs() {
		global $esraw_fs;

		if ( isset( $esraw_fs ) ) {
			return $esraw_fs;
		}

		// Guard the call so a stripped vendor folder cannot fatal-error the whole site.
		if ( ! function_exists( 'fs_dynamic_init' ) ) {
			return null;
		}

		$esraw_fs = fs_dynamic_init(
			array(
				'id'               => '32398',
				'slug'             => 'easy-shipping-rate',
				'type'             => 'plugin',
				'public_key'       => 'pk_336452b62484834cc402ea9460778',
				'is_premium'       => false,
				'has_addons'       => false,
				'has_paid_plans'   => false,
				'show_trial'       => false,
				'is_org_compliant' => true,
				'menu'             => array(
					'slug'    => 'easy_shipping_rate',
					'support' => false,
				),
				'is_live'          => true,
			)
		);

		return $esraw_fs;
	}

	// Boot Freemius as early as possible, then let other code hook in.
	esraw_fs();
	do_action( 'esraw_fs_loaded' );

	// Route uninstall cleanup through Freemius: it iterates every site on
	// multisite for us, so the callback only needs single-site logic.
	if ( esraw_fs() ) {
		esraw_fs()->add_action( 'after_uninstall', array( \EasyShippingRate\Uninstaller::class, 'run' ) );
	}
}

register_activation_hook( __FILE__, array( \EasyShippingRate\Activator::class, 'activate' ) );
register_deactivation_hook( __FILE__, array( \EasyShippingRate\Deactivator::class, 'deactivate' ) );

// Consent-based developer notifications. Registered independently of WooCommerce
// so the opt-in can be answered and honoured on any admin screen.
add_action( 'admin_init', array( \EasyShippingRate\Notifications::class, 'handle_consent_action' ) );
add_action( 'admin_init', array( \EasyShippingRate\Notifications::class, 'flush_pending' ) );
add_action( 'admin_enqueue_scripts', array( \EasyShippingRate\Notifications::class, 'enqueue_assets' ) );
add_action( 'admin_footer', array( \EasyShippingRate\Notifications::class, 'render_consent_modal' ) );

// Free shipping notice for the Checkout block (StoreAPI + notices store).
add_action( 'woocommerce_blocks_loaded', array( \EasyShippingRate\Frontend\Blocks\FreeShippingNotice::class, 'boot' ) );

/**
 * Declare HPOS and Cart/Checkout Blocks compatibility: this plugin only
 * registers a shipping method and never touches the orders datastore.
 */
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 );
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
		}
	}
);

/** Admin notice shown when the required WooCommerce plugin is inactive. */
function esraw_woocommerce_missing_notice() {
	echo '<div class="notice notice-error"><p>';
	echo esc_html__( 'Easy Shipping Rate for WooCommerce requires WooCommerce to be installed and active.', 'easy-shipping-rate' );
	echo '</p></div>';
}

/**
 * Bootstrap on plugins_loaded so WC_Shipping_Method (our parent class) exists,
 * and bail with a notice rather than fatally erroring when WooCommerce is absent.
 */
function run_esraw() {

	if ( ! class_exists( 'WooCommerce' ) ) {
		add_action( 'admin_notices', 'esraw_woocommerce_missing_notice' );
		return;
	}

	$plugin = new \EasyShippingRate\Plugin();
	$plugin->run();
}
add_action( 'plugins_loaded', 'run_esraw' );

/**
 * Add a "Settings" shortcut to the plugins list.
 *
 * @param array $links Existing action links.
 * @return array
 */
function esraw_plugin_action_links( $links ) {
	$settings_url  = admin_url( 'admin.php?page=easy_shipping_rate' );
	$settings_link = '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Settings', 'easy-shipping-rate' ) . '</a>';
	array_unshift( $links, $settings_link );
	return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'esraw_plugin_action_links' );
