<?php
/**
 * Plugin Name: Carticy Conversion Tracking with GA4, Google Ads, and Meta Pixel for WooCommerce
 * Plugin URI: https://carticy.com/plugins/carticy-conversions-for-woocommerce/
 * Description: Reliable, consent-respecting WooCommerce conversion tracking for GA4, Google Ads, and Meta Pixel - tracked from the order, not the thank-you page.
 * Version: 1.0.0
 * Author: Carticy
 * Author URI: https://carticy.com
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: carticy-conversions-for-woocommerce
 * Domain Path: /languages
 * Requires at least: 6.0
 * Tested up to: 7.0
 * Requires PHP: 8.0
 * Requires Plugins: woocommerce
 * WC requires at least: 8.0
 * WC tested up to: 10.4.3
 *
  *
 * @package Carticy\Conversions
 */

declare(strict_types=1);

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Freemius integration for free/premium version handling.
 *
 * DO NOT REMOVE THIS IF. It allows the premium version to set the correct
 * basename and coexist with the WordPress.org version during upgrades.
 */
if ( function_exists( 'carticy_conversions_fs' ) ) {
	$carticy_conversions_existing_fs = carticy_conversions_fs();

	if ( is_object( $carticy_conversions_existing_fs ) && method_exists( $carticy_conversions_existing_fs, 'set_basename' ) ) {
		$carticy_conversions_existing_fs->set_basename( true, __FILE__ );
	}
} else {
	define( 'CARTICY_CONVERSIONS_VERSION', '1.0.0' );
	define( 'CARTICY_CONVERSIONS_FILE', __FILE__ );
	define( 'CARTICY_CONVERSIONS_PATH', plugin_dir_path( __FILE__ ) );
	define( 'CARTICY_CONVERSIONS_URL', plugin_dir_url( __FILE__ ) );
	define( 'CARTICY_CONVERSIONS_BASENAME', plugin_basename( __FILE__ ) );
	define( 'CARTICY_CONVERSIONS_REST_NAMESPACE', 'carticy-conversions/v1' );

	if ( file_exists( CARTICY_CONVERSIONS_PATH . 'vendor/autoload.php' ) ) {
		require_once CARTICY_CONVERSIONS_PATH . 'vendor/autoload.php';
	} else {
		spl_autoload_register(
			static function ( string $class_name ): void {
				$prefix   = 'Carticy\\Conversions\\';
				$base_dir = CARTICY_CONVERSIONS_PATH . 'src/';
				$length   = strlen( $prefix );

				if ( 0 !== strncmp( $prefix, $class_name, $length ) ) {
					return;
				}

				$relative_class = substr( $class_name, $length );
				$file           = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';

				if ( file_exists( $file ) ) {
					require_once $file;
				}
			}
		);
	}

	if ( file_exists( CARTICY_CONVERSIONS_PATH . 'src/licensing.php' ) ) {
		require_once CARTICY_CONVERSIONS_PATH . 'src/licensing.php';
	}

	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
				);
				\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
					'cart_checkout_blocks',
					__FILE__,
					true
				);
			}
		}
	);

	/**
	 * Check whether the plugin can boot.
	 *
	 * @return bool True when runtime requirements are met.
	 */
	function carticy_conversions_check_requirements(): bool {
		if ( ! class_exists( \Carticy\Conversions\Core\Requirements::class ) ) {
			return false;
		}

		return \Carticy\Conversions\Core\Requirements::is_met();
	}

	/**
	 * Initialize the plugin runtime.
	 */
	function carticy_conversions_init(): void {
		if ( ! carticy_conversions_check_requirements() ) {
			return;
		}

		if ( class_exists( \Carticy\Conversions\Init::class ) ) {
			\Carticy\Conversions\Init::get_instance();
		}
	}
	add_action( 'plugins_loaded', 'carticy_conversions_init', 20 );

	/**
	 * Activation hook.
	 */
	function carticy_conversions_activate(): void {
		if ( class_exists( \Carticy\Conversions\Core\Activator::class ) ) {
			\Carticy\Conversions\Core\Activator::activate();
		}
	}
	register_activation_hook( __FILE__, 'carticy_conversions_activate' );

	/**
	 * Deactivation hook.
	 */
	function carticy_conversions_deactivate(): void {
		if ( class_exists( \Carticy\Conversions\Core\Deactivator::class ) ) {
			\Carticy\Conversions\Core\Deactivator::deactivate();
		}
	}
	register_deactivation_hook( __FILE__, 'carticy_conversions_deactivate' );

	/**
	 * Add plugin action links.
	 *
	 * @param array<int|string, string> $links Existing plugin action links.
	 * @return array<int|string, string>
	 */
	function carticy_conversions_plugin_action_links( array $links ): array {
		$freemius = function_exists( 'carticy_conversions_fs' ) ? carticy_conversions_fs() : null;
		if ( is_object( $freemius )
			&& method_exists( $freemius, 'is_paying' )
			&& method_exists( $freemius, 'get_upgrade_url' )
			&& ! $freemius->is_paying()
		) {
			$links[] = '<a href="' . esc_url( $freemius->get_upgrade_url() ) . '" style="color: #00a32a; font-weight: 600;">'
				. esc_html__( 'Upgrade', 'carticy-conversions-for-woocommerce' ) . '</a>';
		}

		return $links;
	}
	add_filter( 'plugin_action_links_' . CARTICY_CONVERSIONS_BASENAME, 'carticy_conversions_plugin_action_links' );
}
