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

/**
 * Register Client CPT and Taxonomy with Dynamic Slug
 */
final class Sifency_CPT_Client {

	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_client_cpt' ] );
		}
	}

	/**
	 * Register Client Custom Post Type
	 */
	public function register_client_cpt() {

		$options = get_option( 'sifency-options', [] );
		$slug    = ! empty( $options['client_slug'] ) ? sanitize_title( $options['client_slug'] ) : 'client';

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

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

		register_post_type( 'client', $args );

		$this->register_client_taxonomy();
	}

	/**
	 * Register Client Taxonomy
	 */
	private function register_client_taxonomy() {

		$labels = [
			'name'          => esc_html__( 'Client Categories', 'sifency-addons' ),
			'singular_name' => esc_html__( 'Client 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' => 'client-category', 'with_front' => false ],
			'show_in_rest'      => true,
		];

		register_taxonomy( 'client_category', [ 'client' ], $args );
	}
}

new Sifency_CPT_Client();