prefix . 'abp_staff'; $services_table = $wpdb->prefix . 'appointment_services'; $staff_services = $wpdb->prefix . 'abp_staff_services'; /* |-------------------------------------------------------------------------- | Create relationship table (PHPCS safe) |-------------------------------------------------------------------------- */ $staff_services_escaped = esc_sql($staff_services); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$staff_services_escaped}` ( id INT AUTO_INCREMENT PRIMARY KEY, staff_id INT NOT NULL, service_id INT NOT NULL, INDEX (staff_id), INDEX (service_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" ); /* |-------------------------------------------------------------------------- | Helper Functions |-------------------------------------------------------------------------- */ function abp_get_all_services() { global $wpdb; return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}appointment_services"); } function abp_get_services_for_staff($staff_id) { global $wpdb; return $wpdb->get_col( $wpdb->prepare( "SELECT service_id FROM {$wpdb->prefix}abp_staff_services WHERE staff_id = %d", $staff_id ) ); } /* |-------------------------------------------------------------------------- | Handle Add / Update |-------------------------------------------------------------------------- */ if (isset($_POST['abp_add_staff']) || isset($_POST['abp_update_staff'])) { check_admin_referer('abp_add_staff_action', 'abp_add_staff_nonce'); $name = isset($_POST['staff_name']) ? sanitize_text_field(wp_unslash($_POST['staff_name'])) : ''; $email = isset($_POST['staff_email']) ? sanitize_email(wp_unslash($_POST['staff_email'])) : ''; $phone = isset($_POST['staff_phone']) ? sanitize_text_field(wp_unslash($_POST['staff_phone'])) : ''; $services = isset($_POST['staff_services']) ? array_map('intval', wp_unslash($_POST['staff_services'])) : []; $staff_user = !empty($_POST['staff_user']) ? intval(wp_unslash($_POST['staff_user'])) : null; if (!empty($_POST['staff_id'])) { $staff_id = intval(wp_unslash($_POST['staff_id'])); $wpdb->update($staff_table, [ 'name' => $name, 'email' => $email, 'phone' => $phone, 'user_id' => $staff_user ], ['id' => $staff_id]); $wpdb->delete($staff_services, ['staff_id' => $staff_id]); foreach ($services as $service_id) { $wpdb->insert($staff_services, [ 'staff_id' => $staff_id, 'service_id' => $service_id ]); } echo '

Staff updated successfully.

'; unset($_GET['edit_staff']); $edit_staff = null; $edit_services = []; } else { $wpdb->insert($staff_table, [ 'name' => $name, 'email' => $email, 'phone' => $phone, 'user_id' => $staff_user ]); $staff_id = $wpdb->insert_id; foreach ($services as $service_id) { $wpdb->insert($staff_services, [ 'staff_id' => $staff_id, 'service_id' => $service_id ]); } echo '

Staff added successfully.

'; unset($_GET['edit_staff']); $edit_staff = null; $edit_services = []; } } /* |-------------------------------------------------------------------------- | Handle Delete |-------------------------------------------------------------------------- */ if (!empty($_GET['delete_staff']) && is_numeric($_GET['delete_staff'])) { check_admin_referer('abp_delete_staff_action'); if (!current_user_can('manage_appointments')) { wp_die('You are not allowed to delete staff.'); } $staff_id = intval(wp_unslash($_GET['delete_staff'])); $wpdb->delete($staff_services, ['staff_id' => $staff_id]); $wpdb->delete($staff_table, ['id' => $staff_id]); echo '

Staff deleted successfully.

'; } /* |-------------------------------------------------------------------------- | Edit Mode |-------------------------------------------------------------------------- */ $edit_staff = null; $edit_services = []; if (!empty($_GET['edit_staff']) && is_numeric($_GET['edit_staff'])) { $edit_id = intval(wp_unslash($_GET['edit_staff'])); $edit_staff = $wpdb->get_row( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->prepare("SELECT * FROM $staff_table WHERE id = %d", $edit_id) ); $edit_services = abp_get_services_for_staff($edit_id); } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $all_services = abp_get_all_services(); $staff_members = $wpdb->get_results("SELECT * FROM $staff_table ORDER BY id DESC"); /* |-------------------------------------------------------------------------- | Current Tab (PHPCS safe) |-------------------------------------------------------------------------- */ $current_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'appointments'; ?>

get_col( $wpdb->prepare( "SELECT service_id FROM {$wpdb->prefix}abp_staff_services WHERE staff_id != %d", isset($staff_id) ? $staff_id : 0 ) ); $available_services = array_filter($all_services, function ($service) use ($assigned_service_ids, $edit_services) { return !in_array($service->id, $assigned_service_ids) || in_array($service->id, $edit_services); }); ?>

Choose an existing WordPress user with the "Staff" role. (Create user here.)

If service list is empty, please create services first. Only not-assigned services are shown here.

Hold Ctrl (Windows) or Command (Mac) to select multiple options.


All Staff Members

id); $service_names = array_filter(array_map(function ($sid) use ($all_services) { foreach ($all_services as $srv) { if ((int) $srv->id === (int) $sid) { return $srv->service_name; } } return null; }, $assigned_services)); $user_display = ''; if (!empty($staff->user_id)) { $wp_user = get_userdata($staff->user_id); if ($wp_user) { $user_display = $wp_user->display_name . ' (' . $wp_user->user_email . ')'; } } ?>
ID Name Email Phone WP User Services Created Action
id); ?> name); ?> email); ?> phone); ?> created_at); ?> Edit Delete

No staff members found.