// Obsługa zapisywania pomiarów
function kf_save_measurement_ajax() {
check_ajax_referer('kf_progress_nonce', 'nonce');
if (!is_user_logged_in()) {
wp_send_json_error('Musisz być zalogowany, aby zapisać pomiary.');
return;
}
// Pobierz dane z formularza
$user_id = get_current_user_id();
$date = sanitize_text_field($_POST['date']);
$weight = floatval($_POST['weight']);
$waist = isset($_POST['waist']) && $_POST['waist'] ? floatval($_POST['waist']) : null;
$hips = isset($_POST['hips']) && $_POST['hips'] ? floatval($_POST['hips']) : null;
$arm = isset($_POST['arm']) && $_POST['arm'] ? floatval($_POST['arm']) : null;
$thigh = isset($_POST['thigh']) && $_POST['thigh'] ? floatval($_POST['thigh']) : null;
$body_fat = isset($_POST['body_fat']) && $_POST['body_fat'] ? floatval($_POST['body_fat']) : null;
$notes = isset($_POST['notes']) ? sanitize_textarea_field($_POST['notes']) : '';
// Walidacja
if ($weight <= 0) {
wp_send_json_error('Waga musi być większa od zera.');
return;
}
// Sprawdź, czy istnieje już pomiar z tą datą
global $wpdb;
$table_name = $wpdb->prefix . 'kf_measurements';
$existing = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM $table_name WHERE user_id = %d AND date = %s",
$user_id, $date
));
if ($existing) {
wp_send_json_error('Pomiar z tą datą już istnieje. Edytuj istniejący pomiar zamiast dodawać nowy.');
return;
}
// Zapisz pomiar do bazy danych
$result = $wpdb->insert(
$table_name,
array(
'user_id' => $user_id,
'date' => $date,
'weight' => $weight,
'waist' => $waist,
'hips' => $hips,
'arm' => $arm,
'thigh' => $thigh,
'body_fat' => $body_fat,
'notes' => $notes,
'created_at' => current_time('mysql')
),
array('%d', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%s', '%s')
);
if ($result === false) {
wp_send_json_error('Wystąpił błąd podczas zapisywania pomiaru: ' . $wpdb->last_error);
return;
}
wp_send_json_success(array(
'message' => 'Pomiar został pomyślnie zapisany.',
'id' => $wpdb->insert_id
));
}
add_action('wp_ajax_kf_save_measurement', 'kf_save_measurement_ajax');
// Obsługa pobierania pomiarów
function kf_get_measurements_ajax() {
check_ajax_referer('kf_progress_nonce', 'nonce');
if (!is_user_logged_in()) {
wp_send_json_error('Musisz być zalogowany, aby zobaczyć pomiary.');
return;
}
global $wpdb;
$table_name = $wpdb->prefix . 'kf_measurements';
$user_id = get_current_user_id();
$measurements = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE user_id = %d ORDER BY date ASC",
$user_id
));
wp_send_json_success($measurements);
}
add_action('wp_ajax_kf_get_measurements', 'kf_get_measurements_ajax');
Warning: Cannot modify header information - headers already sent by (output started at /home/klient.dhosting.pl/owsiany1991/klinika-formy.pl/public_html/wp-content/themes/divi-child/includes/ajax-handlers.php:1) in /home/klient.dhosting.pl/owsiany1991/klinika-formy.pl/public_html/wp-includes/pluggable.php on line 1450
Warning: Cannot modify header information - headers already sent by (output started at /home/klient.dhosting.pl/owsiany1991/klinika-formy.pl/public_html/wp-content/themes/divi-child/includes/ajax-handlers.php:1) in /home/klient.dhosting.pl/owsiany1991/klinika-formy.pl/public_html/wp-includes/pluggable.php on line 1453