<?php
/**
 * Plugin Name: RewardsWP
 * Plugin URI: https://rewardswp.com
 * Description: Turn one-time buyers into repeat customers with Points and Referrals. RewardsWP helps you launch a loyalty and refer-a-friend program in minutes.
 * Version: 1.4.4
 * Author: AffiliateWP
 * Author URI: https://affiliatewp.com/rewardswp
 * Text Domain: rewardswp
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Requires at least: 6.0
 * Requires PHP: 7.4
 *
 * @package Awesomemotive\Rewardswp
 */

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

if ( is_multisite() ) {
	$rewardswp_is_pro = is_dir( __DIR__ . '/src/Pro' );

	if ( ! $rewardswp_is_pro ) { // <- is lite.
		$rewardswp_lite_base = plugin_basename( __FILE__ );

		$rewardswp_active_plugins         = get_option( 'active_plugins', [] );
		$rewardswp_active_network_plugins = get_site_option( 'active_sitewide_plugins' );

		if (
			isset( $rewardswp_active_network_plugins[ $rewardswp_lite_base ] )
			&& in_array( 'rewardswp-pro/rewardswp.php', $rewardswp_active_plugins, true )
		) {
			// Keep plugin active but silent.
			return;
		}
	}
}

if ( ! defined( 'AM_REWARDSWP_VERSION' ) ) {
	/**
	 * Plugin version.
	 *
	 * @since 1.0.0
	 */
	define( 'AM_REWARDSWP_VERSION', '1.4.4' );
}

// Plugin Folder Path.
if ( ! defined( 'AM_REWARDSWP_PLUGIN_DIR' ) ) {
	define( 'AM_REWARDSWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}

// Plugin Folder URL.
if ( ! defined( 'AM_REWARDSWP_PLUGIN_URL' ) ) {
	define( 'AM_REWARDSWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}

// Plugin Root File.
if ( ! defined( 'AM_REWARDSWP_PLUGIN_FILE' ) ) {
	define( 'AM_REWARDSWP_PLUGIN_FILE', __FILE__ );
}

// Plugin Base Name.
if ( ! defined( 'AM_REWARDSWP_PLUGIN_BASE_NAME' ) ) {
	define( 'AM_REWARDSWP_PLUGIN_BASE_NAME', plugin_basename( AM_REWARDSWP_PLUGIN_FILE ) );
}

// Don't allow multiple versions to be active.
if ( function_exists( 'am_rewardswp' ) ) {

	if ( ! function_exists( 'am_rewardswp_lite_just_activated' ) ) {
		/**
		 * Store temporarily that the Lite version of the plugin was activated.
		 * This is needed because WP does a redirect after activation and
		 * we need to preserve this state to know whether user activated Lite or not.
		 *
		 * @since 1.0.0
		 */
		function am_rewardswp_lite_just_activated() {
			set_transient( 'rewardswp_lite_just_activated', true );
		}

		add_action( 'activate_rewardswp/rewardswp.php', 'am_rewardswp_lite_just_activated' );
	}

	if ( ! function_exists( 'am_rewardswp_deactivate' ) ) {
		/**
		 * Deactivate Lite if RewardsWP already activated.
		 *
		 * @since 1.0.0
		 */
		function am_rewardswp_deactivate() {

			$is_pro    = am_rewardswp()->is_pro();
			$pro_file  = $is_pro ? AM_REWARDSWP_PLUGIN_FILE : __FILE__;
			$lite_file = $is_pro ? __FILE__ : AM_REWARDSWP_PLUGIN_FILE;

			$lite_base = plugin_basename( $lite_file );
			$pro_base  = plugin_basename( $pro_file );

			if (
				! is_multisite()
				|| is_plugin_active_for_network( $pro_base )
				|| ( ! is_plugin_active_for_network( $pro_base ) && ! is_plugin_active_for_network( $lite_base ) )
			) {
				deactivate_plugins( $lite_base );

				/**
				 * Fires on plugin deactivation.
				 *
				 * @since 1.0.0
				 *
				 * @param string $plugin_basename The plugin basename.
				 */
				do_action( 'rewardswp_plugin_deactivated', $lite_base );

				// Run the installation on the next admin visit. @todo Check if it is necessary with the Wizard.
				add_option( 'rewardswp_install', 1 );
			}
		}
	}

	add_action( 'admin_init', 'am_rewardswp_deactivate' );

	if ( ! function_exists( 'am_rewardswp_lite_notice' ) ) {
		/**
		 * Display the notice after deactivation when Pro is still active
		 * and user wanted to activate the Lite version of the plugin.
		 *
		 * @since 1.0.0
		 */
		function am_rewardswp_lite_notice() {

			$is_pro    = am_rewardswp()->is_pro();
			$pro_file  = $is_pro ? AM_REWARDSWP_PLUGIN_FILE : __FILE__;
			$lite_file = $is_pro ? __FILE__ : AM_REWARDSWP_PLUGIN_FILE;

			$lite_base = plugin_basename( $lite_file );
			$pro_base  = plugin_basename( $pro_file );

			// Do not show the notice if upgrade from Lite to Pro.
			if ( (bool) get_transient( 'rewardswp_lite_just_activated' ) === false ) {
				return;
			}

			if (
				! is_multisite()
				|| is_plugin_active_for_network( $pro_base )
				|| ( ! is_plugin_active_for_network( $pro_base ) && ! is_plugin_active_for_network( $lite_base ) )
			) {
				$message = sprintf(
					/* translators: %s - Path to installed plugins. */
					__(
						'Your site already has RewardsWP Pro activated. If you want to switch to RewardsWP Lite, please first go to %s and deactivate RewardsWP.',
						'rewardswp'
					) . ' ' . __( 'Then, you can activate RewardsWP Lite.', 'rewardswp' ),
					is_multisite() ? __( 'Network Admin → Plugins → Installed Plugins', 'rewardswp' ) : __( 'Plugins → Installed Plugins', 'rewardswp' )
				);

				// Currently tried to activate Lite with Pro still active, so display the message.
				printf(
					'<div class="notice rewardswp-notice notice-warning rewardswp-license-notice" id="rewardswp-notice-pro-active">
					<h3 style="margin: .75em 0 0 0;">
						<img src="%1$s" style="vertical-align: text-top; width: 20px; margin-right: 7px;">%2$s
					</h3>
					<p>%3$s</p>
				</div>',
					esc_url( AM_REWARDSWP_PLUGIN_URL . 'assets/img/exclamation-triangle.svg' ),
					esc_html__( 'Heads up!', 'rewardswp' ),
					esc_html( $message )
				);

				delete_transient( 'rewardswp_lite_just_activated' );

				if ( isset( $_GET['activate'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
					unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
				}
			}
		}
	}
	add_action( 'admin_notices', 'am_rewardswp_lite_notice' );
	add_action( 'network_admin_notices', 'am_rewardswp_lite_notice' );

	// Do not process the plugin code further.
	return;
}

// Suppress PHP 8.4+ deprecation warnings from vendor libraries (PHP-DI).
require_once __DIR__ . '/src/Helpers/DeprecationSuppressor.php';
\Awesomemotive\Rewardswp\Helpers\DeprecationSuppressor::register();

// Define the class and the function.
require_once __DIR__ . '/src/AwesomeMotiveRewardsWP.php';

if ( ! function_exists( 'am_rewardswp' ) ) {
	/**
	 * Get the single instance of the RewardsWP class.
	 *
	 * @since 1.0.0
	 *
	 * @return AwesomeMotiveRewardsWP|null
	 */
	function am_rewardswp(): ?AwesomeMotiveRewardsWP { // phpcs:ignore PSR1.Files.SideEffects.FoundWithSymbols
		return AwesomeMotiveRewardsWP::get_instance();
	}
}

if ( function_exists( 'am_rewardswp' ) ) {
	am_rewardswp();
}
