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

/**
 * Register Testimonial CPT and Taxonomy with Dynamic Slug
 */
final class Sifency_CPT_Testimonial {

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

	/**
	 * Register Testimonial Custom Post Type
	 */
	public function register_testimonial_cpt() {

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

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

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

		register_post_type( 'testimonial', $args );

		$this->register_testimonial_taxonomy();
	}

	/**
	 * Register Testimonial Taxonomy
	 */
	private function register_testimonial_taxonomy() {

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

		register_taxonomy( 'testimonial_category', [ 'testimonial' ], $args );
	}
}

new Sifency_CPT_Testimonial();