<?php
/**
 * Plugin Name:       Discountify – Discount Rules & Coupon Management for WooCommerce
 * Plugin URI:        https://wpbens.com/plugin/discountify
 * Description:       Discountify is a discount rules for WooCommerce and WooCommerce coupons & discounts management plugin.
 * Version:           1.0.90
 * Requires PHP:      7.4
 * Author:            Wpbens
 * Author URI:        https://wpbens.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Requires Plugins:  woocommerce
 * Text Domain:       discountify
 * Domain Path:       /languages
 *
 * @package Discountify
 * @category Core
 * @author Discountify
 * @version 1.0.0
 */

use Discountify\Utils\Helper;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * The Main Plugin Requirements Checker
 *
 * @since 1.0.0
 */
final class Discountify {

	private static $instance;

	/**
	 * Current  Version
	 *
	 * @return string
	 */
	public static function get_version() {
		if ( ! function_exists( 'get_plugin_data' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		$plugin_data = get_plugin_data( __FILE__, false, false );

		return ! empty( $plugin_data['Version'] ) ? $plugin_data['Version'] : '1.0.66';
	}

	/**
	 * Singleton Instance
	 *
	 * @return Discountify
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Setup Plugin Requirements
	 *
	 * @since 1.0.0
	 */
	private function __construct() {
		// Load modules
		add_action( 'plugins_loaded', array( $this, 'initialize_modules' ), 999 );
	}

	/**
	 * Initialize Modules
	 *
	 * @since 1.1.0
	 */
	public function initialize_modules() {
		do_action( 'discountify/before_load' );

		require_once plugin_dir_path( __FILE__ ) . 'autoloader.php';
		require_once plugin_dir_path( __FILE__ ) . 'bootstrap.php';
		// required plugin check
		$this->required_plugin();
		// Load Plugin modules and classes
		\Discountify\Bootstrap::instance();

		do_action( 'discountify/after_load' );
	}

	/**
	 * Check required plugin and throw notice
	 */
	public function required_plugin() {
		include_once ABSPATH . 'wp-admin/includes/plugin.php';
		$plugins = array(
			array(
				'name' => 'woccommerce',
				'slug' => 'woocommerce/woocommerce.php',
			),
		);

		foreach ( $plugins as $key => $value ) {
			if ( ! is_plugin_active( $value['slug'] ) ) {
				add_action( 'admin_notices', array( $this, 'woo_plugin_notice' ) );
			}
		}
	}

	/**
	 * Load on plugin
	 */
	public function woo_plugin_notice( $slug ) {

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Standard WP activation parameter
		if ( isset( $_GET['activate'] ) ) {
			unset( $_GET['activate'] );
		}

		if ( class_exists( 'WooCommerce' ) ) {
			$btn['label'] = esc_html__( 'Activate WooCommerce', 'discountify' );
			$btn['url']   = wp_nonce_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=all&paged=1', 'activate-plugin_woocommerce/woocommerce.php' );
		} else {
			$btn['label'] = esc_html__( 'Install WooCommerce', 'discountify' );
			$btn['url']   = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=woocommerce' ), 'install-plugin_woocommerce' );
		}

		Helper::push(
			array(
				'id'          => 'unsupported-woocommerce-version',
				'type'        => 'error',
				'dismissible' => true,
				'btn'         => $btn,
				'message'     => sprintf( esc_html__( 'Discountify requires WooCommerce , which is currently NOT RUNNING.', 'discountify' ) ),
			)
		);
	}
	/**
	 * Assets Directory Url
	 */
	public static function is_pro_active() {
		return empty( class_exists( 'DiscountifyPro' ) ) === 1 ? 0 : 1;
	}

	/**
	 * Assets Directory Url
	 *
	 * @return void
	 */
	public static function assets_url() {
		return trailingslashit( self::plugin_url() . 'assets' );
	}

	/**
	 * Assets Folder Directory Path
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public static function assets_dir() {
		return trailingslashit( self::plugin_dir() . 'assets' );
	}

	/**
	 * Plugin Core File Directory Url
	 *
	 * @since 1.0.0
	 */
	public static function core_url() {
		return trailingslashit( self::plugin_url() . 'core' );
	}

	/**
	 * Plugin Core File Directory Path
	 *
	 * @since 1.0.0
	 */
	public static function core_dir() {
		return trailingslashit( self::plugin_dir() . 'core' );
	}

	/**
	 * Plugin Url
	 *
	 * @since 1.0.0
	 */
	public static function plugin_url() {
		return trailingslashit( plugin_dir_url( self::plugin_file() ) );
	}

	/**
	 * Plugin Directory Path
	 *
	 * @since 1.0.0
	 */
	public static function plugin_dir() {
		return trailingslashit( plugin_dir_path( self::plugin_file() ) );
	}

	/**
	 * Plugins Basename
	 *
	 * @since 1.0.0
	 */
	public static function plugins_basename() {
		return plugin_basename( self::plugin_file() );
	}

	/**
	 * Plugin File
	 *
	 * @since 1.0.0
	 */
	public static function plugin_file() {
		return __FILE__;
	}
}

// Initiate Plugin
Discountify::get_instance();
