<?php

namespace Divi_Essential\Includes;

defined( 'ABSPATH' ) || die();

class Admin {


	const MODULES_NONCE       = 'dnxte_save_admin';
	const SAVE_MODULE_ACTION  = 'dnxte_save_modules_data';
	const SAVE_FEATURE_ACTION = 'dnxte_save_extensions_data';


	public function __construct() {
		add_action( 'admin_menu', array( __CLASS__, 'add_menu' ), 21 );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ), 21 );
		add_action( 'wp_ajax_' . self::MODULES_NONCE, array( __CLASS__, 'save_data' ) );
		add_action( 'wp_ajax_' . self::SAVE_MODULE_ACTION, array( __CLASS__, 'save_modules_data' ) );
		add_action( 'wp_ajax_' . self::SAVE_FEATURE_ACTION, array( __CLASS__, 'save_extensions_data' ) );
		$visibility_feature = get_option( 'dnxte_inactive_extensions', array() );

		add_action( 'customize_register', array( __CLASS__, 'render_browser_scrollbar' ) );
		add_action( 'wp_footer', array( __CLASS__, 'render_browser_scrollbar_data' ) );
		add_action( 'wp_head', array( __CLASS__, 'browser_scrollbar_noscript' ) );
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'frontend_enqueue_scripts' ) );
		add_action( 'dnext_import_mm_demo', array( __CLASS__, 'import_mm_demo' ) );

		add_action( 'wp_ajax_dnxte_maintenance_mode_action', array( __CLASS__, 'maintenance_mode_ajax_callback' ) );
		add_action( 'admin_init', array( __CLASS__, 'dnext_essential_maintenance_mode_option' ) );

		// Extend File Format Filter
		add_filter( 'upload_mimes', array( __CLASS__, 'dnext_extends_file_formats' ) );
		// Extend File Format Ajax Hook
		add_action( 'wp_ajax_dnxte_save_file_formats', array( __CLASS__, 'dnxte_save_file_formats_callback' ) );

		// Extended Font Type Ajax Hook.
		if ( ! in_array( 'extended-font-type', $visibility_feature, true ) ) {
			add_action( 'wp_ajax_dnxte_save_font_uploads', array( __CLASS__, 'dnxte_save_font_uploads_callback' ) );
		}
		
		// Always initialize font upload hooks to handle both enabling and disabling font formats
		self::init_font_uploads();

		if ( ! in_array( 'dnxte-widget-extension', $visibility_feature ) ) {
			add_action(
				'widgets_init',
				function () {
					register_widget( 'dnxte_widget_library' );
				}
			);
			require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/widget-library/widget-library.php';
		}

		// __LICENSE__INFO__

		/**
		 *
		 * 3. the required plugin license action_hook start
		 */
		add_action('admin_init', array(__CLASS__, 'dnext_essential_plugin_updater'), 0);
		add_action('admin_init', array(__CLASS__, 'dnext_essential_register_option'));
		add_action('admin_init', array(__CLASS__, 'dnext_essential_activate_license'));
		add_action('admin_init', array(__CLASS__, 'dnext_essential_deactivate_license'));
		add_action('admin_notices', array(__CLASS__, 'dnext_essential_admin_notices'));
		/**
		 *
		 * 3. the required plugin license action_hook end
		 */

		// __LICENSE__INFO__
		/**
		 * Project Enhance Action/Filter Hooks
		 */
		if ( ! in_array( 'dnxte-customizable-project-posts', $visibility_feature, true ) ) {
			$hide_project_cpt = get_option( 'dnxte_project_enhance_options', 0 );
			if ( isset( $hide_project_cpt['dnxte_hide_projects'] ) && $hide_project_cpt['dnxte_hide_projects'] ) {
				require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/divi-project-enhance/class-project-enhance.php';
			}
			require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/divi-project-enhance/class-project-dashboard.php';
		}
	}

	/**
	 * Initialize font upload hooks
	 */
	public static function init_font_uploads() {
		$visibility_feature = get_option( 'dnxte_inactive_extensions', array() );
		$font_settings = get_option( 'dnxte_font_uploads', array() );

		// Always add the filter for et_pb_supported_font_formats to handle disabling TTF/OTF
		add_filter( 'et_pb_supported_font_formats', array( __CLASS__, 'dnxte_et_check_filetype_and_ext' ), 1);

		// Only add upload filters if extension is enabled and font settings exist
		if ( ! in_array( 'extended-font-type', $visibility_feature, true ) && ! empty( $font_settings ) ) {
			add_filter( 'upload_mimes', array( __CLASS__, 'dnxte_upload_mimes' ) );
			add_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'dnxte_check_filetype_and_ext' ), 10, 4 );
		}
	}

	/**
	 * Render browser scrollbar
	 *
	 */
	public static function render_browser_scrollbar( $wp_customize ) {
		// Always add the panel
		$wp_customize->add_panel(
			'dnxte_divi_essential_panel',
			array(
				'title'       => __( 'Divi Essential', 'divi-essential' ),
				'description' => __( 'Custom settings for Divi enhancements.', 'divi-essential' ),
				'priority'    => 1,
			)
		);

		// Always add the section
		$wp_customize->add_section(
			'dnxte_browser_scrollbar_section',
			array(
				'title'    => __( 'Advanced Browser Scrollbar', 'divi-essential' ),
				'panel'    => 'dnxte_divi_essential_panel',
				'priority' => 10,
			)
		);

		// Check if extension is active
		if ( self::extension_enable_status( 'dnxte-browser-scrollbar' ) ) {
			// Extension is inactive, show notice
			$wp_customize->add_setting(
				'dnxte_browser_scrollbar_notice',
				array(
					'default'           => '',
					'sanitize_callback' => 'sanitize_text_field',
				)
			);

			// Get the admin URL for the extensions page
			$admin_url = admin_url( 'admin.php?page=dnxte-essential#extensions' );
			
			$wp_customize->add_control(
				new \WP_Customize_Control(
					$wp_customize,
					'dnxte_browser_scrollbar_notice',
					array(
						'section'     => 'dnxte_browser_scrollbar_section',
						'type'        => 'hidden',
						'description' => sprintf(
							'<div style="background: #fff; border-left: 4px solid #007cba; padding: 12px; margin: 0 -12px;">
								<p style="margin: 0 0 8px; font-weight: 600;">%s</p>
								<p style="margin: 0;">%s <a href="%s" style="text-decoration: none;">%s</a></p>
							</div>',
							__( 'Browser Scrollbar Extension is Currently Deactivated', 'divi-essential' ),
							__( 'To customize the browser scrollbar, please activate the extension from', 'divi-essential' ),
							esc_url( $admin_url ),
							__( 'Divi Essential Settings', 'divi-essential' )
						),
					)
				)
			);
			
			return;
		}

		// Extension is active, load the customizer options
		require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/browser-scrollbar/browser-scrollbar.php';

		if ( class_exists( 'Dnxte_Browser_Scrollbar_Customizer' ) ) {
			\Dnxte_Browser_Scrollbar_Customizer::register_customizer_options( $wp_customize );
		}
	}

	/**
	 * Render Simplebar Noscript
	*/
	public static function browser_scrollbar_noscript() {
		if ( self::extension_enable_status( 'dnxte-browser-scrollbar' ) ) {
			return;
		}
		?>
		<noscript>
			<style>
				/**
				 * Reinstate scrolling for non-JS clients
				 */
				.simplebar-content-wrapper {
					scrollbar-width: auto;
					-ms-overflow-style: auto;
				}

				.simplebar-content-wrapper::-webkit-scrollbar,
				.simplebar-hide-scrollbar::-webkit-scrollbar {
					display: initial;
					width: initial;
					height: initial;
				}
			</style>
		</noscript>
		<?php
	}

	/**
	 * Render Browser Scrollbar Data
	 */
	public static function render_browser_scrollbar_data() {
		if ( self::extension_enable_status( 'dnxte-browser-scrollbar' ) ) {
			return;
		}

		require DIVI_ESSENTIAL_DIR . 'includes/extensions/browser-scrollbar/handle-scrollbar-data.php';
	}

	/**
	 * Enqueue frontend scripts
	 *
	 * @return void
	 */
	public static function frontend_enqueue_scripts() {
		if ( self::extension_enable_status( 'dnxte-browser-scrollbar' ) ) {
			return;
		}

		wp_enqueue_style( 'simplebar', DIVI_ESSENTIAL_ASSETS . 'css/simplebar.min.css', array(), DIVI_ESSENTIAL_VERSION );
		wp_enqueue_script( 'simplebar-js', DIVI_ESSENTIAL_ASSETS . 'js/simplebar.min.js', array(), DIVI_ESSENTIAL_VERSION );
		wp_enqueue_script( 'dnxte-scrollbar', DIVI_ESSENTIAL_ASSETS . 'admin/js/browser-scrollbar.js', array( 'jquery' ), DIVI_ESSENTIAL_VERSION, true );

		wp_localize_script(
			'dnxte-scrollbar',
			'dnxte_scroll_config',
			array(
				'autohide'    => get_theme_mod( 'dnxte_scrollbar_autohide', 'true' ),
				'shadow'      => (bool) get_theme_mod( 'dnxte_scrollbar_shadow_enable', false ),
				'shadowThumb' => get_theme_mod( 'dnxte_scrollbar_thumb_shadow_color', '#000000' ),
				'shadowTrack' => get_theme_mod( 'dnxte_scrollbar_rail_shadow_color', '#000000' ),
				'alignment'   => get_theme_mod( 'dnxte_scrollbar_align', 'right' ),
				'offsetY'     => get_theme_mod( 'dnxte_scrollbar_shadow_offset_y', 2 ),
				'blur'        => get_theme_mod( 'dnxte_scrollbar_shadow_blur', 4 ),
				'step'        => get_theme_mod( 'dnxte_mouse_scroll_step', 40 ),
				'speed'       => get_theme_mod( 'dnxte_scroll_speed', 1 ),
				'width'       => get_theme_mod( 'dnxte_scrollbar_width', 8 ),
				'radius'      => get_theme_mod( 'dnxte_scrollbar_border_radius', 4 ),
				'thumbColor'  => get_theme_mod( 'dnxte_scrollbar_thumb_color', '#555' ),
				'trackColor'  => get_theme_mod( 'dnxte_scrollbar_track_bg_color', '#ccc' ),
			)
		);
	}

	/**
	 * Add mime types callback
	 *
	 * @param array $mimes
	 *
	 * @return array
	 */
	public static function dnext_extends_file_formats( array $mimes ): array {
		// Bail early if the extension is disabled
		if ( self::extension_enable_status( 'dnxte-extend-file-format' ) ) {
			return $mimes;
		}

		$supported_file_formats = get_option( 'dnxte_file_upload_options', array() );
		if ( ! is_array( $supported_file_formats ) || empty( $supported_file_formats ) ) {
			return $mimes;
		}

		// Define MIME types explicitly
		$mime_map = array(
			'svg'  => 'image/svg+xml',
			'json' => 'application/json',
		);

		foreach ( $supported_file_formats as $format => $enabled ) {
			if ( ! empty( $enabled ) && isset( $mime_map[ $format ] ) ) {
				$mimes[ $format ] = $mime_map[ $format ];
			}
		}

		return $mimes;
	}

	/**
	 * Check if the extension is enabled
	 *
	 * @param string $extension_key
	 *
	 * @return bool
	 */
	public static function extension_enable_status( $extension_key ) {
		$inactive_extensions = get_option( 'dnxte_inactive_extensions', array() );
		if ( is_array( $inactive_extensions ) && in_array( $extension_key, $inactive_extensions, true ) ) {
			return true;
		}

		return false;
	}

	public static function add_menu()
	{
		$extend_file_format_status = self::get_extension_status( 'dnxte-extend-file-format' );
		$dnxte_inactive_extensions = get_option( 'dnxte_inactive_extensions', array() );
		$project_posts_status      = self::get_extension_status( 'dnxte-customizable-project-posts' );
		// $testimonial_posts_status  = self::get_extension_status( 'dnxte-customizable-testimonial-posts' );

		add_menu_page(
			__( 'Divi Essential', 'dnxte-divi-essential' ),
			__( 'Divi Essential', 'dnxte-divi-essential' ),
			'manage_options',
			'dnxte-essential',
			array( __CLASS__, 'render_main' ),
			dnxte_svg_icon(),
			111
		);

		add_submenu_page(
			'dnxte-essential',
			__( 'Divi Essential', 'dnxte-divi-essential' ),
			__( 'Dashboard', 'dnxte-divi-essential' ),
			'manage_options',
			'dnxte-essential',
			array( __CLASS__, 'render_main' )
		);

		if ( is_array( $dnxte_inactive_extensions ) && ! in_array( 'dnxte-maintenance-mode', $dnxte_inactive_extensions, true ) ) {
			/**
			 *
			 * 5. Maintenance Mode settings page
			 */
			add_submenu_page(
				'dnxte-essential',
				__( 'Maintenance Mode', 'dnxte-divi-essential' ),
				__( 'Maintenance Mode', 'dnxte-divi-essential' ),
				'manage_options',
				DNEXT_ESSENTIAL_MAINTENANCE_MODE_PAGE,
				array( __CLASS__, 'render_maintenance_mode_page' )
			);
		}

		if ( is_array( $dnxte_inactive_extensions ) && ! in_array( 'dnxte-back-to-top', $dnxte_inactive_extensions, true ) ) {
			/**
			 *
			 * 5. The required plugin back to top submenu page
			 */
			add_submenu_page(
				'dnxte-essential',
				__( 'Divi Essential Back To Top', 'dnxte-divi-essential' ),
				__( 'Back To Top', 'dnxte-divi-essential' ),
				'manage_options',
				DNEXT_ESSENTIAL_BACK_TO_TOP_PAGE,
				array( __CLASS__, 'render_back_to_top_page' )
			);
		}

		// Extended Font Type.
		$dnxte_inactive_extensions = get_option( 'dnxte_inactive_extensions', array() );
		if ( is_array( $dnxte_inactive_extensions ) && ! in_array( 'extended-font-type', $dnxte_inactive_extensions, true ) ) {
			/**
			 *
			 * 5. The required plugin custom font submenu page
			 */
			add_submenu_page(
				'dnxte-essential',
				__( 'Extended Font Type', 'dnxte-divi-essential' ),
				__( 'Extended Font Type', 'dnxte-divi-essential' ),
				'manage_options',
				'extended-font-type',
				array( __CLASS__, 'render_custom_font_page' )
			);
		}

		/**
		 * Extend File Format Sub-Menu
		 */
		if ( ! $extend_file_format_status ) {
			add_submenu_page(
				'dnxte-essential',
				__( 'Extended File Format Support', 'dnxte-divi-essential' ),
				__( 'Extended File Format', 'dnxte-divi-essential' ),
				'manage_options',
				DNEXT_EXTEND_FILE_FORMAT,
				array( __CLASS__, 'render_extend_file_format' )
			);
		}

		/**
		 *
		 * The required plugin project enhancement submenu page
		 */
		if ( ! $project_posts_status && post_type_exists( 'project' ) ) {
			add_submenu_page(
				'dnxte-essential',
				__( 'Customizable Project Posts', 'dnxte-divi-essential' ),
				__( 'Customizable Project', 'dnxte-divi-essential' ),
				'manage_options',
				'dnxte-customizable-project-posts',
				array( __CLASS__, 'render_project_enhancement_page' ),
			);
		}

		// __LICENSE__INFO__

		/**
		 *
		 * 4. The required plugin license submenu start
		 */
		add_submenu_page(
			'dnxte-essential',
			__( 'Divi Essential License', 'dnxte-divi-essential' ),
			__( 'License', 'dnxte-divi-essential' ),
			'manage_options',
			DNEXT_ESSENTIAL_PLUGIN_LICENSE_PAGE,
			array( __CLASS__, 'render_license_page' ),
			15
		);

		/**
		 *
		 * 4. The required plugin license submenu END
		 */

		// __LICENSE__INFO__
	}

	/**
	 *
	 * The required plugin project enhancement submenu page
	 */
	public static function render_project_enhancement_page() {
		require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/divi-project-enhance/template.php';
	}

	public static function enqueue_scripts( $hooks )
	{

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( 'divi-essential_page_extended-font-type' !== $hooks ) {
		wp_enqueue_style(
			'dnxte-admin',
			DIVI_ESSENTIAL_ASSETS . 'admin/css/admin.css',
			array(),
			DIVI_ESSENTIAL_VERSION,
			'all'
		);

		wp_enqueue_script(
			'dnxte-admin-js',
			DIVI_ESSENTIAL_ASSETS . 'admin/js/admin.js',
			array( 'jquery' ),
			DIVI_ESSENTIAL_VERSION,
			true
		);
		}

		wp_localize_script(
			'dnxte-admin-js',
			'Dnxte_Essential',
			array(
				'ajaxurl'                  => admin_url( 'admin-ajax.php' ),
				'nonce'                    => wp_create_nonce( self::MODULES_NONCE ),
				'action'                   => self::MODULES_NONCE,
				'assets_url'               => DIVI_ESSENTIAL_ASSETS,
				'activated_notification'   => __( 'Maintenance Mode Activated.', 'dnxte-divi-essential' ),
				'deactivated_notification' => __( 'Maintenance Mode Deactivated', 'dnxte-divi-essential' ),
			)
		);

		if ( 'divi-essential_page_dnext-maintenance-mode' === $hooks ) {
			wp_enqueue_style(
				'dnxte-maintenance-mode',
				DIVI_ESSENTIAL_ASSETS . 'admin/css/maintenance-mode.css'
			);

			wp_enqueue_script(
				'dnxte-maintenance-mode',
				DIVI_ESSENTIAL_ASSETS . 'admin/js/maintenance-mode.js',
				array( 'jquery' ),
				DIVI_ESSENTIAL_VERSION,
				true
			);

			wp_localize_script(
				'dnxte-maintenance-mode',
				'DE_EXT_OBJ',
				array(
					'ajaxurl' => admin_url( 'admin-ajax.php' ),
					'nonce'   => wp_create_nonce( self::MODULES_NONCE ),
					'action'  => self::MODULES_NONCE,
				)
			);
		}

		// Extended Font Type
		if ( 'divi-essential_page_extended-font-type' === $hooks ) {
			wp_enqueue_style(
				'extended-font-type',
				DIVI_ESSENTIAL_ASSETS . 'admin/css/custom-font.css'
			);

			wp_enqueue_script(
				'extended-font-type',
				DIVI_ESSENTIAL_ASSETS . 'admin/js/custom-font.js',
				array( 'jquery' ),
				DIVI_ESSENTIAL_VERSION,
				true
			);

			wp_localize_script(
				'extended-font-type',
				'dnxte_ajax',
				array(
					'ajaxurl'  => admin_url( 'admin-ajax.php' ),
					'security' => wp_create_nonce( 'dnxte_save_font_uploads_nonce' ),
				)
			);
		}

		require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/widget-library/widget-library.php';
	}

	public static function save_data( $data ) {
		$posted_data = ! empty( $data ) ? $data : '';
		$data        = array();
		parse_str( $posted_data, $data );

		do_action( 'dnxte_save_admin_data', $data );

		return $data;
	}

	public static function save_modules_data( $data ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		check_ajax_referer( self::MODULES_NONCE, 'nonce' );

		$modules_data = isset($_POST['data']) ? $_POST['data'] : ''; // phpcs:ignore

		$data             = self::save_data( $modules_data );
		$modules          = ! empty( $data['modules'] ) ? $data['modules'] : array();
		$inactive_modules = array_values( array_diff( array_keys( self::get_modules_map() ), $modules ) );

		self::save_inactive_modules( 'modules', $inactive_modules );

		wp_send_json_success();
	}

	public static function save_extensions_data() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		check_ajax_referer( self::MODULES_NONCE, 'nonce' );

		$extensions_data = isset($_POST['data']) ? $_POST['data'] : ''; // phpcs:ignore
		$data             = self::save_data( $extensions_data );
		$feature          = ! empty( $data['extensions'] ) ? $data['extensions'] : array();
		$inactive_modules = array_values( array_diff( array_keys( self::get_extensions_map() ), $feature ) );

		// Import Maintenance Mode demo.
		self::import_maintenance_demo();

		self::save_inactive_modules( 'extensions', $inactive_modules );

		wp_send_json_success();
	}

	/**
	 * Import Maintenance Mode demo in Background from Remote Address.
	 *
	 * @return void
	 */
	public static function import_maintenance_demo() {
		if ( ! class_exists( 'DnxteMaintenanceMode' ) ) {
			require_once plugin_dir_path( __FILE__ ) . 'extensions/maintenance-mode/maintenance-mode.php';
		}

		$maintenance = new DnxteMaintenanceMode();
		if ( method_exists( $maintenance, 'dnxte_import_mm_demo_from_remote' ) ) {
			$maintenance->dnxte_import_mm_demo_from_remote();

			// permalink flush.
			flush_rewrite_rules();

			// Clear cache.
			$maintenance->clear_maintenance_cache();
		}
	}

	public static function get_inactive( $field ) {
		return get_option( 'dnxte_inactive_' . $field, array() );
	}

	public static function save_inactive_modules( $field, $modules = array() ) {
		update_option( 'dnxte_inactive_' . $field, $modules );
	}

	private static function get_free_modules_map() {

		return array(
			'dnxte-divider'                   => array(
				'title'     => __( 'Next Divider', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-next-divider/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNextDivider/icon.svg',
				'is_active' => true,
			),
			'dnxte-image-magnifier'           => array(
				'title'     => __( 'Image Magnifier', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-image-magnifier/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNextImageMagnifier/icon.svg',
				'is_active' => true,
			),
			'dnxte-image-reveal'              => array(
				'title'     => __( 'Image Reveal', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-image-reveal/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNextImageReveal/icon.svg',
				'is_active' => true,
			),
			'dnxte-lottie'                    => array(
				'title'     => __( 'Lottie', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-lottie/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNextLottie/icon.svg',
				'is_active' => true,
			),
			'dnxte-gallery-slider'            => array(
				'title'     => __( 'Gallery Slider', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-gallery-slider/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNextThumbsGallery/icon.svg',
				'is_active' => true,
			),
			'dnxte-image-hotspot'             => array(
				'title'     => __( 'Image Hotspot', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-hotspot/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNextTooltip/icon.svg',
				'is_active' => true,
			),
			'dnxte-3d-cube-slider'            => array(
				'title'     => __( '3d Cube Slider', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-3d-cube-slider/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxte3dCubeSlider/icon.svg',
				'is_active' => true,
			),
			'dnxte-business-hour'             => array(
				'title'     => __( 'Business Hour', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-business-hour/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxteBusinessHour/icon.svg',
				'is_active' => true,
			),
			'dnxte-coverflow-slider'          => array(
				'title'     => __( 'Divi Carousel', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-coverflow-slider/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxteCoverflowSlider/icon.svg',
				'is_active' => true,
			),
			'dnxte-feature-list'              => array(
				'title'     => __( 'Feature List', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-feature-list/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxteFeatureList/icon.svg',
				'is_active' => true,
			),
			'dnxte-floating-element'          => array(
				'title'     => __( 'Floating Element', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-floating-elements/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxteFloatingElement/icon.svg',
				'is_active' => true,
			),
			'dnxte-price-list'                => array(
				'title'     => __( 'Price List', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-price-list/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxtePriceList/icon.svg',
				'is_active' => true,
			),
			'dnxte-promo-box'                 => array(
				'title'     => __( 'Promo Box', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/next-promo-box/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxtePromobox/icon.svg',
				'is_active' => true,
			),
			'dnxte-testimonial-slider'        => array(
				'title'     => __( 'Testimonial Slider', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-testimonial-carousel/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxteTestimonial/icon.svg',
				'is_active' => true,
			),
			'dnxte-flip-box'                  => array(
				'title'     => __( 'Flip Box', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-flip-box/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxtFlipbox/icon.svg',
				'is_active' => true,
			),
			'dnxte-logo-carousel'             => array(
				'title'     => __( 'Logo Carousel', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-logo-carousel/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxtLogoCarousel/icon.svg',
				'is_active' => true,
			),
			'dnxte-person'                    => array(
				'title'     => __( 'Person', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-next-person/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'DiviNxtPerson/icon.svg',
				'is_active' => true,
			),
			'dnxte-3D-flip-box'               => array(
				'title'     => __( '3D Flip Box', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/3d-flip-box/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'Next3dFlipbox/icon.svg',
				'is_active' => true,
			),
			'dnxte-before-after'              => array(
				'title'     => __( 'Before After', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-before-after-module/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextBeforeAfter/icon.svg',
				'is_active' => true,
			),
			'dnxte-blurb'                     => array(
				'title'     => __( 'Blurb', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-next-blurb/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextBlurb/icon.svg',
				'is_active' => true,
			),
			'dnxte-button'                    => array(
				'title'     => __( 'Button', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-next-button/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextButton/icon.svg',
				'is_active' => true,
			),
			'dnxte-circular-image-hover'      => array(
				'title'     => __( 'Circular Image Hover', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-circular-image-effect/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextCircularImageHover/icon.svg',
				'is_active' => true,
			),
			'dnxte-facbook-comment'           => array(
				'title'     => __( 'Facbook Comment', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-facebook-comment/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextComment/icon.svg',
				'is_active' => true,
			),
			'dnxte-dual-button'               => array(
				'title'     => __( 'Dual Button', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-dual-button/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextDualButton/icon.svg',
				'is_active' => true,
			),
			'dnxte-facebook-embedded-comment' => array(
				'title'     => __( 'Facebook Embedded Comment', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-embedded-comment/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextEmbeddedComment/icon.svg',
				'is_active' => true,
			),
			'dnxte-facebook-embedded-post'    => array(
				'title'     => __( 'Facebook Embedded Post', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-facebook-embedded-post/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextEmbeddedPost/icon.svg',
				'is_active' => true,
			),
			'dnxte-facebook-embedded-Video'   => array(
				'title'     => __( 'Facebook Embedded Video', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-facebook-embedded-video/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextEmbeddedVideo/icon.svg',
				'is_active' => true,
			),
			'dnxte-facebook-page'             => array(
				'title'     => __( 'Facebook Page', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-facebook-page/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextFBPage/icon.svg',
				'is_active' => true,
			),
			'dnxte-facebook-share-button'     => array(
				'title'     => __( 'Facebook Share Button', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-facebook-share/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextFBShareButton/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-gradient'             => array(
				'title'     => __( 'Text Gradient', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-gradient-text/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextGradientText/icon.svg',
				'is_active' => true,
			),
			'dnxte-image-accordion'           => array(
				'title'     => __( 'Image Accordion', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-image-accordion/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextImageAccordion/icon.svg',
				'is_active' => true,
			),
			'dnxte-image-hover-box'           => array(
				'title'     => __( 'Image Hover Box', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-image-hover-box-effect/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextImageHoverBox/icon.svg',
				'is_active' => true,
			),
			'dnxte-image-icon-effect'         => array(
				'title'     => __( 'Image Icon Effect', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-image-icon-effect/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextImageIcon/icon.svg',
				'is_active' => true,
			),
			'dnxte-image-scroll'              => array(
				'title'     => __( 'Image Scroll', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-image-scroll-effect/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextImageScroll/icon.svg',
				'is_active' => true,
			),
			'dnxte-facebook-like-button'      => array(
				'title'     => __( 'Facebook Like Button', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-facebook-like/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextLikeButton/icon.svg',
				'is_active' => true,
			),
			'dnxte-masonry-gallery'           => array(
				'title'     => __( 'Masonry Gallery', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-masonry-gallery/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextMasonary/icon.svg',
				'is_active' => true,
			),
			'dnxte-mega-image-effect'         => array(
				'title'     => __( 'Mega Image Effect', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-mega-image-effect/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextMegaImageEffect/icon.svg',
				'is_active' => true,
			),
			'dnxte-minimal-image'             => array(
				'title'     => __( 'Minimal Image', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-minimal-image-hover-effect/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextMinimalImageHover/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-multi-heading'        => array(
				'title'     => __( 'Text Multi Heading', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-multi-heading/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextMultiHeading/icon.svg',
				'is_active' => true,
			),
			'dnxte-rating'                    => array(
				'title'     => __( 'Divi Rating', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-rating/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextRating/icon.svg',
				'is_active' => true,
			),
			'dnxte-divi-review'               => array(
				'title'     => __( 'Divi Review', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-review/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextReview/icon.svg',
				'is_active' => true,
			),
			'dnxte-step-flow'                 => array(
				'title'     => __( 'Step Flow', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/step-flow/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextStepFlow/icon.svg',
				'is_active' => true,
			),
			'dnxte-team-creative'             => array(
				'title'     => __( 'Divi Creative Team', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-creative-team/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTeamCreative/icon.svg',
				'is_active' => true,
			),
			'dnxte-team-member-overlay'       => array(
				'title'     => __( 'Team Member Overlay', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-team-member-overlay/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTeamOverlay/icon.svg',
				'is_active' => true,
			),
			'dnxte-team-overlay-card'         => array(
				'title'     => __( 'Divi Team Overlay Card', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-team-overlay-card/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTeamOverlayCard/icon.svg',
				'is_active' => true,
			),
			'dnxte-team-social-reveal'        => array(
				'title'     => __( 'Team Social Reveal', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-team-social-reveal/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTeamSocialReveal/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-animation'            => array(
				'title'     => __( 'Text Animation', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-animation/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextAnimation/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-color-motion'         => array(
				'title'     => __( 'Text Color Motion', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-color-motion/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextColorMotion/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-divider'              => array(
				'title'     => __( 'Text Divider', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-divider/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextDivider/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-glitch'               => array(
				'title'     => __( 'Text Glitch', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-glitch-text/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextGlitch/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-highlight'            => array(
				'title'     => __( 'Text Highlight', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-highlight/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextHighlight/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-hover-highlight'      => array(
				'title'     => __( 'Text Hover Highlight', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-hover-highlight/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextHoverHighlight/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-mask'                 => array(
				'title'     => __( 'Text Mask', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-mask/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextMask/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-stroke'               => array(
				'title'     => __( 'Text Stroke', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-stroke/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextStroke/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-stroke-motion'        => array(
				'title'     => __( 'Text Stroke Motion', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-stroke-motion/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextStrokeMotion/icon.svg',
				'is_active' => true,
			),
			'dnxte-text-tilt'                 => array(
				'title'     => __( 'Text Tilt', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-text-tilt/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTextTilt/icon.svg',
				'is_active' => true,
			),
			'dnxte-timeline'                  => array(
				'title'     => __( 'Timeline', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-timeline/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTimeline/icon.svg',
				'is_active' => true,
			),
			'dnxte-twitter-follow'            => array(
				'title'     => __( 'Twitter Follow Button', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-twitter-follow/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTwitterFollow/icon.svg',
				'is_active' => true,
			),
			'dnxte-twitter-share'             => array(
				'title'     => __( 'Twitter Tweet Button', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-twitter-button/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTwitterShare/icon.svg',
				'is_active' => true,
			),
			'dnxte-twitter-timeline'          => array(
				'title'     => __( 'Twitter Timeline', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-twitter-timeline/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTwitterTimeline/icon.svg',
				'is_active' => true,
			),
			'dnxte-twitter-tweet'             => array(
				'title'     => __( 'Twitter Tweet', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-twitter-tweet/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextTwitterTweet/icon.svg',
				'is_active' => true,
			),
			'dnxte-ultimate-image-hover'      => array(
				'title'     => __( 'Ultimate Image Hover', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-ultimate-image-effect/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextUltimateImageHover/icon.svg',
				'is_active' => true,
			),
			'dnxte-content-toggle'            => array(
				'title'     => __( 'Content Toggle', 'dnxte-divi-essential' ),
				'demo'      => 'https://www.diviessential.com/divi-content-toggle/',
				'icon'      => DIVI_ESSENTIAL_ICON . 'NextContentToggle/icon.svg',
				'is_active' => true,
			),
			'dnxte-post-carousel'             => array(
				'title' => __( 'Post Carousel', 'dnxte-divi-essential' ),
				'demo'  => 'https://www.diviessential.com/divi-post-carousel/',
				'icon'  => DIVI_ESSENTIAL_ICON . 'NextBlogSlider/icon.svg',
			),
			'dnxte-advanced-tab'              => array(
				'title' => __( 'Advanced tab', 'dnxte-divi-essential' ),
				'demo'  => 'https://www.diviessential.com/divi-advanced-tab/',
				'icon'  => DIVI_ESSENTIAL_ICON . 'NextAdvancedTab/icon.svg',
			),
		);
	}

	private static function get_pro_modules_map() {
		return array();
	}

	public static function get_modules_map() {

		$free_modules_map = self::get_free_modules_map();
		$modules_map      = array_merge( $free_modules_map, self::get_pro_modules_map() );

		uasort( $modules_map, array( __CLASS__, 'sort_widgets' ) );
		return $modules_map;
	}

	public static function get_extensions_map() {

		$modules_map = array(
			'custom-attributes-extension'   => array(
				'title'     => __( 'Custom Attributes', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/custom-attribute/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/custom-attr-v4.svg',
				'is_active' => true,
				'is_new'    => false,
				'desc'      => __( 'Add custom attributes to HTML elements', 'dnxte-divi-essential' ),
			),
			'read-more-extension'           => array(
				'title'     => __( 'Read More', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/read-more/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/read-more-v4.svg',
				'is_active' => true,
				'is_new'    => false,
				'desc'      => __( 'Compress & expand content with Read More', 'dnxte-divi-essential' ),
			),
			'popup-pro-extension'           => array(
				'title'     => __( 'Popup Pro', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/divi-popup-pro/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/popup-pro-v4.svg',
				'is_active' => true,
				'is_new'    => false,
				'desc'      => __( 'Advanced pop-up builder with customizable triggers', 'dnxte-divi-essential' ),
			),
			'dnxte-shortcode-extension'     => array(
				'title'     => __( 'Library Shortcode', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/library-shortcode/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/library-shortcode-v4.svg',
				'is_active' => true,
				'is_new'    => false,
				'desc'      => __( 'Add & reuse elements anywhere using shortcodes', 'dnxte-divi-essential' ),
			),
			'dnxte-widget-extension'        => array(
				'title'     => __( 'Widget Library', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/widget-library/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/widget-library-v4.svg',
				'is_active' => true,
				'is_new'    => false,
				'desc'      => __( 'Widgets Collection for dynamic content creation', 'dnxte-divi-essential' ),
			),
			'dnxte-back-to-top'             => array(
				'title'     => __( 'Back To Top', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/back-to-top/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/back-to-top.svg',
				'is_active' => false,
				'is_new'    => true,
				'desc'      => __( 'Add a "Back to Top" button to your Divi site', 'dnxte-divi-essential' ),
			),
			'dnxte-hide-adminbar-extension' => array(
				'title'     => __( 'Hide Admin Bar', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/hide-admin-bar/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/hide-adminbar.svg',
				'is_new'    => true,
				'is_active' => false,
				'desc'      => __( 'Hide the WordPress admin bar for all users', 'dnxte-divi-essential' ),
			),
			'dnxte-clear-cache-extension'   => array(
				'title'     => __( 'Clear Divi Cache', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/clear-divi-cache/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/clear-cache.svg',
				'is_new'    => true,
				'is_active' => false,
				'desc'      => __( 'One-click Divi Cache cleaner', 'dnxte-divi-essential' ),
			),
			'dnxte-header-navigation-extension'   => array(
				'title'     => __( 'Header & Navigation', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/header-navigation/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/header-navigation.svg',
				'is_new'    => true,
				'is_active' => false,
				'desc'      => __('Customize headers and navigation across your website', 'dnxte-divi-essential' ),
			),			
			'dnxte-extend-file-format'      => array(
				'title'     => __( 'Extended File Format', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/extended-file-format-support/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/extended-file-format.svg',
				'is_active' => true,
				'is_new'    => true,
				'desc'      => __( 'Extended File Format Support for WP Media Library', 'dnxte-divi-essential' ),
			),
			'dnxte-maintenance-mode' => array(
				'title'     => __( 'Maintenance Mode', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/maintenance-mode/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/maintenance-mode.svg',
				'is_active' => false,
				'is_new'    => true,
				'desc'      => __( 'Temporarily put your site under maintenance.', 'dnxte-divi-essential' ),
			),
			'dnxte-customizable-project-posts' => array(
				'title'     => __( 'Customizable Project', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/customizable-project-posts/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/customizable-project-posts.svg',
				'is_active' => true,
				'is_new'    => true,
				'desc'      => __( 'Tailor your project posts and their display.', 'dnxte-divi-essential' ),
			),
			'extended-font-type'             => array(
				'title'     => __( 'Extended Font Type', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/extended-font-type/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/custom-font.svg',
				'is_active' => true,
				'is_new'    => true,
				'desc'      => __( 'Enable WOFF, and WOFF2 font file uploads.', 'dnxte-divi-essential' ),
			),
			'dnxte-browser-scrollbar' => array(
				'title'     => __( 'Browser Scrollbar', 'dnxte-divi-essential' ),
				'demo'      => 'https://diviessential.com/browser-scrollbar/',
				'icon'      => DIVI_ESSENTIAL_EXTENSIONS_PATH . 'icon-image/browser-scrollbar.svg',
				'is_active' => true,
				'is_new'    => true,
				'desc'      => sprintf(
					// Translators: %s is the URL to the WordPress Customizer section for the scrollbar.
					__( 'Customize the scrollbar of your website from <a href="%s" target="_blank">Customizer</a>.', 'dnxte-divi-essential' ),
					esc_url( admin_url( 'customize.php?autofocus[section]=dnxte_browser_scrollbar_section' ) )
				),
			),
		);

		uasort( $modules_map, array( __CLASS__, 'sort_widgets' ) );
		return $modules_map;
	}

	public static function sort_widgets( $k1, $k2 ) {
		return strcasecmp( $k1['title'], $k2['title'] );
	}

	public static function get_tabs() {

		$icon_url = DIVI_ESSENTIAL_ASSETS . 'images/admin/';

		$tabs = array(
			'home'       => array(
				'title'    => esc_html__( 'Home', 'dnxte-divi-essential' ),
				'icon'     => '',
				'renderer' => array( __CLASS__, 'render_home' ),
			),
			'modules'    => array(
				'title'    => esc_html__( 'Modules', 'dnxte-divi-essential' ),
				'icon'     => '',
				'renderer' => array( __CLASS__, 'render_modules' ),
			),
			'extensions' => array(
				'title'    => esc_html__( 'Extensions', 'dnxte-divi-essential' ),
				'icon'     => '',
				'renderer' => array( __CLASS__, 'render_extensions' ),
			),
		);

		return $tabs;
	}

	private static function load_template( $template ) {
		$file = DIVI_ESSENTIAL_DIR . 'includes/admin/view/admin-' . $template . '.php';
		if ( is_readable( $file ) ) {
			include $file;
		}
	}

	public static function render_main() {
		self::load_template( 'main' );
	}

	public static function render_home() {
		self::load_template( 'home' );
	}

	public static function render_modules() {
		self::load_template( 'modules' );
	}
	public static function render_extensions() {
		self::load_template( 'extensions' );
	}
	// 5. The required plugin back to top submenu page END
	public static function render_back_to_top_page() {
		require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/back-to-top/template.php';
	}

	// The required plugin custom font submenu page
	public static function render_custom_font_page() {
		require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/custom-font/custom-font.php';
	}

	/**
	 * Extend File Format Sub-Menu Callback
	 */
	public static function render_extend_file_format() {
		require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/extend-file-format/extend-file-format.php';
	}

	/**
	 * Get any extension status by pass key
	 *
	 * @param string $extension_key
	 * @return bool
	 */
	public static function get_extension_status( $extension_key ) {
		$get_active_extensions = get_option( 'dnxte_inactive_extensions', array() );
		if ( is_array( $get_active_extensions ) && in_array( $extension_key, $get_active_extensions, true ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Save file formats callback
	 */
	public static function dnxte_save_file_formats_callback() {
		check_ajax_referer( 'dnxte_save_file_formats_nonce', 'security' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( 'Permission denied' );
		}

		$supported_formats = array( 'svg', 'json' );
		$saved_data        = array();

		foreach ( $supported_formats as $format ) {
			$saved_data[ $format ] = isset( $_POST[ $format ] ) ? 1 : 0;
		}

		update_option( 'dnxte_file_upload_options', $saved_data );

		wp_send_json_success(
			array(
				'message' => 'Settings saved successfully!',
			)
		);
	}

	/**
	 * Save font uploads callback
	 */
	public static function dnxte_save_font_uploads_callback() {
		check_ajax_referer( 'dnxte_save_font_uploads_nonce', 'security' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( 'Permission denied' );
		}

		$supported_formats = array( 'woff', 'woff2' );
		$saved_data        = array();

		foreach ( $supported_formats as $format ) {
			$saved_data[ $format ] = isset( $_POST[ $format ] ) ? 1 : 0;
		}

		update_option( 'dnxte_font_uploads', $saved_data );

		// Add hooks to enable font uploads if any format is enabled
		$font_uploads_enabled = array_filter( $saved_data );
		// $maintenance->clear_maintenance_cache();
		if ( ! empty( $font_uploads_enabled ) ) {
				add_filter( 'upload_mimes', array( __CLASS__, 'dnxte_upload_mimes' ) );
				add_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'dnxte_check_filetype_and_ext' ), 10, 4 );
		}

		wp_send_json_success(
			array(
				'message' => 'Settings saved successfully!',
			)
		);
	}

	/*
	*
	* 5. Maintenance Mode settings page
	*/
	public static function render_maintenance_mode_page() {
		require_once DIVI_ESSENTIAL_DIR . 'includes/extensions/maintenance-mode/template.php';
	}

	public static function dnext_essential_maintenance_mode_option() {
		register_setting( 'dnext_essential_maintenance_mode', 'dnext_essential_maintenance_mode', array( __CLASS__, 'dnext_essential_sanitize_maintenance_mode' ) );
	}

	public static function dnext_essential_sanitize_maintenance_mode( $new ) {
		if ( ! is_numeric( $new ) ) {
			return 0;
		}

		return $new;
	}

	/**
	 * Maintenance Mode ajax callback
	 *
	 * @return void
	 */
	public static function maintenance_mode_ajax_callback() {
		check_ajax_referer( 'dnxte_maintenance_mode_nonce', 'security' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( 'Permission denied' );
		}

		$mm_page        = isset( $_POST['dnext_mm_page'] ) ? sanitize_text_field( $_POST['dnext_mm_page'] ) : '';
		$mm_type        = isset( $_POST['dnext_mm_type'] ) ? sanitize_text_field( $_POST['dnext_mm_type'] ) : '';
		$mm_header      = isset( $_POST['dnext_mm_header'] ) ? sanitize_text_field( $_POST['dnext_mm_header'] ) : '';
		$mm_footer      = isset( $_POST['dnext_mm_footer'] ) ? sanitize_text_field( $_POST['dnext_mm_footer'] ) : '';
		$mm_full_height = isset( $_POST['dnext_mm_full_height'] ) ? sanitize_text_field( $_POST['dnext_mm_full_height'] ) : '';
		$mm_template    = isset( $_POST['dnext_mm_template'] ) ? sanitize_text_field( $_POST['dnext_mm_template'] ) : '';	

		$saved_data = array(
			'dnext_mm_page'        => $mm_page,
			'dnext_mm_header'      => $mm_header,
			'dnext_mm_footer'      => $mm_footer,
			'dnext_mm_full_height' => $mm_full_height,
			'dnext_mm_template'    => $mm_template,
			'dnext_mm_type'        => $mm_type,
		);

		update_option( 'dnxte_maintenance_mode_options', $saved_data );

		if ( ! class_exists( 'DnxteMaintenanceMode' ) ) {
			require_once plugin_dir_path( __FILE__ ) . 'extensions/maintenance-mode/maintenance-mode.php';
		}

		$maintenance = new DnxteMaintenanceMode();

		$maintenance->clear_maintenance_cache();

		wp_send_json_success(
			array(
				'message' => 'Settings saved successfully!',
			)
		);
	}

	/**
	 * ET check filetype and extension.
	 *
	 * @param array $list
	 *
	 * @return array $list
	 */
	public static function dnxte_et_check_filetype_and_ext( $lists ) {
		$font_settings = get_option( 'dnxte_font_uploads', array() );

		if ( ! is_array( $font_settings ) ) {
			return $lists;
		}

		// Add WOFF and WOFF2 if they are enabled (they are not in Divi by default)
		if ( ! empty( $font_settings['woff'] ) && 1 === (int) $font_settings['woff'] ) {
			if ( ! in_array( 'woff', $lists, true ) ) {
				$lists[] = 'woff';
			}
		}

		if ( ! empty( $font_settings['woff2'] ) && 1 === (int) $font_settings['woff2'] ) {
			if ( ! in_array( 'woff2', $lists, true ) ) {
				$lists[] = 'woff2';
			}
		}

		return $lists;
	}

	// Add this method to handle MIME types
	public static function dnxte_upload_mimes( $mimes ) {
		$font_settings = get_option( 'dnxte_font_uploads', array() );

		// Only add mime types for enabled font formats
		if ( ! empty( $font_settings ) ) {
			if ( ! empty( $font_settings['woff'] ) ) {
				$mimes['woff'] = 'font/woff|application/font-woff|application/x-font-woff|application/octet-stream';
			}
			if ( ! empty( $font_settings['woff2'] ) ) {
				$mimes['woff2'] = 'font/woff2|application/font-woff2|application/x-font-woff2|application/octet-stream';
			}
		}

		return $mimes;
	}

	// Add this method to handle file type checking
	public static function dnxte_check_filetype_and_ext( $data, $file, $filename, $mimes ) {
		$font_settings = get_option( 'dnxte_font_uploads', array() );

		if ( empty( $font_settings ) ) {
			return $data;
		}

		$wp_filetype     = wp_check_filetype( $filename, $mimes );
		$ext             = $wp_filetype['ext'];
		$type            = $wp_filetype['type'];
		$proper_filename = $data['proper_filename'];

		if ( $ext && $type ) {
			return compact( 'ext', 'type', 'proper_filename' );
		}

		// Check for font files - only for enabled formats
		if ( false !== strpos( $filename, '.woff2' ) && ! empty( $font_settings['woff2'] ) ) {
			$ext  = 'woff2';
			$type = 'font/woff2';
		} elseif ( false !== strpos( $filename, '.woff' ) && ! empty( $font_settings['woff'] ) ) {
			$ext  = 'woff';
			$type = 'font/woff';
		}

		if ( $ext && $type ) {
			return compact( 'ext', 'type', 'proper_filename' );
		}

		return $data;
	}

	// __LICENSE__INFO__

	/**
	 *
	 * 5. the required plugin license function start
	 */
	public static function dnext_essential_plugin_updater() {

		// retrieve our license key from the DB
		$license_key = '';
		if ( get_option( 'dnext_essential_license_key' ) !== null ) {
			$license_key = trim( get_option( 'dnext_essential_license_key' ) ?? '' );
		}

		// setup the updater
		$edd_updater = new \DNEXT_Essential_Plugin_Updater_Class(
			DNEXT_ESSENTIAL_STORE_URL,
			DIVI_ESSENTIAL_FILE,
			array(
				'version' => DIVI_ESSENTIAL_VERSION, // current version number
				'license' => $license_key, // license key (used get_option above to retrieve from DB)
				'item_id' => DNEXT_ESSENTIAL_ITEM_ID, // ID of the product
				'author'  => 'Divi Next', // author of this plugin
				'beta'    => false,
			)
		);
	}

	public static function render_license_page() {
		$license = get_option( 'dnext_essential_license_key' );
		$status  = get_option( 'dnext_essential_license_status' );
		?>
		<div class="wrap">
			<h2><?php esc_html_e( 'Divi Essential License Options', 'dnxte-divi-essential' ); ?></h2>
			<form method="post" action="options.php">
				<?php settings_fields( 'dnext_essential_license' ); ?>
				<table class="form-table">
					<tbody>
						<tr valign="top">
							<th scope="row" valign="top">
								<?php esc_html_e( 'License Key', 'dnxte-divi-essential' ); ?>
							</th>
							<td>
								<input id="dnext_essential_license_key" name="dnext_essential_license_key" type="password" class="regular-text" value="<?php esc_attr_e( $license ); ?>" />
								<label class="description" for="dnext_essential_license_key"><?php esc_html_e( 'Enter your license key', 'dnxte-divi-essential' ); ?></label>
							</td>
							<tr valign="top">
								<th scope="row" valign="top">
									<?php esc_html_e( 'Activate License', 'dnxte-divi-essential' ); ?>
								</th>
								<td>
									<?php if ( false !== $status && 'valid' == $status ) { ?>
										<span style="color:green;"><?php esc_html_e( 'is_active', 'dnxte-divi-essential' ); ?></span>
										<?php wp_nonce_field( 'dnext_essential_nonce', 'dnext_essential_nonce' ); ?>
										<input type="submit" class="button-secondary" name="dnext_essential_plugin_license_deactivate" value="<?php esc_attr_e( 'Deactivate License', 'dnxte-divi-essential' ); ?>" />
										<?php
									} else {
										wp_nonce_field( 'dnext_essential_nonce', 'dnext_essential_nonce' );
										?>
										<input type="submit" class="button-secondary" name="dnext_essential_plugin_license_activate" value="<?php esc_attr_e( 'Activate License', 'dnxte-divi-essential' ); ?>" />
									<?php } ?>
								</td>
							</tr>
					</tbody>
				</table>
				<?php submit_button(); ?>
			</form>
		</div>
		<?php
	}


	public static function dnext_essential_register_option() {
		// creates our settings in the options table
		register_setting('dnext_essential_license', 'dnext_essential_license_key', 'dnext_essential_sanitize_license');
	}

	public static function dnext_essential_sanitize_license( $new ) {
		$old = get_option( 'dnext_essential_license_key' );
		if ( $old && $old != $new ) {
			delete_option( 'dnext_essential_license_status' ); // new license has been entered, so must reactivate
		}
		return $new;
	}

	public static function dnext_essential_activate_license() {

		// listen for our activate button to be clicked
		if ( isset( $_POST['dnext_essential_plugin_license_activate'] ) ) {

			// run a quick security check
			if ( ! check_admin_referer( 'dnext_essential_nonce', 'dnext_essential_nonce' ) ) {
				return;
			}
			// get out if we didn't click the Activate button

			// retrieve the license from the database
			$license = '';
			if ( get_option( 'dnext_essential_license_key' ) !== null ) {
				$license = trim( get_option( 'dnext_essential_license_key' ) ?? '' );
			}

			// data to send in our API request
			$api_params = array(
				'edd_action' => 'activate_license',
				'license'    => $license,
				'item_name'  => rawurlencode( DNEXT_ESSENTIAL_ITEM_NAME ), // the name of our product in EDD
				'url'        => home_url(),
			);

			// Call the custom API.
			$response = wp_remote_post(
				DNEXT_ESSENTIAL_STORE_URL,
				array(
					'timeout'   => 15,
					'sslverify' => false,
					'body'      => $api_params,
				)
			);

			// make sure the response came back okay
			if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {

				if ( is_wp_error( $response ) ) {
					$message = $response->get_error_message();
				} else {
					$message = __( 'An error occurred, please try again.', 'dnxte-divi-essential' );
				}
			} else {

				$license_data = json_decode( wp_remote_retrieve_body( $response ) );

				if ( false === $license_data->success ) {

					switch ( $license_data->error ) {

						case 'expired':
							$message = sprintf(
								__( 'Your license key expired on %s.', 'dnxte-divi-essential' ),
								date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'mysql' ) ) )
							);
							break;

						case 'disabled':
						case 'revoked':
							$message = __( 'Your license key has been disabled.', 'dnxte-divi-essential' );
							break;

						case 'missing':
							$message = __( 'Invalid license.', 'dnxte-divi-essential' );
							break;

						case 'invalid':
						case 'site_inactive':
							$message = __( 'Your license is not active for this URL.', 'dnxte-divi-essential' );
							break;

						case 'item_name_mismatch':
							$message = sprintf( __( 'This appears to be an invalid license key for %s.', 'dnxte-divi-essential' ), DNEXT_ESSENTIAL_ITEM_NAME );
							break;

						case 'no_activations_left':
							$message = __( 'Your license key has reached its activation limit.', 'dnxte-divi-essential' );
							break;

						default:
							$message = __( 'An error occurred, please try again.', 'dnxte-divi-essential' );
							break;
					}
				}
			}

			// Check if anything passed on a message constituting a failure
			if ( ! empty( $message ) ) {
				$base_url = admin_url( 'admin.php?page=' . DNEXT_ESSENTIAL_PLUGIN_LICENSE_PAGE );
				$redirect = add_query_arg(
					array(
						'sl_activation' => 'false',
						'message'       => rawurlencode( $message ),
					),
					$base_url
				);

				wp_safe_redirect( $redirect );
				exit();
			}

			// $license_data->license will be either "valid" or "invalid"

			update_option( 'dnext_essential_license_status', $license_data->license );
			wp_safe_redirect( admin_url( 'admin.php?page=' . DNEXT_ESSENTIAL_PLUGIN_LICENSE_PAGE ) );
			exit();
		}
	}

	public static function dnext_essential_deactivate_license() {

		// listen for our activate button to be clicked
		if ( isset( $_POST['dnext_essential_plugin_license_deactivate'] ) ) {

			// run a quick security check
			if ( ! check_admin_referer( 'dnext_essential_nonce', 'dnext_essential_nonce' ) ) {
				return;
			}
			// get out if we didn't click the Activate button

			// retrieve the license from the database
			$license = '';
			if ( get_option( 'dnext_essential_license_key' ) !== null ) {
				$license = trim( get_option( 'dnext_essential_license_key' ) ?? '' );
			}

			// data to send in our API request
			$api_params = array(
				'edd_action' => 'deactivate_license',
				'license'    => $license,
				'item_name'  => rawurlencode( DNEXT_ESSENTIAL_ITEM_NAME ), // the name of our product in EDD
				'url'        => home_url(),
			);

			// Call the custom API.
			$response = wp_remote_post(
				DNEXT_ESSENTIAL_STORE_URL,
				array(
					'timeout'   => 15,
					'sslverify' => false,
					'body'      => $api_params,
				)
			);

			// make sure the response came back okay
			if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {

				if ( is_wp_error( $response ) ) {
					$message = $response->get_error_message();
				} else {
					$message = __( 'An error occurred, please try again.', 'dnxte-divi-essential' );
				}

				$base_url = admin_url( 'admin.php?page=' . DNEXT_ESSENTIAL_PLUGIN_LICENSE_PAGE );
				$redirect = add_query_arg(
					array(
						'sl_activation' => 'false',
						'message'       => rawurlencode( $message ),
					),
					$base_url
				);

				wp_safe_redirect( $redirect );
				exit();
			}

			// decode the license data
			$license_data = json_decode( wp_remote_retrieve_body( $response ) );

			// $license_data->license will be either "deactivated" or "failed"
			if ( 'deactivated' == $license_data->license ) {
				delete_option( 'dnext_essential_license_status' );
			}

			wp_safe_redirect( admin_url( 'admin.php?page=' . DNEXT_ESSENTIAL_PLUGIN_LICENSE_PAGE ) );
			exit();
		}
	}

	public function dnext_essential_check_license() {

		global $wp_version;

		$license = '';
		if ( get_option( 'dnext_essential_license_key' ) !== null ) {
			$license = trim( get_option( 'dnext_essential_license_key' ) ?? '' );
		}

		/**
		 *
		 * 5. the required plugin license function END
		 */
		$api_params = array(
			'edd_action' => 'check_license',
			'license'    => $license,
			'item_name'  => rawurlencode( DNEXT_ESSENTIAL_ITEM_NAME ),
			'url'        => home_url(),
		);

		// Call the custom API.
		$response = wp_remote_post(
			DNEXT_ESSENTIAL_STORE_URL,
			array(
				'timeout'   => 15,
				'sslverify' => false,
				'body'      => $api_params,
			)
		);

		if ( is_wp_error( $response ) ) {
			return false;
		}

		$license_data = json_decode( wp_remote_retrieve_body( $response ) );

		if ( 'valid' == $license_data->license ) {
			echo 'valid';
			exit;
			// this license is still valid
		} else {
			echo 'invalid';
			exit;
			// this license is no longer valid
		}
	}

	public static function dnext_essential_admin_notices() {
		if ( isset( $_GET['sl_activation'] ) && ! empty( $_GET['message'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended

			switch ( $_GET['sl_activation'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended

				case 'false':
					$message = urldecode( sanitize_text_field( $_GET['message'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
					?>
						<div class="error">
							<p>
							<?php
							echo $message; // phpcs:ignore WordPress.Security.EscapeOutput
							?>
								</p>
						</div>
					<?php
					break;

				case 'true':
				default:
					// Developers can put a custom success message here for when activation is successful if they way.
					break;
			}
		}
	}

	/**
	 *
	 * 5. the required plugin license function END
	 */
	// __LICENSE__INFO__
}
