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

/**
 * Register Award CPT and Taxonomy with Dynamic Slug
 */
final class Sifency_CPT_Award {

	public function __construct() {
		if ( class_exists( 'Sifency_Addons_Theme_Builder_Master' ) 
			&& Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() 
		) {
			add_action( 'init', [ $this, 'register_award_cpt' ] );
		}
	}

	/**
	 * Register Award Custom Post Type
	 */
	public function register_award_cpt() {
		$options = get_option( 'sifency-options', [] );
		$slug    = ! empty( $options['award_slug'] ) ? sanitize_title( $options['award_slug'] ) : 'award';

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

		$args = [
			'label'           => esc_html__( 'Awards', 'sifency-addons' ),
			'labels'          => $labels,
			'public'          => true,
			'has_archive'     => true,
			'rewrite'         => [ 'slug' => $slug, 'with_front' => false ],
			'menu_icon'       => 'dashicons-awards',
			'show_in_rest'    => true,
			'supports'        => [ 'title', 'thumbnail', 'excerpt', 'revisions' ],
			'show_in_menu'    => true,
			'capability_type' => 'post',
		];

		register_post_type( 'award', $args );

		$this->register_award_taxonomy();
	}

	/**
	 * Register Award Taxonomy
	 */
	private function register_award_taxonomy() {
		$labels = [
			'name'          => esc_html__( 'Award Categories', 'sifency-addons' ),
			'singular_name' => esc_html__( 'Award Category', 'sifency-addons' ),
			'search_items'  => esc_html__( 'Search Categories', 'sifency-addons' ),
			'all_items'     => esc_html__( 'All Categories', 'sifency-addons' ),
			'edit_item'     => esc_html__( 'Edit Category', 'sifency-addons' ),
			'update_item'   => esc_html__( 'Update Category', 'sifency-addons' ),
			'add_new_item'  => esc_html__( 'Add New Category', 'sifency-addons' ),
			'new_item_name' => esc_html__( 'New Category Name', 'sifency-addons' ),
			'menu_name'     => esc_html__( 'Categories', 'sifency-addons' ),
		];

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

		register_taxonomy( 'award_category', [ 'award' ], $args );
	}
}

new Sifency_CPT_Award();