<?php
/**
 * The file that defines the core plugin class for disable elementor icons
 *
 * @since      1.0.0
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 */

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

/**
 * Define captwiki_Icons_Disable class for the free version.
 *
 * @since 1.0.0
 */
class Captwiki_Disable_Icons {

	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		if ( ! is_admin() ) {
			add_action( 'elementor/frontend/after_register_styles', array( $this, 'captwiki_ext_ele_disable_icons' ), 20 );
			add_action( 'wp_enqueue_scripts', array( $this, 'captwiki_disable_eicons' ), 11 );
		}
	}

	/**
	 * Disable Elementor Icons
	 *
	 * @since 1.0.0
	 */
	public function captwiki_ext_ele_disable_icons() {

		$disable_icons = $this->captwiki_get_disable_icons();

		if ( ! empty( $disable_icons ) ) {
			foreach ( array( 'solid', 'regular', 'brands' ) as $icons ) {

				if ( in_array( $icons, $disable_icons, true ) ) {
					wp_deregister_style( 'elementor-icons-fa-' . $icons );
				}
			}
		}
	}

	/**
	 * Disable Elementor Icons
	 *
	 * @since 1.0.0
	 */
	public function captwiki_disable_eicons() {

		$disable_icons = $this->captwiki_get_disable_icons();

		if ( ! empty( $disable_icons ) && in_array( 'eicons', $disable_icons, true ) ) {
			wp_dequeue_style( 'elementor-icons' );
			wp_deregister_style( 'elementor-icons' );
		}
	}

	/**
	 * Get disable icons settings from WP extensions option.
	 *
	 * @since 1.0.0
	 * @return array Disable icons settings.
	 */
	private function captwiki_get_disable_icons() {
		$wp_extensions = get_option( 'captwiki_wp_extensions', array() );

		if (
			is_array( $wp_extensions )
			&& ! empty( $wp_extensions['settings']['Captwiki_Disable_Icons'] )
			&& is_array( $wp_extensions['settings']['Captwiki_Disable_Icons'] )
		) {
			return $wp_extensions['settings']['Captwiki_Disable_Icons'];
		}

		return array();
	}
}

new Captwiki_Disable_Icons();
