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() { ?>
prefix . 'appointment_services'; $service = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $service_id)); if ($service) { $price = $service->price; // 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_redirect( home_url( 'index.php/abp-bookings/' ) ); 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 '

You are already logged in.

'; } ob_start(); ?>
$log, 'user_password' => $pwd, 'remember' => true, ); $user = wp_signon($creds, false); if (is_wp_error($user)) { echo '

Login failed. Please check your credentials.

'; } else { wp_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.

'; } ob_start(); ?>
$username, 'user_email' => $email, 'user_pass' => $password, 'display_name' => $full_name, ); $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_redirect(home_url('')); exit; } } return ob_get_clean(); } // Shortcode for Bookings page public function abp_bookings_page_shortcode() { if (isset($_POST['cancel_booking_id'])) { // Verify nonce in form handler 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 ''; } else { echo '

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

'; } 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:

prefix . 'appointment_booking'; $services_table = $wpdb->prefix . 'appointment_services'; $staff_table = $wpdb->prefix . 'abp_staff'; $staff_services_table = $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 {$appointments_table} ab INNER JOIN {$staff_services_table} ss ON ab.service_id = ss.service_id INNER JOIN {$staff_table} 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 {$appointments_table} ab INNER JOIN {$staff_services_table} ss ON ab.service_id = ss.service_id INNER JOIN {$staff_table} 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 = $wpdb->prefix . 'appointment_booking'; $staff_table = $wpdb->prefix . 'abp_staff'; $staff_services_table = $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; } 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']); $new_status = sanitize_text_field(wp_unslash($_POST['appointment_status'])); $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'])) { $appointment_id = intval($_POST['appointment_id']); $wpdb->delete( $appointments_table, ['id' => $appointment_id], ['%d'] ); echo '

Appointment deleted successfully.

'; } if ( in_array( 'staff', (array) $current_user->roles ) ) { // Show only staff's own appointments $appointments = $wpdb->get_results( $wpdb->prepare( " SELECT DISTINCT ab.* FROM {$appointments_table} ab INNER JOIN ( {$staff_services_table} ss INNER JOIN {$staff_table} 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); ?>