<?php

/**
 * The file of the DomainForSale class.
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package DomainForSale
 */

namespace ThemeAtelier\DomainForSale\Includes;

use ThemeAtelier\DomainForSale\Admin\GutenbergBlock\Gutenberg_Block_Init;
use ThemeAtelier\DomainForSale\Includes\Import_Export;
use ThemeAtelier\DomainForSale\Includes\Loader;
use ThemeAtelier\DomainForSale\Frontend\Helpers\Helpers;
use ThemeAtelier\DomainForSale\Admin\Admin;
use ThemeAtelier\DomainForSale\Frontend\Frontend;
use ThemeAtelier\DomainForSale\Frontend\Helpers\ThemeSupport;
use ThemeAtelier\DomainForSale\Admin\ElementorAddons\Element_Shortcode_Addons;

// don't call the file directly.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * The main class of the plugin.
 *
 * Handle all the class and methods of the plugin.
 *
 * @author     ThemeAtelier <themeatelierbd@gmail.com>
 */
class DomainForSale
{
    /**
     * Plugin version
     *
     * @since    1.0.0
     * @access   protected
     * @var string
     */
    protected $version;

    /**
     * Plugin slug
     *
     * @since    1.0.0
     * @access   protected
     * @var string
     */
    protected $plugin_slug;

    /**
     * Main Loader.
     *
     * The loader that's responsible for maintaining and registering all hooks that empowers
     * the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var object
     */
    protected $loader;
    /**
     * Constructor for the DomainForSale class.
     *
     * Sets up all the appropriate hooks and actions within the plugin.
     */
    public function __construct()
    {
        $this->load_dependencies();
        $this->define_admin_hooks();
        $this->define_public_hooks();
        add_action('plugin_loaded', array($this, 'domain_for_sale_load_textdomain'));
        add_action('activated_plugin', [$this, 'domain_for_sale_redirect_to']);

        /**
         * Elementor Shortcode Widgets
         */
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
        if ((is_plugin_active('elementor/elementor.php') || is_plugin_active_for_network('elementor/elementor.php'))) {
            new Element_Shortcode_Addons();
        }

        new Gutenberg_Block_Init();
    }

    /**
     * Redirect after active
     *
     * @param string $plugin The plugin help page.
     */
    public function domain_for_sale_redirect_to($plugin)
    {
        if (DOMAIN_FOR_SALE_BASENAME === $plugin && ! (defined('DOING_AJAX') && DOING_AJAX) && (! (defined('WP_CLI') && WP_CLI))) {
            wp_safe_redirect(admin_url('admin.php?page=domain-for-sale'));
            exit();
        }
    }

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

    /**
     * The slug 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_slug()
    {
        return $this->plugin_slug;
    }

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

    /**
     * Load the required dependencies for this plugin.
     *
     * Include the following files that make up the plugin:
     *
     * - Loader. Orchestrates the hooks of the plugin.
     * - Teamproi18n. Defines internationalization functionality.
     * - Admin. Defines all hooks for the admin area.
     * - Frontend. 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.
         */
        $this->loader = new Loader();
    }

    /**
     * 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 Frontend();
        $plugin_helpers = new Helpers();
        $theme_support = new ThemeSupport();
        $this->loader->add_action('wp_loaded', $plugin_helpers, 'register_all_scripts');
        $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_shortcode('dfs_contact_form', $plugin_public, 'dfs_shortcode');

        $this->loader->add_action('wp_loaded', $theme_support, 'theme_support');
        add_filter('template_include', [$this, 'dfs_load_single_domain_template'], 99);
    }

    public function dfs_load_single_domain_template($template)
    {
        if (is_singular('dfs_template')) {
            $plugin_template = Helpers::dfs_locate_template('single-dfs_template.php');
            if (file_exists($plugin_template)) {
                return $plugin_template;
            }
        }
        return $template;
    }

    // load text domain from plugin folder
    public function domain_for_sale_load_textdomain()
    {
        load_plugin_textdomain('domain-for-sale', false, dirname(plugin_basename(__FILE__)) . "/languages");
    }

    /**
     * Register all of the hooks related to the admin dashboard functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_admin_hooks()
    {
        $plugin_admin = new Admin($this->get_plugin_slug(), $this->get_version());
        $plugin_helpers = new Helpers();
        $this->loader->add_action('wp_loaded', $plugin_helpers, 'register_all_scripts');
        $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_filter('manage_dfs_template_posts_columns', $plugin_admin, 'filter_domain_for_sale_admin_column');
        $this->loader->add_action('manage_dfs_template_posts_custom_column', $plugin_admin, 'display_domain_for_sale_admin_fields', 10, 2);

        $this->loader->add_filter('manage_domain_listing_posts_columns', $plugin_admin, 'filter_domain_listing_admin_column');
        $this->loader->add_action('manage_domain_listing_posts_custom_column', $plugin_admin, 'display_domain_listing_admin_fields', 10, 2);
    }
}
