<?php
/**
 * Plugin Name: Recarto – WooCommerce Abandoned Cart Recovery – Send Cart Recovery Email Plugin – SMS
 * Plugin URI: https://villatheme.com/extensions/woo-abandoned-cart-recovery/
 * Description: Capture abandoned cart & send reminder emails to the customers.
 * Version: 1.1.11
 * Author: پرشین اسکریپت
 * Author URI: httpswww.persianscript.ir/
 * Text Domain: woo-abandoned-cart-recovery
 * Domain Path: /languages
 * Copyright 2019-2026 VillaTheme.com. All rights reserved.
 * Requires Plugins: woocommerce
 * Requires at least: 5.0
 * Tested up to: 7.1
 * WC requires at least: 7.0
 * WC tested up to: 11.0
 * Requires PHP: 7.0
 **/

use WACVP\Inc\Data;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
define( 'WACVP_VERSION', '1.1.11' );

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

define( 'WACVP_SLUG', 'woocommerce-abandoned-cart-recovery' );
define( 'WACVP_DIR', WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . "woocommerce-abandoned-cart-recovery" . DIRECTORY_SEPARATOR );
define( 'WACVP_LANGUAGES', WACVP_DIR . "languages" . DIRECTORY_SEPARATOR );
define( 'WACVP_INCLUDES', WACVP_DIR . "includes" . DIRECTORY_SEPARATOR );
define( 'WACVP_VIEWS', WACVP_DIR . "views" . DIRECTORY_SEPARATOR );
define( 'WACVP_TEMPLATES', WACVP_INCLUDES . "templates" . DIRECTORY_SEPARATOR );
define( 'WACVP_GUEST_ID_LIMIT', 100000000 );


class WACVPInit {

	public function __construct() {

		add_action( 'plugins_loaded', [ $this, 'init' ] );
		register_activation_hook( __FILE__, [ $this, 'wacvp_activate' ] );

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

	public function init() {
		$include_dir = plugin_dir_path( __FILE__ ) . 'includes/';

		if ( ! class_exists( 'VillaTheme_Require_Environment' ) ) {
			include_once $include_dir . 'support.php';
		}
		$environment = new \VillaTheme_Require_Environment( [
				'plugin_name'     => 'Recarto – WooCommerce Abandoned Cart Recovery – Send Cart Recovery Email Plugin – SMS',
				'php_version'     => '7.0',
				'wp_version'      => '5.0',
				'require_plugins' => [
					[
						'slug'             => 'woocommerce',
						'name'             => 'WooCommerce',
						'required_version' => '7.0'
					]
				]
			]
		);

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

		add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [ $this, 'wacvp_add_action_links' ] );
		add_action( 'wpmu_new_blog', [ $this, 'wacvp_activate_new_blog' ] );
		add_filter( 'wpmu_drop_tables', [ $this, 'wacv_delete_plugin_tables', 10, 2 ] );

		$this->load_classes();
	}

	public function before_woocommerce_init() {
		if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
		}
	}

	protected function load_classes() {
		require_once WACVP_INCLUDES . "define.php";
	}

	function wacvp_add_action_links( $links ) {
		$settings_link = '<a href="' . admin_url( 'admin.php?page=wacv_settings' ) . '">' . esc_html__( 'Settings', 'woo-abandoned-cart-recovery' ) . '</a>';
		array_unshift( $links, $settings_link );

		return $links;
	}

	function wacvp_activate( $network_wide ) {
		require_once WACVP_INCLUDES . "plugin.php";
		$wacv_plugin = \WACVP\Inc\Plugin::get_instance();
		$wacv_plugin->activate( $network_wide );
	}

	function wacvp_activate_new_blog( $blog_id ) {
		if ( is_plugin_active_for_network( 'woocommerce-abandoned-cart-recovery/woocommerce-abandoned-cart-recovery.php' ) ) {
			switch_to_blog( $blog_id );
			require_once WACVP_INCLUDES . "plugin.php";
			$wacv_plugin = \WACVP\Inc\Plugin::get_instance();
			$wacv_plugin->single_active();
			restore_current_blog();
		}
	}

	function wacv_delete_plugin_tables( $tables, $blog_id ) {
		if ( empty( $blog_id ) || 1 == $blog_id || $blog_id != $GLOBALS['blog_id'] ) {
			return $tables;
		}
		global $wpdb;
		$blog_prefix   = $wpdb->get_blog_prefix( $blog_id );
		$plugin_tables = array(
			'wacv_abandoned_cart_record',
			'wacv_guest_info_record',
			'wacv_email_history',
			'wacv_cart_log',
			'wacv_email_unique'
		);
		foreach ( $plugin_tables as $k => $table ) {
			$tables[ $table ] = $blog_prefix . $table;
		}

		return $tables;
	}

	function WACV_Data() {
		return Data::get_instance();
	}
}

new WACVPInit();
