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

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

	final class Sifency_CPT_Event {

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

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

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

			$slug = ! empty( $options['event_slug'] )
				? sanitize_title( $options['event_slug'] )
				: 'event';

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

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

			register_post_type( 'event', $args );

			$this->register_taxonomy();
		}

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

			$labels = [
				'name'          => __( 'Event Categories', 'sifency-addons' ),
				'singular_name' => __( 'Event 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'       => 'event-category',
					'with_front' => false,
				],
			];

			register_taxonomy( 'event_category', [ 'event' ], $args );
		}
	}
	new Sifency_CPT_Event();
}