<?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.6
* 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;

$plugin_version = get_file_data( __FILE__, array( 'Version' => 'Version' ) );

define( 'CEMBRAPAYVERSION', $plugin_version['Version'] ?? '1.0.6' );
define( 'CEMBRAPAYTITLE', $plugin_version['Plugin Name'] ?? 'CembraPay Payment Gateway for WooCommerce' );
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( 'before_woocommerce_init', function() {
	if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
	}
} );
?>
