<?php
/**
 * Peachpay_Alert_Service class
 *
 * @package PeachPay
 */

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

/**
 * Displays various admin alerts for PeachPay. Different from the dependency service as none of these will need PeachPay to abort operations.
 * Constructor adds the WordPress action responsible for running the alert code.
 */
class PeachPay_Alert_Service {
	/** Option key for dismissing the third-party field notice. */
	const DISMISS_THIRD_PARTY_FIELD_NOTICE_OPTION = 'peachpay_dismiss_third_party_field_notice';

	/**
	 * Constructor method. This PHP magic method is called automatically as the class is instantiated.
	 */
	public function __construct() {
		add_action( 'admin_notices', array( $this, 'display_alerts' ), 11 );
		add_action( 'admin_init', array( $this, 'handle_dismiss_third_party_field_notice' ) );
	}

	/**
	 * Handles dismissal of the third-party field editor notice.
	 */
	public function handle_dismiss_third_party_field_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! isset( $_GET['peachpay_dismiss_third_party_field_notice'] ) || '1' !== $_GET['peachpay_dismiss_third_party_field_notice'] ) {
			return;
		}

		check_admin_referer( 'peachpay_dismiss_third_party_field_notice' );
		update_option( self::DISMISS_THIRD_PARTY_FIELD_NOTICE_OPTION, '1' );

		wp_safe_redirect( remove_query_arg( array( 'peachpay_dismiss_third_party_field_notice', '_wpnonce' ) ) );
		exit;
	}

	/**
	 * The base function for displaying admin alerts. This should be used for any alerts caused by code only run on the cart page, such
	 * as checkout field rendering.
	 */
	public function display_alerts() {
		// If third party field editors are being used, display a warning.
		if ( self::third_party_field_plugins_exist() && ! get_option( self::DISMISS_THIRD_PARTY_FIELD_NOTICE_OPTION, false ) ) {
			$dismiss_url = wp_nonce_url(
				add_query_arg( 'peachpay_dismiss_third_party_field_notice', '1' ),
				'peachpay_dismiss_third_party_field_notice'
			);
			?>
			<div class="notice peachpay-notice notice-warning is-dismissible">
				<p><b><?php esc_html_e( 'PeachPay for WooCommerce', 'peachpay-for-woocommerce' ); ?></b></p>
				<p>
				<?php
				esc_html_e( "Looks like you're using a third party plugin to handle additional checkout fields for your store. PeachPay now has its own field editor built in! While we will do our best to convert any third party fields, it is recommended to make the switch to our native editor for the best experience. Unsupported fields may cause issues with completing orders. Check out our field editor ", 'peachpay-for-woocommerce' );
				echo '<a href="' . esc_url( admin_url( 'admin.php?page=peachpay&tab=field&section=shipping' ) ) . '">';
				esc_html_e( 'here.', 'peachpay-for-woocommerce' );
				echo '</a>';
				?>
				</p>
				<button class="notice-dismiss" type="button" onclick="window.location = '<?php echo esc_url( $dismiss_url ); ?>'">
					<span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'peachpay-for-woocommerce' ); ?></span>
				</button>
			</div>
			<?php
		}
	}

	/**
	 * Returns true if any of this list of third party plugins is activated, false otherwise.
	 */
	private function third_party_field_plugins_exist() {
		if ( is_plugin_active( 'woo-checkout-field-editor-pro/checkout-form-designer.php' ) ||
			is_plugin_active( 'flexible-checkout-fields/flexible-checkout-fields.php' ) ||
			is_plugin_active( 'woocommerce-checkout-manager/woocommerce-checkout-manager.php' ) ) {

			return true;
		}
		return false;
	}
}
