function cp_sessions_page_safe() {
if (!current_user_can(cp_caps_safe())) return;
echo '
Sessions
';
// ✅ Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cp_action']) && $_POST['cp_action'] === 'save_session' && check_admin_referer('cp_save_session_safe')) {
$date = $_POST['session_date'] ?? date('Y-m-d');
$time = $_POST['started_at'] ?? '09:00';
$started_at = $date . ' ' . $time;
$data = [
'client_id' => (int) $_POST['client_id'],
'coach_user_id' => (int) $_POST['coach_user_id'],
'started_at' => $started_at,
'duration_minutes' => (int) $_POST['duration_minutes'],
'session_type' => sanitize_text_field($_POST['session_type']),
'goals_enc' => cp_encrypt_safe($_POST['goals']),
'challenges_enc' => cp_encrypt_safe($_POST['challenges']),
'insights_enc' => cp_encrypt_safe($_POST['insights']),
'assignments_enc' => cp_encrypt_safe($_POST['assignments']),
'commitments_enc' => cp_encrypt_safe($_POST['commitments']),
'accountability_enc' => cp_encrypt_safe($_POST['accountability']),
'coach_reflections_enc' => cp_encrypt_safe($_POST['coach_reflections']),
'emotional_start' => sanitize_text_field($_POST['emotional_start']),
'emotional_end' => sanitize_text_field($_POST['emotional_end']),
'next_session_date' => $_POST['next_session_date'] ?? null,
'resources_shared_enc' => cp_encrypt_safe($_POST['resources_shared']),
];
cp_upsert_session_safe(0, $data);
echo '
';
}
$clients = cp_get_clients_safe();
$coaches = cp_get_coaches_safe();
echo '
Create New Session
';
echo '
';
echo '
';
}