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() { ?>
' . esc_html__('You are already logged in.', 'advanced-appointment-booking') . '
'; } ob_start(); ?>' . 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(); ?>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 '' . esc_html__('No bookings found.', 'advanced-appointment-booking') . '
'; echo '' . esc_html($total_appointments ? $total_appointments : 0) . '
' . esc_html($total_services ? $total_services : 0) . '
' . esc_html($approved_appointments ? $approved_appointments : 0) . '
' . esc_html($total_customers ? $total_customers : 0) . '
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.
Appointment deleted successfully.
No appointments found.
'; return; } ?>| ID | User | Phone | Service Id | Booking Date | Booking Time | Status | Actions | |
|---|---|---|---|---|---|---|---|---|
| id); ?> | name); ?> | email); ?> | phone); ?> | service_id); ?> | booking_date); ?> | booking_time); ?> |