prefix . 'appointment_services'; function abp_get_services($force_update = false) { global $wpdb; $table_name = $wpdb->prefix . 'appointment_services'; $services = wp_cache_get('services_list', 'appointment_services'); if ($force_update || false === $services) { $services = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}appointment_services"); wp_cache_set('services_list', $services, 'appointment_services'); } return $services; } function abp_get_service($service_id) { global $wpdb; $table_name = $wpdb->prefix . 'appointment_services'; $cache_key = 'service_' . intval($service_id); $service = wp_cache_get($cache_key, 'appointment_services'); if (false === $service) { $service = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}appointment_services WHERE id = %d", $service_id)); wp_cache_set($cache_key, $service, 'appointment_services'); } return $service; } function abp_clear_services_cache() { wp_cache_delete('services_list', 'appointment_services'); } if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['service_name']) && !isset($_POST['service_id'])) { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'save_service')) { wp_die('Nonce verification failed'); } $service_name = sanitize_text_field(wp_unslash($_POST['service_name'])); $duration = isset($_POST['duration']) ? intval($_POST['duration']) : 0; $price = isset($_POST['price']) ? floatval($_POST['price']) : 0; $description = isset($_POST['description']) ? sanitize_textarea_field(wp_unslash($_POST['description'])) : ''; $wpdb->insert($table_name, [ 'service_name' => $service_name, 'duration' => $duration, 'price' => $price, 'description' => $description ]); abp_clear_services_cache(); echo '

Service added successfully!

'; } if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['service_id'])) { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'update_service')) { wp_die('Nonce verification failed'); } $service_id = intval($_POST['service_id']); $service_name = sanitize_text_field(wp_unslash($_POST['service_name'])); $duration = isset($_POST['duration']) ? intval($_POST['duration']) : 0; $price = isset($_POST['price']) ? floatval($_POST['price']) : 0; $description = isset($_POST['description']) ? sanitize_textarea_field(wp_unslash($_POST['description'])) : ''; $wpdb->update($table_name, [ 'service_name' => $service_name, 'duration' => $duration, 'price' => $price, 'description' => $description ], ['id' => $service_id]); abp_clear_services_cache(); echo '

Service updated successfully!

'; } // Handle service deletion if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_service_id'])) { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'delete_service')) { // wp_die('Nonce verification failed'); } $service_id = intval($_POST['delete_service_id']); $wpdb->delete($table_name, ['id' => $service_id]); abp_clear_services_cache(); echo '

Service deleted successfully!

'; } // Fetch service data if editing $edit_service = null; if (isset($_GET['edit']) && isset($_GET['id']) && !empty($_GET['id'])) { $edit_service = abp_get_service(intval($_GET['id'])); } ?>


List of Services

ID Service Name Duration Price Description Actions '; foreach ($services as $service) { echo ' ' . esc_html($service->id) . ' ' . esc_html($service->service_name) . ' ' . esc_html($service->duration) . ' mins $' . esc_html($service->price) . ' ' . esc_html($service->description) . '
'; } echo ''; } else { echo '

No services added yet.

'; } ?>