HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/doccure-wp/wp-content/themes/doccure/directory/front-end/ajax-hooks2.php
<?php 
function doccure_delete_doctor() {
	//$settings	= $_POST['settings'];
	$json		= array();
 
 	$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}	
 
	if (!isset($_POST['post_id']) || !is_numeric($_POST['post_id'])) {
        wp_send_json_error(['message' => 'Invalid request']);
        wp_die();
    }

	if (!isset($_POST['user_id']) || !is_numeric($_POST['user_id'])) {
        wp_send_json_error(['message' => 'Invalid request']);
        wp_die();
    }

    $post_id = intval($_POST['post_id']);
	$user_id = intval($_POST['user_id']);
    $current_user = wp_get_current_user();
    $post = get_post($post_id);
	$parent_id = get_post_meta($post_id, 'parent_id', true );
    if (!$post) {
        wp_send_json_error(['message' => 'Post not found']);
        wp_die();
    }

    // Check if the user is the post author or has delete permissions
    if ($parent_id != $current_user->ID && !current_user_can('delete_others_posts')) {
        wp_send_json_error(['message' => 'You do not have permission to delete this clinic.']);
        wp_die();
    }

    // Delete the post
    $deleted = wp_delete_post($post_id, true);

	$user_deleted = wp_delete_user($user_id);

 	$json['type']	= 'success';	
	$json['message']	= esc_html__('Doctor deleted successfully','doccure' );
	wp_send_json( $json );	


    wp_die();
}

add_action('wp_ajax_doccure_delete_doctor', 'doccure_delete_doctor');
add_action('wp_ajax_nopriv_doccure_delete_doctor', 'doccure_delete_doctor');


function doccure_delete_leave_request() {
	//$settings	= $_POST['settings'];
	$json		= array();
 
 	$do_check = check_ajax_referer('doccure_ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it again', 'doccure');
			wp_send_json( $json );
		}	
 
	if (!isset($_POST['leave_id']) || !is_numeric($_POST['leave_id'])) {
        $json['type'] = 'error';
        $json['message'] = esc_html__('Invalid leave request ID', 'doccure');
        wp_send_json( $json );
    }

    $leave_id = intval($_POST['leave_id']);
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;

    global $wpdb;
    $table_name = $wpdb->prefix . 'staff_leave_requests';

    // Check if leave request exists and belongs to current user
    $leave_request = $wpdb->get_row($wpdb->prepare(
        "SELECT * FROM $table_name WHERE id = %d AND staff_id = %d",
        $leave_id, $user_id
    ));

    if (!$leave_request) {
        $json['type'] = 'error';
        $json['message'] = esc_html__('Leave request not found or you do not have permission to delete it', 'doccure');
        wp_send_json( $json );
    }

    // Delete the leave request from custom table
    $deleted = $wpdb->delete(
        $table_name,
        array('id' => $leave_id, 'staff_id' => $user_id),
        array('%d', '%d')
    );

    if ($deleted === false) {
        error_log("Failed to delete leave request: " . $wpdb->last_error);
        $json['type'] = 'error';
        $json['message'] = esc_html__('Failed to delete leave request. Please try again.', 'doccure');
        wp_send_json( $json );
    }

 	$json['type']	= 'success';	
	$json['message']	= esc_html__('Leave request deleted successfully','doccure' );
	wp_send_json( $json );	

    wp_die();
}

add_action('wp_ajax_doccure_delete_leave_request', 'doccure_delete_leave_request');
add_action('wp_ajax_nopriv_doccure_delete_leave_request', 'doccure_delete_leave_request');

function doccure_delete_staff() {
	//$settings	= $_POST['settings'];
	$json		= array();
 
 	$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}	
 
	if (!isset($_POST['post_id']) || !is_numeric($_POST['post_id'])) {
        wp_send_json_error(['message' => 'Invalid request']);
        wp_die();
    }

	if (!isset($_POST['user_id']) || !is_numeric($_POST['user_id'])) {
        wp_send_json_error(['message' => 'Invalid request']);
        wp_die();
    }

    $post_id = intval($_POST['post_id']);
	$user_id = intval($_POST['user_id']);
    $current_user = wp_get_current_user();
    $post = get_post($post_id);
	$parent_id = get_post_meta($post_id, 'parent_id', true );
    if (!$post) {
        wp_send_json_error(['message' => 'Post not found']);
        wp_die();
    }

    // Check if the user is the post author or has delete permissions
    if ($parent_id != $current_user->ID && !current_user_can('delete_others_posts')) {
        wp_send_json_error(['message' => 'You do not have permission to delete this clinic.']);
        wp_die();
    }

    // Delete the post
    $deleted = wp_delete_post($post_id, true);

	$user_deleted = wp_delete_user($user_id);

 	$json['type']	= 'success';	
	$json['message']	= esc_html__('staff deleted successfully','doccure' );
	wp_send_json( $json );	


    wp_die();
}

add_action('wp_ajax_doccure_delete_staff', 'doccure_delete_staff');
add_action('wp_ajax_nopriv_doccure_delete_staff', 'doccure_delete_staff');
/**
 * Update doctor Profile

 */
if( !function_exists( 'doccure_update_doctor_profile' ) ){
    function doccure_update_doctor_profile(){       
        global $current_user,$doccure_options;               
        $json = array();
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}	

        $required_fields = array(
            'am_first_name'   	=> esc_html__('First  name is required', 'doccure'),
			'am_last_name'  	=> esc_html__('Last name is required', 'doccure'),
			'am_mobile_number'  => esc_html__('Personal mobile number is required', 'doccure'),
        );

        foreach ($required_fields as $key => $value) {
           if( empty( $_POST[$key] ) ){
            $json['type'] 		= 'error';
            $json['message'] 	= $value;        
            wp_send_json($json);
           }
        }
		
		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);

		$enable_options		= !empty($doccure_options['doctors_contactinfo']) ? $doccure_options['doctors_contactinfo'] : '';
		
        //Form data
        $display_name 		= !empty($_POST['display_name']) ? ($_POST['display_name']) : '';
		$am_first_name 		= !empty($_POST['am_first_name']) ? ($_POST['am_first_name']) : '';
		$am_mobile_number 	= !empty($_POST['am_mobile_number']) ? ($_POST['am_mobile_number']) : '';
        $am_last_name  		= !empty($_POST['am_last_name'] ) ? ($_POST['am_last_name']) : '';
		$am_name_base  		= !empty($_POST['am_name_base'] ) ? ($_POST['am_name_base']) : '';
		$am_gender  		= !empty($_POST['am_gender'] ) ? ($_POST['am_gender']) : '';
		$am_web_url			= !empty( $_POST['am_web_url'] ) ?  $_POST['am_web_url']  : '';
		
		$address   = !empty($_POST['address']) ? sanitize_text_field($_POST['address']) : '';
		$longitude = !empty($_POST['longitude']) ? sanitize_text_field($_POST['longitude']) : '';
		$latitude  = !empty($_POST['latitude']) ? sanitize_text_field($_POST['latitude']) : '';
		$location  = !empty($_POST['location']) ? $_POST['location'] : '';
		
		$am_sub_heading  		= !empty($_POST['am_sub_heading'] ) ? ($_POST['am_sub_heading']) : '';
		$am_starting_price  	= !empty($_POST['am_starting_price'] ) ? ($_POST['am_starting_price']) : '';
		$am_short_description  	= !empty($_POST['am_short_description'] ) ? sanitize_textarea_field($_POST['am_short_description']) : '';
		$am_memberships_name  	= !empty($_POST['am_memberships_name'] ) ? $_POST['am_memberships_name'] : array();
		$am_phone_numbers  		= !empty($_POST['am_phone_numbers'] ) ? $_POST['am_phone_numbers'] : array();
        $content				= !empty($_POST['content'] ) ? wp_kses_post($_POST['content']) : '';
		$registration_number = isset($_POST['am_registration_number']) ? sanitize_text_field($_POST['am_registration_number']) : '';
		$am_document = isset($_POST['am_document']) ? sanitize_text_field($_POST['am_document']) : '';
		$total_experience = isset($_POST['total_experience']) ? sanitize_text_field($_POST['total_experience']) : '';
		$qualification = isset($_POST['qualification']) ? sanitize_text_field($_POST['qualification']) : '';
		$speciality = isset($_POST['speciality']) ? sanitize_text_field($_POST['speciality']) : '';
		
		update_post_meta($post_id, 'am_gender', $am_gender);
		
		update_user_meta($user_id, '_registration_number', $registration_number);
 		update_user_meta($user_id, 'doc_number', $doc_number );
 		update_user_meta( $user_id, 'position', $am_sub_heading );
		if ( $qualification !== '' ) {
			update_user_meta( $user_id, 'qualification', $qualification );
		}
		if ( $speciality !== '' ) {
			update_user_meta( $user_id, 'speciality', $speciality );
		}
		if ( $total_experience !== '' ) {
			update_user_meta( $user_id, 'total_experience', $total_experience );
		}

		// Persist address/coordinates on the linked profile
		if ( $address !== '' || $longitude !== '' || $latitude !== '' ) {
			update_post_meta( $post_id, '_address', $address );
			update_post_meta( $post_id, '_longitude', $longitude );
			update_post_meta( $post_id, '_latitude', $latitude );
		}

		// Update locations taxonomy based on posted location field (IDs or slugs)
		$location_ids = array();
		if ( ! empty( $location ) ) {
			if ( is_array( $location ) ) {
				foreach ( $location as $item ) {
					if ( is_numeric( $item ) ) {
						$location_ids[] = (int) $item;
					} else {
						$term_id = function_exists( 'doccure_get_term_by_type' ) ? doccure_get_term_by_type( 'slug', sanitize_text_field( $item ), 'locations' ) : 0;
						if ( $term_id ) {
							$location_ids[] = (int) $term_id;
						}
					}
				}
			} else {
				// Single value: either ID or slug
				if ( is_numeric( $location ) ) {
					$location_ids[] = (int) $location;
				} else {
					$term_id = function_exists( 'doccure_get_term_by_type' ) ? doccure_get_term_by_type( 'slug', sanitize_text_field( $location ), 'locations' ) : 0;
					if ( $term_id ) {
						$location_ids[] = (int) $term_id;
					}
				}
			}
		}
		if ( ! empty( $location_ids ) ) {
			wp_set_post_terms( $post_id, $location_ids, 'locations' );
		}

		
        //Update user meta
        update_user_meta($user_id, 'first_name', $am_first_name);
		update_user_meta($user_id, 'last_name', $am_last_name);
		update_user_meta($user_id, 'mobile_number', $am_mobile_number);
		

 wp_update_user([
    'ID' => $user_id,
    'display_name' => $display_name
]);

		
		$post_meta['am_first_name']		= $am_first_name;
		$post_meta['am_mobile_number']	= $am_mobile_number;
		$post_meta['am_last_name']		= $am_last_name;
		$post_meta['am_name_base']		= $am_name_base;
		$post_meta['am_gender']			= $am_gender;
		$post_meta['total_experience']	= $total_experience;
		$post_meta['qualification']		= $qualification;
		$post_meta['speciality']		= $speciality;
		
		$post_meta['am_starting_price']		= $am_starting_price;
		$post_meta['am_sub_heading']		= $am_sub_heading;
		$post_meta['am_short_description']	= $am_short_description;
		$post_meta['am_web_url']			= $am_web_url;
		$post_meta['am_memberships_name']	= $am_memberships_name;
		
		if( !empty($enable_options) && $enable_options === 'yes' ){
			$post_meta['am_phone_numbers']		= $am_phone_numbers;
		}
		
		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);
		
		$display_name	= !empty($display_name) ? $display_name : $am_first_name.' '.$am_last_name;
		
        //Update Doctor Post        
        $doctor_user = array(
            'ID'           => $post_id,
            'post_title'   => $display_name,
            'post_content' => $content,
        );
		wp_update_post( $doctor_user );
		
		//update languages
		$lang		= array();
		if( !empty( $_POST['settings']['languages'] ) ){
			foreach( $_POST['settings']['languages'] as $key => $item ){
				$lang[] = $item;
			}
		}
		
		wp_set_post_terms($post_id, $lang, 'languages');
		
		
		//Profile avatar
        $profile_avatar = array();
        if( !empty( $_POST['basics']['avatar']['attachment_id'] ) ){
            $profile_avatar = $_POST['basics']['avatar'];
        } else {                                
            if( !empty( $_POST['basics']['avatar'] ) ){
                $profile_avatar = doccure_temp_upload_to_media($_POST['basics']['avatar'], $post_id);
            }
        }
		//delete prevoius attachment ID
		$pre_attachment_id = get_post_thumbnail_id($post_id);
		if ( !empty($pre_attachment_id) && !empty( $profile_avatar['attachment_id'] ) && intval($pre_attachment_id) != intval($profile_avatar['attachment_id'])) {
			wp_delete_attachment($pre_attachment_id, true);
		}
		
		//update thumbnail
		if (!empty($profile_avatar['attachment_id'])) {
			delete_post_thumbnail($post_id);
			set_post_thumbnail($post_id, $profile_avatar['attachment_id']);
		} else {
			wp_delete_attachment( $pre_attachment_id, true );
		}
		
        $json['type']    = 'success';
        $json['message'] = esc_html__('Settings Updated.', 'doccure');        
        wp_send_json($json);
    }
            
    add_action('wp_ajax_doccure_update_doctor_profile', 'doccure_update_doctor_profile');
    add_action('wp_ajax_nopriv_doccure_update_doctor_profile', 'doccure_update_doctor_profile');
}

if( !function_exists( 'doccure_update_staff_profile' ) ){
    function doccure_update_staff_profile(){       
        global $current_user,$doccure_options;               
        $json = array();
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}	

        $required_fields = array(
            'am_first_name'   	=> esc_html__('First  name is required', 'doccure'),
			'am_last_name'  	=> esc_html__('Last name is required', 'doccure'),
			'am_mobile_number'  => esc_html__('Personal mobile number is required', 'doccure'),
        );

        foreach ($required_fields as $key => $value) {
           if( empty( $_POST[$key] ) ){
            $json['type'] 		= 'error';
            $json['message'] 	= $value;        
            wp_send_json($json);
           }
        }
		
		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);

		$enable_options		= !empty($doccure_options['doctors_contactinfo']) ? $doccure_options['doctors_contactinfo'] : '';
		
        //Form data
        $display_name 		= !empty($_POST['display_name']) ? ($_POST['display_name']) : '';
		$am_first_name 		= !empty($_POST['am_first_name']) ? ($_POST['am_first_name']) : '';
		$am_mobile_number 	= !empty($_POST['am_mobile_number']) ? ($_POST['am_mobile_number']) : '';
        $am_last_name  		= !empty($_POST['am_last_name'] ) ? ($_POST['am_last_name']) : '';
		$am_name_base  		= !empty($_POST['am_name_base'] ) ? ($_POST['am_name_base']) : '';
		$am_gender  		= !empty($_POST['am_gender'] ) ? ($_POST['am_gender']) : '';
		$am_web_url			= !empty( $_POST['am_web_url'] ) ?  $_POST['am_web_url']  : '';

		$am_sub_heading  		= !empty($_POST['am_sub_heading'] ) ? ($_POST['am_sub_heading']) : '';
		$am_starting_price  	= !empty($_POST['am_starting_price'] ) ? ($_POST['am_starting_price']) : '';
		$am_short_description  	= !empty($_POST['am_short_description'] ) ? sanitize_textarea_field($_POST['am_short_description']) : '';
		$am_memberships_name  	= !empty($_POST['am_memberships_name'] ) ? $_POST['am_memberships_name'] : array();
		$am_phone_numbers  		= !empty($_POST['am_phone_numbers'] ) ? $_POST['am_phone_numbers'] : array();
        $content				= !empty($_POST['content'] ) ? $_POST['content'] : '';
		$registration_number = isset($_POST['am_registration_number']) ? sanitize_text_field($_POST['am_registration_number']) : '';
		$am_document = isset($_POST['am_document']) ? sanitize_text_field($_POST['am_document']) : '';

		update_post_meta($post_id, 'am_gender', $am_gender);
		
		

		update_user_meta($user_id, '_registration_number', $registration_number);
 		update_user_meta($user_id, 'doc_number', $doc_number );
 		update_user_meta( $user_id, 'position', $am_sub_heading );

		
        //Update user meta
        update_user_meta($user_id, 'first_name', $am_first_name);
		update_user_meta($user_id, 'last_name', $am_last_name);
		update_user_meta($user_id, 'mobile_number', $am_mobile_number);
		

 wp_update_user([
    'ID' => $user_id,
    'display_name' => $display_name
]);

		
		$post_meta['am_first_name']		= $am_first_name;
		$post_meta['am_mobile_number']	= $am_mobile_number;
		$post_meta['am_last_name']		= $am_last_name;
		$post_meta['am_name_base']		= $am_name_base;
		$post_meta['am_gender']			= $am_gender;
		
		$post_meta['am_starting_price']		= $am_starting_price;
		$post_meta['am_sub_heading']		= $am_sub_heading;
		$post_meta['am_short_description']	= $am_short_description;
		$post_meta['am_web_url']			= $am_web_url;
		$post_meta['am_memberships_name']	= $am_memberships_name;
		
		if( !empty($enable_options) && $enable_options === 'yes' ){
			$post_meta['am_phone_numbers']		= $am_phone_numbers;
		}
		
		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);
		
		$display_name	= !empty($display_name) ? $display_name : $am_first_name.' '.$am_last_name;
		
        //Update Doctor Post        
        $doctor_user = array(
            'ID'           => $post_id,
            'post_title'   => $display_name,
            'post_content' => $content,
        );
		wp_update_post( $doctor_user );
		
		//update languages
		$lang		= array();
		if( !empty( $_POST['settings']['languages'] ) ){
			foreach( $_POST['settings']['languages'] as $key => $item ){
				$lang[] = $item;
			}
		}
		
		wp_set_post_terms($post_id, $lang, 'languages');
		
		
		//Profile avatar
        $profile_avatar = array();
        if( !empty( $_POST['basics']['avatar']['attachment_id'] ) ){
            $profile_avatar = $_POST['basics']['avatar'];
        } else {                                
            if( !empty( $_POST['basics']['avatar'] ) ){
                $profile_avatar = doccure_temp_upload_to_media($_POST['basics']['avatar'], $post_id);
            }
        }
		//delete prevoius attachment ID
		$pre_attachment_id = get_post_thumbnail_id($post_id);
		if ( !empty($pre_attachment_id) && !empty( $profile_avatar['attachment_id'] ) && intval($pre_attachment_id) != intval($profile_avatar['attachment_id'])) {
			wp_delete_attachment($pre_attachment_id, true);
		}
		
		//update thumbnail
		if (!empty($profile_avatar['attachment_id'])) {
			delete_post_thumbnail($post_id);
			set_post_thumbnail($post_id, $profile_avatar['attachment_id']);
		} else {
			wp_delete_attachment( $pre_attachment_id, true );
		}
		
        $json['type']    = 'success';
        $json['message'] = esc_html__('Settings Updated.', 'doccure');        
        wp_send_json($json);
    }
            
    add_action('wp_ajax_doccure_update_staff_profile', 'doccure_update_staff_profile');
    add_action('wp_ajax_nopriv_doccure_staff_doctor_profile', 'doccure_update_staff_profile');
}
/**
 * Update patient Profile

 */
if( !function_exists( 'doccure_update_patient_profile' ) ){
    function doccure_update_patient_profile(){       
        global $current_user,$doccure_options;               
        $json = array();
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges
		
		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}

        $required_fields = array(
            'am_first_name'   	=> esc_html__('First  name is required', 'doccure'),
			'am_last_name'  	=> esc_html__('Last name is required', 'doccure'),
			'am_mobile_number'  => esc_html__('Personal mobile number is required', 'doccure'),
        );

        foreach ($required_fields as $key => $value) {
           if( empty( $_POST[$key] ) ){
            $json['type'] 		= 'error';
            $json['message'] 	= $value;        
            wp_send_json($json);
           }
        }
		
		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);

        //Form data
        $display_name 		= !empty($_POST['display_name']) ? sanitize_text_field($_POST['display_name']) : '';
		$am_first_name 		= !empty($_POST['am_first_name']) ? sanitize_text_field($_POST['am_first_name']) : '';
		$am_mobile_number 	= !empty($_POST['am_mobile_number']) ? sanitize_text_field($_POST['am_mobile_number']) : '';
        $am_last_name  		= !empty($_POST['am_last_name'] ) ? sanitize_text_field($_POST['am_last_name']) : '';
		$am_name_base  		= !empty($_POST['am_name_base'] ) ? sanitize_text_field($_POST['am_name_base']) : '';
		

		$am_sub_heading  		= !empty($_POST['am_sub_heading'] ) ? sanitize_text_field($_POST['am_sub_heading']) : '';
		
		$am_short_description  	= !empty($_POST['am_short_description'] ) ? sanitize_textarea_field($_POST['am_short_description']) : '';
		
		$location 				= !empty($_POST['location']) ? doccure_get_term_by_type('slug',sanitize_text_field($_POST['location']),'locations' ): '';
		$address				= !empty($_POST['address'] ) ? $_POST['address'] : '';
		$longitude				= !empty($_POST['longitude'] ) ? $_POST['longitude'] : '';
		$latitude				= !empty($_POST['latitude'] ) ? $_POST['latitude'] : '';
		$content				= !empty($_POST['content'] ) ? $_POST['content'] : '';
		
		wp_set_post_terms( $post_id, $location, 'locations' );
		update_post_meta($post_id, '_address', $address);
		update_post_meta($post_id, '_longitude', $longitude);
		update_post_meta($post_id, '_latitude', $latitude);
		update_post_meta($post_id, '_mobile_number', $am_mobile_number);
				
        //Update user meta
        update_user_meta($user_id, 'first_name', $am_first_name);
		update_user_meta($user_id, 'last_name', $am_last_name);
		update_user_meta($user_id, 'mobile_number', $am_mobile_number);
		
		$post_meta['am_first_name']		= $am_first_name;
		$post_meta['am_mobile_number']	= $am_mobile_number;
		$post_meta['am_last_name']		= $am_last_name;
		$post_meta['am_name_base']		= $am_name_base;
		
		$post_meta['am_sub_heading']		= $am_sub_heading;
		$post_meta['am_short_description']	= $am_short_description;
		
		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);
		
        //Update Doctor Post        
        $doctor_user = array(
            'ID'           => $post_id,
            'post_title'   => $display_name,
            'post_content' => $content,
        );
		wp_update_post( $doctor_user );
		
		//Profile avatar
        $profile_avatar = array();
        if( !empty( $_POST['basics']['avatar']['attachment_id'] ) ){
            $profile_avatar = $_POST['basics']['avatar'];
        } else {                                
            if( !empty( $_POST['basics']['avatar'] ) ){
                $profile_avatar = doccure_temp_upload_to_media($_POST['basics']['avatar'], $post_id);
            }
        }
		//delete prevoius attachment ID
		$pre_attachment_id = get_post_thumbnail_id($post_id);
		if ( !empty($pre_attachment_id) && !empty( $profile_avatar['attachment_id'] ) && intval($pre_attachment_id) != intval($profile_avatar['attachment_id'])) {
			wp_delete_attachment($pre_attachment_id, true);
		}
		
		//update thumbnail
		if (!empty($profile_avatar['attachment_id'])) {
			delete_post_thumbnail($post_id);
			set_post_thumbnail($post_id, $profile_avatar['attachment_id']);
		} else {
			wp_delete_attachment( $pre_attachment_id, true );
		}
		
        $json['type']    = 'success';
        $json['message'] = esc_html__('Settings Updated.', 'doccure');        
        wp_send_json($json);
    }
            
    add_action('wp_ajax_doccure_update_patient_profile', 'doccure_update_patient_profile');
    add_action('wp_ajax_nopriv_doccure_update_patient_profile', 'doccure_update_patient_profile');
}

/**
 * Update doctor update booking

 */
if( !function_exists( 'doccure_update_doctor_booking_options' ) ){
    function doccure_update_doctor_booking_options(){  
		global $current_user;               
        $json = array();
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);
		$am_booking_contact	= !empty($_POST['am_booking_contact']) ? $_POST['am_booking_contact'] : '';
		$am_booking_detail	= !empty($_POST['am_booking_detail']) ? $_POST['am_booking_detail'] : '';
		$post_meta['am_booking_contact']	= $am_booking_contact;
		$post_meta['am_booking_detail']		= $am_booking_detail;
		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);
		$json['type']    = 'success';
		$json['message'] = esc_html__('Settings Updated.', 'doccure');        

		wp_send_json($json);
	}
	add_action('wp_ajax_doccure_update_doctor_booking_options', 'doccure_update_doctor_booking_options');
    add_action('wp_ajax_nopriv_doccure_update_doctor_booking_options', 'doccure_update_doctor_booking_options');
}
/**
 * Update doctor Profile Education & Exprience

 */
if( !function_exists( 'doccure_update_doctor_education' ) ){
    function doccure_update_doctor_education(){       
        global $current_user;               
        $json = array();
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges
		
		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$post_meta		= doccure_get_post_meta($post_id);
		$post_type		= get_post_type($post_id);
		
		if( $_POST['am_education']) {
			$post_meta['am_education']	= $_POST['am_education'];
			update_post_meta($post_id, 'am_education_data', $post_meta['am_education']);
		}else{
			$post_meta['am_education']	= array();
			update_post_meta($post_id, 'am_education_data', $post_meta['am_education']);
		}

		if( $_POST['am_experiences']) {
			$post_meta['am_experiences']	= $_POST['am_experiences'];
			// Store the actual experiences array in am_experiences_data
			update_post_meta($post_id, 'am_experiences_data', $post_meta['am_experiences']);
		}else{
			$post_meta['am_experiences']	= array();
			update_post_meta($post_id, 'am_experiences_data', $post_meta['am_experiences']);
		}

		if( $_POST['am_education']) {
			$post_meta['am_education']	= $_POST['am_education'];
			update_post_meta($post_id, 'am_doctors_data', $post_meta);
		}else{
			$post_meta['am_education']	= array();
			update_post_meta($post_id, 'am_doctors_data', $post_meta);
		}
		
		if( $_POST['am_experiences']) {
			$post_meta['am_experiences']	= $_POST['am_experiences'];
			update_post_meta($post_id, 'am_doctors_data', $post_meta);    
		}else{
			$post_meta['am_experiences']	= array();
			update_post_meta($post_id, 'am_doctors_data', $post_meta);    
		}
		
 		$json['type']    = 'success';
		$json['message'] = esc_html__('Settings Updated.', 'doccure');        
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_update_doctor_education', 'doccure_update_doctor_education');
    add_action('wp_ajax_nopriv_doccure_update_doctor_education', 'doccure_update_doctor_education');
}

/**
 * Update doctor Profile Education & Exprience

 */
if( !function_exists( 'doccure_update_doctor_award' ) ){
    function doccure_update_doctor_award(){       
        global $current_user;               
        $json = array();
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$dc_downloads	= doccure_is_feature_value( 'dc_downloads', $user_id);
		$dc_awards		= doccure_is_feature_value( 'dc_awards', $user_id);

		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);
		$awards			= !empty( $_POST['am_award'] ) ? $_POST['am_award'] : array();
		$download_files	= !empty( $_POST['am_downloads'] ) ? $_POST['am_downloads'] : array();

		$dc_total_files		= count($download_files);
		$dc_total_awards	= count($awards);

		$dc_total_files		= !empty($dc_total_files) ? intval($dc_total_files) : 0;
		$dc_total_awards	= !empty($dc_total_awards) ? intval($dc_total_awards) : 0;
		
		if( !empty( $awards ) ) {
			foreach( $awards as $key => $award ) {
				if( empty( $award['title'] ) ){
					$json['type'] 		= 'error';
					$json['message'] 	= esc_html__('Award title is required', 'doccure');
					wp_send_json($json);
				}
			}
		}

		// if(empty($dc_downloads) || $dc_total_files > $dc_downloads ){
		// 	$json['type'] 		= 'error';
		// 	$json['message'] 	= esc_html__('Your package limit for submitting downloads has reached to maximum. Please upgrade or buy package to submit more downloads.', 'doccure');
		// 	wp_send_json($json);
		// }

		if( empty($dc_awards) || $dc_total_awards > $dc_awards ){
			$json['type'] 		= 'error';
			$json['message'] 	= esc_html__('Your package limit for submitting awards has reached to maximum. Please upgrade or buy package to submit more awards.', 'doccure');
			wp_send_json($json);
		}
		
		$post_meta['am_award']	= $awards;
		
		if( $download_files || $awards ) {
			$downloads	= $download_files;
			
			if( !empty( $downloads ) ) {
				$download_array	= array();
				foreach( $downloads as $key => $download ) {
					
					if( !empty( $_POST['am_downloads'][$key]['attachment_id'] ) ){
						$download_array[$key]['media'] 			= $download['media'];
						$download_array[$key]['id'] 			= $download['attachment_id'];
						
					} else {
						$new_uploaded_file 						= doccure_temp_upload_to_media($download['media'], $post_id);
						$download_array[$key]['media'] 			= $new_uploaded_file['url'];
						$download_array[$key]['id'] 			= $new_uploaded_file['attachment_id'];
					}
				}
				
			}
			
			
			$post_meta['am_downloads']	= $download_array;
			update_post_meta($post_id, 'am_award', $awards);
			update_post_meta($post_id, 'am_awards_data', $post_meta['am_downloads']);
			update_post_meta($post_id, 'am_doctors_data', $post_meta);
			$json['type']    = 'success';
			$json['message'] = esc_html__('Settings Updated.', 'doccure');   
		}
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_update_doctor_award', 'doccure_update_doctor_award');
    add_action('wp_ajax_nopriv_doccure_update_doctor_award', 'doccure_update_doctor_award');
}

/**
 * Update doctor Profile Education & Exprience

 */
if( !function_exists( 'doccure_update_ap_location' ) ){
    function doccure_update_ap_location(){   
        global $current_user,$doccure_options;               
        $json = array();
		$post_id		= !empty( $_POST['post_id'] ) ? sanitize_text_field($_POST['post_id']) : '';
		$services		= !empty( $_POST['service'] ) ? $_POST['service'] : array();
		$consultant_fee	 = !empty( $_POST['consultant_fee'] ) ? sanitize_text_field( $_POST['consultant_fee'] ) : 0;
		$user_id		 = $current_user->ID;
		
		if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges
		
		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}

		if( empty($post_id)) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Something went wrong, please contact to administrator', 'doccure');
            wp_send_json($json);
		}

		$allow_consultation_zero	 = !empty( $doccure_options['allow_consultation_zero'] ) ? $doccure_options['allow_consultation_zero'] : 'no';

		if( !empty($allow_consultation_zero) && $allow_consultation_zero === 'no' ){
			if( empty($consultant_fee)) {
				$json['type'] 		= 'error';
				$json['message'] 	= esc_html__('Consultation fee is required', 'doccure');
				wp_send_json($json);
			}
		}

        $post_author	= get_post_field('post_author', $post_id);
		
		if( $post_author != $user_id) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('You are not an authorized user to update this.', 'doccure');
            wp_send_json($json);
		}
		
		if( !empty( $post_id ) ){
			update_post_meta( $post_id ,'_consultant_fee',$consultant_fee);
			update_post_meta( $post_id,'_team_services',$services);
			$json['type']    = 'success';
			$json['message'] = esc_html__('Settings have been updated', 'doccure');

			wp_send_json($json);
		}
		 
    }
            
    add_action('wp_ajax_doccure_update_ap_location', 'doccure_update_ap_location');
    add_action('wp_ajax_nopriv_doccure_update_ap_location', 'doccure_update_ap_location');
}

/**
 * Update doctor Profile Education & Exprience

 */
if( !function_exists( 'doccure_update_ap_services' ) ){
    function doccure_update_ap_services(){              
		global $current_user,$doccure_options;               
        $json = array();
		$post_id		= !empty( $_POST['post_id'] ) ? sanitize_text_field($_POST['post_id']) : '';
		$services		= !empty( $_POST['service'] ) ? $_POST['service'] : array();
		$consultant_fee	 = !empty( $_POST['consultant_fee'] ) ? sanitize_text_field( $_POST['consultant_fee'] ) : 0;
		$allow_consultation_zero	 = !empty( $doccure_options['allow_consultation_zero'] ) ? $doccure_options['allow_consultation_zero'] : 'no';
		$user_id		 = $current_user->ID;
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges
		
		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}

		if( empty($post_id)) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Something went wrong, please contact to administrator', 'doccure');
            wp_send_json($json);
		}
		
		if(!empty($allow_consultation_zero) && allow_consultation_zero === 'no'){
			if( empty($consultant_fee)) {
				$json['type'] 		= 'error';
				$json['message'] 	= esc_html__('Consultation fee is required', 'doccure');
				wp_send_json($json);
			}
		}
		
        $post_author	= get_post_field('post_author', $post_id);
		
		if( $post_author != $user_id) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('You are not an authorized user to update this.', 'doccure');
            wp_send_json($json);
		}
		
		if( !empty( $post_id ) ){
			update_post_meta( $post_id ,'_consultant_fee',$consultant_fee);
			update_post_meta( $post_id,'_team_services',$services);
			$json['type']    = 'success';
			$json['message'] = esc_html__('Providing Services are Updated.', 'doccure');

			wp_send_json($json);
		}
		 
    }
            
    add_action('wp_ajax_doccure_update_ap_services', 'doccure_update_ap_services');
    add_action('wp_ajax_nopriv_doccure_update_ap_services', 'doccure_update_ap_services');
}

 
/**
 * Update doctor Profile Education & Exprience

 */
if( !function_exists( 'doccure_update_gallery' ) ){
    function doccure_update_gallery(){       
        global $current_user, $post;               
        $json 				= array();
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if( function_exists('doccure_check_video_url') ){
			if( !empty($_POST['am_videos']) ){
				foreach( $_POST['am_videos'] as $video_url ){
					$check_video = doccure_check_video_url($video_url);
					if( empty($check_video) || $check_video === false ){
						$json['type'] 		= 'error';
						$json['message'] 	= esc_html__('Please add valid video URL','doccure');        
						wp_send_json($json);
					}
					
				}
			}
		}
		
		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);
		$am_gallery		= !empty($_POST['am_gallery']) ? $_POST['am_gallery'] : array();
		$am_videos		= !empty($_POST['am_videos']) ? $_POST['am_videos'] : array();
		$gallery		= !empty($_POST['gallery']['images_gallery_new']) ? $_POST['gallery']['images_gallery_new'] : array();

		if( !empty($am_gallery) || !empty( $gallery ) ) {
			$post_meta['am_gallery']	= $am_gallery;
			if( !empty( $gallery ) ){
				$new_index	= !empty($post_meta['am_gallery']) ?  max(array_keys($post_meta['am_gallery'])) : 0;
				foreach( $gallery as $new_gallery ){
					$new_index ++;
					$profile_gallery 							= doccure_temp_upload_to_media($new_gallery, $post_id);
					$post_meta['am_gallery'][$new_index]		= $profile_gallery;
				}
			}
		}else{
			$post_meta['am_gallery']	= array();
		}
		
		
		$post_meta['am_videos']	= $am_videos;
		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);  
		
		$json['type']    = 'success';
		$json['message'] = esc_html__('Settings Updated.', 'doccure');    
		
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_update_gallery', 'doccure_update_gallery');
    add_action('wp_ajax_nopriv_doccure_update_gallery', 'doccure_update_gallery');
}


/**
 * Update doctor social profiles

 */
if( !function_exists( 'doccure_social_profiles' ) ){
    function doccure_social_profiles(){       
        global $current_user, $post;               
        $json 				= array();
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges
		
		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}

		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);
		$post_meta['am_socials']		= !empty($_POST['basics']) ? $_POST['basics'] : array();
		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);  
		
		$json['type']    = 'success';
		$json['message'] = esc_html__('Settings Updated.', 'doccure');    
		
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_social_profiles', 'doccure_social_profiles');
    add_action('wp_ajax_nopriv_doccure_social_profiles', 'doccure_social_profiles');
}
/**
 * Update doctor Profile Education & Exprience

 */
if( !function_exists( 'doccure_update_doctor_registrations' ) ){
    function doccure_update_doctor_registrations(){       
        global $current_user, $post;               
        $json 				= array();
		$am_documents_array	= array();
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$post_meta		= doccure_get_post_meta( $post_id);
		$post_type		= get_post_type($post_id);
		
		if( $_POST['am_registration_number']) {
			$post_meta['am_registration_number']	= $_POST['am_registration_number'];      
		}
		
		if( $_POST['am_document']) {
			$am_documents	= $_POST['am_document'];
			if( !empty( $am_documents ) ) {
				if( !array_key_exists("id",$am_documents) && !empty(  $am_documents['url']  ) ) {
					$uploaded_file 					= doccure_temp_upload_to_media($am_documents['url'], $post_id);
					$am_documents_array['url'] 			= $uploaded_file['url'];
					$am_documents_array['id'] 			= $uploaded_file['attachment_id'];
				} else {
					$am_documents_array['url'] 			= $am_documents['url'];
					$am_documents_array['id'] 			= $am_documents['id'];
				}
			}
			
			$post_meta['am_document']	= $am_documents_array;
			update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);
			$json['type']    = 'success';
			$json['message'] = esc_html__('Settings Updated.', 'doccure');        
		}
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_update_doctor_registrations', 'doccure_update_doctor_registrations');
    add_action('wp_ajax_nopriv_doccure_update_doctor_registrations', 'doccure_update_doctor_registrations');
}

/**
 * Update doctor Profile Education & Exprience

 */
if( !function_exists( 'doccure_update_specialities' ) ){
    function doccure_update_specialities(){       
        global $current_user;               
        
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$json 		= array();
		$meta_data 	= array();

		$user_id	= $current_user->ID;
		$post_id  	= doccure_get_linked_profile_id($user_id);
		$dc_services		= doccure_is_feature_value( 'dc_services', $user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$post_meta		= doccure_get_post_meta( $post_id );
		$post_type		= get_post_type($post_id);
		$post_meta		= !empty( $post_meta ) ? $post_meta : array();
		$specialities	= !empty( $_POST['am_specialities'] ) ? $_POST['am_specialities'] : array();

		
		$specialities_array	= array();
		
		if( !empty( $specialities ) ){
			foreach( $specialities as $keys => $vals ){
				if( !empty( $vals['speciality_id'] ) ){
					$specialities_array[] = $vals['speciality_id'];
					$meta_data[$vals['speciality_id']] = array();
					$service			= array();
					if( !empty( $vals['services'] ) ) {
						foreach( $vals['services'] as $key => $val ) {
							if( !empty( $val['service'] ) ){
								$service[] = $val['service'];
								$meta_data[$vals['speciality_id']][$val['service']] = $val;
								
								if( !empty($post_type) && ($post_type ==='doctors') ){
									$service_count	= count($service);
									$service_count	= !empty($service_count) ? intval($service_count) : 0;
									$dc_services	= doccure_is_feature_value( 'dc_services', $user_id);
									if( ( empty($dc_services) ) || ( $service_count > $dc_services )  ){
										$json['type'] 		= 'error';
										$json['message'] 	= sprintf( esc_html__('Your package has a limit(%s) for submitting services under the speciality', 'doccure'),$dc_services);
										wp_send_json($json);
									} 
								}
							}
						}
					}
				}
			}
		}

		$post_meta['am_specialities']	= $meta_data;
		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);
		
		if( !empty( $service ) ){
			wp_set_post_terms( $post_id, $service, 'services' );
		}
		
		if( !empty( $specialities_array ) ){
			wp_set_post_terms( $post_id, $specialities_array, 'specialities' );
		}
		
		$json['type']    = 'success';
		$json['message'] = esc_html__('Services are Updated.', 'doccure');
		
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_update_specialities', 'doccure_update_specialities');
    add_action('wp_ajax_nopriv_doccure_update_specialities', 'doccure_update_specialities');
}

/**
 * Update account settings

 */
if( !function_exists( 'doccure_update_account_settings' ) ){
    function doccure_update_account_settings(){       
        global $current_user, $post;               
        $json = array();
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}

		$post_type		= get_post_type($post_id);
		$settings		= doccure_get_account_settings($post_type);
		
		if( !empty( $settings ) ){
			foreach( $settings as $key => $value ){
				$save_val 	= !empty( $_POST['settings'][$key] ) ? $_POST['settings'][$key] : '';
				$db_val 	= !empty( $save_val ) ?  $save_val : 'off';
				update_post_meta($post_id, $key, $db_val);
			}
			$json['type']    = 'success';
			$json['message'] = esc_html__('Account settings are Updated.', 'doccure'); 
		}
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_update_account_settings', 'doccure_update_account_settings');
    add_action('wp_ajax_nopriv_doccure_update_account_settings', 'doccure_update_account_settings');
}

/**
 * Update hospitals Profile

 */
if( !function_exists( 'doccure_update_hospitals_profile' ) ){
    function doccure_update_hospitals_profile(){       
        global $current_user, $post;               
		$json 			= array();
		$post_meta 		= array();
		$user_id		 = $current_user->ID;
		$post_id  		 = doccure_get_linked_profile_id($user_id);
		
        if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}

        $required_fields = array(
            'am_first_name'   	=> esc_html__('First  name is required', 'doccure'),
            'am_last_name'  	=> esc_html__('Last name is required', 'doccure'),
        );

        foreach ($required_fields as $key => $value) {
           if( empty( $_POST[$key] ) ){
            $json['type'] 		= 'error';
            $json['message'] 	= $value;        
            wp_send_json($json);
           }
        }
		$post_type		= get_post_type($post_id);
		$post_meta		= get_post_meta($post_id, 'am_' . $post_type . '_data',true);
		
		
		if( !empty( $post_type ) && $post_type === 'hospitals' ){
			$post_meta['am_week_days']		= !empty( $_POST['am_week_days'] ) ?  $_POST['am_week_days']  : array();
			$post_meta['am_mobile_number']	= !empty( $_POST['am_mobile_number'] ) ?  $_POST['am_mobile_number']  : '';
			$post_meta['am_phone_numbers']	= !empty( $_POST['am_phone_numbers'] ) ?  $_POST['am_phone_numbers']  : array();
			$post_meta['am_web_url']		= !empty( $_POST['am_web_url'] ) ?  $_POST['am_web_url']  : '';

			$post_meta['am_availability']	= !empty( $_POST['am_availability'] ) ? sanitize_text_field( $_POST['am_availability'] ) : '';
			$post_meta['am_sub_heading']	= !empty( $_POST['am_sub_heading'] ) ? sanitize_text_field( $_POST['am_sub_heading'] ) : '';
			
			if( !empty( $_POST['am_other_time'] ) ) {
				$post_meta['am_other_time']	= sanitize_text_field( $_POST['am_other_time'] );
			} else {
				$post_meta['am_other_time']	= '';
			}
		}
		
        //Form data
        $am_first_name 			= !empty($_POST['am_first_name']) ? sanitize_text_field($_POST['am_first_name']) : '';
        $am_last_name  			= !empty($_POST['am_last_name'] ) ? sanitize_text_field($_POST['am_last_name']) : '';
		$am_short_description 	= !empty($_POST['am_short_description'] ) ? sanitize_text_field($_POST['am_short_description']) : '';
		$display_name 			= !empty($_POST['display_name']) ? sanitize_text_field($_POST['display_name']) : '';
		$location 				= !empty($_POST['location']) ? doccure_get_term_by_type('slug',sanitize_text_field($_POST['location']),'locations' ): '';
		$address				= !empty($_POST['address'] ) ? $_POST['address'] : '';
		$longitude				= !empty($_POST['longitude'] ) ? $_POST['longitude'] : '';
		$latitude				= !empty($_POST['latitude'] ) ? $_POST['latitude'] : '';
		$am_sub_heading  		= !empty($_POST['am_sub_heading'] ) ? sanitize_text_field($_POST['am_sub_heading']) : '';
        $content				= !empty($_POST['content'] ) ? $_POST['content'] : '';
		
        //Update user meta
        update_user_meta($user_id, 'first_name', $am_first_name);
        update_user_meta($user_id, 'last_name', $am_last_name);
		
		$post_meta['am_first_name']			= $am_first_name;
		$post_meta['am_last_name']			= $am_last_name;
		$post_meta['am_sub_heading']		= $am_sub_heading;
		$post_meta['am_short_description']	= $am_short_description;

		update_post_meta($post_id, 'am_' . $post_type . '_data', $post_meta);
		
		wp_set_post_terms( $post_id, $location, 'locations' );
		update_post_meta($post_id, '_address', $address);
		update_post_meta($post_id, '_longitude', $longitude);
		update_post_meta($post_id, '_latitude', $latitude);
		
        //Update Hospital Post        
        $hospital_profile = array(
            'ID'           => $post_id,
            'post_title'   => $display_name,
            'post_content' => $content,
        );
		wp_update_post( $hospital_profile );
		
		//update languages
		$lang		= array();
		if( !empty( $_POST['settings']['languages'] ) ){
			foreach( $_POST['settings']['languages'] as $key => $item ){
				$lang[] = $item;
			}
		}
		
		wp_set_post_terms($post_id, $lang, 'languages');
		
		//Profile avatar
        $profile_avatar = array();
        if( !empty( $_POST['basics']['avatar']['attachment_id'] ) ){
            $profile_avatar = $_POST['basics']['avatar'];
        } else {                                
            if( !empty( $_POST['basics']['avatar'] ) ){
                $profile_avatar = doccure_temp_upload_to_media($_POST['basics']['avatar'], $post_id);
            }
        }
		
		//delete prevoius attachment ID
		$pre_attachment_id = get_post_thumbnail_id($post_id);
		if ( !empty($pre_attachment_id) && !empty( $profile_avatar['attachment_id'] ) && intval($pre_attachment_id) != intval($profile_avatar['attachment_id'])) {
			wp_delete_attachment($pre_attachment_id, true);
		}
		
		//update thumbnail
		if (!empty($profile_avatar['attachment_id'])) {
			delete_post_thumbnail($post_id);
			set_post_thumbnail($post_id, $profile_avatar['attachment_id']);
		} else {
			wp_delete_attachment( $pre_attachment_id, true );
		}
		
        $json['type']    = 'success';
        $json['message'] = esc_html__('Settings Updated.', 'doccure');        
        wp_send_json($json);
    }
            
    add_action('wp_ajax_doccure_update_hospitals_profile', 'doccure_update_hospitals_profile');
    add_action('wp_ajax_nopriv_doccure_update_hospitals_profile', 'doccure_update_hospitals_profile');
}
/**
 * delete account

 */
if ( !function_exists( 'doccure_user_by_email' ) ) {
	function doccure_user_by_email() {
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in
		
		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$json	= array();
		$email	= !empty( $_POST['email'] ) ? is_email( $_POST['email'] )  : '';
		
		if( empty($email) ){
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Email address is invalid','doccure');        
            wp_send_json($json);
		} else {
			
			$user_info 		= get_user_by('email',$email);
			
			$user_type		= !empty($user_info->roles[0]) ? $user_info->roles[0] : '';
			if( !empty($user_type) && $user_type !='regular_users' ){
				$json['type'] 			= 'success';
				$json['success_type'] 	= 'other';
				$json['message'] 		= esc_html__('This email address is being used for one of the other user other than patient. Please user another email address to find or add patient.','doccure');
			} else if(!empty($user_info) && $user_type ==='regular_users' ){
				$last_name	= get_user_meta($user_info->ID, 'last_name', true );
				$first_name	= get_user_meta($user_info->ID, 'first_name', true );
				$mobile_number	= get_user_meta($user_info->ID, 'mobile_number', true );
				$json['type'] 			= 'success';
				$json['success_type'] 	= 'registered';
				$json['first_name'] 	= !empty($first_name) ? $first_name :'';
				$json['last_name'] 		= !empty($last_name) ? $last_name : '';
				$json['mobile_number'] 	= !empty($mobile_number) ? $mobile_number : '';
				$json['user_id'] 		= $user_info->ID;
				$json['message'] 		= esc_html__('Patient exist','doccure');
			} else {
				$json['type'] 			= 'success';
				$json['success_type'] 	= 'new';
			}
			wp_send_json($json);
		}
	}
	add_action('wp_ajax_doccure_user_by_email', 'doccure_user_by_email');
    add_action('wp_ajax_nopriv_doccure_user_by_email', 'doccure_user_by_email');
}
/**
 * delete account

 */
if ( !function_exists( 'doccure_delete_account' ) ) {

	function doccure_delete_account() {
		global $current_user;
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$post_id	= doccure_get_linked_profile_id($current_user->ID);
		$user 		= wp_get_current_user(); //trace($user);
		$json 		= array();

		if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$required = array(
            'password'   	=> esc_html__('Password is required', 'doccure'),
            'retype'  		=> esc_html__('Retype your password', 'doccure'),
            'reason' 		=> esc_html__('Select reason to delete your account', 'doccure'),
        );

        foreach ($required as $key => $value) {
           if( empty( sanitize_text_field($_POST['delete'][$key] ) )){
            $json['type'] = 'error';
            $json['message'] = $value;        
            wp_send_json($json);
           }
        }
		
		$password	= !empty( $_POST['delete']['password'] ) ? sanitize_text_field( $_POST['delete']['password'] )  : '';
		$retype		= !empty( $_POST['delete']['retype'] ) ? sanitize_text_field( $_POST['delete']['retype'] )  : '';
		if (empty($password) || empty($retype)) {
            $json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Please add your password and retype password.', 'doccure');
            wp_send_json( $json );
        }
		
		$user_name 	 = doccure_get_username($user->data->ID);
		$user_email	 = $user->user_email;
        $is_password = wp_check_password($password, $user->user_pass, $user->data->ID);
		
	
		if( $is_password ){
			wp_delete_user($user->data->ID);
			wp_delete_post($post_id,true);
			
			extract($_POST['delete']);
			$reason		 = doccure_get_account_delete_reasons($reason);
			
			//Send email to users
			if (class_exists('doccure_Email_helper')) {
				if (class_exists('doccureDeleteAccount')) {
					$email_helper 	= new doccureDeleteAccount();
					$emailData 		= array();
					
					$emailData['username'] 			= esc_html( $user_name );
					$emailData['reason'] 			= esc_html( $reason );
					$emailData['email'] 			= esc_html( $user_email );
					$emailData['description'] 		= sanitize_textarea_field( $description );
					$email_helper->send($emailData);
				}
			}

			$json['type'] = 'success';
			$json['message'] = esc_html__('You account has been deleted.', 'doccure');

			wp_send_json( $json );
		} else{
			$json['type'] = 'error';
			$json['message'] = esc_html__('Password doesn\'t match', 'doccure');
			wp_send_json( $json );
		}
	}

	add_action( 'wp_ajax_doccure_delete_account', 'doccure_delete_account' );
	add_action( 'wp_ajax_nopriv_doccure_delete_account', 'doccure_delete_account' );
}

/**
 * Update User Password

 */
if (!function_exists('doccure_change_user_password')) {

    function doccure_change_user_password() {
        global $current_user;
        $user_identity = $current_user->ID;
        $json = array();
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
        if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$old_password	= !empty( $_POST['password'] ) ? sanitize_text_field($_POST['password']) : '';
		$password		= !empty( $_POST['retype'] ) ? sanitize_text_field($_POST['retype']) : '';
		if( empty( $old_password ) || empty( $password ) ){
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Current and new password fields are required.', 'doccure');
            wp_send_json( $json );
		}
		
        $user 			= wp_get_current_user(); //trace($user);
        $is_password 	= wp_check_password($old_password, $user->user_pass, $user->data->ID);

        if ($is_password) {

            if (empty($old_password) ) {
                $json['type'] 		= 'error';
                $json['message'] 	= esc_html__('Please add your new password.', 'doccure');
             } else {
				wp_update_user(array('ID' => $user_identity, 'user_pass' => $password));
				$json['type'] 		= 'success';
				$json['message'] 	= esc_html__('Password Updated.', 'doccure');
			}
			
        } else {
            $json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Old Password doesn\'t matched with the existing password', 'doccure');
        }

       wp_send_json( $json );
    }

    add_action('wp_ajax_doccure_change_user_password', 'doccure_change_user_password');
    add_action('wp_ajax_nopriv_doccure_change_user_password', 'doccure_change_user_password');
}


/**
 * Update User email

 */
if (!function_exists('doccure_change_user_email')) {

    function doccure_change_user_email() {
		global $current_user;
		$user_identity = $current_user->ID;
		$useremail	= !empty( $_POST['useremail'] ) ? sanitize_text_field($_POST['useremail']) : '';
		$json = array();

		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent

		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if(!is_email($useremail)){
			$json['type'] 		= 'error';
			$json['message'] 	= esc_html__('Please add a valid email address', 'doccure');
			wp_send_json( $json );
		}

		$user_data	= wp_update_user(array('ID' => $user_identity, 'user_email' => $useremail));

		if ( is_wp_error( $user_data ) ) {
			$error_string = $user_data->get_error_message();
			$json['type'] 		= 'error';
			$json['message'] 	= $error_string;
			wp_send_json( $json );
		} else {
			$json['type'] 		= 'success';
			$json['message'] 	= esc_html__('Email has been updated', 'doccure');
			wp_send_json( $json );
		}
		

		wp_send_json( $json );
    }

    add_action('wp_ajax_doccure_change_user_email', 'doccure_change_user_email');
    add_action('wp_ajax_nopriv_doccure_change_user_email', 'doccure_change_user_email');
}

/**
 * Remove single Save item

 */
if ( !function_exists( 'doccure_remove_save_item' ) ) {

	function doccure_remove_save_item() {
		$json			=  array();
		$post_id		= !empty( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : '';
		$item_id		= !empty( $_POST['item_id'] ) ? array(intval( $_POST['item_id'] )) : array();
		$item_type		= !empty( $_POST['item_type'] ) ? ( $_POST['item_type'] ) : '';
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if( !empty($post_id) && !empty($item_type) && !empty($item_id) ){
			$save_items_ids		= get_post_meta( $post_id, $item_type, true);
			$updated_values 	= array_diff(  $save_items_ids , $item_id);
			update_post_meta( $post_id, $item_type, $updated_values );
			
			$json['type'] 		= 'success';
            $json['message'] 	= esc_html__('Remove save item successfully.', 'doccure');
            wp_send_json($json);
		} else{
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Some error occur, please try again later', 'doccure');
            wp_send_json($json);
		}		
	}

	add_action( 'wp_ajax_doccure_remove_save_item', 'doccure_remove_save_item' );
	add_action( 'wp_ajax_nopriv_doccure_remove_save_item', 'doccure_remove_save_item' );
}

/**
 * Remove Multiple Save item

 */
if ( !function_exists( 'doccure_remove_save_multipuleitems' ) ) {

	function doccure_remove_save_multipuleitems() {
		$json			=  array();
		$post_id		= !empty( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : '';
		$item_type		= !empty( $_POST['item_type'] ) ? sanitize_text_field( $_POST['item_type'] ) : '';
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_privileges') ) { 
			doccure_validate_privileges($post_id);
		} //if user is logged in and have privileges

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if( !empty($post_id) && !empty($item_type) && !empty($item_type) ){
			update_post_meta( $post_id, $item_type, '' );
			
			$json['type'] 		= 'success';
            $json['message'] 	= esc_html__('Remove save items successfully.', 'doccure');
            wp_send_json($json);
		} else{
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Some error occur, please try again later', 'doccure');
            wp_send_json($json);
		}		
	}

	add_action( 'wp_ajax_doccure_remove_save_multipuleitems', 'doccure_remove_save_multipuleitems' );
	add_action( 'wp_ajax_nopriv_doccure_remove_save_multipuleitems', 'doccure_remove_save_multipuleitems' );
}

/**
 * Add to Cart

 */
if ( !function_exists( 'doccure_update_cart' ) ) {

	function doccure_update_cart() {
		$json				=  array();
		$product_id		= !empty( $_POST['id'] ) ? intval( $_POST['id'] ) : '';
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if( !empty( $product_id )) {
			if ( class_exists('WooCommerce') ) {
			
				global $current_user, $woocommerce;
				
				$woocommerce->cart->empty_cart(); //empty cart before update cart
				$user_id			= $current_user->ID;
				$is_cart_matched	= doccure_matched_cart_items($product_id);
				
				if ( isset( $is_cart_matched ) && $is_cart_matched > 0) {
					$json = array();
					$json['type'] 			= 'success';
					$json['message'] 		= esc_html__('You have already in cart, We are redirecting to checkout', 'doccure');
					$json['checkout_url'] 	= wc_get_cart_url();
					wp_send_json($json);
				}
				
				$cart_meta					= array();
				$user_type					= doccure_get_user_type( $user_id );
				$pakeges_features			= doccure_get_pakages_features();
				
				if ( !empty ( $pakeges_features )) {
					
					foreach( $pakeges_features as $key => $vals ) {
						
						if( $vals['user_type'] === $user_type || $vals['user_type'] === 'common' ) {
							$item			= get_post_meta($product_id,$key,true);
							$text			=  !empty( $vals['text'] ) ? ' '.sanitize_text_field($vals['text']) : '';
							
							if( $key === 'dc_duration' ) {
								$feature 	= doccure_get_duration_types($item,'title');
							}else if( $key === 'dc_duration_days' ) {
								$pkg_duration	= get_post_meta($product_id,'dc_duration',true);
								$duration 		= doccure_get_duration_types($pkg_duration,'title');
								if( $duration === 'others') {
									$feature 	= doccure_get_duration_types($item,'value');
								} else {
									$feature	= '';	
									$key		= '';
								}
							}else {
								$feature 	= $item;
							}
							
							if( !empty( $key )){
								$cart_meta[$key]	= $feature.$text;
							}
						}
					}
				}
				
				$cart_data = array(
					'product_id' 		=> $product_id,
					'cart_data'     	=> $cart_meta,
					'payment_type'     	=> 'subscription',
				);

				$woocommerce->cart->empty_cart();
				$cart_item_data = $cart_data;
				WC()->cart->add_to_cart($product_id, 1, null, null, $cart_item_data);

				$json = array();
				$json['type'] 			= 'success';
				$json['message'] 		= esc_html__('Please wait you are redirecting to checkout page.', 'doccure');
				$json['checkout_url']	= wc_get_cart_url();
				wp_send_json($json);
			} else {
				$json = array();
				$json['type'] 		= 'error';
				$json['message'] 	= esc_html__('Please install WooCommerce plugin to process this order', 'doccure');
			}
			
		} else{
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Some error occur, please try again later', 'doccure');
            wp_send_json($json);
		}	
		
	}

	add_action( 'wp_ajax_doccure_update_cart', 'doccure_update_cart' );
	add_action( 'wp_ajax_nopriv_doccure_update_cart', 'doccure_update_cart' );
}

/**
 * FAQ support

 */
if ( !function_exists( 'doccure_support_faq' ) ) {

	function doccure_support_faq() {
		$json			=  array();
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		$query_type		= !empty( $_POST['query_type'] ) ? $_POST['query_type'] : '';
		$details		= !empty( $_POST['details'] ) ? $_POST['details'] : '';
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if( empty($details) ) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Message is required.', 'doccure');
            wp_send_json($json);
		} else if( empty($query_type) ) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Query type is required.', 'doccure');
            wp_send_json($json);
		}else if( !empty(details) && !empty($query_type) ){
			$json['type'] 		= 'success';
            $json['message'] 	= esc_html__('Remove save items successfully.', 'doccure');
            wp_send_json($json);
		} else{
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Some error occur, please try again later', 'doccure');
            wp_send_json($json);
		}
		
	}

	add_action( 'wp_ajax_doccure_support_faq', 'doccure_support_faq' );
	add_action( 'wp_ajax_nopriv_doccure_support_faq', 'doccure_support_faq' );
}






/**
 * follow action

 */
if ( !function_exists( 'doccure_follow_doctors' ) ) {

	function doccure_follow_doctors() {
		global $current_user;
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$post_id = !empty( $_POST['id'] ) ? intval( $_POST['id'] ) : '';
		$json = array();

		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$linked_profile   	= doccure_get_linked_profile_id($current_user->ID);
		$post_type			= get_post_type($post_id);
		$post_key			= '_saved_'.$post_type;
		$saved_doctors 		= get_post_meta($linked_profile, $post_key, true);
		
		$json       = array();
        $wishlist   = array();
        $wishlist   = !empty( $saved_doctors ) && is_array( $saved_doctors ) ? $saved_doctors : array();

        if (!empty($post_id)) {
            if( in_array($post_id, $wishlist ) ){             
                $json['type'] 		= 'error';
                $json['message'] 	= esc_html__('This is already to your Favorites', 'doccure');
                wp_send_json( $json );
            } else {
				$wishlist[] = $post_id;
				$wishlist   = array_unique( $wishlist );
				update_post_meta( $linked_profile, $post_key, $wishlist );

				$json['type'] 		= 'success';
				$json['message'] 	= esc_html__('Successfully! added to your Favorites', 'doccure');
				wp_send_json( $json );
			}
        }
        
        $json['type'] = 'error';
        $json['message'] = esc_html__('Oops! something is going wrong.', 'doccure');
        wp_send_json( $json );
	}

	add_action( 'wp_ajax_doccure_follow_doctors', 'doccure_follow_doctors' );
	add_action( 'wp_ajax_nopriv_doccure_follow_doctors', 'doccure_follow_doctors' );
}

/**
 * add question 

 */
if( !function_exists( 'doccure_question_submit' ) ){
    function doccure_question_submit(){       
        global $current_user, $post, $doccure_options;               
        $json = array();
		
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		$user_id		 	= $current_user->ID;
		$post_setting		= !empty( $doccure_options['forum_question_status'] ) ?  $doccure_options['forum_question_status'] : 'pending';
		
		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

        //security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if( empty($_POST['speciality']) || empty($_POST['title']) || empty($_POST['description']) ) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('All fields are required.', 'doccure');
            wp_send_json($json);
		}
		
		if( empty($current_user)) {
			$json['type'] 		= 'error';
            $json['message'] 	= esc_html__('Please login to submit question.', 'doccure');
            wp_send_json($json);
		}
		
		$post_title			= !empty( $_POST['title'] ) ? sanitize_text_field( $_POST['title']) : '';
		$post_content		= !empty( $_POST['description'] ) ?  $_POST['description'] : '';
		$speciality			= !empty( $_POST['speciality'] ) ? $_POST['speciality'] : array(0);
		
		if(! empty( $post_title ) ){
			$post_array['post_title']		= $post_title;
			$post_array['post_content']		= $post_content;
			$post_array['post_author']		= $user_id;
			$post_array['post_type']		= 'healthforum';
			$post_array['post_status']		= $post_setting;
			$post_id 						= wp_insert_post($post_array);
			
			//Send email to user
			if (class_exists('doccure_Email_helper')) {
				if (class_exists('doccureForum')) {
					$term_data	= get_term_by('slug',$speciality,'specialities' );
					$email_helper = new doccureForum();
					$emailData = array();
					$emailData['question'] = $post_title;
					$emailData['description']  	= $post_content;
					$emailData['name']     		= doccure_get_username( $user_id );
					$emailData['category']      = !empty($term_data->name) ? $term_data->name : $speciality;
					$email_helper->send($emailData);
				}
			} 
			
			if( $post_id ) {
				wp_set_object_terms($post_id,$speciality,'specialities');
				$json['type']    = 'success';
        		$json['message'] = esc_html__('Question is submitted successfully.', 'doccure');    
			}
		}
		
		wp_send_json($json);
        
    }
            
    add_action('wp_ajax_doccure_question_submit', 'doccure_question_submit');
    add_action('wp_ajax_nopriv_doccure_question_submit', 'doccure_question_submit');
}

/**
 * Get hospitals by key change

 */
if ( !function_exists( 'doccure_get_hospitals' ) ) {

	function doccure_get_hospitals() {
		global $current_user;
		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ($do_check == false) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure_core');
			wp_send_json($json);
		}
		
		$s	 		= sanitize_text_field($_REQUEST['term']);
		$results 	= new WP_Query( array( 'posts_per_page' => -1, 's' => esc_html( $s ), 'post_type' => 'hospitals' ) );
		$items 		= array();
		
		if ( !empty( $results->posts ) ) {
			foreach ( $results->posts as $result ) {
				$suggestion 			= array();
				$suggestion['label'] 	= $result->post_title;
				$suggestion['id'] 		= $result->ID;
				$exist_post				= doccure_get_total_posts_by_meta( 'hospitals_team','hospital_id',$result->ID,array( 'publish','pending' ), $current_user->ID );
				
				if( empty( $exist_post )) {
					$items[] = $suggestion;
				} 
				
			}
		}

		$response = $_GET["callback"] . "(" . json_encode($items) . ")";
		echo do_shortcode($response);
		exit;
	}

	add_action( 'wp_ajax_doccure_get_hospitals', 'doccure_get_hospitals' );
	add_action( 'wp_ajax_nopriv_doccure_get_hospitals', 'doccure_get_hospitals' );
}

/**
 * Change post status

 */
if ( !function_exists( 'doccure_change_post_status' ) ) {

	function doccure_change_post_status() {
		global $current_user;
		if( function_exists('doccure_is_demo_site') ) { 
			doccure_is_demo_site() ;
		}; //if demo site then prevent
		
		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		$post_id 	= !empty( $_POST['id'] ) ? sanitize_text_field( $_POST['id'] ) : '';
		$status 	= !empty( $_POST['status'] ) ? sanitize_text_field( $_POST['status'] ) : '';
		
		$json 		= array();
		$emailData	= array();

		if( empty( $post_id ) ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__( 'Doctor ID is missing.', 'doccure' );
			wp_send_json( $json );
		}
		
		if( empty( $status ) ) {
			$json['type'] 		= 'error';
			$json['message'] 	= esc_html__( 'Doctor status is required.', 'doccure' );
			wp_send_json( $json );
		}
		
		$doctor_id 		= get_post_field( 'post_author', $post_id );
		$doctor_profile	= doccure_get_linked_profile_id( $doctor_id);
		$doctor_name	= doccure_full_name($doctor_profile);
		$doctor_name	= !empty( $doctor_name ) ? esc_html( $doctor_name ) : '';
		$author_id 		= get_post_meta( $post_id ,'hospital_id', true);
		$hospital_link	= get_the_permalink( $author_id );
		$hospital_link	= !empty( $hospital_link ) ? esc_url( $hospital_link ) : '';
		$hospital_name	= doccure_full_name($author_id);
		$hospital_name	= !empty( $hospital_name ) ? esc_html( $hospital_name ) : '';
		$author_id		= doccure_get_linked_profile_id( $author_id,'post');
		
		$total_price 		= get_post_meta( $post_id ,'hospital_id', true);
		$consultant_fee 		= get_post_meta( $post_id ,'hospital_id', true);
		$contents 		= get_post_meta( $post_id ,'hospital_id', true);
		

 				
		$am_booking_new = get_post_meta($post_id, '_am_booking', true); 
				$post_meta = maybe_unserialize($am_booking_new);
				$consultant_fee = $post_meta['_consultant_fee'];
				$total_price = $post_meta['_price'];
				 

		if( $current_user->ID != intval( $author_id ) ) {
			$json['type'] 		= 'error';
			$json['message'] 	= esc_html__( 'You have no permission for this change.', 'doccure' );
			wp_send_json( $json );
		}
		
		if( !empty($post_id) && !empty( $status ) ){
		   $post_data 		= array(
								  'ID'           => $post_id,
								  'post_status'  => $status
							  );

			wp_update_post( $post_data );
			
			if( !empty( $post_id ) && !empty( $status ) ) {
				
				$doctor_info				= get_userdata($doctor_id);
				$emailData['email']			= $doctor_info->user_email;
				$emailData['doctor_name']	= $doctor_name;
				$emailData['hospital_link']	= $hospital_link;
				$emailData['hospital_name']	= $hospital_name;

				$emailData['price']				= doccure_price_format($total_price,'return');
				$emailData['consultant_fee']	= doccure_price_format($consultant_fee,'return');
				$emailData['description']		= "";
				
				if (class_exists('doccure_Email_helper')) {
					if (class_exists('doccureHospitalTeamNotify')) {
						$email_helper = new doccureHospitalTeamNotify();
						if( $status === 'publish' ){
							$email_helper->send_approved_email($emailData);
						} else if( $status === 'trash' ){
							$email_helper->send_cancelled_email($emailData);
						}
					}
				}
				
				$json['url'] 		= doccure_Profile_Menu::doccure_profile_menu_link('team', $current_user->ID,'manage',true);
				$json['type'] 		= 'success';
				$json['message'] 	= esc_html__('you have successfully update this doctor status.', 'doccure');
				wp_send_json( $json );
				
			}
			
		} else {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Oops! something is going wrong.', 'doccure');
			wp_send_json( $json );
		}
	}

	add_action( 'wp_ajax_doccure_change_post_status', 'doccure_change_post_status' );
	add_action( 'wp_ajax_nopriv_doccure_change_post_status', 'doccure_change_post_status' );
}

/**
 * Get Booking data

 */
if ( !function_exists( 'doccure_get_booking_data' ) ) {

	function doccure_get_booking_data() {
		$post_id 			= !empty( $_POST['id'] ) ? intval( $_POST['id'] ) : '';
		$doctor_id 			= !empty( $_POST['doctor_id'] ) ? intval( $_POST['doctor_id'] ) : '';
		$json 				= array();
		
		if( function_exists('doccure_validate_user') ) { 
			doccure_validate_user();
		}; //if user is logged in

		//security check
		$do_check = check_ajax_referer('ajax_nonce', 'security', false);
		if ( $do_check == false ) {
			$json['type'] = 'error';
			$json['message'] = esc_html__('Security check failed, this could be because of your browser cache. Please clear the cache and check it againe', 'doccure');
			wp_send_json( $json );
		}
		
		if(!empty( $post_id ) ){
			
			$json['consultant_fee'] = '';
			$am_consultant_fee		= get_post_meta( $post_id ,'_consultant_fee',true);
			$consultant_fee			= !empty( $am_consultant_fee ) ? doccure_price_format( $am_consultant_fee,'return') : doccure_price_format( 0,'return');
			
			if( isset( $consultant_fee ) ) {
				$json['consultant_fee'] = '<ul class="at-taxesfees"><li id="consultant_fee"><span>'.esc_html__('Consultation fee','doccure').'<em>'.$consultant_fee.'<span class=" dc-consultant-fee dc-service-price" data-price="'.$am_consultant_fee.'" data-tipso="Verified user"></span></em></span></li><li class="at-toteltextfee"><span>'.esc_html__('Total','doccure').'<em id="dc-total-price" data-price="'.$am_consultant_fee.'">'.$consultant_fee.'</em></span></li></ul>';
			}
			
			$service_html			= '';
			$day					= strtolower(date('D'));
			$date					= date('Y-m-d');
			$reponse_slots			= doccure_get_time_slots_spaces($post_id,$day,$date);
			$norecourd_found		= apply_filters('doccure_empty_records_html','dc-empty-articls dc-emptyholder-sm',esc_html__( 'There are no any sloat available.', 'doccure' ),true);
			$reponse_slots			= !empty($reponse_slots) ? $reponse_slots : $norecourd_found;
			$json['time_slots']		= $reponse_slots;

			$service_html			= apply_filters('doccure_get_group_services_with_speciality',$post_id,'','return','location',$doctor_id);

			$json['type'] 				= 'success';
			$json['booking_services'] 	= $service_html;
			wp_send_json( $json );
		}else{
			$json['type'] 				= 'error';
			$json['message'] 			= esc_html__('You need to select hospital.', 'doccure');
			wp_send_json( $json );
		}
	}

	add_action( 'wp_ajax_doccure_get_booking_data', 'doccure_get_booking_data' );
	add_action( 'wp_ajax_nopriv_doccure_get_booking_data', 'doccure_get_booking_data' );
}