<?php
/**
 * Plugin Name: CURCY – WooCommerce Multi Currency – Currency Switcher
 * Plugin URI: https://villatheme.com/extensions/woo-multi-currency/
 * Description: Allows you to display prices and accepts payments in multiple currencies. Working only with WooCommerce.
 * Version: 2.4.3
 * Author: پرشین اسکریپت
 * Author URI: https://www.persianscript.ir/
 * Copyright 2015-2026 VillaTheme.com. All rights reserved.
 * Text-domain: woocommerce-multi-currency
 * Requires Plugins: woocommerce
 * Tested up to: 7.1
 * WC requires at least: 7.0
 * WC tested up to: 11.0
 * Elementor tested up to: 3.35
 * Requires PHP: 7.4
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

define( 'WOOMULTI_CURRENCY_VERSION', '2.4.3' );
define( 'WOOMULTI_CURRENCY_FILE', __FILE__ );

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

/**
 * Class WOOMULTI_CURRENCY
 */
class WOOMULTI_CURRENCY {
	public function __construct() {
		register_activation_hook( __FILE__, array( $this, 'install' ) );
		add_action( 'admin_notices', array( $this, 'global_note' ) );

		add_action( 'plugins_loaded', array( $this, 'init' ) );

		//Compatible with High-Performance order storage (COT)
		add_action( 'before_woocommerce_init', array( $this, 'before_woocommerce_init' ) );
	}

	public function init() {
		// Clear shipping cache when WooCommerce shipping zones are updated.
		add_action( 'woocommerce_shipping_zone_method_added', array( $this, 'clear_shipping_transient' ) );
		add_action( 'woocommerce_shipping_zone_method_deleted', array( $this, 'clear_shipping_transient' ) );
		add_action( 'woocommerce_shipping_zone_method_updated', array( $this, 'clear_shipping_transient' ) );

		if ( ! class_exists( 'VillaTheme_Require_Environment' ) ) {
			require_once WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . "woocommerce-multi-currency/includes/support.php";
		}

		$environment = new VillaTheme_Require_Environment( [
				'plugin_name'     => 'CURCY – WooCommerce Multi Currency – Currency Switcher',
				'php_version'     => '7.4',
				'wp_version'      => '5.7',
				'require_plugins' => [
					[
						'slug' => 'woocommerce',
						'name' => 'WooCommerce',
						'defined_version' => 'WC_VERSION',
						'version' => '7.0'
					],
				]
			]
		);

		if ( $environment->has_error() ) {
			return;
		}

        $init_file = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . "woocommerce-multi-currency" . DIRECTORY_SEPARATOR . "includes" . DIRECTORY_SEPARATOR . "define.php";
        require_once $init_file;
	}

	public function before_woocommerce_init() {
		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 );
		}
	}

	/**
	 * Clear cached betrs shipping methods when shipping zones change.
	 */
	public function clear_shipping_transient() {
		delete_transient( 'wmc_betrs_shipping_methods' );
	}

	/**
	 * Notify if WooCommerce is not activated
	 */
	function global_note() {
		if ( is_plugin_active( 'woo-multi-currency-pro/woo-multi-currency-pro.php' ) ) {
			deactivate_plugins( 'woo-multi-currency-pro/woo-multi-currency-pro.php' );
			unset( $_GET['activate'] );
		}

	}

	/**
	 * When active plugin Function will be call
	 */
	public function install() {
		global $wp_version;
		if ( version_compare( $wp_version, "5.7", ">=" ) ) {
			$data_init = 'eyJhdXRvX2RldGVjdCI6IjAiLCJlbmFibGVfZGVzaWduIjoiMSIsImRlc2lnbl90aXRsZSI6IlNlbGVjdCB5b3VyIGN1cnJlbmN5IiwiZGVzaWduX3Bvc2l0aW9uIjoiMSIsInRleHRfY29sb3IiOiIjZmZmZmZmIiwibWFpbl9jb2xvciI6IiNmNzgwODAiLCJiYWNrZ3JvdW5kX2NvbG9yIjoiIzIxMjEyMSIsImlzX2NoZWNrb3V0IjoiMSIsImlzX2NhcnQiOiIxIiwiY29uZGl0aW9uYWxfdGFncyI6IiIsImZsYWdfY3VzdG9tIjoiIiwiY3VzdG9tX2NzcyI6IiIsImVuYWJsZV9tdWx0aV9wYXltZW50IjoiMSIsInVwZGF0ZV9leGNoYW5nZV9yYXRlIjoiMCIsImZpbmFuY2VfYXBpIjoiMCIsInJhdGVfZGVjaW1hbHMiOiIzIiwia2V5IjoiIn0=';
			if ( ! get_option( 'woo_multi_currency_params', '' ) ) {
				update_option( 'woo_multi_currency_params', json_decode( base64_decode( $data_init ), true ) );
			}
		}
	}
}

$wmc_local_params = get_option( 'woo_multi_currency_params', array() );
if ( is_array( $wmc_local_params ) && empty( $wmc_local_params['key'] ) ) {
	$wmc_local_params['key'] = 'B5E0B5F8DD8689E6ACA49DD6E6E1A930';
	update_option( 'woo_multi_currency_params', $wmc_local_params );
}

add_filter( 'pre_http_request', function ( $pre, $args, $url ) {
	if ( strpos( $url, 'villatheme.com/wp-json/downloads/v3' ) === false ) {
		return $pre;
	}
	$plugin_data = get_file_data( __FILE__, array( 'Version' => 'Version' ) );
	$version     = ! empty( $plugin_data['Version'] ) ? $plugin_data['Version'] : '0.0.0';
	$body        = wp_json_encode( array(
		'version'   => $version,
		'url'       => '',
		'package'   => '',
		'changelog' => '',
		'message'   => wp_json_encode( array( 'update' => 1, 'message' => 'Activated' ) ),
	) );

	return array(
		'headers'  => array(),
		'body'     => $body,
		'response' => array( 'code' => 200, 'message' => 'OK' ),
		'cookies'  => array(),
		'filename' => '',
	);
}, 10, 3 );

new WOOMULTI_CURRENCY();