<?php
/**
 * Plugin Name:       RawType Order Exporter — Spreadsheet & CSV for WooCommerce
 * Plugin URI:        https://rawtype.dev/rawtype-order-exporter-woocommerce
 * Description:       Export WooCommerce orders, products and customers to Excel or CSV. Visual query builder with smart filters and one-click bulk export.
 * Version:           1.1.0
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Author:            Rawtypedev
 * Author URI:        https://rawtype.dev
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       rawtype-order-exporter-woocommerce
 * Domain Path:       /languages
 * WC requires at least: 8.2
 * WC tested up to:      10.8
 *
 * @package RawTypeOE
 */

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

/**
 * Plugin version.
 */
define( 'RAWTYPE_OE_VERSION', '1.1.0' );

/**
 * Plugin file path.
 */
define( 'RAWTYPE_OE_PLUGIN_FILE', __FILE__ );

/**
 * Plugin directory path.
 */
define( 'RAWTYPE_OE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

/**
 * Plugin directory URL.
 */
define( 'RAWTYPE_OE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/**
 * Plugin basename.
 */
define( 'RAWTYPE_OE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

// Autoload vendor-prefixed dependencies (scoped by Strauss during build).
if ( file_exists( RAWTYPE_OE_PLUGIN_DIR . 'vendor-prefixed/autoload.php' ) ) {
	require_once RAWTYPE_OE_PLUGIN_DIR . 'vendor-prefixed/autoload.php';
}

// Autoload remaining dependencies.
if ( file_exists( RAWTYPE_OE_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
	require_once RAWTYPE_OE_PLUGIN_DIR . 'vendor/autoload.php';
}

// Core includes.
require_once RAWTYPE_OE_PLUGIN_DIR . 'includes/class-plugin.php';
require_once RAWTYPE_OE_PLUGIN_DIR . 'includes/class-activator.php';
require_once RAWTYPE_OE_PLUGIN_DIR . 'includes/class-deactivator.php';

/**
 * Plugin activation hook.
 */
register_activation_hook( __FILE__, array( 'RawTypeOE\\Free\\Activator', 'activate' ) );

/**
 * Plugin deactivation hook.
 */
register_deactivation_hook( __FILE__, array( 'RawTypeOE\\Free\\Deactivator', 'deactivate' ) );

/**
 * Declare WooCommerce HPOS compatibility.
 *
 * @return void
 */
function rawtype_oe_declare_hpos_compatibility() {
	if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
			'custom_order_tables',
			RAWTYPE_OE_PLUGIN_BASENAME,
			true
		);
	}
}
add_action( 'before_woocommerce_init', 'rawtype_oe_declare_hpos_compatibility' );

/**
 * Register an admin notice when activation validation failed.
 *
 * @return void
 */
function rawtype_oe_maybe_show_activation_notice() {
	$activation_error = get_option( 'rawtype_oe_activation_error', '' );

	if ( empty( $activation_error ) ) {
		return;
	}

	add_action( 'admin_notices', 'rawtype_oe_admin_notice_activation_error' );
}

/**
 * Render activation error notice and clear the stored flag.
 *
 * @return void
 */
function rawtype_oe_admin_notice_activation_error() {
	$screen = get_current_screen();

	if ( ! $screen || 'plugins' !== $screen->id ) {
		return;
	}

	$activation_error = get_option( 'rawtype_oe_activation_error', '' );

	if ( empty( $activation_error ) ) {
		return;
	}

	if ( 'unsupported_wc_version' === $activation_error ) {
		$message = __( 'RawType Order Exporter requires WooCommerce 8.2 or newer.', 'rawtype-order-exporter-woocommerce' );
	} else {
		$message = __( 'RawType Order Exporter requires WooCommerce to be installed and active.', 'rawtype-order-exporter-woocommerce' );
	}

	delete_option( 'rawtype_oe_activation_error' );
	?>
	<div class="notice notice-error">
		<p><?php echo esc_html( $message ); ?></p>
	</div>
	<?php
}

/**
 * Show an admin notice when WooCommerce is missing.
 *
 * @return void
 */
function rawtype_oe_admin_notice_missing_wc() {
	$screen = get_current_screen();

	if ( ! $screen || 'plugins' !== $screen->id ) {
		return;
	}

	?>
	<div class="notice notice-error">
		<p>
			<?php
			echo esc_html__(
				'RawType Order Exporter requires WooCommerce to be installed and active.',
				'rawtype-order-exporter-woocommerce'
			);
			?>
		</p>
	</div>
	<?php
}

/**
 * Initialize the plugin.
 *
 * @return void
 */
function rawtype_oe_init() {
	rawtype_oe_maybe_show_activation_notice();

	if ( ! class_exists( 'WooCommerce' ) ) {
		add_action( 'admin_notices', 'rawtype_oe_admin_notice_missing_wc' );

		if ( current_user_can( 'activate_plugins' ) && function_exists( 'deactivate_plugins' ) ) {
			deactivate_plugins( RAWTYPE_OE_PLUGIN_BASENAME );

			// phpcs:ignore WordPress.Security.NonceVerification.Recommended
			if ( isset( $_GET['activate'] ) ) {
				unset( $_GET['activate'] );
			}
		}

		return;
	}

	$plugin = new RawTypeOE\Free\Plugin();
	$plugin->run();
}
add_action( 'plugins_loaded', 'rawtype_oe_init', 10 );
