<?php
/**
 * Plugin Name:       CartCue - Sales Automation for WooCommerce
 * Plugin URI:        https://www.userelements.com/cartcue-woocommerce-automation/
 * Description:       Simple ecommerce automations that take 2 minutes to launch. Set-and-forget automation for WooCommerce. Recover abandoned carts, welcome new customers, and win back lapsed buyers — no monthly SaaS fee, no data leaving your server.
 * Version:           1.0.3
 * Author:            UserElements
 * Author URI:        https://userelements.com
 * License:           GPL-2.0+
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       cartcue-sales-automation
 * Domain Path:       /languages
 * Requires PHP:      7.4
 * Requires at least: 6.2
 * Requires Plugins:  woocommerce
 * WC requires at least: 3.6
 * WC tested up to:   9.9
 */

defined( 'ABSPATH' ) || exit;

if ( version_compare( PHP_VERSION, '7.4', '<' ) ) {
	add_action(
		'admin_notices',
		static function (): void {
			echo '<div class="notice notice-error"><p>';
			echo esc_html(
				sprintf(
					/* translators: %s: minimum PHP version */
					__( 'CartCue requires PHP %s or newer.', 'cartcue-sales-automation' ),
					'7.4'
				)
			);
			echo '</p></div>';
		}
	);
	return;
}

/**
 * Yield to CartCue Pro when it is active — Pro supersedes the free plugin.
 */
if ( ! function_exists( 'is_plugin_active' ) ) {
	require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( is_plugin_active( 'cartcue-sales-automation-pro/cartcue-sales-automation.php' ) ) {
	return;
}

define( 'CARTCUE_VERSION',    '1.0.3' );
define( 'CARTCUE_PLUGIN_FILE', __FILE__ );
define( 'CARTCUE_PLUGIN_DIR',  plugin_dir_path( __FILE__ ) );
define( 'CARTCUE_PLUGIN_URL',  plugin_dir_url( __FILE__ ) );

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

/** PSR-4 autoloader for the CartCue\ namespace. */
spl_autoload_register( static function ( string $class ): void {
	$prefix   = 'CartCue\\';
	$base_dir = CARTCUE_PLUGIN_DIR . 'includes/';
	$len      = strlen( $prefix );
	if ( strncmp( $prefix, $class, $len ) !== 0 ) {
		return;
	}
	$file = $base_dir . str_replace( '\\', DIRECTORY_SEPARATOR, substr( $class, $len ) ) . '.php';
	if ( file_exists( $file ) ) {
		require $file;
	}
} );

/** Load translations before any __() calls (WordPress 6.7+). */
add_action(
	'init',
	static function (): void {
		// phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound -- required on init before translated strings on WP 6.7+.
		load_plugin_textdomain(
			'cartcue-sales-automation',
			false,
			dirname( plugin_basename( CARTCUE_PLUGIN_FILE ) ) . '/languages'
		);
	},
	0
);

/** Boot after WooCommerce and translations are ready. */
add_action(
	'init',
	static function (): void {
		if ( ! class_exists( 'WooCommerce' ) ) {
			if ( is_admin() ) {
				add_action(
					'admin_notices',
					static function (): void {
						echo '<div class="notice notice-error"><p>';
						esc_html_e( 'CartCue requires WooCommerce to be installed and active.', 'cartcue-sales-automation' );
						echo '</p></div>';
					}
				);
			}
			return;
		}
		CartCue\Plugin::instance();
	},
	20
);

register_activation_hook(   __FILE__, [ 'CartCue\Installer', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'CartCue\Installer', 'deactivate' ] );
