<?php
/**
 * Plugin Name: Yummy Cookies
 * Description: A lightweight plugin that extends Elementor's Dynamic Tags with Cookies 🍪
 * Plugin URI: https://chrisjayden.com
 * Author: Chris Jayden
 * Version: 1.0.0
 * Author URI: https://chrisjayden.com/?utm_source=wp-plugins&utm_campaign=author-uri&utm_medium=wp-dash
 *
 * Text Domain: yummy-cookies
 * Domain Path: /languages
 *
 * Yummy Cookies is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Yummy Cookies is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

define( 'YUMMY_COOKIES_VERSION', '1.0.0' );

/**
 * Will stop plugin from loading if required plugin is not active.
 *
 * @author Chris Jayden <chris@chrisjayden.com>
 */
function yummy_cookies_cant_activate_msg() {
	?>
		<div class="notice notice-error is-dismissible checkout-wc">
			<?php
				echo sprintf( __( '<h3><strong>%s</strong>! Elementor is required to use Yummy Cookies.</h3>', 'yummy-cookies' ), 'Oh no 😳' );
			?>
			<p>
			<?php
				echo sprintf( __( 'To prevent fatal errors, we have loaded %1$s into <strong>%2$s</strong>, which disables all features and functionality.', 'yummy-cookies' ), 'Yummy Cookies', __( 'safe mode', 'yummy-cookies' ) );
			?>
		</div>
	<?php
}

/**
 * Check if Yummy Cookies can activate. If not, will stop plugin from loading to prevent fatal errors.
 *
 * @author Chris Jayden <chris@chrisjayden.com>
 */
function yummy_cookies_can_activate() {

	$can_activate = true;

	if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
		require_once ABSPATH . '/wp-admin/includes/plugin.php';
	}

	if ( is_multisite() ) {
		// This plugin is network activated (Elementor must be network activated).
		if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
			$can_activate = is_plugin_active_for_network( 'elementor/elementor.php' ) ? true : false;
			// Locally activated (Elementor can be network or locally activated).
		} else {
			$can_activate = is_plugin_active( 'elementor/elementor.php' ) ? true : false;
		}
	} else {
		$can_activate = is_plugin_active( 'elementor/elementor.php' ) ? true : false;
	}

	if ( false === $can_activate ) {
		return false;
	} else {
		return true;
	}

}

if ( yummy_cookies_can_activate() ) {

	/**
	 * The code that runs during plugin activation.
	 */
	function activate_yummy_cookies() {
		require_once plugin_dir_path( __FILE__ ) . 'includes/class-yummy-cookies-activator.php';
		Yummy_Cookies_Activator::activate();
	}

	/**
	 * The code that runs during plugin deactivation.
	 */
	function deactivate_yummy_cookies() {
		require_once plugin_dir_path( __FILE__ ) . 'includes/class-yummy-cookies-deactivator.php';
		Yummy_Cookies_Deactivator::deactivate();
	}

	register_activation_hook( __FILE__, 'activate_yummy_cookies' );
	register_deactivation_hook( __FILE__, 'deactivate_yummy_cookies' );

	/**
	 * The core plugin class that is used to define admin-specific hooks, and public-facing site hooks.
	 */
	require plugin_dir_path( __FILE__ ) . 'includes/class-yummy-cookies.php';

	/**
	 * Begins execution of the plugin.
	 *
	 * Since everything within the plugin is registered via hooks,
	 * then kicking off the plugin from this point in the file does
	 * not affect the page life cycle.
	 *
	 * @since    1.0.0
	 */
	function run_yummy_cookies() {
		$plugin = new Yummy_Cookies();
		$plugin->run();
	}

	run_yummy_cookies();
} else {
	add_action( 'admin_notices', 'yummy_cookies_cant_activate_msg' );
}
