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

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

if (!class_exists('Geroz_Custom_Post_Type')) {
    class Geroz_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()
        {

            $service_slug = cs_get_option('service_slug');
            $service_slug = !empty($service_slug) ? sanitize_title($service_slug) : 'all-service';

            $project_slug = cs_get_option('project_slug');
            $project_slug = !empty($project_slug) ? sanitize_title($project_slug) : 'all-project';

            $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' => 'service',
                    'args' => array(
                        'label' => esc_html__('Service', 'geroz-core'),
                        'description' => esc_html__('Service', 'geroz-core'),
                        'labels' => array(
                            'name' => esc_html_x('Service', 'Post Type General Name', 'geroz-core'),
                            'singular_name' => esc_html_x('Service', 'Post Type Singular Name', 'geroz-core'),
                            'menu_name' => esc_html__('Service', 'geroz-core'),
                            'all_items' => esc_html__('Services', 'geroz-core'),
                            'view_item' => esc_html__('View Service', 'geroz-core'),
                            'add_new_item' => esc_html__('Add New Service', 'geroz-core'),
                            'add_new' => esc_html__('Add New Service', 'geroz-core'),
                            'edit_item' => esc_html__('Edit Service', 'geroz-core'),
                            'update_item' => esc_html__('Update Service', 'geroz-core'),
                            'search_items' => esc_html__('Search Service', 'geroz-core'),
                            'not_found' => esc_html__('Not Found', 'geroz-core'),
                            'not_found_in_trash' => esc_html__('Not found in Trash', 'geroz-core'),
                            'featured_image' => esc_html__('Service Image', 'geroz-core'),
                            'remove_featured_image' => esc_html__('Remove Service Image', 'geroz-core'),
                            'set_featured_image' => esc_html__('Set Service Image', 'geroz-core'),
                        ),
                        'supports' => array('title', 'thumbnail', 'excerpt', 'editor', 'comments'),
                        'hierarchical' => false,
                        'public' => true,
                        "publicly_queryable" => true,
                        'show_ui' => true,
                        'show_in_menu' => 'geroz_theme_options',
                        'rewrite'      => array('slug' => $service_slug, 'with_front' => true),
                        'can_export' => true,
                        'capability_type' => 'post',
                        "show_in_rest" => true,
                        'query_var' => true
                    )
                ],
                [
                    'post_type' => 'project',
                    'args' => array(
                        'label' => esc_html__('Project', 'geroz-core'),
                        'description' => esc_html__('Project', 'geroz-core'),
                        'labels' => array(
                            'name' => esc_html_x('Project', 'Post Type General Name', 'geroz-core'),
                            'singular_name' => esc_html_x('Project', 'Post Type Singular Name', 'geroz-core'),
                            'menu_name' => esc_html__('Project', 'geroz-core'),
                            'all_items' => esc_html__('Projects', 'geroz-core'),
                            'view_item' => esc_html__('View Project', 'geroz-core'),
                            'add_new_item' => esc_html__('Add New Project', 'geroz-core'),
                            'add_new' => esc_html__('Add New Project', 'geroz-core'),
                            'edit_item' => esc_html__('Edit Project', 'geroz-core'),
                            'update_item' => esc_html__('Update Project', 'geroz-core'),
                            'search_items' => esc_html__('Search Project', 'geroz-core'),
                            'not_found' => esc_html__('Not Found', 'geroz-core'),
                            'not_found_in_trash' => esc_html__('Not found in Trash', 'geroz-core'),
                            'featured_image' => esc_html__('Project Image', 'geroz-core'),
                            'remove_featured_image' => esc_html__('Remove Project Image', 'geroz-core'),
                            'set_featured_image' => esc_html__('Set Project Image', 'geroz-core'),
                        ),
                        'supports' => array('title', 'thumbnail', 'excerpt', 'editor', 'comments'),
                        'hierarchical' => false,
                        'public' => true,
                        "publicly_queryable" => true,
                        'show_ui' => true,
                        'show_in_menu' => 'geroz_theme_options',
                        'rewrite'      => array('slug' => $project_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', 'geroz-core'),
                        'description' => esc_html__('team', 'geroz-core'),
                        'labels' => array(
                            'name' => esc_html_x('Team', 'Post Type General Name', 'geroz-core'),
                            'singular_name' => esc_html_x('Team', 'Post Type Singular Name', 'geroz-core'),
                            'menu_name' => esc_html__('Teams', 'geroz-core'),
                            'all_items' => esc_html__('Teams', 'geroz-core'),
                            'view_item' => esc_html__('View Teams', 'geroz-core'),
                            'add_new_item' => esc_html__('Add New Team Member', 'geroz-core'),
                            'add_new' => esc_html__('Add New Team Member', 'geroz-core'),
                            'edit_item' => esc_html__('Edit Team', 'geroz-core'),
                            'update_item' => esc_html__('Update Team', 'geroz-core'),
                            'search_items' => esc_html__('Search Team', 'geroz-core'),
                            'not_found' => esc_html__('Not Found', 'geroz-core'),
                            'not_found_in_trash' => esc_html__('Not found in Trash', 'geroz-core'),
                            'featured_image' => esc_html__('Team Image', 'geroz-core'),
                            'remove_featured_image' => esc_html__('Remove Team Image', 'geroz-core'),
                            'set_featured_image' => esc_html__('Set Team Image', 'geroz-core'),
                        ),
                        'supports' => array('title', 'thumbnail', 'excerpt', 'editor', 'comments'),
                        'hierarchical' => false,
                        'public' => true,
                        "publicly_queryable" => true,
                        'show_ui' => true,
                        'show_in_menu' => 'geroz_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' => 'service-cat',
                    'object_type' => 'service',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Service Category", 'geroz-core'),
                            "singular_name" => esc_html__("Service Category", 'geroz-core'),
                            "menu_name" => esc_html__("Service Category", 'geroz-core'),
                            "all_items" => esc_html__("All Service Category", 'geroz-core'),
                            "add_new_item" => esc_html__("Add New Service Category", 'geroz-core')
                        ),
                        "public" => true,
                        "hierarchical" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'service-cat', 'with_front' => true),
                        "show_admin_column" => true,
                        "show_in_rest" => true,
                        "show_in_quick_edit" => true,
                    )
                ),
                array(
                    'taxonomy' => 'project-cat',
                    'object_type' => 'project',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Project Category", 'geroz-core'),
                            "singular_name" => esc_html__("Project Category", 'geroz-core'),
                            "menu_name" => esc_html__("Project Category", 'geroz-core'),
                            "all_items" => esc_html__("All Project Category", 'geroz-core'),
                            "add_new_item" => esc_html__("Add New Project Category", 'geroz-core')
                        ),
                        "public" => true,
                        "hierarchical" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'project-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", 'geroz-core'),
                            "singular_name" => esc_html__("Team Category", 'geroz-core'),
                            "menu_name" => esc_html__("Team Category", 'geroz-core'),
                            "all_items" => esc_html__("All Team Category", 'geroz-core'),
                            "add_new_item" => esc_html__("Add New Team Category", 'geroz-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' => 'service-tag',
                    'object_type' => 'service',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Service Tag", 'geroz-core'),
                            "singular_name" => esc_html__("Service Tag", 'geroz-core'),
                            "menu_name" => esc_html__("Service Tag", 'geroz-core'),
                            "all_items" => esc_html__("All Service Tag", 'geroz-core'),
                            "add_new_item" => esc_html__("Add New Service Tag", 'geroz-core')
                        ),
                        "public" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'service-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' => 'project-tag',
                    'object_type' => 'project',
                    'args' => array(
                        "labels" => array(
                            "name" => esc_html__("Project Tag", 'geroz-core'),
                            "singular_name" => esc_html__("Project Tag", 'geroz-core'),
                            "menu_name" => esc_html__("Project Tag", 'geroz-core'),
                            "all_items" => esc_html__("All Project Tag", 'geroz-core'),
                            "add_new_item" => esc_html__("Add New Project Tag", 'geroz-core')
                        ),
                        "public" => true,
                        "show_ui" => true,
                        "show_in_menu" => true,
                        "show_in_nav_menus" => true,
                        "query_var" => true,
                        "rewrite" => array('slug' => 'project-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", 'geroz-core'),
                            "singular_name" => esc_html__("Team Tag", 'geroz-core'),
                            "menu_name" => esc_html__("Team Tag", 'geroz-core'),
                            "all_items" => esc_html__("All Team Tag", 'geroz-core'),
                            "add_new_item" => esc_html__("Add New Team Tag", 'geroz-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('Geroz_Custom_Post_Type')) {
        Geroz_Custom_Post_Type::getInstance();
    }
}