<?php
/**
 * Plugin Name: LVMR Addons
 * Description: Custom Elementor Addons for Love You More.
 * Plugin URI:  https://sabbir.me/
 * Version:     1.0.0
 * Author:      sabbir
 * Author URI:  https://sabbir.me/
 * Text Domain: lvmr-addons
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

final class LVMR_Addons {

	const VERSION = '1.0.0';
	const MINIMUM_ELEMENTOR_VERSION = '3.0.0';
	const MINIMUM_PHP_VERSION = '7.0';

	private static $_instance = null;

	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	public function __construct() {
		add_action( 'plugins_loaded', [ $this, 'init' ] );
	}

	public function init() {
		// Check if Elementor installed and activated
		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_missing_main_plugin' ] );
			return;
		}

		// Check for required Elementor version
		if ( ! version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version' ] );
			return;
		}

		// Check for required PHP version
		if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] );
			return;
		}

		// Add Plugin actions
		add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] );
        add_action( 'elementor/elements/categories_registered', [ $this, 'add_elementor_widget_categories' ] );
        add_action( 'elementor/frontend/after_register_styles', [ $this, 'register_styles' ] );
        add_action( 'elementor/frontend/after_register_scripts', [ $this, 'register_scripts' ] );
	}

    public function register_styles() {
        wp_register_style( 'lvmr-product-carousel', plugins_url( 'assets/css/product-carousel.css', __FILE__ ), [], self::VERSION );
        wp_register_style( 'lvmr-scrollable-content', plugins_url( 'assets/css/scrollable-content.css', __FILE__ ), [], self::VERSION );
    }

    public function register_scripts() {
        // Enqueue Swiper js natively used by Elementor
        wp_register_script( 'lvmr-product-carousel', plugins_url( 'assets/js/product-carousel.js', __FILE__ ), [ 'jquery', 'elementor-frontend' ], self::VERSION, true );
    }

    public function add_elementor_widget_categories( $elements_manager ) {
		$elements_manager->add_category(
			'lvmr-addons',
			[
				'title' => __( 'LVMR Addons', 'lvmr-addons' ),
				'icon' => 'fa fa-plug',
			]
		);
	}

	public function admin_notice_missing_main_plugin() {
		if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );
		$message = sprintf(
			/* translators: 1: Plugin name 2: Elementor */
			esc_html__( '"%1$s" requires "%2$s" to be installed and activated.', 'lvmr-addons' ),
			'<strong>' . esc_html__( 'LVMR Addons', 'lvmr-addons' ) . '</strong>',
			'<strong>' . esc_html__( 'Elementor', 'lvmr-addons' ) . '</strong>'
		);
		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );
	}

	public function admin_notice_minimum_elementor_version() {
		if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );
		$message = sprintf(
			/* translators: 1: Plugin name 2: Elementor 3: Required Elementor version */
			esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'lvmr-addons' ),
			'<strong>' . esc_html__( 'LVMR Addons', 'lvmr-addons' ) . '</strong>',
			'<strong>' . esc_html__( 'Elementor', 'lvmr-addons' ) . '</strong>',
			 self::MINIMUM_ELEMENTOR_VERSION
		);
		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );
	}

	public function admin_notice_minimum_php_version() {
		if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );
		$message = sprintf(
			/* translators: 1: Plugin name 2: PHP 3: Required PHP version */
			esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'lvmr-addons' ),
			'<strong>' . esc_html__( 'LVMR Addons', 'lvmr-addons' ) . '</strong>',
			'<strong>' . esc_html__( 'PHP', 'lvmr-addons' ) . '</strong>',
			 self::MINIMUM_PHP_VERSION
		);
		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );
	}

	public function register_widgets( $widgets_manager ) {
		// Require widget files
		require_once( __DIR__ . '/widgets/product-carousel.php' );
		require_once( __DIR__ . '/widgets/scrollable-content.php' );
        
		// Register widgets
		$widgets_manager->register( new \LVMR_Product_Carousel_Widget() );
		$widgets_manager->register( new \LVMR_Scrollable_Widget() );
	}

}

LVMR_Addons::instance();
