<?php
/**
 * Plugin Name: Payment Gateway for ZainPay for WooCommerce
 * Plugin URI: https://ibrahim.ng/payment-gateway-for-zainpay-for-woocommerce
 * Description: WooCommerce payment gateway for ZainPay
 * Version: 1.0.4
 * Author: Ibrahim Nasir
 * Author URI: https://ibrahim.ng
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Requires Plugins: woocommerce
 * Requires at least: 6.2
 * Requires PHP: 7.4
 * WC requires at least: 8.0
 * WC tested up to: 10.9
 * Text Domain: payment-gateway-for-zainpay-for-woocommerce
 * Domain Path: /languages
 */

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

function pgzw_blocks_support() {
	if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
		require_once __DIR__ . '/includes/class-pgzw-blocks-support.php';
		add_action(
			'woocommerce_blocks_payment_method_type_registration',
			function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
				$payment_method_registry->register( new PGZW_Blocks_Support() );
			}
		);
	}
}

define( 'PGZW_MAIN_FILE', __FILE__ );
define( 'PGZW_URL', untrailingslashit( plugins_url( '/', __FILE__ ) ) );
define( 'PGZW_VERSION', '1.0.4' );

function pgzw_init() {

	if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
		add_action( 'admin_notices', 'pgzw_missing_notice' );
		return;
	}

	add_action( 'admin_notices', 'pgzw_testmode_notice' );

	if ( ! class_exists( 'Zainpay\SDK\Engine' ) ) {
		$sdk_autoload_path = __DIR__ . '/vendor/autoload.php';
		if ( file_exists( $sdk_autoload_path ) ) {
			require_once $sdk_autoload_path;
		}
		if ( ! class_exists( 'Zainpay\SDK\Engine' ) ) {
			add_action( 'admin_notices', function() {
				if ( wp_doing_ajax() || ! is_admin() ) {
					return;
				}
				$screen = get_current_screen();
				if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins' ) ) ) {
					return;
				}
				echo '<div class="notice notice-error"><p>' . esc_html__( 'ZainPay SDK autoloader not found. Please run composer install in the plugin root.', 'payment-gateway-for-zainpay-for-woocommerce' ) . '</p></div>';
			} );
			return;
		}
	}

	require_once __DIR__ . '/includes/class-pgzw-utils.php';
	require_once __DIR__ . '/includes/class-pgzw-gateway.php';

	add_action( 'wp_ajax_pgzw_process_inline_payment', 'pgzw_process_inline_payment_ajax' );
	add_action( 'wp_ajax_nopriv_pgzw_process_inline_payment', 'pgzw_process_inline_payment_ajax' );
	add_action( 'wp_ajax_pgzw_create_order_and_init_payment', 'pgzw_create_order_and_init_payment_ajax' );
	add_action( 'wp_ajax_nopriv_pgzw_create_order_and_init_payment', 'pgzw_create_order_and_init_payment_ajax' );
	add_action( 'wp_ajax_pgzw_check_payment_status', 'pgzw_check_payment_status_ajax' );
	add_action( 'wp_ajax_nopriv_pgzw_check_payment_status', 'pgzw_check_payment_status_ajax' );
	add_action( 'wp_ajax_pgzw_record_txn_ref', 'pgzw_record_txn_ref_ajax' );
	add_action( 'wp_ajax_nopriv_pgzw_record_txn_ref', 'pgzw_record_txn_ref_ajax' );

	add_filter( 'woocommerce_payment_gateways', 'pgzw_add_zainpay_gateway', 99 );

	add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'pgzw_plugin_action_links' );
}
add_action( 'plugins_loaded', 'pgzw_init', 99 );
add_action( 'woocommerce_blocks_loaded', 'pgzw_blocks_support' );
add_action( 'in_admin_header', 'pgzw_filter_admin_notices_for_settings_page', 999 );

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 );
	}
} );

function pgzw_plugin_action_links( $links ) {
	$settings_link = array(
		'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=pgzw_zainpay' ) . '" title="' . __( 'View ZainPay WooCommerce Settings', 'payment-gateway-for-zainpay-for-woocommerce' ) . '">' . __( 'Settings', 'payment-gateway-for-zainpay-for-woocommerce' ) . '</a>',
	);
	return array_merge( $settings_link, $links );
}

function pgzw_add_zainpay_gateway( $methods ) {
	$methods[] = 'PGZW_Gateway';
	return $methods;
}

function pgzw_filter_admin_notices_for_settings_page() {
	if ( ! is_admin() ) {
		return;
	}
	$screen = get_current_screen();
	if ( ! $screen || 'woocommerce_page_wc-settings' !== $screen->id ) {
		return;
	}
	$page    = sanitize_key( filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?: '' );
	$tab     = sanitize_key( filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?: '' );
	$section = sanitize_key( filter_input( INPUT_GET, 'section', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?: '' );

	if ( 'wc-settings' !== $page || 'checkout' !== $tab || 'pgzw_zainpay' !== $section ) {
		return;
	}
	remove_all_actions( 'admin_notices' );
	remove_all_actions( 'all_admin_notices' );
	pgzw_testmode_notice();
}

function pgzw_missing_notice() {
	if ( wp_doing_ajax() || ! is_admin() ) {
		return;
	}
	$screen = get_current_screen();
	if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins' ) ) ) {
		return;
	}
	/* translators: %s: WooCommerce download link */
	echo '<div class="error"><p><strong>' . wp_kses_post( sprintf( __( 'ZainPay requires WooCommerce to be installed and active. You can download %s here.', 'payment-gateway-for-zainpay-for-woocommerce' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) ) . '</strong></p></div>';
}

function pgzw_testmode_notice() {
	if ( wp_doing_ajax() || ! is_admin() ) {
		return;
	}
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}
	$screen = get_current_screen();
	if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'woocommerce_page_wc-settings', 'plugins' ) ) ) {
		return;
	}
	$zainpay_settings = get_option( 'woocommerce_pgzw_zainpay_settings' );
	if ( isset( $zainpay_settings['enabled'] ) && 'yes' === $zainpay_settings['enabled'] && isset( $zainpay_settings['testmode'] ) && 'yes' === $zainpay_settings['testmode'] ) {
		/* translators: %s: WooCommerce settings link */
		echo '<div class="notice notice-warning is-dismissible"><p>' . wp_kses_post( sprintf( __( 'ZainPay 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.', 'payment-gateway-for-zainpay-for-woocommerce' ), esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=pgzw_zainpay' ) ) ) ) . '</p></div>';
	}
}

function pgzw_process_inline_payment_ajax() {
	$gateway = pgzw_get_gateway_instance();
	if ( $gateway ) {
		$gateway->process_inline_payment_ajax();
	}
}

function pgzw_create_order_and_init_payment_ajax() {
	$gateway = pgzw_get_gateway_instance();
	if ( $gateway ) {
		$gateway->create_order_and_init_payment_ajax();
	}
}

function pgzw_check_payment_status_ajax() {
	$gateway = pgzw_get_gateway_instance();
	if ( $gateway ) {
		$gateway->check_payment_status_ajax();
	}
}

function pgzw_record_txn_ref_ajax() {
	$gateway = pgzw_get_gateway_instance();
	if ( $gateway ) {
		$gateway->record_txn_ref_ajax();
	}
}

function pgzw_get_gateway_instance() {
	if ( ! class_exists( 'PGZW_Gateway' ) ) {
		return null;
	}
	return new PGZW_Gateway();
}
