<?php
/**
 * Plugin Name: Bani Payments for WooCommerce
 * Plugin URI: https://docs.getbani.com/bani-pop/
 * Author: Bani
 * Author URI: https://bani.africa/
 * Description: Start accepting mobile money, cryptocurrency, and bank transfer payments from customers in Africa today.
 * Version: 1.0.9
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * WC requires at least: 4.7
 * WC tested up to: 6.7
 * Text Domain: bani-payments-for-woocommerce
 *
 * Class WC_Gateway_Bani file.
 *
 * @package WooCommerce\Bani
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

define( 'WC_BANI_MAIN_FILE', __FILE__ );
define( 'WC_BANI_URL', untrailingslashit( plugins_url( '/', __FILE__ ) ) );
define( 'WC_BANI_VERSION', '1.0.9' );

if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
	return;
}

/**
 * Declare HPOS (High-Performance Order Storage) compatibility.
 */
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
		);
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
			'cart_checkout_blocks',
			__FILE__,
			true
		);

	}
} );

function bani_payment_init() {
	if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
		add_action( 'admin_notices', 'jcm_wc_bani_wc_missing_notice' );
		return;
	}
	add_action( 'admin_notices', 'jcm_wc_bani_testmode_notice' );

	require_once plugin_dir_path( __FILE__ ) . '/includes/class-wc-payment-gateway-bani.php';

	// +++ ADD after requiring class-wc-payment-gateway-bani.php in woo-bani.php +++
	if ( class_exists( '\Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
		require_once plugin_dir_path( __FILE__ ) . 'includes/class-wc-bani-blocks.php';
		add_action(
			'woocommerce_blocks_payment_method_type_registration',
			function ( \Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
				$payment_method_registry->register( new \WC_Bani_Blocks_Integration() );
			}
		);
	}


	add_filter( 'woocommerce_payment_gateways', 'add_to_woo_bani_payment_gateway' );
	add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'jcm_woo_bani_plugin_action_links' );
}
add_action( 'plugins_loaded', 'bani_payment_init', 99 );

function jcm_woo_bani_plugin_action_links( $links ) {
	$settings_link = array(
		'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=bani' ) . '" title="' . esc_attr__( 'View Bani WooCommerce Settings', 'bani-payments-for-woocommerce' ) . '">' . esc_html__( 'Settings', 'bani-payments-for-woocommerce' ) . '</a>',
	);
	return array_merge( $settings_link, $links );
}

function jcm_wc_bani_wc_missing_notice() {
	echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Bani requires WooCommerce to be installed and active. Click %s to install WooCommerce.', 'bani-payments-for-woocommerce' ), '<a href="' . admin_url( 'plugin-install.php?tab=plugin-information&plugin=woocommerce&TB_iframe=true&width=772&height=539' ) . '" class="thickbox open-plugin-details-modal">' . esc_html__( 'here', 'bani-payments-for-woocommerce' ) . '</a>' ) . '</strong></p></div>';
}

function add_to_woo_bani_payment_gateway( $gateways ) {
	$gateways[] = 'WC_Gateway_Bani';
	return $gateways;
}

function jcm_wc_bani_testmode_notice() {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$bani_settings = get_option( 'woocommerce_bani_settings' );
	$test_mode     = isset( $bani_settings['testmode'] ) ? $bani_settings['testmode'] : '';

	if ( 'yes' === $test_mode ) {
		echo '<div class="error"><p>' . sprintf( esc_html__( 'Bani test mode is still enabled, Click <strong><a href="%s">here</a></strong> to disable it when you want to start accepting live payment on your site.', 'bani-payments-for-woocommerce' ), esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=bani' ) ) ) . '</p></div>';
	}
}
