<?php

/**
 * Plugin Name: HyperPay Payments
 * Description: Hyperpay is the first one stop-shop service company for online merchants in MENA Region.<strong>If you have any question, please <a href="http://www.hyperpay.com/" target="_new">contact Hyperpay</a>.</strong>
 * Version:     6.4.0
 * Text Domain: hyperpay-gateways
 * Domain Path: /languages
 * License:     GPLv2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Author:      Hyperpay Team
 * Author URI:  https://www.hyperpay.com
 * Requires at least: 5.3
 * Requires PHP: 7.1
 * WC requires at least: 3.0.9
 * WC tested up to: 8.3.1
 * 
 *
 */


namespace Hyperpay\Gateways;

use Hyperpay\Gateways\Helpers\Log;
use Hyperpay\Gateways\Main;

(function () {

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

// TODO: implement flush 

    require plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';





    /**
     * Register schedule to remove expired log files
     */

    add_action('hyperpay_remove_expired_logs', [Log::class, 'removeExpiredLogs']);

    if (!wp_next_scheduled('hyperpay_remove_expired_logs')) {
        wp_schedule_event(time(), 'daily', 'hyperpay_remove_expired_logs');
    }



    /**
     * HyperPay plugin url root
     */

    if (!defined('HYPERPAY_PLUGIN_DIR')) {

        define('HYPERPAY_PLUGIN_DIR', untrailingslashit(plugins_url('/', __FILE__)));
    }

    /**
     * Initialize the plugin and its modules.
     */

    add_action('plugins_loaded',  [Main::class, 'load']);


    /*
    * Load plugin textdomain.
    */
    add_action('init', function () {

        
        wp_enqueue_style('hyperpay_custom_style', HYPERPAY_PLUGIN_DIR . '/src/assets/css/style.css', [], '6.0.0');

        if (is_rtl())
            wp_enqueue_style('hyperpay_custom_style_ar', HYPERPAY_PLUGIN_DIR . '/src/assets/css/style-rtl.css', [], '6.0.0');
    });

    /**
     * add capture function to order actions
     */
    add_action('woocommerce_order_actions',  function ($actions, $order) {
        $is_pre_authorization = $order->get_meta('is_pre_authorization');
        if (is_array($actions) && $is_pre_authorization) {
            $actions['capture_payment'] = __('Capture Pre Authorization', 'hyperpay-gateways');
        }
        return $actions;
    }, 10, 2);

    /**
     * add HPOS wooCommerce feature
     */
    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);
        }
    });

add_filter( 'woocommerce_payment_tokens', 'hide_my_gateway_tokens_on_checkout', 10, 3 );

function hide_my_gateway_tokens_on_checkout( $tokens, $user_id, $gateway_id ) {
    // Replace 'hyperpay' with your actual gateway ID
    if ( 'hyperpay' !== $gateway_id ) {
        return $tokens;
    }

    // Check if we are in a Store API request (Checkout Block uses REST API)
    // or if we are on the checkout page.
    if ( is_checkout() || defined( 'REST_REQUEST' ) ) {
        // Return empty array to hide tokens for this gateway
        return array();
    }

    return $tokens;
}


})();
