<?php
/**
 * Theme Custom Post Type(CPTs)
 * @package Feron
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
    exit(); //exit if access directly
}

if (!class_exists('Feron_Custom_Post_Type')) {
    class Feron_Custom_Post_Type
    {

        //$instance variable
        private static $instance;

        public function __construct()
        {
            //register post type
            add_action('init', array($this, 'register_custom_post_type'));
        }

        /**
         * get Instance
         * @since  2.0.0
         */
        public static function getInstance()
        {
            if (null == self::$instance) {
                self::$instance = new self();
            }

            return self::$instance;
        }

        /**
         * Register Custom Post Type
         * @since  2.0.0
         */
        public function register_custom_post_type()
        {

            $matches_slug = cs_get_option('matches_slug');
            $matches_slug = !empty($matches_slug) ? sanitize_title($matches_slug) : 'all-matches';

            $game_slug = cs_get_option('game_slug');
            $game_slug = !empty($game_slug) ? sanitize_title($game_slug) : 'all-game';

            $team_slug = cs_get_option('team_slug');
            $team_slug = !empty($team_slug) ? sanitize_title($team_slug) : 'all-team';


            if (!defined('ELEMENTOR_VERSION')) {
                return;
            }
            $all_post_type = array(
                [
                    'post_type' => 'matches',
                    'args' => array(
                        'label' => esc_html__('Matches', 'feron-core'),
                        'description' => esc_html__('Matches', 'feron-core'),
                        'labels' => array(
                            'name' => esc_html_x('Matches', 'Post Type General Name', 'feron-core'),
                            'singular_name' => esc_html_x('Matches', 'Post Type Singular Name', 'feron-core'),
                            'menu_name' => esc_html__('Matches', 'feron-core'),
                            'all_items' => esc_html__('Matchess', 'feron-core'),
                            'view_item' => esc_html__('View Matches', 'feron-core'),
                            'add_new_item' => esc_html__('Add New Matches', 'feron-core'),
                            'add_new' => esc_html__('Add New Matches', 'feron-core'),
                            'edit_item' => esc_html__('Edit Matches', 'feron-core'),
                            'update_item' => esc_html__('Update Matches', 'feron-core'),
                            'search_items' => esc_html__('Search Matches', 'feron-core'),
                            'not_found' => esc_html__('Not Found', 'feron-core'),
                            'not_found_in_trash' => esc_html__('Not found in Trash', 'feron-core'),
                            'featured_image' => esc_html__('Matches Image', 'feron-core'),
                            'remove_featured_image' => esc_html__('Remove Matches Image', 'feron-core'),
                            'set_featured_image' => esc_html__('Set Matches Image', 'feron-core'),
                        ),
                        'supports' => array('title', 'thumbnail', 'excerpt', 'editor', 'comments'),
                        'hierarchical' => false,
                        'public' => true,
                        "publicly_queryable" => true,
                        'show_ui' => true,
                        'show_in_menu' => 'feron_theme_options',
                        'rewrite'      => array('slug' => $matches_slug, 'with_front' => true),
                        'can_export' => true,
                        'capability_type' => 'post',
                        "show_in_rest" => true,
                        'query_var' => true
                    )
                ],
                [
                    'post_type' => 'game',
                    'args' => array(
                        'label' => esc_html__('Game', 'feron-core'),
                        'description' => esc_html__('Game', 'feron-core'),
                        'labels' => array(
                            'name' => esc_html_x('Game', 'Post Type General Name', 'feron-core'),
                            'singular_name' => esc_html_x('Game', 'Post Type Singular Name', 'feron-core'),
                            'menu_name' => esc_html__('Game', 'feron-core'),
                            'all_items' => esc_html__('Games', 'feron-core'), //Project
                            'view_item' => esc_html__('View Game', 'feron-core'),
                            'add_new_item' => esc_html__('Add New Game', 'feron-core'),
                            'add_new' => esc_html__('Add New Game', 'feron-core'),
                            'edit_item' => esc_html__('Edit Game', 'feron-core'),
                            'update_item' => esc_html__('Update Game', 'feron-core'),
                            'search_items' => esc_html__('Search Game', 'feron-core'),
                            'not_found' => esc_html__('Not Found', 'feron-core'),
                            'not_found_in_trash' => esc_html__('Not found in Trash', 'feron-core'),
                            'featured_image' => esc_html__('Game Image', 'feron-core'),
                            'remove_featured_image' => esc_html__('Remove Game Image', 'feron-core'),
                            'set_featured_image' => esc_html__('Set Game Image', 'feron-core'),
                        ),
                        'supports' => array('title', 'thumbnail', 'excerpt', 'editor', 'comments'),
                        'hierarchical' => false,
                        'public' => true,
                        "publicly_queryable" => true,
                        'show_ui' => true,
                        'show_in_menu' => 'feron_theme_options',
                        'rewrite'      => array('slug' => $game_slug, 'with_front' => true),
                        'can_export' => true,
                        'capability_type' => 'post',
                        "show_in_rest" => true,
                        'query_var' => true
                    )
                ],
                [
                    'post_type' => 'team',
                    'args' => array(
                        'label' => esc_html__('team', 'feron-core'),
                        'description' => esc_html__('team', 'feron-core'),
                        'labels' => array(
                            'name' => esc_html_x('Team', 'Post Type General Name', 'feron-core'),
                            'singular_name' => esc_html_x('Team', 'Post Type Singular Name', 'feron-core'),
                            'menu_name' => esc_html__('Teams', 'feron-core'),
                            'all_items' => esc_html__('Teams', 'feron-core'),
                            'view_item' => esc_html__('View Teams', 'feron-core'),
                            'add_new_item' => esc_html__('Add New Team Member', 'feron-core'),
                            'add_new' => esc_html__('Add New Team Member', 'feron-core'),
                            'edit_item' => esc_html__('Edit Team', 'feron-core'),
                            'update_item' => esc_html__('Update Team', 'feron-core'),
                            'search_items' => esc_html__('Search Team', 'feron-core'),
                            'not_found' => esc_html__('Not Found', 'feron-core'),
                            'not_found_in_trash' => esc_html__('Not found in Trash', 'feron-core'),
                            'featured_image' => esc_html__('Team Image', 'feron-core'),
                            'remove_featured_image' => esc_html__('Remove Team Image', 'feron-core'),
                            'set_featured_image' => esc_html__('Set Team Image', 'feron-core'),
                        ),
                        'supports' => array('title', 'thumbnail', 'excerpt', 'editor', 'comments'),
                        'hierarchical' => false,
                        'public' => true,
                        "publicly_queryable" => true,
                        'show_ui' => true,
                        'show_in_menu' => 'feron_theme_options',
                        'rewrite'      => array('slug' => $team_slug, 'with_front' => true),
                        'can_export' => true,
                        'capability_type' => 'post',
                        "show_in_rest" => true,
                        'query_var' => true
                    )
                ]
            );

            if (!empty($all_post_type) && is_array($all_post_type)) {

                foreach ($all_post_type as $post_type) {
                    call_user_func_array('register_post_type', $post_type);
                }
            }


            /**
             * Custom Taxonomy Register
             * @since 1.0.0
             */

            $all_custom_taxonmy = array(
                array(
                    'taxonomy' => 'matches-cat',
                    'object_type' => 'matches',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Matches Category", 'feron-core'),
                            "singular_name" => esc_html__("Matches Category", 'feron-core'),
                            "menu_name" => esc_html__("Matches Category", 'feron-core'),
                            "all_items" => esc_html__("All Matches Category", 'feron-core'),
                            "add_new_item" => esc_html__("Add New Matches Category", 'feron-core')
                        ),
                        "public" => true,
                        "hierarchical" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'matches-cat', 'with_front' => true),
                        "show_admin_column" => true,
                        "show_in_rest" => true,
                        "show_in_quick_edit" => true,
                    )
                ),
                array(
                    'taxonomy' => 'game-cat',
                    'object_type' => 'game',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Game Category", 'feron-core'),
                            "singular_name" => esc_html__("Game Category", 'feron-core'),
                            "menu_name" => esc_html__("Game Category", 'feron-core'),
                            "all_items" => esc_html__("All Game Category", 'feron-core'),
                            "add_new_item" => esc_html__("Add New Game Category", 'feron-core')
                        ),
                        "public" => true,
                        "hierarchical" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'game-cat', 'with_front' => true),
                        "show_admin_column" => true,
                        "show_in_rest" => true,
                        "show_in_quick_edit" => true,
                    )
                ),
                array(
                    'taxonomy' => 'team-cat',
                    'object_type' => 'team',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Team Category", 'feron-core'),
                            "singular_name" => esc_html__("Team Category", 'feron-core'),
                            "menu_name" => esc_html__("Team Category", 'feron-core'),
                            "all_items" => esc_html__("All Team Category", 'feron-core'),
                            "add_new_item" => esc_html__("Add New Team Category", 'feron-core')
                        ),
                        "public" => true,
                        "hierarchical" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'team-cat', 'with_front' => true),
                        "show_admin_column" => true,
                        "show_in_rest" => true,
                        "show_in_quick_edit" => true,
                    )
                )
            );

            if (is_array($all_custom_taxonmy) && !empty($all_custom_taxonmy)) {
                foreach ($all_custom_taxonmy as $taxonomy) {
                    call_user_func_array('register_taxonomy', $taxonomy);
                }
            }


            /**
             * Custom Tags Register
             * @since 1.0.0
             */

            $all_custom_tags = array(
                array(
                    'taxonomy' => 'matches-tag',
                    'object_type' => 'matches',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Matches Tag", 'feron-core'),
                            "singular_name" => esc_html__("Matches Tag", 'feron-core'),
                            "menu_name" => esc_html__("Matches Tag", 'feron-core'),
                            "all_items" => esc_html__("All Matches Tag", 'feron-core'),
                            "add_new_item" => esc_html__("Add New Matches Tag", 'feron-core')
                        ),
                        "public" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'matches-tag'),
                        "show_admin_column" => true,
                        "show_in_rest" => true,
                        "show_in_quick_edit" => true,
                        'hierarchical' => false,
                        'update_count_callback' => '_update_post_term_count',
                    )
                ),
                array(
                    'taxonomy' => 'game-tag',
                    'object_type' => 'game',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Game Tag", 'feron-core'),
                            "singular_name" => esc_html__("Game Tag", 'feron-core'),
                            "menu_name" => esc_html__("Game Tag", 'feron-core'),
                            "all_items" => esc_html__("All Game Tag", 'feron-core'),
                            "add_new_item" => esc_html__("Add New Game Tag", 'feron-core')
                        ),
                        "public" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'game-tag'),
                        "show_admin_column" => true,
                        "show_in_rest" => true,
                        "show_in_quick_edit" => true,
                        'hierarchical' => false,
                        'update_count_callback' => '_update_post_term_count',
                    )
                ),
                array(
                    'taxonomy' => 'team-tag',
                    'object_type' => 'team',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Team Tag", 'feron-core'),
                            "singular_name" => esc_html__("Team Tag", 'feron-core'),
                            "menu_name" => esc_html__("Team Tag", 'feron-core'),
                            "all_items" => esc_html__("All Team Tag", 'feron-core'),
                            "add_new_item" => esc_html__("Add New Team Tag", 'feron-core')
                        ),
                        "public" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'team-tag'),
                        "show_admin_column" => true,
                        "show_in_rest" => true,
                        "show_in_quick_edit" => true,
                        'hierarchical' => false,
                        'update_count_callback' => '_update_post_term_count',
                    )
                ),
            );

            if (is_array($all_custom_tags) && !empty($all_custom_tags)) {
                foreach ($all_custom_tags as $tags) {
                    call_user_func_array('register_taxonomy', $tags);
                }
            }


            flush_rewrite_rules();
        }

    }//end class

    if (class_exists('Feron_Custom_Post_Type')) {
        Feron_Custom_Post_Type::getInstance();
    }
}