<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Register Doc CPT and Taxonomy (Dynamic Slug)
 */
if (
	class_exists( 'Sifency_Addons_Theme_Builder_Master' ) &&
	Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal()
) {

	final class Sifency_CPT_Doc {

		/**
		 * Constructor
		 */
		public function __construct() {
			add_action( 'init', [ $this, 'register_post_type' ] );
		}

		/**
		 * Register Doc Post Type
		 */
		public function register_post_type() {

			$options = get_option( 'sifency-options', [] );

			$slug = ! empty( $options['doc_slug'] )
				? sanitize_title( $options['doc_slug'] )
				: 'doc';

			$labels = [
				'name'               => __( 'Docs', 'sifency-addons' ),
				'singular_name'      => __( 'Doc', 'sifency-addons' ),
				'add_new'            => __( 'Add New', 'sifency-addons' ),
				'add_new_item'       => __( 'Add New Doc', 'sifency-addons' ),
				'edit_item'          => __( 'Edit Doc', 'sifency-addons' ),
				'new_item'           => __( 'New Doc', 'sifency-addons' ),
				'view_item'          => __( 'View Doc', 'sifency-addons' ),
				'search_items'       => __( 'Search Docs', 'sifency-addons' ),
				'not_found'          => __( 'No docs found', 'sifency-addons' ),
				'not_found_in_trash' => __( 'No docs found in trash', 'sifency-addons' ),
				'menu_name'          => __( 'Docs', 'sifency-addons' ),
			];

			$args = [
				'labels'        => $labels,
				'public'        => true,
				'has_archive'   => true,
				'menu_icon'     => 'dashicons-book-alt',
				'show_in_rest'  => true,
				'supports'      => [ 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' ],
				'rewrite'       => [
					'slug'       => $slug,
					'with_front' => false,
				],
			];

			register_post_type( 'doc', $args );

			$this->register_taxonomy();
		}

		/**
		 * Register Doc Category Taxonomy
		 */
		private function register_taxonomy() {

			$labels = [
				'name'          => __( 'Doc Categories', 'sifency-addons' ),
				'singular_name' => __( 'Doc Category', 'sifency-addons' ),
				'search_items'  => __( 'Search Categories', 'sifency-addons' ),
				'all_items'     => __( 'All Categories', 'sifency-addons' ),
				'edit_item'     => __( 'Edit Category', 'sifency-addons' ),
				'update_item'   => __( 'Update Category', 'sifency-addons' ),
				'add_new_item'  => __( 'Add New Category', 'sifency-addons' ),
				'new_item_name' => __( 'New Category Name', 'sifency-addons' ),
				'menu_name'     => __( 'Categories', 'sifency-addons' ),
			];

			$args = [
				'hierarchical'      => true,
				'labels'            => $labels,
				'public'            => true,
				'show_ui'           => true,
				'show_admin_column' => true,
				'show_in_rest'      => true,
				'query_var'         => true,
				'rewrite'           => [
					'slug'       => 'doc-category',
					'with_front' => false,
				],
			];

			register_taxonomy( 'doc_category', [ 'doc' ], $args );
		}
	}
	new Sifency_CPT_Doc();
}
