<?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       https://immowp.fr
 * @since      1.0.0
 *
 * @package    Immowp_Gestion_Immobiliere
 * @subpackage Immowp_Gestion_Immobiliere/includes
 */

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

// Include the WordPress Plugin Administration API.


/**
 * 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    Immowp_Gestion_Immobiliere
 * @subpackage Immowp_Gestion_Immobiliere/includes
 * @author     ImmoWP <contact@immowp.fr>
 */
class Immowp_Gestion_Immobiliere {

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

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		if ( defined( 'IMMOWP_GESTION_IMMOBILIERE_LITE_VERSION' ) ) {
			$this->version = IMMOWP_GESTION_IMMOBILIERE_LITE_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'immowp-gestion-immobiliere';

		$this->load_dependencies();
		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Immowp_Gestion_Immobiliere_Loader. Orchestrates the hooks of the plugin.
	 * - Immowp_Gestion_Immobiliere_i18n. Defines internationalization functionality.
	 * - Immowp_Gestion_Immobiliere_Admin. Defines all hooks for the admin area.
	 * - Immowp_Gestion_Immobiliere_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'includes/class-immowp-gestion-immobiliere-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'includes/class-immowp-gestion-immobiliere-i18n.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'admin/class-immowp-gestion-immobiliere-admin.php';
		require_once plugin_dir_path( __DIR__ ) . 'admin/class-immowp-gestion-immobiliere-metaboxes.php';
		require_once plugin_dir_path( __DIR__ ) . 'admin/class-immowp-gestion-immobiliere-metaboxes-annonce.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( __DIR__ ) . 'public/class-immowp-gestion-immobiliere-public.php';

		$this->loader = new Immowp_Gestion_Immobiliere_Loader();
	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Since WordPress 4.6, translations are automatically loaded for plugins
	 * hosted on WordPress.org. No manual action is required.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale() {
		// WordPress automatically loads translations for plugins hosted on WordPress.org since 4.6+.
		// No action needed.
		
		// Keeping this method for backwards compatibility and potential future use.
	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {

		$plugin_admin             = new Immowp_Gestion_Immobiliere_Admin( $this->get_plugin_name(), $this->get_version() );
		$plugin_metaboxes         = new Immowp_Gestion_Immobiliere_Metaboxes( $this->get_plugin_name(), $this->get_version() );
		$plugin_metaboxes_annonce = new Immowp_Gestion_Immobiliere_Metaboxes_Annonce( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

		// Register the custom single page template.
		$this->loader->add_filter( 'admin_menu', $plugin_admin, 'create_custom_submenu' );

		// Add promotional banner to admin pages
		$this->loader->add_action( 'admin_notices', $plugin_admin, 'add_immowp_promotional_banner' );
		$this->loader->add_action( 'wp_ajax_dismiss_immowp_promo_banner', $plugin_admin, 'handle_banner_dismissal' );

		// Register the custom post type.
		$this->loader->add_action( 'init', $plugin_admin, 'register_post_type' );

		// Register the custom single page template.
		$this->loader->add_filter( 'single_template', $plugin_admin, 'register_single_template' );

		// Register the custom archive page template.
		// $this->loader->add_filter('archive_template', $plugin_admin, 'register_archive_template');

		// Add custom columns to the "Real Estate Listing" post type table
		$this->loader->add_filter( 'manage_immowp_gestion_immo_posts_columns', $plugin_admin, 'manage_columns' );

		// Add custom column content to the "Real Estate Listing" post type table
		$this->loader->add_action( 'manage_immowp_gestion_immo_posts_custom_column', $plugin_admin, 'custom_columns', 10, 2 );

		// shortcode
		$this->loader->add_shortcode( 'immowp_property', $plugin_admin, 'immowp_property' );

		// fields iwp agences
		$this->loader->add_action( 'immowp_agences_add_form_fields', $plugin_metaboxes, 'agence_add_term_fields' );
		$this->loader->add_action( 'immowp_agences_edit_form_fields', $plugin_metaboxes, 'agence_edit_term_fields', 10, 2 );
		$this->loader->add_action( 'created_immowp_agences', $plugin_metaboxes, 'agence_save_term_fields', 10, 2 );
		$this->loader->add_action( 'edited_immowp_agences', $plugin_metaboxes, 'agence_save_term_fields', 10, 2 );

		// fields iwp agences
		$this->loader->add_action( 'immowp_agent_add_form_fields', $plugin_metaboxes, 'agent_add_term_fields' );
		$this->loader->add_action( 'immowp_agent_edit_form_fields', $plugin_metaboxes, 'agent_edit_term_fields', 10, 2 );
		$this->loader->add_action( 'created_immowp_agent', $plugin_metaboxes, 'agent_save_term_fields', 10, 2 );
		$this->loader->add_action( 'edited_immowp_agent', $plugin_metaboxes, 'agent_save_term_fields', 10, 2 );

		// annonces
		$this->loader->add_action( 'add_meta_boxes_immowp_gestion_immo', $plugin_metaboxes_annonce, 'create_image_gallery_metabox' );
		$this->loader->add_action( 'save_post_immowp_gestion_immo', $plugin_metaboxes_annonce, 'save_metabox_values' );



		// manage color


		// redirect after deleete
		$this->loader->add_action( 'template_redirect', $plugin_admin, 'redirect_if_deleted_custom_post_type' );

		// taxonomy
		$this->loader->add_action( 'taxonomy_template', $plugin_admin, 'call_taxonomy_template_from_directory' );
		// $this->loader->add_action('wp', $plugin_admin,'show_full_query');
		$this->loader->add_filter( 'generate_rewrite_rules', $plugin_admin, 'taxonomy_rewrite_fix' );
	}

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

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

		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
		$this->loader->add_filter( 'script_loader_tag', $plugin_public, 'add_type_attribute', 10, 3 );
	}

	/**
	 * 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    Immowp_Gestion_Immobiliere_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;
	}
}
