<?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://wordpress.org/plugins/genealogical-tree
 * @since   1.0.0
 *
 * @package    Genealogical_Tree
 * @subpackage Genealogical_Tree/includes
 */

namespace Zqe;

/**
 * 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    Genealogical_Tree
 * @subpackage Genealogical_Tree/includes
 * @author     ak devs <akdevs.fr@gmail.com>
 */
class Genealogical_Tree {

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

    /**
     * The helper class for the plugin.
     *
     * @since  1.0.0
     * @access public
     * @var    Genealogical_Tree_Helper  $helper  Provides helper functions for the plugin.
     */
    public $helper;

    /**
     * The admin class for the plugin.
     *
     * @since  1.0.0
     * @access public
     * @var    Genealogical_Tree_Admin  $admin  Handles admin-specific functionality.
     */
    public $admin;

    /**
     * The public class for the plugin.
     *
     * @since  1.0.0
     * @access public
     * @var    Genealogical_Tree_Public  $public  Handles public-facing functionality.
     */
    public $public;

    /**
     * The unique identifier of this plugin.
     *
     * @since  1.0.0
     * @access protected
     * @var    string  $name  The string used to uniquely identify this plugin.
     */
    protected $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() {
        $this->version = defined('GENEALOGICAL_TREE_VERSION') ? GENEALOGICAL_TREE_VERSION : '1.0.0';
        $this->name = 'genealogical-tree';

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

    /**
     * Load the required dependencies for this plugin.
     *
     * Create instances of the loader, helper, admin, and public classes.
     *
     * @since  1.0.0
     * @access private
     */
    private function load_dependencies() {
        $this->loader = new \Zqe\Genealogical_Tree_Loader();
        $this->helper = new \Zqe\Genealogical_Tree_Helper();
        $this->admin = new \Zqe\Genealogical_Tree_Admin($this);
        $this->public = new \Zqe\Genealogical_Tree_Public($this);
    }

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

    /**
     * Register all of the hooks related to the admin area functionality.
     *
     * @since  1.0.0
     * @access private
     */
    private function define_admin_hooks() {
        $plugin_admin = new \Zqe\Genealogical_Tree_Admin($this);

        $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('init', $plugin_admin, 'init_post_type_and_taxonomy');
        $this->loader->add_action('admin_menu', $plugin_admin, 'admin_menu');
		
		$this->loader->add_action( 'wp_ajax_search_members', $plugin_admin, 'search_members_ajax' );
		$this->loader->add_action( 'wp_ajax_nopriv_search_members', $plugin_admin, 'search_members_ajax' );

		$this->loader->add_action('wp_ajax_get_posts_by_term_or_no_term', $plugin_admin, 'get_posts_by_term_or_no_term_ajax');
		$this->loader->add_action('wp_ajax_nopriv_get_posts_by_term_or_no_term', $plugin_admin, 'get_posts_by_term_or_no_term_ajax'); 

		$this->loader->add_action('wp_ajax_delete_posts_by_ids', $plugin_admin, 'delete_posts_by_ids_ajax');
		

        // Meta Boxes
        $this->loader->add_action('add_meta_boxes', $plugin_admin, 'add_meta_boxes_gt_member', 10, 2);
        $this->loader->add_action('add_meta_boxes', $plugin_admin, 'add_meta_boxes_gt_family', 10, 2);
        $this->loader->add_action('add_meta_boxes', $plugin_admin, 'add_meta_boxes_gt_tree', 10, 2);

        // Meta Updates
        $this->loader->add_action('post_updated', $plugin_admin, 'update_meta_boxes_gt_member');
        $this->loader->add_action('post_updated', $plugin_admin, 'update_meta_boxes_gt_tree');

        // Member Columns
        $this->loader->add_filter('manage-gt-member_posts_columns', $plugin_admin, 'member_posts_columns');
        $this->loader->add_action('manage_gt-member_posts_custom_column', $plugin_admin, 'member_posts_custom_column', 10, 2);
        $this->loader->add_filter('manage_edit-gt-member_sortable_columns', $plugin_admin, 'member_sortable_columns');

        // Tree Columns
        $this->loader->add_filter('manage_gt-tree_posts_columns', $plugin_admin, 'tree_posts_columns');
        $this->loader->add_action('manage_gt-tree_posts_custom_column', $plugin_admin, 'tree_posts_custom_column', 10, 2);

        // Member Page Tab
        $this->loader->add_action('init', $plugin_admin, 'init_add_rewrite_rule_gt_member_tab');
        $this->loader->add_filter('query_vars', $plugin_admin, 'query_vars_gt_member_tab', 10, 1);

        // User Role Update
        $this->loader->add_action('user_register', $plugin_admin, 'user_register_action');
        $this->loader->add_filter('editable_roles', $plugin_admin, 'translate_custom_role_names');

        // Post Deletion
        $this->loader->add_action('before_delete_post', $plugin_admin, 'before_delete_post');
        $this->loader->add_filter('post_class', $plugin_admin, 'post_class_filter', 10, 3);

        // BuddyPress Support
        $this->loader->add_filter('user_has_cap', $plugin_admin, 'bp_manage_capabilities', 1, 4);
        $this->loader->add_action('bp_setup_nav', $plugin_admin, 'bp_family_tree_tab');
        $this->loader->add_action('bp_template_title', $plugin_admin, 'bp_family_tree_title');
        $this->loader->add_action('bp_template_content', $plugin_admin, 'bp_family_tree_content');

        // Export/Import Posts
        $this->loader->add_action('admin_post_process_export_post', $plugin_admin, 'process_export_post');
        $this->loader->add_action('admin_post_process_import_post', $plugin_admin, 'process_import_post');

        // Custom Columns for Type Tree
        $this->loader->add_filter('manage_gt-tree_posts_columns', $plugin_admin, 'tree_add_family_and_root_columns');
        $this->loader->add_action('manage_posts_custom_column', $plugin_admin, 'tree_populate_family_and_root_columns', 10, 2);
        
        // Post Type Tree Quick Edit Fields
        $this->loader->add_action('quick_edit_custom_box', $plugin_admin, 'tree_quick_edit_fields', 10, 2);
        $this->loader->add_action('save_post', $plugin_admin, 'tree_quick_edit_save');

        // Upgrade Handling
        $plugin_upgrade = new \Zqe\Genealogical_Tree_Upgrade();
        $this->loader->add_action('wp_ajax_fix_ver_upgrade_ajax', $plugin_upgrade, 'ver_upgrade');
        $this->loader->add_action('wp_ajax_nopriv_fix_ver_upgrade_ajax', $plugin_upgrade, 'ver_upgrade');

        if (!get_site_option('_gt_version_fixed_through_notice') && version_compare($this->get_version(), '2.1.9', '<=')) {
            $this->loader->add_action('admin_notices', $plugin_upgrade, 'admin_notice__info');
        }

        // Family Group Handling
        $plugin_admin_family = new \Zqe\Inc\Genealogical_Tree_Admin_Family_Group($this);
        $this->loader->add_filter('parent_file', $plugin_admin_family, 'set_family_group_current_menu', 100);
        $this->loader->add_action('wp_ajax_generate_default_tree', $plugin_admin_family, 'generate_default_tree');
        $this->loader->add_action('wp_ajax_nopriv_generate_default_tree', $plugin_admin_family, 'generate_default_tree');
        $this->loader->add_action('admin_notices', $plugin_admin_family, 'family_group_validation_notice_handler');
        $this->loader->add_action('create_gt-family-group', $plugin_admin_family, 'create_family_group');
        $this->loader->add_action('edited_gt-family-group', $plugin_admin_family, 'update_family_group', 10, 2);
        $this->loader->add_action('admin_init', $plugin_admin_family, 'add_family_group_meta');

        // Filter to modify the arguments passed to `get_terms()`
        $this->loader->add_filter('get_terms_args', $plugin_admin_family, 'gt_family_group_filter', 10, 2);
    }

    /**
     * Register all of the hooks related to the API functionality.
     *
     * @since  1.0.0
     * @access private
     */
    private function define_api_hooks() {
        if (gt_fs()->is__premium_only() && (gt_fs()->is_plan('pro') || gt_fs()->is_trial())) {
            $plugin_api = new \Zqe\Genealogical_Tree_Api($this);
            $this->loader->add_action('rest_api_init', $plugin_api, 'register_rest_route_member_ind');
            $this->loader->add_action('rest_api_init', $plugin_api, 'register_rest_route_family_fam');
            $this->loader->add_action('rest_api_init', $plugin_api, 'register_rest_route_member_popover');
            $this->loader->add_action('rest_api_init', $plugin_api, 'register_rest_route_member_ind_ancestor');
        }
    }

    /**
     * Register all of the hooks related to the public-facing functionality.
     *
     * @since  1.0.0
     * @access private
     */
    private function define_public_hooks() {
        $plugin_public = new \Zqe\Genealogical_Tree_Public($this);

        $this->loader->add_action('init', $plugin_public, 'process_registration_post');
        $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('the_content', $plugin_public, 'the_content_filter', 10, 1);
        $this->loader->add_filter('get_the_excerpt', $plugin_public, 'get_the_excerpt_filter', 10, 2);
        $this->loader->add_action('pre_get_posts', $plugin_public, 'pre_get_posts');

        $this->loader->add_action('login_form_middle', $plugin_public, 'add_lost_password_link');

        add_shortcode('tree', array($plugin_public, 'tree_shortcode'));
        add_shortcode('gt-tree', array($plugin_public, 'gt_tree_shortcode'));
        add_shortcode('gt-tree-list', array($plugin_public, 'gt_tree_list_shortcode'));

        add_shortcode('gt-member', array($plugin_public, 'gt_member_shortcode'));
        add_shortcode('gt-members', array($plugin_public, 'gt_members_shortcode'));

        add_shortcode('gt-user-registration', array($plugin_public, 'gt_user_registration_shortcode'));
        add_shortcode('gt-user-login', array($plugin_public, 'gt_user_login_shortcode'));
    }

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

    /**
     * Get the name of the plugin.
     *
     * @since  1.0.0
     * @return string  The name of the plugin.
     */
    public function get_name() {
        return $this->name;
    }

    /**
     * Get the loader instance.
     *
     * @since  1.0.0
     * @return Genealogical_Tree_Loader  The loader instance.
     */
    public function get_loader() {
        return $this->loader;
    }

    /**
     * Get the helper instance.
     *
     * @since  1.0.0
     * @return Genealogical_Tree_Helper  The helper instance.
     */
    public function get_helper() {
        return $this->helper;
    }

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