<?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://www.triboomedia.it
 * @since      1.0.0
 *
 * @package    Tbm_Tagmailer
 * @subpackage Tbm_Tagmailer/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    Tbm_Tagmailer
 * @subpackage Tbm_Tagmailer/includes
 * @author     Dev Triboomedia <dev.publishing@triboo.it>
 */
class Tbm_Tagmailer {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Tbm_Tagmailer_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( 'TBM_TAGMAILER_VERSION' ) ) {
			$this->version = TBM_TAGMAILER_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'tbm-tagmailer';

		/**
		 * Load the dependencies
		 */
		$this->load_dependencies();

		/**
		 * Set the hooks
		 */
		$this->define_admin_hooks();
		$this->define_public_hooks();
		$this->set_image_size();
		$this->set_locale();
		$this->set_ajax();
		$this->set_acf();
		$this->set_shortcode();
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Tbm_Tagmailer_Loader. Orchestrates the hooks of the plugin.
	 * - Tbm_Tagmailer_i18n. Defines internationalization functionality.
	 * - Tbm_Tagmailer_Admin. Defines all hooks for the admin area.
	 * - Tbm_Tagmailer_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() {

		/**
		 * Gump validator
		 */
		if ( ! class_exists( 'GUMP' ) ) {
			require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/gump/gump.class.php';
			require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/gump/src/ArrayHelpers.php';
			require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/gump/src/EnvHelpers.php';
		}

		/**
		 * The class responsible for defining all function of the ACF plugin.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-acf.php';

		/**
		 * The class responsible for orchestrating the display of the plugin.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-display.php';

		/**
		 * The class responsible for orchestrating the display of the layouts.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-display-layouts.php';

		/**
		 * The class responsible for orchestrating the common functions of the plugin.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-common.php';

		/**
		 * The class responsible for orchestrating the ajax call
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-ajax.php';

		/**
		 * The class responsible for defining image sizes.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-image-sizes.php';

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-i18n.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'admin/class-tbm-tagmailer-admin.php';

		/**
		 * The class responsible for shortcodes
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'includes/class-tbm-tagmailer-shortcode.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once TBM_TAGMAILER_PLUGIN_DIR . 'public/class-tbm-tagmailer-public.php';

		$this->loader = new Tbm_Tagmailer_Loader();

	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Tbm_Tagmailer_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale() {
		$plugin_i18n = new Tbm_Tagmailer_i18n();
		$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
	}

	private function set_image_size() {
		$image_sizes = new Tbm_Tagmailer_Image_Sizes();
		$this->loader->add_action( 'after_setup_theme', $image_sizes, 'add_image_sizes' );
	}

	/**
	 * Register all 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 Tbm_Tagmailer_Admin( $this->get_plugin_name(), $this->get_version() );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'check_plugin_init' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
	}

	private function set_acf() {
		$plugin_acf = new Tbm_Tagmailer_Acf();
		$this->loader->add_action( 'acf/init', $plugin_acf, 'acf_init_pages' );
		$this->loader->add_action( 'acf/init', $plugin_acf, 'acf_init_admin' );
		$this->loader->add_action( 'acf/init', $plugin_acf, 'acf_init_blocks' );
		$this->loader->add_action( 'acf/init', $plugin_acf, 'acf_init_filters' );
		$this->loader->add_filter( 'acf/settings/url', $plugin_acf, 'acf_settings_url' );
		//$this->loader->add_filter( 'acf/settings/show_admin', $plugin_acf, '__return_false' );
		$this->loader->add_filter( 'acf/settings/show_updates', $plugin_acf, '__return_false' );
	}

	private function set_ajax() {
		$plugin_ajax = new Tbm_Tagmailer_Ajax();
		$this->loader->add_action( 'wp_ajax_tbm_newsletter_subscribe', $plugin_ajax, 'tbm_newsletter_subscribe_action' );
		$this->loader->add_action( 'wp_ajax_nopriv_tbm_newsletter_subscribe', $plugin_ajax, 'tbm_newsletter_subscribe_action' );
	}

	private function set_shortcode() {
		$shortcode = new Tbm_Tagmailer_Shortcode();
		$this->loader->add_action( 'init', $shortcode, 'tagmailer_register_block' );
		$this->loader->add_action( 'init', $shortcode, 'register_shortcode' );
	}

	/**
	 * 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 Tbm_Tagmailer_Public( $this->get_plugin_name(), $this->get_version() );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'register_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'register_scripts' );
		$this->loader->add_action( 'init', $plugin_public, 'output_newsletter' );
		$this->loader->add_filter( 'the_content', $plugin_public, 'embed_form' );
	}

	/**
	 * 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.
	 *
	 * @return    string    The name of the plugin.
	 * @since     1.0.0
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

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

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

}
