<?php
/**
 * Plugin Name: NMI Payment Gateway for WooCommerce
 * Plugin URI: https://www.robustsoftech.com/plugins/nmi-payment-gateway-pro
 * Description: Accept secure credit card payments on your WooCommerce store with the Network Merchants Inc (NMI) Payment Gateway. By Robust Softech.
 * Version: 1.1.1
 * Author: Robust Softech Private Limited
 * Author URI: https://www.robustsoftech.com
 * Contributors: robustsoftech, naushad1988, ishpreet
 * Requires at least: 4.5
 * Tested up to: 6.9
 * WC requires at least: 9.0.0
 * WC tested up to: 10.0.4
 * WC HPOS Compatible: yes
 * Requires PHP: 7.4
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * Text Domain: robust-nmi-payment-gateway
 * Domain Path: /languages
 *
 * @author Robust Softech Private Limited
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

add_action( 'plugins_loaded', 'robust_nmi_init_gateway', 0 );

function robust_nmi_init_gateway() {
	if ( ! class_exists( 'WC_Payment_Gateway_CC' ) || ! class_exists( 'WC_Payment_Gateway' ) ) {
		return;
	}

	load_plugin_textdomain( 'robust-nmi-payment-gateway', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

	include 'includes/cc.php';

	/**
	 * Add the gateway to WooCommerce.
	 **/
	function robust_nmi_add_gateway( $methods ) {
		if ( class_exists( 'Robust_NMI_Gateway' ) ) {
			$methods[] = 'Robust_NMI_Gateway';
		}
		return $methods;
	}
	add_filter( 'woocommerce_payment_gateways', 'robust_nmi_add_gateway' );
	
	// Add settings link to plugins page
	add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'robust_nmi_add_action_links' );
}

/**
 * Add action links to plugins page (Settings | Upgrade to PRO)
 */
function robust_nmi_add_action_links( $links ) {
	$settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=robust_nmi_gateway' ) ) . '" title="' . esc_attr__( 'Configure NMI Payment Gateway', 'robust-nmi-payment-gateway' ) . '">' . esc_html__( 'Settings', 'robust-nmi-payment-gateway' ) . '</a>';
	array_unshift( $links, $settings_link );

	$upgrade_link = '<a href="https://www.robustsoftech.com/plugins/nmi-payment-gateway-pro" target="_blank" rel="noopener noreferrer" style="color:#d63638;font-weight:600;">' . esc_html__( 'Upgrade to PRO', 'robust-nmi-payment-gateway' ) . '</a>';
	$links[] = $upgrade_link;

	return $links;
}

/**
 * Add/modify meta row links (Visit plugin site, Author link) — open in new tab
 * Also appends the Upgrade to PRO link in the meta row.
 */
function robust_nmi_row_meta( $links, $file ) {
	if ( plugin_basename( dirname( __FILE__ ) . '/gateway-nmipay.php' ) !== $file ) {
		return $links;
	}

	// Make existing meta links (Visit plugin site, author) open in a new tab
	foreach ( $links as $key => $link ) {
		if ( strpos( $link, '<a ' ) !== false ) {
			$links[ $key ] = str_replace(
				'<a ',
				'<a target="_blank" rel="noopener noreferrer" ',
				$link
			);
		}
	}

	return $links;
}
add_filter( 'plugin_row_meta', 'robust_nmi_row_meta', 10, 2 );

// Declare HPOS compatibility
add_action( 'before_woocommerce_init', function() {
	if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
	}
});
