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

/**
 * Register Service CPT and Taxonomy with Dynamic Slug
 */
final class Sifency_CPT_Service {

	/**
	 * Hook initialization
	 */
	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_post_type' ] );
		}
	}

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

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

		$slug = ! empty( $options['service_slug'] )
			? sanitize_title( $options['service_slug'] )
			: 'service';

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

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

		register_post_type( 'service', $args );

		$this->register_taxonomy();
	}

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

		$labels = [
			'name'          => __( 'Service Categories', 'sifency-addons' ),
			'singular_name' => __( 'Service 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,
			'query_var'         => true,
			'show_in_rest'      => true,
			'rewrite'           => [
				'slug'       => 'service-category',
				'with_front' => false,
			],
		];

		register_taxonomy( 'service_category', [ 'service' ], $args );
	}
}
new Sifency_CPT_Service();