<?php
/**
 * Plugin Name:             Tami Payment Gateway for WooCommerce
 * Plugin URI:              https://www.tami.com.tr/
 * Description:             The exact payment method you want for WooCommerce: Tami
 * Version:                 1.5
 * Requires at least:       5.2
 * Requires PHP:            7.2
 * Author:                  grilabs
 * Author URI:              https://www.gri.net/
 * WC requires at least:    7.5
 * WC tested up to:         7.9.0
 * Text Domain:             tami-payment
 * Domain Path:             /languages
 * License:                 LGPL v3 or later
 * License URI:             http://www.gnu.org/licenses/lgpl-3.0.txt
 */

use Automattic\WooCommerce\Utilities\OrderUtil;

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

define( 'TAMI_MIN_WC_VER', '6.0' );
define( 'TAMI_WOOCOMMERCE_MODULE_VERSION', '1.5' );
define( 'TAMI_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
function tami_woocommerce_missing_notice() {
	/* translators: 1. URL link. */
	echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Tami WooCommerce gateway requires WooCommerce to be installed and active. You can download %s here.', 'tami-payment' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
}

function tami_woocommerce_wc_not_supported() {
	/* translators: $1. Minimum WooCommerce version. $2. Current WooCommerce version. */
	echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Tami requires WooCommerce %1$s or greater to be installed and active. WooCommerce %2$s is no longer supported.', 'tami-payment' ), esc_html( TAMI_MIN_WC_VER ), esc_html( WC_VERSION ) ) . '</strong></p></div>';
}

include TAMI_PLUGIN_PATH . '/init.php';

add_action( 'init', 'tamiwc_gateway_tami_init' );
add_action( 'plugins_loaded', function () {
	load_plugin_textdomain( 'tami-payment', false, plugin_basename( TAMI_PLUGIN_PATH ) . '/languages' );
} );

add_action( 'plugins_loaded', function () {
	tami_woocommerce_init();
	add_action( 'woocommerce_blocks_loaded', 'tami_woocommerce_block_support' );
} );

function tamiwc_gateway_tami_init() {
	if ( ! class_exists( 'WooCommerce' ) ) {
		add_action( 'admin_notices', 'tami_woocommerce_missing_notice' );

		return;
	}

	if ( version_compare( WC_VERSION, TAMI_MIN_WC_VER, '<' ) ) {
		add_action( 'admin_notices', 'tami_woocommerce_wc_not_supported' );

		return;
	}
}

function tami_isHposDisabled(): bool {
	return ! class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) ||
	       ! method_exists( OrderUtil::class, 'custom_orders_table_usage_is_enabled' ) ||
	       ! OrderUtil::custom_orders_table_usage_is_enabled();
}

// HPOS Support
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 );
	}
} );