<?php
/**
* Plugin Name: CembraPay Payment Gateway for WooCommerce
* Plugin URI: https://www.cloudweb.ch
* Description: Uses CembraPay database for WooCommerce checkout and allows or denies payments through invoices.
* Version: 1.0.5
* Author: cloudWEB
* Author URI: https://www.cloudweb.ch
* Developer: Adrian Emil Tudorache
* Developer URI: https://www.tudorache.me
* Requires Plugins: woocommerce
* Text Domain: cembrapay
* Domain Path: /languages
*
* WC requires at least: 8.0
* WC tested up to: 9.4.2
*
* License: CembraPay proprietary
*/

/**
* @package CembraPay Payment Gateway for WooCommerce
* @author  Adrian Emil Tudorache
* @license GPL-2.0+
* @link    https://www.tudorache.me/
**/
namespace CembraPayWoo;

if ( ! defined( 'ABSPATH' ) ) {
    header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
    exit;
}

global $wpdb;

define( 'CEMBRAPATH', plugin_dir_path( __FILE__ ) );
define( 'CEMBRAURL', plugin_dir_url( __FILE__ ) );
define( 'CEMBRABASE', plugin_basename( __FILE__ ) );
define( 'CEMBRADOMAIN', 'cembrapay' );
define( 'CEMBRA_LOGS_TABLE', $wpdb->prefix . 'cembrapay_logs' );

$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . 'woocommerce/woocommerce.php';

if (
    in_array( $plugin_path, wp_get_active_and_valid_plugins() ) || 
	in_array( $plugin_path, wp_get_active_network_plugins() )
) {

    function cembrapay_load_plugin_textdomain() {
	    load_plugin_textdomain( 'cembrapay', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
	}

	add_action( 'plugins_loaded', __NAMESPACE__ . '\cembrapay_load_plugin_textdomain' );

    $files = array(
        '/inc/init.php',
        '/inc/payment-gateway.php',
        '/inc/plugin/install.php',
        '/inc/plugin/uninstall.php',
    );

    foreach ( $files as $file ) {
        require_once( __DIR__ . $file );
    }

    /**
     * Trigger the installation function
     */
    function cembrapay_activate_install(): void {
        cembrapay_install();
        flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, __NAMESPACE__ . '\cembrapay_activate_install' );

    /**
     * Trigger the uninstall function
     */
    function cembrapay_deactivate_uninstall(): void {
        cembrapay_uninstall();
        flush_rewrite_rules();
    }
    register_uninstall_hook( __FILE__, __NAMESPACE__ . '\cembrapay_deactivate_uninstall' );

}

add_action( 'wp', function() {
    if( ! isset( $_GET['cembrapaytest'] ) ) {
        return false;
    }

    echo '<pre>';
    $date_format_wp = get_option('date_format'); // e.g., 'F j, Y'
    $date_string    = 'März 4, 1970';
    $locale         = get_locale();

    // Map WP format to ICU format for IntlDateFormatter
    $wp_to_icu = [
        'F' => 'MMMM', // full month name
        'M' => 'MMM',  // short month
        'm' => 'MM',   // month number with leading zero
        'j' => 'd',    // day
        'd' => 'dd',   // day with leading zero
        'Y' => 'yyyy', // year,
        ',' => '',
        '.' => ''
    ];
    $icu_format = strtr($date_format_wp, $wp_to_icu);

    $fmt = new \IntlDateFormatter(
        $locale,
        \IntlDateFormatter::NONE,
        \IntlDateFormatter::NONE,
        get_option('timezone_string') ?: 'UTC',
        \IntlDateFormatter::GREGORIAN,
        $icu_format
    );

    $timestamp = $fmt->parse($date_string);

    if ($timestamp !== false) {
        var_dump( $date_format_wp, date('Y-m-d', $timestamp) );
    }
    echo '</pre>';

    wp_die();

} );

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