<?php
/*
 * Plugin Name:	Meditest Doctor Appointment
 * Description: Allow your customers to book your service like appointment, event, reservation, etc. Manage your bookings through wp admin dashboard. No overlap, automated email, no headache.
 * Author: 		itindeed
 * Version:		1.0.1
 * Plugin URI: https://itindeed.com/mt-service-booking
 * Author URI: 	itindeed.com
 * License:     GNU GENERAL PUBLIC LICENSE Version 3,
 * License URI: https://www.gnu.org/licenses/gpl-3.0.txt
 * Text Domain: mt-service-booking
*/

namespace mt_service_booking_plugin;
/**
 * Use namespace to avoid conflict
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}




//************** Starts Custom post type service booking with meta boxes ***************//
/**
 * Class mt_service_booking_plugin_base_class
 * @package mt_service_booking_plugin
 *
 * Use actual name of post type for
 * easy readability.
 *
 * Potential conflicts removed by namespace
 */
if ( !class_exists( 'mt_service_booking_plugin_base_class' ) ) {
    class mt_service_booking_plugin_base_class {

        /**
         * When class is instantiated
         */
        public function __construct() {
            
            add_action('init', array($this, 'mt_service_post_type'));// Register the post type
            add_action('init', array($this, 'doctorsprofile_post_type'));// Register the post type
            add_action('init', array($this, 'mt_add_texonomy_to_booking_service'));// Register texonomy
            add_action('add_meta_boxes', array($this,'mt_add_meta_boxes_to_booking_service')); //add meta boxes
            add_action('save_post', array($this,'mt_save_all_posts_types_meta_fields_meta')); //add meta boxes
            add_filter( 'archive_template',  array($this,'mt_get_archive_template') ) ; 
            add_filter( 'single_template',  array($this,'mt_get_single_template') ) ; 
            add_filter( 'use_block_editor_for_post_type', array($this,'mt_disable_gutenberg_pages'), 10, 2 );
            add_action('init', array($this, 'mt_enqueue_public_scripts'));
            add_action( 'wp_enqueue_scripts', array($this,'mt_enqueue_public_scripts')  );
            
            
        }

        /**
         * Register post type
         */
        public function doctorsprofile_post_type() {

            $type               = 'doctorsprofile';
            $slug               = 'doctorsprofile';
            $name               = 'Docter Profile';
            $singular_name      = 'Docter Profile';
            
            $labels = array(
                'name'                  => $name,
                'singular_name'         => $singular_name,
                'add_new'               => __('Add New', 'mt-service-booking'),
                'add_new_item'          => 'Add New '   . $singular_name,
                'edit_item'             => 'Edit '      . $singular_name,
                'new_item'              => 'New '       . $singular_name,
                'all_items'             => 'All '       . $name,
                'view_item'             => 'View '      . $name,
                'search_items'          => 'Search '    . $name,
                'not_found'             => 'No '        . strtolower($name) . ' found',
                'not_found_in_trash'    => 'No '        . strtolower($name) . ' found in Trash',
                'parent_item_colon'     => '',
                'menu_name'             => $name
            );
            $args = array(
                'labels'                => $labels,
                'public'                => true,
                'publicly_queryable'    => true,
                'show_ui'               => true,
                'show_in_menu'          => false,
                'show_in_admin_bar'     => true,
                'query_var'             => true,
                'rewrite'               => array( 
                                                'slug'        => 'doctor-profile',
                                                'with_front'  => true,
                                                'pages'       => true,
                                                'feeds'       => true
                                            ),
                'capability_type'       => 'post',
                'has_archive'           => true,
                'hierarchical'          => true,
                'menu_icon'             => 'dashicons-buddicons-buddypress-logo',
                'menu_position'         => 26,
                'supports'              => array( 'title' , 'editor' ,'thumbnail'),
                'show_in_rest'          => true
            );
            register_post_type( $type, $args );
            flush_rewrite_rules();
    }

        public function mt_service_post_type() {

                $type               = 'mt_service';
                $slug               = 'mt_service';
                $name               = 'Services';
                $singular_name      = 'Booking Service';
                
                $labels = array(
                    'name'                  => $name,
                    'singular_name'         => $singular_name,
                    'add_new'               => __('Add New', 'mt-service-booking'),
                    'add_new_item'          => 'Add New '   . $singular_name,
                    'edit_item'             => 'Edit '      . $singular_name,
                    'new_item'              => 'New '       . $singular_name,
                    'all_items'             => 'All '       . $name,
                    'view_item'             => 'View '      . $name,
                    'search_items'          => 'Search '    . $name,
                    'not_found'             => 'No '        . strtolower($name) . ' found',
                    'not_found_in_trash'    => 'No '        . strtolower($name) . ' found in Trash',
                    'parent_item_colon'     => '',
                    'menu_name'             => $name
                );
                $args = array(
                    'labels'                => $labels,
                    'public'                => true,
                    'publicly_queryable'    => true,
                    'show_ui'               => true,
                    'show_in_menu'          => false,
                    'show_in_admin_bar'     => true,
                    'query_var'             => true,
                    'rewrite'               => array( 
                                                    'slug'        => 'doctor-booking',
                                                    'with_front'  => true,
                                                    'pages'       => true,
                                                    'feeds'       => true
                                                ),
                    'capability_type'       => 'post',
                    'has_archive'           => true,
                    'hierarchical'          => true,
                    'menu_icon'             => 'dashicons-buddicons-buddypress-logo',
                    'menu_position'         => 26,
                    'supports'              => array(  'thumbnail'),
                    'show_in_rest'          => true
                );
                register_post_type( $type, $args );
                flush_rewrite_rules();
        }

        //Add texonomy
        public function mt_add_texonomy_to_booking_service(){
            $labels = array(
                'name'              => _x( 'Service Types', 'taxonomy general name' ),
                'singular_name'     => _x( 'Service Type', 'taxonomy singular name' ),
                'search_items'      => __( 'Search Service Types' ),
                'all_items'         => __( 'All Service Types' ),
                'parent_item'       => __( 'Parent Service Type' ),
                'parent_item_colon' => __( 'Parent Service Type:' ),
                'edit_item'         => __( 'Edit Service Type' ), 
                'update_item'       => __( 'Update Service Type' ),
                'add_new_item'      => __( 'Add New Service Type' ),
                'new_item_name'     => __( 'New Service Type' ),
                'menu_name'         => __( 'Service Types' ),
              );
              $args = array(
                'labels' => $labels,
                'hierarchical' => true,
                'show_ui'           => true,
                'show_admin_column' => true,
              );
              register_taxonomy( 'mt_service_type', 'mt_service', $args );
            
        }


        //adding meta box to save additional meta data for the content type
        public function mt_add_meta_boxes_to_booking_service(){
            //add a meta box
            add_meta_box(
                'mt_service_meta_box', //id
                __('Add Service Info', 'mt-service-booking'), //title
                array($this,'mt_callback_to_show_the_service_meta_boxes'),  //display function
                'mt_service', //content type 
                'normal', //context

            );
            
        }

         /**
         * Register post type
         */

   

        //displays the back-end admin output for the event information
        public function mt_callback_to_show_the_service_meta_boxes( $post ) {
            wp_nonce_field( basename( __FILE__ ), 'mt_nonce' );
            $mt_service_stored_meta = get_post_meta( $post->ID );
            $mttexteditor = get_post_meta( $post->ID, 'mttexteditor', true );
        ?>
            
          <div class="mt-service-header-info">
                <label for="mt-service-header-info"> <h3> <?php _e( 'Service Main Info', 'mt-service-booking' ); ?> </h3> </label>
                <p>
                    <label for="mt_display_service_title" class="mt-row-title"><?php _e( "Service Sub Title:", 'mt-service-booking' ); ?></label>
                    <input type="text" name="mt_display_service_title" id="mt_display_service_title" value="<?php if ( isset ( $mt_service_stored_meta['mt_display_service_title'] ) ) echo $mt_service_stored_meta['mt_display_service_title'][0]; ?>" />
                </p>
                <p>
                    <label for="mt_display_service_location" class="mt-row-location"><?php _e( "Location:", 'mt-service-booking' ); ?></label>
                    <input type="text" name="mt_display_service_location" id="mt_display_service_location" value="<?php if ( isset ( $mt_service_stored_meta['mt_display_service_location'] ) ) echo $mt_service_stored_meta['mt_display_service_location'][0]; ?>" />
                </p>
                <p>
                    <label for="mt_display_service_price" class="mt-row-price"><?php _e( "Service Price:", 'mt-service-booking' ); ?></label>
                    <input type="text" name="mt_display_service_price" id="mt_display_service_price" value="<?php if ( isset ( $mt_service_stored_meta['mt_display_service_price'] ) ) echo $mt_service_stored_meta['mt_display_service_price'][0]; ?>" />
                </p>
            </div>

            <!-- service reservation duration -->
            <div class="mt-service-availability-container mt-service-meta-field"> 
                <label for="mt-time-slot"> <h3> <?php _e( 'Service Availability', 'mt-service-booking' ); ?> </h3> </label> 
                <p>
                    <label for="mt_service_availability_starts_at" class="mt-row-date-availability"><?php _e( "Will be Available From: ", 'mt-service-booking' ); ?></label>
                    <input type="date" name="mt_service_availability_starts_at" id="mt_service_availability_starts_at" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_availability_starts_at'] ) ) echo $mt_service_stored_meta['mt_service_availability_starts_at'][0]; ?>" />
                </p>
                <p>
                    <label for="mt_service_availability_ends_at" class="mt-row-date-availability"><?php _e( "Will be Unavailable From: ", 'mt-service-booking' ); ?></label>
                    <input type="date" name="mt_service_availability_ends_at" id="mt_service_availability_ends_at" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_availability_ends_at'] ) ) echo $mt_service_stored_meta['mt_service_availability_ends_at'][0]; ?>" />
                </p>
                <footer class="blockquote-footer text-danger"> <?php _e( 'If you don\'t set these value, this service will not be available for booking.', 'mt-service-booking' ); ?> </footer>
            </div>

            <!-- weekly Off-days  -->
            <div class="mt-off-days">
              <label for="mt-off-days"> <h3> <?php _e( 'Weekly Off Days ', 'mt-service-booking' ); ?> </h3> </label>
                <p>
                    <input type="checkbox" name="mt_service_off_day_sun" id="mt_service_off_day_sun" value="1" <?php if ( isset ( $mt_service_stored_meta['mt_service_off_day_sun'] ) ) checked( $mt_service_stored_meta['mt_service_off_day_sun'][0], '1' ); ?> />
                    <label for="mt_service_off_day_sun" class="mt-row-off_days"><?php _e( "Sunday:", 'mt-service-booking' )?></label>
                    
                    <input type="checkbox" name="mt_service_off_day_mon" id="mt_service_off_day_mon" value="2" <?php if ( isset ( $mt_service_stored_meta['mt_service_off_day_mon'] ) ) checked( $mt_service_stored_meta['mt_service_off_day_mon'][0], '2' ); ?> />
                    <label for="mt_service_off_day_mon" class="mt-row-off_days"><?php _e( "Monday:", 'mt-service-booking' )?></label>
                    
                    <input type="checkbox" name="mt_service_off_day_tues" id="mt_service_off_day_tues" value="3" <?php if ( isset ( $mt_service_stored_meta['mt_service_off_day_tues'] ) ) checked( $mt_service_stored_meta['mt_service_off_day_tues'][0], '3' ); ?> />
                    <label for="mt_service_off_day_tues" class="mt-row-off_days"><?php _e( "Tuesday:", 'mt-service-booking' )?></label>
                    
                    <input type="checkbox" name="mt_service_off_day_wed" id="mt_service_off_day_wed" value="4" <?php if ( isset ( $mt_service_stored_meta['mt_service_off_day_wed'] ) ) checked( $mt_service_stored_meta['mt_service_off_day_wed'][0], '4' ); ?> />
                    <label for="mt_service_off_day_wed" class="mt-row-off_days"><?php _e( "Wednesday:", 'mt-service-booking' )?></label>
                    
                    <input type="checkbox" name="mt_service_off_day_thurs" id="mt_service_off_day_thurs" value="5" <?php if ( isset ( $mt_service_stored_meta['mt_service_off_day_thurs'] ) ) checked( $mt_service_stored_meta['mt_service_off_day_thurs'][0], '5' ); ?> />
                    <label for="mt_service_off_day_thurs" class="mt-row-off_days"><?php _e( "Thursday:", 'mt-service-booking' )?></label>
                    
                    <input type="checkbox" name="mt_service_off_day_fri" id="mt_service_off_day_fri" value="6" <?php if ( isset ( $mt_service_stored_meta['mt_service_off_day_fri'] ) ) checked( $mt_service_stored_meta['mt_service_off_day_fri'][0], '6' ); ?> />
                    <label for="mt_service_off_day_fri" class="mt-row-off_days"><?php _e( "Friday:", 'mt-service-booking' )?></label>
                    
                    <input type="checkbox" name="mt_service_off_day_sat" id="mt_service_off_day_sat" value="7" <?php if ( isset ( $mt_service_stored_meta['mt_service_off_day_sat'] ) ) checked( $mt_service_stored_meta['mt_service_off_day_sat'][0], '7' ); ?> />
                    <label for="mt_service_off_day_sat" class="mt-row-off_days"><?php _e( "Saturday:", 'mt-service-booking' )?></label>
                </p>
            </div>
            <!-- weekly Off-days ends  -->

            <!-- service reservation duration -->
            <div class="mt-servation-duration-container mt-service-meta-field"> 
                <label for="mt-time-slot"> <h3> <?php _e( 'Full Day Reservation ', 'mt-service-booking' ); ?> </h3> </label> 
                <p>
                    <label for="mt_service_full_day_reservation" class="mt-row-time_slot"><?php _e( "Is this service for full day reservation? ( 24 hours ) ", 'mt-service-booking' ); ?></label>
                    <input type="checkbox" name="mt_service_full_day_reservation" id="mt_service_full_day_reservation" value="checked" <?php if ( isset ( $mt_service_stored_meta['mt_service_full_day_reservation'] ) ) checked( $mt_service_stored_meta['mt_service_full_day_reservation'][0], 'checked' ); ?> />
                </p>
            </div>
            

             <!-- AM Time Slot  -->
            <div class="mt-service-meta-field mt-time-slot-container">
                <label for="mt-time-slot"> <h3> <?php _e( 'AM Time Slot ', 'mt-service-booking' ); ?> </h3> </label>
                <p>
                    <label for="mt_service_am_starting_time" class="mt-row-time_slot"><?php _e( "Starting Time:", 'mt-service-booking' ); ?></label>
                    <input min='00:00' max= '11:00' type="time" name="mt_service_am_starting_time" id="mt_service_am_starting_time" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_am_starting_time'] ) ) echo $mt_service_stored_meta['mt_service_am_starting_time'][0]; ?>" />
                </p>
                <p>
                    <label for="mt_service_am_ending_time" class="mt-row-time_slot"><?php _e( "Ending Time:", 'mt-service-booking' ); ?></label>
                    <input min='01:00' max= '11:59' type="time" name="mt_service_am_ending_time" id="mt_service_am_ending_time" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_am_ending_time'] ) ) echo $mt_service_stored_meta['mt_service_am_ending_time'][0]; ?>" />
                </p>
                <p>
                    <label for="mt_service_am_slot_duration" class="mt-row-time_slot"><?php _e( "Slot Duration in Munites:", 'mt-service-booking' ); ?></label>
                    <input type="number" name="mt_service_am_slot_duration" id="mt_service_am_slot_duration" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_am_slot_duration'] ) ) echo $mt_service_stored_meta['mt_service_am_slot_duration'][0]; ?>" />
                </p>
            </div>
            <!-- AM Time Slot Ends  -->

            <!-- PM Time Slot Starts  -->
            <div class="mt-service-meta-field mt-time-slot-container">
                <label for="mt-time-slot"> <h3> <?php _e( 'PM Time Slot ', 'mt-service-booking' ); ?> </h3> </label>
                <p>
                    <label for="mt_service_pm_starting_time" class="mt-row-time_slot"><?php _e( "Starting Time:", 'mt-service-booking' ); ?></label>
                    <input min='12:00' max= '22:59' type="time" name="mt_service_pm_starting_time" id="mt_service_pm_starting_time" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_pm_starting_time'] ) ) echo $mt_service_stored_meta['mt_service_pm_starting_time'][0]; ?>" />
                </p>
                <p>
                    <label for="mt_service_pm_ending_time" class="mt-row-time_slot"><?php _e( "Ending Time:", 'mt-service-booking' ); ?></label>
                    <input min='13:00' max= '23:59' type="time" name="mt_service_pm_ending_time" id="mt_service_pm_ending_time" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_pm_ending_time'] ) ) echo $mt_service_stored_meta['mt_service_pm_ending_time'][0]; ?>" />
                </p>
                <p>
                    <label for="mt_service_pm_slot_duration" class="mt-row-time_slot"><?php _e( "Slot Duration in Munites:", 'mt-service-booking' ); ?></label>
                    <input type="number" name="mt_service_pm_slot_duration" id="mt_service_pm_slot_duration" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_pm_slot_duration'] ) ) echo $mt_service_stored_meta['mt_service_pm_slot_duration'][0]; ?>" />
                </p>
            </div>
          <!-- PM Time Slot Ends  -->

          <!-- Orders per slot  -->
          <div class="mt-service-meta-field mt-permitted-booking-orders-container">
                <label for="mt-time-slot"> <h3> <?php _e( 'Orders per Day or Time Slot ', 'mt-service-booking' ); ?> </h3> </label>
                <p>
                    <label for="mt_service_orders_per_slot" class="mt-row-time_slot"><?php _e( "How many orders do you want per day or time slot? ", 'mt-service-booking' ); ?></label>
                    <input type="number" min="1" name="mt_service_orders_per_slot" id="mt_service_orders_per_slot" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_orders_per_slot'] ) ) echo $mt_service_stored_meta['mt_service_orders_per_slot'][0]; ?>" />
                </p>
            </div>
          <!-- Orders per slot Ends  -->

          <!-- Service Provider Email Address Starts  -->
          <div class="mt-service-meta-field">
                <label for="mt_service_provider_email"> <h3> <?php _e( 'Service Provider Email Address ', 'mt-service-booking' ); ?> </h3> </label>
                <p>
                    <label for="mt_service_provider_email" class="mt-row-email"><?php _e( "Email Address: ", 'mt-service-booking' ); ?><?php echo get_the_author_meta('user_email'); ?></label>
                    <input type="email" name="mt_service_provider_email" id="mt_service_provider_email" value="<?php if ( isset ( $mt_service_stored_meta['mt_service_provider_email'] ) ) echo $mt_service_stored_meta['mt_service_provider_email'][0]; ?>" />
                </p>

            </div>
          <!-- Service Provider Email Address Ends  -->

          <!-- Service description -->
            <div class="mt-service-meta-field mt-service-description-container">
                <label for="mttexteditor_check"> <h3> <?php _e( "Service Description ", 'mt-service-booking' ); ?> </h3> </label>
                <p>
                    <label for="mttexteditor_check" class="mt-row-time_slot"><?php _e( "Has description for single service page? ", 'mt-service-booking' );?></label>
                    <input type="checkbox" name="mttexteditor_check" id="mttexteditor_check" value="description" <?php if ( isset ( $mt_service_stored_meta['mttexteditor_check'] ) ) checked( $mt_service_stored_meta['mttexteditor_check'][0], 'description' ); ?> />
                </p>
                <div id="mt-texteditor-container">
                    <?php
                        wp_editor(
                            wpautop( $mttexteditor ),
                            'mttexteditor',
                            array( 'wpautop' => false )
                        );
                    ?>
                </div>
            </div>
          
          
        <?php }

        // *************** Save all post types meta fields ************* //
        public function mt_save_all_posts_types_meta_fields_meta( $post_id ) {
            // Checks save status 
            $is_autosave = wp_is_post_autosave( $post_id );
            $is_revision = wp_is_post_revision( $post_id );
            $is_valid_nonce = ( isset( $_POST[ 'mt_nonce' ] ) && wp_verify_nonce( $_POST[ 'mt_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

            // Exits script depending on save status 
            if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
                return;
            }
        // *************** post-type: Reviews **************//

            // Checks for input and sanitizes/saves if needed 
            if( isset( $_POST[ 'mt_display_service_title' ] ) ) {
                $mt_display_service_title = htmlspecialchars($_POST[ 'mt_display_service_title']);
                $mt_display_service_title_sanitized = sanitize_text_field($mt_display_service_title);
                update_post_meta( $post_id, 'mt_display_service_title', $mt_display_service_title_sanitized );
            }
            if( isset( $_POST[ 'mt_display_service_location' ] ) ) {
                $mt_display_service_location = htmlspecialchars($_POST[ 'mt_display_service_location']);
                $mt_display_service_location_sanitized = sanitize_text_field($mt_display_service_location);
                update_post_meta( $post_id, 'mt_display_service_location', $mt_display_service_location_sanitized );
            }
            if( isset( $_POST[ 'mt_display_service_price' ] ) ) {
                $mt_display_service_price = htmlspecialchars($_POST[ 'mt_display_service_price']);
                $mt_display_service_price_sanitized = sanitize_text_field($mt_display_service_price);
                update_post_meta( $post_id, 'mt_display_service_price', $mt_display_service_price_sanitized );
            }

            // *************** Service Availability starts at **************//
            if( isset( $_POST[ 'mt_service_availability_starts_at' ] ) ) {
                $mt_service_availability_starts_at = htmlspecialchars($_POST[ 'mt_service_availability_starts_at']);
                $mt_service_availability_starts_at_sanitized = sanitize_text_field($mt_service_availability_starts_at);
                update_post_meta( $post_id, 'mt_service_availability_starts_at', $mt_service_availability_starts_at_sanitized );
            }

            // *************** Service Availability ends at **************//
            if( isset( $_POST[ 'mt_service_availability_ends_at' ] ) ) {
                $mt_service_availability_ends_at = htmlspecialchars($_POST[ 'mt_service_availability_ends_at']);
                $mt_service_availability_ends_at_sanitized = sanitize_text_field($mt_service_availability_ends_at);
                update_post_meta( $post_id, 'mt_service_availability_ends_at', $mt_service_availability_ends_at_sanitized );
            }

            // *************** Off-days **************//
            if( isset( $_POST[ 'mt_service_off_day_sun' ] ) ) {
                update_post_meta( $post_id, 'mt_service_off_day_sun', '1' );
            } else {
                update_post_meta( $post_id, 'mt_service_off_day_sun', '' );
            }

            if( isset( $_POST[ 'mt_service_off_day_mon' ] ) ) {
                update_post_meta( $post_id, 'mt_service_off_day_mon', '2' );
            } else {
                update_post_meta( $post_id, 'mt_service_off_day_mon', '' );
            }

            if( isset( $_POST[ 'mt_service_off_day_tues' ] ) ) {
                update_post_meta( $post_id, 'mt_service_off_day_tues', '3' );
            } else {
                update_post_meta( $post_id, 'mt_service_off_day_tues', '' );
            }

            if( isset( $_POST[ 'mt_service_off_day_wed' ] ) ) {
                update_post_meta( $post_id, 'mt_service_off_day_wed', '4' );
            } else {
                update_post_meta( $post_id, 'mt_service_off_day_wed', '' );
            }

            if( isset( $_POST[ 'mt_service_off_day_thurs' ] ) ) {
                update_post_meta( $post_id, 'mt_service_off_day_thurs', '5' );
            } else {
                update_post_meta( $post_id, 'mt_service_off_day_thurs', '' );
            }

            if( isset( $_POST[ 'mt_service_off_day_fri' ] ) ) {
                update_post_meta( $post_id, 'mt_service_off_day_fri', '6' );
            } else {
                update_post_meta( $post_id, 'mt_service_off_day_fri', '' );
            }

            if( isset( $_POST[ 'mt_service_off_day_sat' ] ) ) {
                update_post_meta( $post_id, 'mt_service_off_day_sat', '7' );
            } else {
                update_post_meta( $post_id, 'mt_service_off_day_sat', '' );
            }


            // *************** Full Day Reservation **************// 
            if( isset( $_POST[ 'mt_service_full_day_reservation' ] ) ) {
                update_post_meta( $post_id, 'mt_service_full_day_reservation', 'checked' );
            } else {
                update_post_meta( $post_id, 'mt_service_full_day_reservation', '' );
            }
            

            // *************** AM Time slot **************//
            $default_mt_service_am_starting_time = "09:00";
            $default_mt_service_am_ending_time = "11:00";
            $default_mt_service_am_slot_duration = "60";
            if( isset( $_POST[ 'mt_service_am_starting_time' ] ) ) {
                $mt_service_am_starting_time = htmlspecialchars($_POST[ 'mt_service_am_starting_time']);
                $mt_service_am_starting_time_sanitized = sanitize_text_field($mt_service_am_starting_time);
                update_post_meta( $post_id, 'mt_service_am_starting_time', $mt_service_am_starting_time_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_am_starting_time', $default_mt_service_am_starting_time );
            }

            if( isset( $_POST[ 'mt_service_am_ending_time' ] ) ) {
                $mt_service_am_ending_time = htmlspecialchars($_POST[ 'mt_service_am_ending_time']);
                $mt_service_am_ending_time_sanitized = sanitize_text_field($mt_service_am_ending_time);
                update_post_meta( $post_id, 'mt_service_am_ending_time', $mt_service_am_ending_time_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_am_ending_time', $default_mt_service_am_ending_time );
            }

            if( isset( $_POST[ 'mt_service_am_slot_duration' ] ) ) {
                $mt_service_am_slot_duration = htmlspecialchars($_POST[ 'mt_service_am_slot_duration']);
                $mt_service_am_slot_duration_sanitized = absint($mt_service_am_slot_duration);
                update_post_meta( $post_id, 'mt_service_am_slot_duration', $mt_service_am_slot_duration_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_am_slot_duration', $default_mt_service_am_slot_duration );
            }

            // *************** PM Time slot **************//
            $default_mt_service_pm_starting_time = "15:00";
            $default_mt_service_pm_ending_time = "22:00";
            $default_mt_service_pm_slot_duration = "60";
            if( isset( $_POST[ 'mt_service_pm_starting_time' ] ) ) {
                $mt_service_pm_starting_time = htmlspecialchars($_POST[ 'mt_service_pm_starting_time']);
                $mt_service_pm_starting_time_sanitized = sanitize_text_field($mt_service_pm_starting_time);
                update_post_meta( $post_id, 'mt_service_pm_starting_time', $mt_service_pm_starting_time_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_pm_starting_time', $default_mt_service_pm_starting_time );
            }

            if( isset( $_POST[ 'mt_service_pm_ending_time' ] ) ) {
                $mt_service_pm_ending_time = htmlspecialchars($_POST[ 'mt_service_pm_ending_time']);
                $mt_service_pm_ending_time_sanitized = sanitize_text_field($mt_service_pm_ending_time);
                update_post_meta( $post_id, 'mt_service_pm_ending_time', $mt_service_pm_ending_time_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_pm_ending_time', $default_mt_service_pm_ending_time );
            }
            if( isset( $_POST[ 'mt_service_pm_slot_duration' ] ) ) {
                $mt_service_pm_slot_duration = htmlspecialchars($_POST[ 'mt_service_pm_slot_duration']);
                $mt_service_pm_slot_duration_sanitized = absint($mt_service_pm_slot_duration);
                update_post_meta( $post_id, 'mt_service_pm_slot_duration', $mt_service_pm_slot_duration_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_pm_slot_duration', $default_mt_service_pm_slot_duration );
            }

            // *************** Orders per slot **************//
            $default_order_per_slot = 1;
            if( isset( $_POST[ 'mt_service_orders_per_slot' ] ) ) {
                $mt_service_orders_per_slot = htmlspecialchars($_POST[ 'mt_service_orders_per_slot']);
                $mt_service_orders_per_slot_sanitized = absint($mt_service_orders_per_slot);
                update_post_meta( $post_id, 'mt_service_orders_per_slot', $mt_service_orders_per_slot_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_orders_per_slot', $default_order_per_slot);
            }

            // *************** Service Provider Email Address **************//
            $mt_service_provider_email_default = get_bloginfo('admin_email');
            if(isset( $_POST[ 'mt_service_provider_email' ] )) {
                $mt_service_provider_email = htmlspecialchars($_POST[ 'mt_service_provider_email']);
                $mt_service_provider_email_sanitized = sanitize_email($mt_service_provider_email);
                update_post_meta( $post_id, 'mt_service_provider_email', $mt_service_provider_email_sanitized );
            } else {
                update_post_meta( $post_id, 'mt_service_provider_email', $mt_service_provider_email_default );
            }
            
            // *************** mttexteditor **************//
            if( isset( $_POST[ 'mttexteditor_check' ] ) ) {
                update_post_meta( $post_id, 'mttexteditor_check', 'description' );
            } else {
                update_post_meta( $post_id, 'mttexteditor_check', '' );
            }

            if( isset( $_POST[ 'mttexteditor' ] ) ) {
                $mttexteditor_sanitized = wp_kses_post($_POST[ 'mttexteditor']);
                update_post_meta( $post_id, 'mttexteditor', $mttexteditor_sanitized );
            }

            
        }

        
        
        public function mt_get_archive_template( $archive_template ) {
            global $post;
        
            if ( is_post_type_archive ( 'mt_service' ) ) {
                 $archive_template = dirname( __FILE__ ) . '/archive-mt_service.php';
            }
            return $archive_template;
        }

        public function mt_get_single_template( $mt_single_service ) {
            global $post;
            // global $post_type;
        
            if ( is_singular( 'mt_service' ) ) {
                 $mt_single_service = dirname( __FILE__ ) . '/single-mt_service.php';
            }
            return $mt_single_service;
        }

        public function mt_disable_gutenberg_pages( $can_edit, $post_type ) {
            if ( 'mt_service' === $post_type ) {
              return false;
            }
            return $can_edit;
          }

        
         

        public function mt_enqueue_public_scripts(){
  
            wp_enqueue_style('mt-calendar', plugin_dir_url(__FILE__) . 'calendar/mt-calendar.css', array(), '1.1', false );
            wp_enqueue_style('mt-all', plugin_dir_url(__FILE__) . 'assets/public/css/mt-all.css', array(), '1.1', false );
            
            wp_enqueue_script("jquery");
            
            wp_enqueue_script('popper-js', plugin_dir_url(__FILE__) . 'assets/js/popper.min.js', array(), '1.1', true );
            wp_enqueue_script('bootstrap-js', plugin_dir_url(__FILE__) . 'assets/js/bootstrap-v4.3.1.min.js', array(), '1.1', true );
            wp_enqueue_script('chosen.jquery', plugin_dir_url(__FILE__) . 'assets/js/chosen.jquery.js', array(), '1.1', true );
            wp_enqueue_script('pseudo-ripple-js', plugin_dir_url(__FILE__) . 'calendar/jquery-pseudo-ripple.js', array(), '1.1', true );
            wp_enqueue_script('nao-calendar-js', plugin_dir_url(__FILE__) . 'calendar/jquery-nao-calendar.js', array(), '1.1', true ); 
            wp_register_script( "mt-script-js", plugin_dir_url(__FILE__) . 'assets/public/js/script.js', array('jquery') );
            
            wp_localize_script( 'mt-script-js', 'frontend_ajax_object',
                array( 
                    'ajaxurl' => admin_url( 'admin-ajax.php' )
                )
            );
            wp_enqueue_script('mt-script-js', plugin_dir_url(__FILE__) . 'assets/public/js/script.js', array(), '1.1', true );

        }
   
          
                
                
                
    }
}
/**
 * Instantiate class
 */
$mt_service_booking_plugin = new mt_service_booking_plugin_base_class();



class mt_database {
    public function __construct() {
        register_activation_hook( __FILE__, array($this,'create_mt_bookings_table'));
        register_activation_hook( __FILE__, array($this,'mt_default_booking'));
        register_activation_hook( __FILE__, array($this,'create_mt_settings_table'));
        register_activation_hook( __FILE__, array($this,'mt_settings_default_data'));
        
    }

    public function create_mt_bookings_table(){
            global $wpdb;
            $sql_enquiry = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}mt_bookings (
            `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `approve_booking` varchar(20),
            `service_id` int(11) NOT NULL,
            `service_name` varchar(200),
            `service_price` text,
            `booked_date_id` text,
            `booked_slot_id` text,
            `booked_date` text,
            `booked_time_slot` text,
            `customer_name` varchar(200),
            `customer_email` text,
            `customer_phone` text,
            `booking_time` TIMESTAMP NOT NULL,
            `customer_IP` VARCHAR(100) NOT NULL,
            `starting_time_ms` BIGINT NOT NULL,
            `available_orders` int(11) NOT NULL
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";


        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql_enquiry );
        

    }

    public function mt_default_booking(){
        global $wpdb;
        $mt_bookings_table_name = $wpdb->prefix . "mt_bookings";
    
        $dataInsert = $wpdb->insert( $mt_bookings_table_name, array(
            'service_id' => 0,
            'service_name' => "Default Data",
            'service_price' => "Default Data",
            'booked_date_id' => "Default Data",
            'booked_slot_id' => "Default Data",
            'booked_date' => "Default Data",
            'booked_time_slot' => "Default Data",
            'customer_name' => "Default Data",
            'customer_email' => "Default Data",
            'customer_phone' => "Default Data"
        ) );
    

    }

    public function create_mt_settings_table(){
            global $wpdb;
            $sql_enquiry_notifiction_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}mt_settings (
            `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `admin_mail_subject` text,
            `admin_mail_body` text,
            `customer_mail_pending_subject` text,
            `customer_mail_pending_body` text,
            `customer_mail_confirmed_subject` text,
            `customer_mail_confirmed_body` text,
            `customer_mail_cancel_subject` text,
            `customer_mail_cancel_body` text,
            `customer_cookie_duration` int(11)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";


        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql_enquiry_notifiction_table );

    }
    public function mt_settings_default_data() {
		global $wpdb;
        $mt_settings_data = $wpdb->prefix . 'mt_settings';
		$wpdb->insert($mt_settings_data, array(
            'admin_mail_subject' => "Service provider's email subject",
            'admin_mail_body' => "Service provider's email message body",
            'customer_mail_pending_subject' => "Booking Pending",
            'customer_mail_pending_body' => "pending booking email body",
            'customer_mail_confirmed_subject' => "Booking Confirmed",
            'customer_mail_confirmed_body' => "Confirmation email message body",
            'customer_mail_cancel_subject' => "Booking Cancelled",
            'customer_mail_cancel_body' => "Cancel email message body",
            'customer_cookie_duration' => 30
        ));

    }
    

}
$mt_database = new mt_database();


class mt_post_pages {
    public function __construct() {
        $mt_page_slug = get_page_by_path( 'doctor-booking' );
        if(!$mt_page_slug){
            register_activation_hook( __FILE__, array($this,'mt_create_archive_page'));
        } 
        
        
    }

    public function mt_create_archive_page(){

        $booking_services  = array( 
                            'post_title'     => 'Doctor Booking',
                            'post_type'      => 'page',
                            'post_name'      => 'Doctor Booking',
                            'post_status'    => 'publish',
                            'comment_status' => 'closed'
                        );

        $PageID = wp_insert_post( $booking_services, FALSE ); 

    }

    
}
$mt_post_pages = new mt_post_pages();


class mt_internationlization {
    public function __construct() {
        // Initialize the plugin
		add_action( 'init', array( $this, 'mt_load_plugin_textdomain' ) );
        
    }

    public function mt_load_plugin_textdomain(){
        load_plugin_textdomain( 'mt-service-booking', false, plugin_basename( dirname( __FILE__ ) ) . "/languages/" );
    }

    
}
$mt_internationlization = new mt_internationlization();


include( plugin_dir_path( __FILE__ ) . 'mt-ajax-calls.php');
include( plugin_dir_path( __FILE__ ) . 'mt-service-booking-admin.php');
// Add support for 'thumbnail' feature to 'doctorsprofile' post type
// Add support for 'thumbnail' feature to 'doctorsprofile' post type
// Add meta box for selecting doctor profile
// Add meta box for selecting doctor profile
