<?php
/**
 * Plugin Name: Multi Currency Switcher
 * Plugin URI: https://wordpress.org/plugins/multi-currency-switcher
 * Description: Currency Converter Lite
 * Version: 1.0.1
 * Tags: currency switcher, currency switcher woocommerce, currency switcher WordPress, currency converter plugin, currency switcher extension, currency switcher plugin, currency switcher at checkout, woocommerce, WordPress, woocommerce extension, donation currency switcher
 * Author: WPExperts
 * Author URI: https://wpexpert.io/
 * Developer: WPExperts
 * Developer URI: https://wpexperts.io/
 * Text Domain: multi-currency-switcher
 * Requires at least: 4.0
 * Requires PHP: 7.0
 * WC requires at least: 4.0
 * WC tested up to: 10.0
 * Tested up to: 6.8
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 * 
 */

namespace Wpexperts\CurrencySwitcherForWoocommerce;

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

/**
 * Define plugin constants
 */
define( 'WCCS_DIR', __DIR__ );
define( 'WCCS_VERSION', '1.0.1' );
define( 'WCCS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'WCCS_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );

if ( file_exists( WCCS_DIR . '/vendor/autoload.php' ) ) {
	require_once WCCS_DIR . '/vendor/autoload.php';
}

use Wpexperts\CurrencySwitcherForWoocommerce\Activator; 
use Wpexperts\CurrencySwitcherForWoocommerce\Deactivator; 
use Wpexperts\CurrencySwitcherForWoocommerce\Helper; 
use Wpexperts\CurrencySwitcherForWoocommerce\Storage; 
use Wpexperts\CurrencySwitcherForWoocommerce\SwitcherWidget; 
use Wpexperts\CurrencySwitcherForWoocommerce\WCCS; 
use Wpexperts\CurrencySwitcherForWoocommerce\Settings; 
use Wpexperts\CurrencySwitcherForWoocommerce\AjaxProcess; 
use Wpexperts\CurrencySwitcherForWoocommerce\Cron; 

class WCCS_Currency_Switcher {

	// Hold the class instance.
	private static $_instance = null;   

	/**
	 * Cloning is forbidden.
	 *
	 * @since 1.0
	 */
	public function __clone() {
		wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'multi-currency-switcher' ), '1.0' );
	}

	/**
	 * Unserializing instances of this class is forbidden.
	 *
	 * @since 1.0
	 */
	public function __wakeup() {
		wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'multi-currency-switcher' ), '1.0' );
	}

	/**
	 * Contructor of class.
	 *
	 * @since 1.0
	 */
	public function __construct() {

		Activator::Instance();
		add_action('woocommerce_init', array( $this, 'run' ), 10);
		add_action( 'before_woocommerce_init', array( $this, 'hpos_compatibility' ), 10 );
		add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
		add_filter( 'plugin_row_meta', array( $this, 'wccs_modify_plugin_view_details_link' ), 15, 2 );
		add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'wccs_add_action_links' ) );

		register_activation_hook( __FILE__, array( $this, 'wccs_activation' ) );
		register_deactivation_hook( __FILE__, array( $this, 'wccs_deactivation' ) );
	}

	public function wccs_add_action_links( $actions ) {
		$temp = $actions['deactivate'];
		unset($actions['deactivate']);

		$mylinks = array(
			'<a href="https://wpexperts.io/products/currency-switcher-for-woocommerce/?utm_source=plugin_page&utm_medium=currency-switcher" target="_blank"><b>Get Premium</b></a>',
			'<a href="' . admin_url( 'admin.php?page=wccs-settings' ) . '">Settings</a>',
			$temp
		);
		$actions = array_merge( $actions, $mylinks );
		return $actions;
	}

	/**
	 * Load Text Domain.
	 *
	 * @since 1.0
	 */
	public function load_textdomain() {
		load_plugin_textdomain( 'multi-currency-switcher', false, basename( __DIR__ ) . '/languages/' );
	}

	/**
	 * HPOS Compatibility & Cart & Checkout Compatibilty.
	 *
	 * @since 1.0
	 */
	public function hpos_compatibility() {
		if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
		}
	}

	/**
	 * Run.
	 *
	 * @since 1.0
	 */
	public function run() {
		if ( Activator::check_woo_dependecies() ) {         

			AjaxProcess::Instance(); //Init Ajax Process
			Cron::Instance();
			Storage::Instance();
			$GLOBALS['WCCS'] = WCCS::Instance();
			Settings::Instance();
			
		}
	}


	public function wccs_activation() {
		Activator::Instance();
	}

	public function wccs_deactivation() {
		Deactivator::Instance();
	}

	public function wccs_modify_plugin_view_details_link( $plugin_meta, $plugin_file ) {
		
		// Check if this is the plugin you want to modify
		if ( 'currency-switcher-for-woocommerce/wc-currency-switcher.php' === $plugin_file ) {

			unset($plugin_meta[2]);

			// Modify the link
			$plugin_meta[] = sprintf(
				'<a href="%s" aria-label="%s" target="_blank">%s</a>',
				esc_url( 'https://woocommerce.com/products/currency-switcher-for-woocommerce/' ),
				__( 'Visit plugin site', 'multi-currency-switcher' ),
				__( 'View Details', 'multi-currency-switcher' )
			);
		}

		return $plugin_meta;
	}

	/**
	 * Singleton Instance Method to initiate class.
	 *
	 * @since 1.0
	 */
	public static function Instance() {
		if ( null === self::$_instance ) {
			self::$_instance = new WCCS_Currency_Switcher();
		}

		return self::$_instance;
	}
}

WCCS_Currency_Switcher::Instance();
