true, ] ); } // Add manage_appointments capability to Staff $staff_role = get_role('staff'); if ($staff_role && !$staff_role->has_cap('manage_appointments')) { $staff_role->add_cap('manage_appointments'); } $admin_role = get_role('administrator'); if ($admin_role && !$admin_role->has_cap('manage_appointments')) { $admin_role->add_cap('manage_appointments'); } } // Register shortcodes public function abp_register_shortcodes() { add_shortcode('appointment_login_form', [$this, 'abp_appointment_login_form_shortcode']); add_shortcode('appointment_register_form', [$this, 'abp_appointment_register_form_shortcode']); add_shortcode('abp_bookings_page', [$this, 'abp_bookings_page_shortcode']); add_shortcode('book_appointment_form', [$this, 'abp_book_appointment_form_shortcode']); } //menue and submenue code public function abp_render_admin_page() { ?>
get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}appointment_services WHERE id = %d", $service_id)); if ($service) { $price = $service->price; // Is this service already booked for the selected date/time? $conflict = $wpdb->get_var( $wpdb->prepare( " SELECT COUNT(*) FROM {$wpdb->prefix}appointment_booking b WHERE b.service_id = %d AND b.booking_date = %s AND b.booking_time = %s AND b.status IN ('pending','confirmed') ", $service_id, $booking_date, $booking_time ) ); $post1 = get_page_by_path('book-appointment', OBJECT, 'page'); if ($conflict > 0) { set_transient( 'abp_booking_error', 'This service is already booked for the selected date and time.', 30 ); wp_safe_redirect(get_permalink($post1->ID)); exit; } // Insert appointment booking data $wpdb->insert( $wpdb->prefix . 'appointment_booking', [ 'user_id' => get_current_user_id(), 'service_id' => $service_id, 'booking_date' => $booking_date, 'booking_time' => $booking_time, 'name' => $name, 'email' => $email, 'phone' => $phone, 'price' => $price, 'status' => 'pending' ], [ '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%f', '%s' ] ); // Check if insert was successful if ($wpdb->insert_id) { // wp_safe_redirect(home_url('index.php/abp-bookings/')); $bookings_page = get_page_by_path('abp-bookings'); if ($bookings_page instanceof WP_Post) { wp_safe_redirect(get_permalink($bookings_page->ID)); exit; } wp_safe_redirect(home_url()); exit; } else { wp_die('Failed to book the appointment.'); } } else { wp_die('Invalid service selected.'); } } // Login form shortcode public function abp_appointment_login_form_shortcode() { if (is_user_logged_in()) { return '

' . esc_html__('You are already logged in.', 'advanced-appointment-booking') . '

'; } ob_start(); ?>
' . esc_html__('Security check failed. Please try again.', 'advanced-appointment-booking') . '

'; } else { $log = isset($_POST['log']) ? sanitize_text_field(wp_unslash($_POST['log'])) : ''; $pwd = isset($_POST['pwd']) ? sanitize_text_field(wp_unslash($_POST['pwd'])) : ''; $creds = [ 'user_login' => $log, 'user_password' => $pwd, 'remember' => true, ]; $user = wp_signon($creds, false); if (is_wp_error($user)) { echo '

' . esc_html__('Login failed. Please check your credentials.', 'advanced-appointment-booking') . '

'; } else { wp_safe_redirect(home_url('/')); exit; } } } return ob_get_clean(); } // Register form shortcode public function abp_appointment_register_form_shortcode() { if (is_user_logged_in()) { return '

You are already logged in.

'; } // Respect the site's "Anyone can register" setting, as WordPress core does. // Without this, accounts could be created even when registration is disabled. if (!get_option('users_can_register')) { return '

' . esc_html__('User registration is currently not allowed.', 'advanced-appointment-booking') . '

'; } ob_start(); ?>
$username, 'user_email' => $email, 'user_pass' => $password, 'display_name' => $full_name, 'role' => get_option('default_role'), ); $user_id = wp_insert_user($userdata); if (is_wp_error($user_id)) { echo '

Registration failed. Please try again.

'; } else { wp_set_current_user($user_id); wp_set_auth_cookie($user_id); wp_safe_redirect(home_url('')); exit; } } return ob_get_clean(); } // Shortcode for Bookings page public function abp_bookings_page_shortcode() { if (isset($_POST['cancel_booking_id'])) { if (!isset($_POST['book_appointment_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['book_appointment_nonce'])), 'book_appointment_action')) { wp_die('Invalid request.'); } if (is_user_logged_in()) { $current_user_id = get_current_user_id(); global $wpdb; $table_name = $wpdb->prefix . 'appointment_booking'; $booking_id = intval($_POST['cancel_booking_id']); $wpdb->update( $table_name, array('status' => 'canceled'), array('id' => $booking_id, 'user_id' => $current_user_id) ); wp_safe_redirect(add_query_arg('booking_status', 'canceled', get_permalink())); } } // Display the bookings if user is logged in if (!is_user_logged_in()) { return '

Please login to view your bookings.

'; } $current_user_id = get_current_user_id(); global $wpdb; $table_name = $wpdb->prefix . 'appointment_booking'; $bookings = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}appointment_booking WHERE user_id = %d", $current_user_id)); ob_start(); if ($bookings) { echo '
'; echo '

Your Appointments

'; echo '
'; foreach ($bookings as $booking) { // Sanitize and escape the fetched service name $service_name = sanitize_text_field($wpdb->get_var($wpdb->prepare("SELECT service_name FROM {$wpdb->prefix}appointment_services WHERE id = %d", $booking->service_id))); $status_class = $booking->status === 'canceled' ? 'status-canceled' : 'status-' . $booking->status; $status_label = ucfirst($booking->status); echo '
'; echo '
'; echo '
' . esc_html($service_name) . '
'; echo '
'; echo 'Date: ' . esc_html($booking->booking_date) . ''; echo 'Time: ' . esc_html($booking->booking_time) . ''; echo '
'; echo '
' . esc_html($status_label) . '
'; echo '
'; // details if ($booking->status !== 'canceled') { echo '
'; echo ''; wp_nonce_field('book_appointment_action', 'book_appointment_nonce'); echo ''; echo '
'; } echo '
'; // item } echo '
'; // list echo '
'; // container } else { echo '
'; echo '

' . esc_html__('No bookings found.', 'advanced-appointment-booking') . '

'; echo '
'; } return ob_get_clean(); } //end function abp_book_appointment_form_shortcode() { ob_start(); // Fetch available services global $wpdb; $table_name = $wpdb->prefix . 'appointment_services'; $services = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}appointment_services"); ?>

Select Service

Select Date

Select Time

Enter Your Details

Summary

Service:

Price: $

Date:

Time:

sanitize_table_name($wpdb->prefix . 'appointment_booking'); $services_table = $this->sanitize_table_name($wpdb->prefix . 'appointment_services'); $staff_table = $this->sanitize_table_name($wpdb->prefix . 'abp_staff'); $staff_services_table = $this->sanitize_table_name($wpdb->prefix . 'abp_staff_services'); // If user is staff -> show only their appointments if (in_array('staff', (array) $current_user->roles, true)) { $total_appointments = $wpdb->get_var( $wpdb->prepare( " SELECT COUNT(DISTINCT ab.id) FROM {$wpdb->prefix}appointment_booking ab INNER JOIN {$wpdb->prefix}abp_staff_services ss ON ab.service_id = ss.service_id INNER JOIN {$wpdb->prefix}abp_staff st ON ss.staff_id = st.id WHERE st.user_id = %d ", $current_user->ID ) ); $approved_appointments = $wpdb->get_var( $wpdb->prepare( " SELECT COUNT(DISTINCT ab.id) FROM {$wpdb->prefix}appointment_booking ab INNER JOIN {$wpdb->prefix}abp_staff_services ss ON ab.service_id = ss.service_id INNER JOIN {$wpdb->prefix}abp_staff st ON ss.staff_id = st.id WHERE st.user_id = %d AND ab.status = %s ", $current_user->ID, 'approved' ) ); // Staff don’t see customers / services $total_customers = null; $total_services = null; } else { $total_appointments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}appointment_booking"); $approved_appointments = $wpdb->get_var($wpdb->prepare( " SELECT COUNT(*) FROM {$wpdb->prefix}appointment_booking WHERE status = %s", 'approved' )); $total_customers = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->users}"); $total_services = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}appointment_services"); } echo '
'; echo '

Total Appointments

' . esc_html($total_appointments ? $total_appointments : 0) . '

'; if (!in_array('staff', (array) $current_user->roles, true)) { echo '

Total Services

' . esc_html($total_services ? $total_services : 0) . '

'; } echo '

Approved Appointments

' . esc_html($approved_appointments ? $approved_appointments : 0) . '

'; if (!in_array('staff', (array) $current_user->roles, true)) { echo '

Total Customers

' . esc_html($total_customers ? $total_customers : 0) . '

'; } echo '
'; } //end // Fetch appointments public function abp_render_appointments() { global $wpdb; $appointments_table = $this->sanitize_table_name($wpdb->prefix . 'appointment_booking'); $staff_table = $this->sanitize_table_name($wpdb->prefix . 'abp_staff'); $staff_services_table = $this->sanitize_table_name($wpdb->prefix . 'abp_staff_services'); $current_user = wp_get_current_user(); if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $appointments_table)) != $appointments_table) { echo '

No appointments found or table missing.

'; return; } $is_staff = in_array('staff', (array) $current_user->roles, true); // Restrict staff to appointments for services assigned to them; admins can act on any appointment. $abp_staff_owns_appointment = function ($appointment_id) use ($wpdb, $current_user) { $owned_id = $wpdb->get_var( $wpdb->prepare( " SELECT ab.id FROM {$wpdb->prefix}appointment_booking ab INNER JOIN {$wpdb->prefix}abp_staff_services ss ON ab.service_id = ss.service_id INNER JOIN {$wpdb->prefix}abp_staff st ON ss.staff_id = st.id WHERE ab.id = %d AND st.user_id = %d ", $appointment_id, $current_user->ID ) ); return (int) $owned_id === (int) $appointment_id; }; if (isset($_POST['update_appointment_status']) && isset($_POST['appointment_id']) && isset($_POST['appointment_status'])) { // Verify nonce if (!isset($_POST['appointment_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['appointment_nonce'])), 'update_appointment_status_nonce')) { die('Nonce verification failed.'); } $appointment_id = intval($_POST['appointment_id']); $allowed_statuses = ['pending', 'approved', 'canceled']; $new_status = sanitize_text_field(wp_unslash($_POST['appointment_status'])); if (!in_array($new_status, $allowed_statuses, true)) { wp_die('Invalid status.'); } if ($is_staff && !$abp_staff_owns_appointment($appointment_id)) { wp_die('You are not allowed to modify this appointment.'); } $wpdb->update( $appointments_table, ['status' => $new_status], ['id' => $appointment_id], ['%s'], ['%d'] ); echo '

Appointment status updated.

'; } if (isset($_POST['delete_appointment']) && isset($_POST['appointment_id'])) { // Verify nonce if (!isset($_POST['appointment_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['appointment_nonce'])), 'update_appointment_status_nonce')) { die('Nonce verification failed.'); } $appointment_id = intval($_POST['appointment_id']); if ($is_staff && !$abp_staff_owns_appointment($appointment_id)) { wp_die('You are not allowed to delete this appointment.'); } $wpdb->delete( $appointments_table, ['id' => $appointment_id], ['%d'] ); echo '

Appointment deleted successfully.

'; } if ($is_staff) { // Show only staff's own appointments $appointments = $wpdb->get_results( $wpdb->prepare( " SELECT DISTINCT ab.* FROM {$wpdb->prefix}appointment_booking ab INNER JOIN ( {$wpdb->prefix}abp_staff_services ss INNER JOIN {$wpdb->prefix}abp_staff st ON ss.staff_id = st.id ) ON ab.service_id = ss.service_id WHERE st.user_id = %d ", $current_user->ID ) ); } else { $appointments = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}appointment_booking"); } if (empty($appointments)) { echo '

No appointments found.

'; return; } ?>

Appointments

ID User Email Phone Service Id Booking Date Booking Time Status Actions
id); ?> name); ?> email); ?> phone); ?> service_id); ?> booking_date); ?> booking_time); ?>