<?php
/**
 * Plugin Name:          TextTango
 * Description:          Send SMS from WordPress and WooCommerce with TextTango — passwordless OTP login, transactional order notifications, and delivery receipts.
 * Version:              0.20.3
 * Requires at least:    6.4
 * Requires PHP:         8.1
 * Author:               TextTango
 * Author URI:           https://texttango.com
 * License:              GPL-2.0-or-later
 * License URI:          https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:          texttango
 * Domain Path:          /languages
 * WC requires at least: 8.0
 * WC tested up to:      10.9
 *
 * @package TextTango
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

const TEXTTANGO_VERSION    = '0.20.3';
const TEXTTANGO_DB_VERSION = '10';
const TEXTTANGO_MIN_PHP    = '8.1';
define( 'TEXTTANGO_FILE', __FILE__ );
define( 'TEXTTANGO_PATH', plugin_dir_path( __FILE__ ) );
define( 'TEXTTANGO_URL', plugin_dir_url( __FILE__ ) );

/**
 * Render a dependency failure as an admin notice.
 *
 * Used before the autoloader is available, so it cannot rely on any plugin class.
 *
 * @param string $message Already-translated message.
 */
function texttango_bootstrap_failure( string $message ): void {
	add_action(
		'admin_notices',
		static function () use ( $message ) {
			printf(
				'<div class="notice notice-error"><p><strong>%s</strong> %s</p></div>',
				esc_html__( 'TextTango:', 'texttango' ),
				esc_html( $message )
			);
		}
	);
}

if ( version_compare( PHP_VERSION, TEXTTANGO_MIN_PHP, '<' ) ) {
	texttango_bootstrap_failure(
		sprintf(
			/* translators: 1: required PHP version, 2: current PHP version. */
			esc_html__( 'requires PHP %1$s or newer. This site runs PHP %2$s.', 'texttango' ),
			TEXTTANGO_MIN_PHP,
			PHP_VERSION
		)
	);
	return;
}

if ( ! file_exists( TEXTTANGO_PATH . 'vendor/autoload.php' ) ) {
	texttango_bootstrap_failure(
		esc_html__( 'dependencies are missing. Run "composer install" in the plugin directory.', 'texttango' )
	);
	return;
}

require_once TEXTTANGO_PATH . 'vendor/autoload.php';

// Third-party libraries, namespaced under TextTango\Vendor\ by Strauss so they
// can never collide with another plugin's copy (spec §12).
if ( file_exists( TEXTTANGO_PATH . 'vendor-prefixed/autoload.php' ) ) {
	require_once TEXTTANGO_PATH . 'vendor-prefixed/autoload.php';
}

// Action Scheduler resolves the newest registered copy across every plugin on the
// site, so it is deliberately NOT prefixed and must be loaded at plugin-load time.
require_once TEXTTANGO_PATH . 'vendor/woocommerce/action-scheduler/action-scheduler.php';

/*
 * The public API (spec §18). Required at plugin-load time rather than autoloaded,
 * so the functions exist for any plugin that loads before this one boots — a
 * caller cannot be expected to know this plugin's load order.
 */
require_once TEXTTANGO_PATH . 'src/api.php';

if ( defined( 'WP_CLI' ) && WP_CLI ) {
	WP_CLI::add_command( 'texttango', TextTango\Cli\Commands::class );
}

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

add_action(
	'before_woocommerce_init',
	static function () {
		if ( ! class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			return;
		}

		// High-Performance Order Storage and block checkout (spec §12, §16).
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', TEXTTANGO_FILE, true );
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', TEXTTANGO_FILE, true );
	}
);

add_action( 'plugins_loaded', array( TextTango\Plugin::class, 'instance' ) );
