<?php
/**
 * Plugin Name: Smart Offers
 * Plugin URI: https://www.storeapps.org/product/smart-offers/
 * Description: Earn more by creating a powerful sales funnel of upsells, downsells, backend, order bump and one-click checkout offers. Show special offers on any page of your choice. Cut down steps in the checkout process by offering quick & direct checkout.
 * Version: 4.9.0
 * Author: StoreApps
 * Author URI: https://www.storeapps.org/
 * Developer: StoreApps
 * Developer URI: https://www.storeapps.org/
 * Requires at least: 4.9.0
 * Tested up to: 6.7.1
 * WC requires at least: 3.0.0
 * WC tested up to: 9.4.2
 * Requires Plugins: woocommerce
 * Text Domain: smart-offers
 * Domain Path: /languages/
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 * Copyright (c) 2013-2024 StoreApps. All rights reserved.
 */

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

// De-activate WooCommerce Buy Now plugin if it is activated.
// Not checking against the option to always deactivate BN to avoid fatal error.
// @since 4.0.0
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( function_exists( 'deactivate_plugins' ) ) {
	if ( is_plugin_active( 'woocommerce-buy-now/woocommerce-buy-now.php' ) ) {
		deactivate_plugins( 'woocommerce-buy-now/woocommerce-buy-now.php' );
		update_option( 'so_wc_bn_deactivated', 'yes', 'no' );
	}
}

/**
 * Include class having function to execute during activation of the plugin
 */
include_once 'class-sa-smart-offers.php';
if ( ! class_exists( 'Sa_Storeapps_SDK' ) ) {
require_once plugin_dir_path( __FILE__ ).'sa-includes/class-storeapps-g.php';
}
/**
 * On activation
 */
register_activation_hook( __FILE__, array( 'SA_Smart_Offers', 'so_activate' ) );

add_action( 'plugins_loaded', 'initialize_so' );
/**
 * Initialize Smart Offers.
 */
function initialize_so() {

	$active_plugins = (array) get_option( 'active_plugins', array() );
	if ( is_multisite() ) {
		$active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
	}

	if ( ( in_array( 'woocommerce/woocommerce.php', $active_plugins, true ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins ) ) ) {

		// Define constants
		if ( ! defined( 'SO_PLUGIN_FILE' ) ) {
			define( 'SO_PLUGIN_FILE', __FILE__ );
		}
		if ( ! defined( 'SMART_OFFERS' ) ) {
			define( 'SMART_OFFERS', substr( plugin_basename( __FILE__ ), 0, strpos( plugin_basename( __FILE__ ), '/' ) ) );
		}
		if ( ! defined( 'SO_PRE_URL' ) ) {
			define( 'SO_PRE_URL', plugins_url().'/'.strtolower('smart-offers').'/' );
		}
		if ( ! defined( 'SA_SO_PLUGIN_DIRPATH' ) ) {
			define( 'SA_SO_PLUGIN_DIRPATH', dirname( __FILE__ ) );
		}
		if ( ! defined( 'SO_PLUGIN_BASE_NM' ) ) {
			define( 'SO_PLUGIN_BASE_NM', plugin_basename( __FILE__ ) );
		}

		if ( ! class_exists( 'StoreApps_Upgrade_4_0' ) ) {
			require_once 'sa-includes/class-storeapps-upgrade-4-0.php';
		}

		// Include SO.
		$GLOBALS['sa_smart_offers'] = new SA_Smart_Offers();

		// Include BN.
		include_once 'modules/buy-now/php/index.php';
	} else {
		if ( is_admin() ) {
			?>
			<div class="notice notice-error">
				<p><?php echo esc_html__( 'Smart Offers requires WooCommerce to be activated.', 'smart-offers' ); ?></p>
			</div>
			<?php
		}
	}

}
