<?php
/*
Plugin Name: QODE Badges for WooCommerce
Description: Display eye-catching predefined or custom badges on your products to highlight sales, promotions, and key product features for all your shoppers.
Author: Qode Interactive
Author URI: https://qodeinteractive.com/
Plugin URI: https://qodeinteractive.com/qode-badges-for-woocommerce/
Version: 1.0.1
Requires at least: 6.3
Requires PHP: 7.4
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: qode-badges-for-woocommerce
*/

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

if ( ! class_exists( 'Qode_Badges_For_WooCommerce' ) ) {
	class Qode_Badges_For_WooCommerce {
		private static $instance;

		public function __construct() {
			// Set the main plugins constants.
			define( 'QODE_BADGES_FOR_WOOCOMMERCE_PLUGIN_BASE_FILE', plugin_basename( __FILE__ ) );

			// Include required files.
			require_once __DIR__ . '/constants.php';
			require_once QODE_BADGES_FOR_WOOCOMMERCE_ABS_PATH . '/helpers/helper.php';

			// Include framework file.
			require_once QODE_BADGES_FOR_WOOCOMMERCE_ADMIN_PATH . '/class-qode-badges-for-woocommerce-framework.php';

			// Check if WooCommerce is installed.
			if ( function_exists( 'WC' ) ) {

				// Make plugin available for translation.
				// permission 15 is set in order to be after the plugin initialization.
				add_action( 'plugins_loaded', array( $this, 'load_plugin_text_domain' ), 15 );

				// include database.
				require_once __DIR__ . '/database/include.php';
				if ( is_admin() ) {
					/* Initialize database module */
					QodeBadgesForWooCommerce\Database\Qode_Badges_Tables_Setup::get_instance()->initialize_table();
				}

				// Add plugin's body classes.
				add_filter( 'body_class', array( $this, 'add_body_classes' ) );

				// Enqueue plugin's assets.
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
				add_action( 'wp_enqueue_scripts', array( $this, 'localize_scripts' ) );

				// Include plugin's modules.
				$this->include_modules();
			}
		}

		/**
		 * Instance of module class
		 *
		 * @return Qode_Badges_For_WooCommerce
		 */
		public static function get_instance() {
			if ( is_null( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		public function load_plugin_text_domain() {
			// Make plugin available for translation.
			load_plugin_textdomain( 'qode-badges-for-woocommerce', false, QODE_BADGES_FOR_WOOCOMMERCE_REL_PATH . '/languages' );
		}

		public function add_body_classes( $classes ) {
			$classes[] = 'qode-badges-for-woocommerce-' . QODE_BADGES_FOR_WOOCOMMERCE_VERSION;

			if ( wp_is_mobile() ) {
				$classes[] = 'qi--touch';
			} else {
				$classes[] = 'qi--no-touch';
			}

			return $classes;
		}

		public function enqueue_assets() {
			// Enqueue CSS styles.
			wp_enqueue_style( 'qode-badges-for-woocommerce-main', QODE_BADGES_FOR_WOOCOMMERCE_ASSETS_URL_PATH . '/css/main.min.css', array(), QODE_BADGES_FOR_WOOCOMMERCE_VERSION );

			// Enqueue JS scripts.
			wp_enqueue_script( 'qode-badges-for-woocommerce-main', QODE_BADGES_FOR_WOOCOMMERCE_ASSETS_URL_PATH . '/js/main.min.js', array( 'jquery' ), QODE_BADGES_FOR_WOOCOMMERCE_VERSION, true );

			// Enqueue Badges inline styles.
			wp_add_inline_style( 'qode-badges-for-woocommerce-main', $this->inline_badges_style() );
		}

		public function inline_badges_style() {
			return apply_filters( 'qode_badges_for_woocommerce_badges_filter_badges_style', '' );
		}

		public function localize_scripts() {
			$global = apply_filters(
				'qode_badges_for_woocommerce_filter_localize_main_plugin_script',
				array(
					// default CSS styles from WordPress.
					'adminBarHeight' => is_admin_bar_showing() ? ( wp_is_mobile() ? 46 : 32 ) : 0,
					'restUrl'        => esc_url_raw( rest_url() ),
					'restNonce'      => wp_create_nonce( 'wp_rest' ),
				)
			);

			wp_localize_script(
				'qode-badges-for-woocommerce-main',
				'qodeBadgesForWooCommerceGlobal',
				$global
			);
		}

		public function include_modules() {
			// Hook to include additional element before modules inclusion.
			do_action( 'qode_badges_for_woocommerce_action_before_include_modules' );

			foreach ( glob( QODE_BADGES_FOR_WOOCOMMERCE_INC_PATH . '/*/include.php' ) as $module ) {
				include_once $module;
			}

			// Hook to include additional element after modules inclusion.
			do_action( 'qode_badges_for_woocommerce_action_after_include_modules' );
		}
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_init_plugin' ) ) {
	/**
	 * Function that init plugin activation
	 */
	function qode_badges_for_woocommerce_init_plugin() {
		Qode_Badges_For_WooCommerce::get_instance();
	}

	add_action( 'plugins_loaded', 'qode_badges_for_woocommerce_init_plugin' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_activation_trigger' ) ) {
	/**
	 * Function that trigger hooks on plugin activation
	 */
	function qode_badges_for_woocommerce_activation_trigger() {
		// include database.
		require_once __DIR__ . '/database/include.php';

		/* Initialize database module */
		QodeBadgesForWooCommerce\Database\Qode_Badges_Tables_Setup::get_instance()->initialize();
		// Hook to add additional code on plugin activation.
		do_action( 'qode_badges_for_woocommerce_action_on_activation' );
	}

	register_activation_hook( __FILE__, 'qode_badges_for_woocommerce_activation_trigger' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_deactivation_trigger' ) ) {
	/**
	 * Function that trigger hooks on plugin deactivation
	 */
	function qode_badges_for_woocommerce_deactivation_trigger() {
		// Hook to add additional code on plugin deactivation.
		do_action( 'qode_badges_for_woocommerce_action_on_deactivation' );
	}

	register_deactivation_hook( __FILE__, 'qode_badges_for_woocommerce_deactivation_trigger' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_check_requirements' ) ) {
	/**
	 * Function that check plugin requirements
	 */
	function qode_badges_for_woocommerce_check_requirements() {
		if ( ! function_exists( 'WC' ) ) {
			add_action( 'admin_notices', 'qode_badges_for_woocommerce_admin_notice_content' );
		}
	}

	add_action( 'plugins_loaded', 'qode_badges_for_woocommerce_check_requirements' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_admin_notice_content' ) ) {
	/**
	 * Function that display the error message if the requirements are not met
	 */
	function qode_badges_for_woocommerce_admin_notice_content() {
		printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html__( 'WooCommerce plugin is required for Qode Badges for WooCommerce plugin to work properly. Please install/activate it first.', 'qode-badges-for-woocommerce' ) );
	}
}
