<?php
/**
 * Register post type
 *
 * @package SOVLIX\WEMEET
 */

namespace SOVLIX\WEMEET;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


if ( ! class_exists( 'Wemeet_Post_Types' ) ) {
	/**
	 * The Wemeet_Post_Types class
	 *
	 * @since 1.0.0
	 */
	class Wemeet_Post_Types {
		/**
		 * Wemeet_Post_Types constructor.
		 */
		public function __construct() {
			add_action( 'init', array( $this, 'wemeet_register_post_type' ) );
			add_filter( 'single_template', array( $this, 'wemeet_wemeet_template' ) );

			// Hook into save_post to flush rewrite rules when a meeting is saved or updated.
			add_action( 'save_post', array( $this, 'wemeet_flush_rewrite_rules_on_save' ) );
		}

		/**
		 * Register meeting post type
		 *
		 * @return void
		 *
		 * @since 1.0.0
		 */
		public function wemeet_register_post_type() {
			$labels = array(
				'name'               => _x( 'Meeting', 'Post type general name', 'wemeet' ),
				'singular_name'      => _x( 'Meeting', 'Post type singular name', 'wemeet' ),
				'menu_name'          => _x( 'Meetings', 'Admin Menu text', 'wemeet' ),
				'name_admin_bar'     => _x( 'Meeting', 'Add New on Toolbar', 'wemeet' ),
				'add_new'            => __( 'Add New', 'wemeet' ),
				'add_new_item'       => __( 'Add New Meeting', 'wemeet' ),
				'new_item'           => __( 'New Meeting', 'wemeet' ),
				'edit_item'          => __( 'Edit Meeting', 'wemeet' ),
				'view_item'          => __( 'View Meeting', 'wemeet' ),
				'all_items'          => __( 'All Meetings', 'wemeet' ),
				'search_items'       => __( 'Search Meetings', 'wemeet' ),
				'parent_item_colon'  => __( 'Parent Meetings:', 'wemeet' ),
				'not_found'          => __( 'No meetings found.', 'wemeet' ),
				'not_found_in_trash' => __( 'No meetings found in trash.', 'wemeet' ),
			);

			$args = array(
				'labels'             => $labels,
				'public'             => false,
				'publicly_queryable' => true,
				'show_ui'            => true,
				'show_in_menu'       => false,
				'query_var'          => true,
				'rewrite'            => array(
					'slug'       => 'webex-meeting',
					'with_front' => false,
				),
				'capability_type'    => 'post',
				'has_archive'        => true,
				'hierarchical'       => false,
				'menu_position'      => null,
				'supports'           => array( 'title', 'excerpt' ),
			);

			register_post_type( 'wemeet_meeting', $args );
		}

		/**
		 * Redirect to Single meeting template
		 *
		 * @param mixed $single Single meeting template.
		 *
		 * @return mixed
		 *
		 * @since 1.0.0
		 */
		public function wemeet_wemeet_template( $single ) {
			global $post;
			if ( 'wemeet_meeting' === $post->post_type ) {

				if ( ! locate_template( 'wemeet-single-meeting.php' ) && file_exists( WEMEET_FRONTEND . '/wemeet-single-meeting.php' ) ) {
					return WEMEET_FRONTEND . '/wemeet-single-meeting.php';
				}
			}
			return $single;
		}

		/**
		 * Flush rewrite rules when a meeting is saved or updated
		 *
		 * @param int $post_id Post ID.
		 */
		public function wemeet_flush_rewrite_rules_on_save( $post_id ) {
			$post_type = get_post_type( $post_id );

			if ( 'wemeet_meeting' === $post_type ) {
				flush_rewrite_rules();
			}
		}
	}
}
