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

namespace SOVLIX\GOODMEET;

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


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

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

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

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

			register_post_type( 'goodmeet_meeting', $args );
		}

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

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

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

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