<?php
/**
 * Plugin Name:       Sugar Calendar Bookings Scheduling Appointments Lite
 * Plugin URI:        https://sugarcalendar.com
 * Description:       Bookings made sweet and simple - for you and your customers.
 * Author:            Sugar Calendar
 * Author URI:        https://sugarcalendar.com
 * License:           GNU General Public License v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       sugar-calendar-bookings-scheduling-appointments-lite
 * Domain Path:       /assets/languages
 * Requires PHP:      7.4
 * Requires at least: 6.2
 * Version:           1.5.1
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Local variables, not globals.
if ( is_multisite() ) {
	$is_pro = file_exists( __DIR__ . '/src/Pro/Pro.php' );

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

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

		if (
			isset( $active_network_plugins[ $lite_base ] )
			&& in_array( 'sugar-calendar-bookings-scheduling-appointments/sugar-calendar-bookings-scheduling-appointments.php', $active_plugins, true )
		) {
			// Keep plugin active but silent.
			return;
		}
	}
}
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

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

if ( ! defined( 'SCBOOKINGS_PLUGIN_PATH' ) ) {
	/**
	 * Plugin Folder Path.
	 *
	 * @since 1.0.0
	 */
	define( 'SCBOOKINGS_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}

if ( ! defined( 'SCBOOKINGS_PLUGIN_URL' ) ) {
	/**
	 * Plugin Folder URL.
	 *
	 * @since 1.0.0
	 */
	define( 'SCBOOKINGS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}

if ( ! defined( 'SCBOOKINGS_PLUGIN_FILE' ) ) {
	/**
	 * Plugin Root File.
	 *
	 * @since 1.0.0
	 */
	define( 'SCBOOKINGS_PLUGIN_FILE', __FILE__ );
}

// Don't allow multiple versions to be active.
if ( function_exists( 'sugar_calendar_bookings' ) ) {
	if ( ! function_exists( 'scbookings_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 the user activated Lite or not.
		 *
		 * @since 1.0.0
		 */
		function scbookings_lite_just_activated() {

			set_transient( 'scbookings_lite_just_activated', true );
		}
	}
	add_action( 'activate_sugar-calendar-bookings-scheduling-appointments-lite/sugar-calendar-bookings-scheduling-appointments-lite.php', 'scbookings_lite_just_activated' );

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

			$pro_file  = sugar_calendar_bookings()->is_pro() ? SCBOOKINGS_PLUGIN_FILE : __FILE__;
			$lite_file = sugar_calendar_bookings()->is_pro() ? __FILE__ : SCBOOKINGS_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 );
			}
		}
	}
	add_action( 'admin_init', 'scbookings_lite_deactivate' );

	if ( ! function_exists( 'scbookings_lite_notice' ) ) {
		/**
		 * Display the notice after Lite deactivation when Pro is still active
		 * and user wanted to activate the Lite version of the plugin.
		 *
		 * @since        1.0.0
		 *
		 * @noinspection HtmlUnknownTarget
		 */
		function scbookings_lite_notice() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh

			// Do not show the notice Lite was not just activated.
			if ( (bool) get_transient( 'scbookings_lite_just_activated' ) === false ) {
				return;
			}

			$pro_file  = sugar_calendar_bookings()->is_pro() ? SCBOOKINGS_PLUGIN_FILE : __FILE__;
			$lite_file = sugar_calendar_bookings()->is_pro() ? __FILE__ : SCBOOKINGS_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 ) )
			) {
				$message = sprintf(
				/* translators: %s - Path to installed plugins. */
					__( 'Your site already has Sugar Calendar Bookings Pro activated. If you want to switch to Sugar Calendar Bookings Lite, please first go to %s and deactivate Sugar Calendar Bookings. Then, you can activate Sugar Calendar Bookings Lite.', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
					is_multisite() ? __( 'Network Admin → Plugins → Installed Plugins', 'sugar-calendar-bookings-scheduling-appointments-lite' ) : __( 'Plugins → Installed Plugins', 'sugar-calendar-bookings-scheduling-appointments-lite' )
				);

				printf(
					'<div class="notice scbookings-notice notice-warning scbookings-license-notice" id="scbookings-notice-pro-active">
                            <h3 style="margin: .75em 0 0 0;"><img src="%1$s" alt="" style="vertical-align: text-top; width: 20px; margin-right: 7px;">%2$s</h3>
                            <p>%3$s</p>
                          </div>',
					esc_url( SCBOOKINGS_PLUGIN_URL . 'assets/images/icons/exclamation-triangle.svg' ),
					esc_html__( 'Heads up!', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
					esc_html( $message )
				);

				delete_transient( 'scbookings_lite_just_activated' );

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

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

// Load the dev mode file if it exists.
if (
	defined( 'SCBOOKINGS_DEV_MODE' ) && SCBOOKINGS_DEV_MODE &&
	file_exists( SCBOOKINGS_PLUGIN_PATH . 'dev.php' )
) {
	require_once SCBOOKINGS_PLUGIN_PATH . 'dev.php';
}

require_once SCBOOKINGS_PLUGIN_PATH . 'vendor/autoload.php';

/*
 * This function should be wrapped to condition, otherwise `function_exists( 'sugar_calendar_bookings' )` at the beginning of
 * this file will return `true` which is incorrect.
 *
 * @see https://www.php.net/manual/en/function.function-exists.php#110163
 */
if ( ! function_exists( 'sugar_calendar_bookings' ) ) {
	/**
	 * Global function-holder. Works similar to a singleton's instance().
	 *
	 * @since 1.0.0
	 *
	 * @return SugarCalendarBookings\Plugin
	 */
	function sugar_calendar_bookings() {

		return SugarCalendarBookings\Plugin::instance();
	}
}

// Initialize the plugin.
sugar_calendar_bookings();
