<?php

if ( ! defined( 'ABSPATH' ) ) {
	 exit;
}

/**
 * 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://registrationformbuilder.com
 * @since      1.0.0
 *
 * @package    Vedrixa_Forms
 * @subpackage Vedrixa_Forms/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    Vedrixa_Forms
 * @subpackage Vedrixa_Forms/includes
 * @author     Vedrixa <support@registrationformbuilder.com>
 */
class Registration_Form_Builder {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Registration_Form_Builder_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    $registration_form_builder    The string used to uniquely identify this plugin.
	 */
	protected $registration_form_builder;

	/**
	 * 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 Vedrixa Forms plugin slug and version for use 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() {
		$this->registration_form_builder = 'vedrixa-forms-registration-builder';
		$this->version                   = '1.0.0';
		$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:
	 *
	 * - Registration_Form_Builder_Loader. Orchestrates the hooks of the plugin.
	 * - Registration_Form_Builder_I18n. Defines internationalization functionality.
	 * - Registration_Form_Builder_Admin. Defines all hooks for the admin area.
	 * - Registration_Form_Builder_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() {

                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-registration-form-builder-activator.php';
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wpefb-sanitizer.php';
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-registration-form-builder-deactivator.php';
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wpefb-dbhandler.php';
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wpefb-request.php';
		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-registration-form-builder-loader.php';

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

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-registration-form-builder-admin.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-registration-form-builder-public.php';

                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wpefb-form-renderer.php';
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wpefb-helper.php';
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wpefb-features.php';

		$this->loader = new Registration_Form_Builder_Loader();

	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Registration_Form_Builder_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 Registration_Form_Builder_I18n();

		$this->loader->add_action( 'plugins_loaded', $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 define_admin_hooks() {

		$plugin_admin = new Registration_Form_Builder_Admin( $this->get_registration_form_builder(), $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' );
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'registration_form_builder_admin_menu' );
		$this->loader->add_action( 'wpefb_manage_forms_before_page_title', $plugin_admin, 'render_manage_forms_review_notice' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'maybe_handle_manage_forms_submission' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'maybe_handle_manage_forms_review_notice_action' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'maybe_handle_form_dashboard_submission' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'maybe_handle_form_dashboard_navigation' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'maybe_handle_submission_user_action' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'maybe_handle_global_settings_submission' );
		$this->loader->add_action( 'wp_ajax_wefb_save_form_structure', $plugin_admin, 'wefb_save_form_structure' );

	}

	/**
	 * 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 Registration_Form_Builder_Public( $this->get_registration_form_builder(), $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_action( 'phpmailer_init', $plugin_public, 'configure_phpmailer' );
		$this->loader->add_filter( 'show_admin_bar', $plugin_public, 'control_admin_toolbar_visibility' );
		$this->loader->add_action( 'init', $plugin_public, 'register_shortcodes' );
		$this->loader->add_action( 'init', $plugin_public, 'maybe_process_account_activation' );
		$this->loader->add_action( 'wp_body_open', $plugin_public, 'render_activation_notice' );
		$this->loader->add_action( 'wp_footer', $plugin_public, 'render_activation_notice' );
		$this->loader->add_filter( 'register_url', $plugin_public, 'filter_register_url' );
		$this->loader->add_filter( 'authenticate', $plugin_public, 'block_inactive_users_login', 30, 3 );
		$this->loader->add_filter( 'query_vars', $plugin_public, 'register_preview_query_vars' );
		$this->loader->add_action( 'template_redirect', $plugin_public, 'maybe_render_preview' );

	}
	/**
	 * 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_registration_form_builder() {
		return $this->registration_form_builder;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Registration_Form_Builder_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;
	}

}
