<?php
/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       tarokkina_free/fer
 * @since      1.0.0
 *
 * @package    Tarokkina_free
 * @subpackage Tarokkina_free/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    Tarokkina_free
 * @subpackage Tarokkina_free/includes
 * @author     tarokkina_free <tarokkina_free@gmailcom>
 */
class Tarokkina_free {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Tarokkina_free_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;
	protected $plugin_name;
	protected $version;


	public function __construct() {
		if ( defined( 'TAROKKINA_FREE_VERSION' ) ) {
			$this->version = TAROKKINA_FREE_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'tarokina-free';



		// Preview Elementor
    	require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/elementor-styles_free.php';
		
		if (is_admin() ) {
			$getElementor = (isset($_GET['action'])) ? sanitize_text_field($_GET['action']) : null;
			$getDivi = ( isset($_GET['et_fb']) ) ? sanitize_text_field( $_GET['et_fb'] ) : null;
			
			if ($getElementor == 'elementor' || $getDivi == 1 ) {	
			}else{
				// CARBON FIEDS
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'lib/carbon-fields/carbon-fields-plugin.php';
			}
		}
		

		// AÑADIR ASYNC al js del tarot
		function TkNa_async_tarokina_free($tag, $handle, $src) {
			if ($handle === 'tarokinafree') {
				if (false === stripos($tag, 'async')) {
					$tag = str_replace('<script ', '<script async ', $tag);
				}
			}
			return $tag;
		}
		add_filter('script_loader_tag', 'TkNa_async_tarokina_free', 1, 3);


		// AÑADIR defer AL SCRIPT back_tarots.js
		function TkNa_defer_back_tarots_free($tag, $handle, $src) {
			if ($handle === 'tarokkina_free_js_back_tarots') {
				if (false === stripos($tag, 'defer')) {
					$tag = str_replace('<script ', '<script defer ', $tag);
				}
			}
			return $tag;
		}
		add_filter('script_loader_tag', 'TkNa_defer_back_tarots_free', 1, 3);



		$this->TkNa_load_dependencies();
		$this->TkNa_set_locale();
		$this->TkNa_admin_hooks();
		$this->TkNa_define_public_hooks();

	}


	private function TkNa_load_dependencies() {
		$urlPostype = (isset($_GET['post_type'])) ? sanitize_text_field($_GET['post_type']) : '';

		if (is_admin() ) {

			// Custom Post Type
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/tarokkina_free_custom_type.php';

			if ($urlPostype == 'tarokkina_free') {
		    	require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/sidebars/sidebar_all.php';
			 }


			require_once TAROKINA_ADMIN_PATH_FREE . 'fields/fields_cartas.php';

		}


		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-tarokkina_free-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-tarokkina_free-i18n.php';

	
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-tarokkina_free-admin.php';


		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-tarokkina_free-public.php';


		$this->loader = new Tarokkina_free_Loader();


	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Tarokkina_free_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function TkNa_set_locale() {

		$plugin_i18n = new Tarokkina_free_i18n();

		$this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_textdomain' );

	}


	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function TkNa_admin_hooks() {
		$urlPostype = (isset($_GET['post_type'])) ? sanitize_text_field($_GET['post_type']) : '';
		$plugin_admin = new Tarokkina_free_Admin( $this->get_plugin_name(), $this->get_version() );
		$urlPage = (isset($_GET['page'])) ? sanitize_text_field($_GET['page']) : '';
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'TkNa_enqueue_styles_free' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'TkNa_enqueue_scripts_free' );

		// Add admin notice to rate plugin
		$this->loader->add_action('admin_notices', $plugin_admin, 'TkNa_rate_notice',15);
		$this->loader->add_action('admin_init', $plugin_admin, 'TkNa_ignore_notice');



		// creando el option de imagenes
		$this->loader->add_action( 'admin_head', $plugin_admin, 'TkNaP_array_cartas_free');


		if (is_admin() && $urlPostype == 'tarokkina_free') {



			//  TkNa_free_Escritor - setup_theme  wp_loaded  admin_init  carbon_fields_register_fields
			$this->loader->add_action( 'carbon_fields_register_fields', $plugin_admin, 'TkNa_free_Escritor' );

			// Instalador de tarokina free
			$this->loader->add_action( 'carbon_fields_theme_options_container_saved', $plugin_admin, 'TkNa_free_install',11);

			// TkNa_free_link_card
			$this->loader->add_action( 'post_submitbox_start', $plugin_admin, 'TkNa_free_link_card' );

			// tknaNameCard
			$this->loader->add_action( 'edit_form_after_editor', $plugin_admin, 'TkNa_GridAdonsCards' );

			// LINK Review in web for Tarokina Free
			$this->loader->add_filter( 'admin_footer_text', $plugin_admin, 'TkNa_free_admin_footer' );
	
			// Per Page Tarokina Free
			$this->loader->add_filter( 'edit_tarokkina_free_per_page', $plugin_admin, 'TkNa_free_per_page', 10, 1 );

			// Logo
			$this->loader->add_action( 'in_admin_header', $plugin_admin, 'TkNaP_free_cabecera',15 );

	    }


		// Creando el option del tarot free
		$this->loader->add_action( 'carbon_fields_theme_options_container_saved', $plugin_admin, 'TkNa_free_option',1);

		// 2.- Creando el option de los tarots y generando las cartas al salvar las cartas
		$this->loader->add_action( 'save_post_tarokkina_free', $plugin_admin, 'TkNa_free_option',2);

		// 3.- Creando el option de los tarots y generando las cartas al salvar en la tabla de cartas, Bulk actions
		$checkCartas = get_option('_tarokki_checkCartas_free');
		if (!$checkCartas) {
			$this->loader->add_action( 'admin_head', $plugin_admin, 'TkNa_free_option');
			update_option('_tarokki_checkCartas_free','yes' );
		}

		if (is_admin()) {
			$cids = (isset($_GET['cids'])) ? sanitize_text_field($_GET['cids']) : '';
			if ($cids == 2) {

				$this->loader->add_action( 'admin_head', $plugin_admin, 'TkNa_free_option',1);
				delete_option('_tarokki_checkCartas_free' );
				$this->loader->add_action('admin_notices', $plugin_admin, 'TkNaP_notice_clear_cache_free');

			}
	
		}


		// If update plugin... update tarots - Notice update plugin
		$this->loader->add_action( 'upgrader_process_complete', $plugin_admin, 'TkNaP_pro_create_tarots_update_free',10, 2);


		// Links in plugins.php
		add_filter( 'plugin_action_links_tarokina-free/tarokina-free.php',  array( $plugin_admin, 'TkNa_free_links_plugin' ) );


		// TkNa_Grid_addons
		$this->loader->add_action( 'carbon_fields_container_tarot_free_after_fields', $plugin_admin, 'TkNa_Grid_addons',11 );


	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function TkNa_define_public_hooks() {

		$plugin_public = new Tarokkina_free_Public( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'init', $plugin_public, 'TkNa_free_shortcode' );

		// CSS Shortcode
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'TkNa_free_css_shortcode' );


		// Ajax Tarot2
		$this->loader->add_action( 'wp_ajax_nopriv_e-tarotfree', $plugin_public, 'TkNa_free_tarotAjax2' );


		// Ajax Tarot2-2
		$this->loader->add_action( 'wp_ajax_e-tarotfree', $plugin_public, 'TkNa_free_tarotAjax2');


		// Remove Shortcodes from excerpt and home
		$this->loader->add_action( 'wp', $plugin_public, 'TkNa_free_remove_shortcode');

	}





	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Tarokkina_free_Loader    Orchestrates the hooks of the plugin.
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}

}
