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.
Staff added successfully.
Staff deleted successfully.
| ID | Name | Phone | WP User | Services | Created | Action | |
|---|---|---|---|---|---|---|---|
| id); ?> | name); ?> | email); ?> | phone); ?> | created_at); ?> | Edit Delete |
No staff members found.