Update repo

This commit is contained in:
2026-04-22 12:51:16 +02:00
parent d303a55638
commit d31591e287
24 changed files with 3994 additions and 3501 deletions
+303 -243
View File
@@ -1,244 +1,304 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function statpress_add_new_page() {
echo '<div class="wrap"><h1>Dodaj Nowy Trening</h1>';
// Obsługa zapisu formularza (musi być przed renderowaniem, aby wyświetlić komunikat)
statpress_handle_activity_form_submission();
// Formularz dodawania
statpress_render_add_form();
echo '</div>';
}
function statpress_edit_activity_page() {
global $wpdb;
$activity_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
if ( $activity_id === 0 ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie podano ID aktywności do edycji.</p></div>';
return;
}
// Handle form submission for update
statpress_handle_activity_form_submission();
$table_activities = $wpdb->prefix . 'statpress_activities';
$activity = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_activities WHERE id = %d", $activity_id ) );
if ( ! $activity ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie znaleziono aktywności o podanym ID.</p></div>';
return;
}
echo '<div class="wrap"><h1>Edytuj Trening</h1>';
statpress_render_add_form( $activity );
echo '</div>';
}
/**
* Obsługa zapisu nowego lub edytowanego wpisu do bazy danych
*/
function statpress_handle_activity_form_submission() {
global $wpdb;
// Sprawdź czy formularz został wysłany
if ( ! isset( $_POST['statpress_submit_activity'] ) ) {
return;
}
$activity_id = isset( $_POST['activity_id'] ) ? intval( $_POST['activity_id'] ) : 0;
$nonce_action = $activity_id > 0 ? 'statpress_edit_entry_' . $activity_id : 'statpress_add_entry';
// Weryfikacja bezpieczeństwa (Nonce)
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], $nonce_action ) ) {
echo '<div class="notice notice-error"><p>Błąd weryfikacji bezpieczeństwa formularza.</p></div>';
return;
}
// Use the refactored function to save data.
// We can pass $_POST directly as the function will sanitize it.
$result = statpress_save_activity_data( $_POST, $activity_id );
if ( $activity_id > 0 ) {
$message = 'Trening zaktualizowany pomyślnie!';
} else {
$message = 'Trening dodany pomyślnie!';
}
if ( $result ) {
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $message ) . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>Wystąpił błąd podczas zapisu do bazy.</p></div>';
}
}
/**
* Renderowanie formularza HTML
*/
function statpress_render_add_form( $activity = null ) {
// Enqueue media scripts for the uploader
wp_enqueue_media();
global $wpdb;
$table_categories = $wpdb->prefix . 'statpress_categories';
$table_event_types = $wpdb->prefix . 'statpress_event_types';
$table_equipment = $wpdb->prefix . 'statpress_equipment';
$categories = $wpdb->get_results( "SELECT * FROM $table_categories ORDER BY name ASC" );
$event_types = $wpdb->get_results( "SELECT * FROM $table_event_types ORDER BY name ASC" );
$equipment_list = $wpdb->get_results( "SELECT * FROM $table_equipment ORDER BY name ASC" );
$is_edit_mode = ! is_null( $activity );
$nonce_action = $is_edit_mode ? 'statpress_edit_entry_' . $activity->id : 'statpress_add_entry';
$form_title = $is_edit_mode ? 'Edytuj Aktywność' : 'Dodaj Nową Aktywność';
$button_text = $is_edit_mode ? 'Zaktualizuj Trening' : 'Zapisz Trening';
?>
<div class="postbox">
<div class="postbox-header"><h2 class="hndle"><?php echo esc_html( $form_title ); ?></h2></div>
<div class="inside">
<form method="post" action="">
<input type="hidden" name="activity_id" value="<?php echo $is_edit_mode ? esc_attr( $activity->id ) : '0'; ?>">
<?php wp_nonce_field( $nonce_action, '_wpnonce' ); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="title">Tytuł</label></th>
<td><input type="text" name="title" id="title" class="large-text" placeholder="np. Poranny trening w lesie" value="<?php echo $is_edit_mode ? esc_attr( $activity->title ) : ''; ?>" required></td>
</tr>
<tr>
<th scope="row"><label for="category_id">Kategoria</label></th>
<td>
<select name="category_id" id="category_id" required>
<?php foreach ( $categories as $cat ) : ?>
<option value="<?php echo esc_attr( $cat->id ); ?>" <?php echo $is_edit_mode ? selected( $activity->category_id, $cat->id, false ) : selected( $cat->name, 'Rower', false ); ?>><?php echo esc_html( $cat->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="date">Data</label></th>
<td><input type="date" name="date" id="date" value="<?php echo $is_edit_mode ? esc_attr( $activity->date ) : current_time( 'Y-m-d' ); ?>" class="regular-text" required></td>
</tr>
<tr>
<th scope="row"><label for="distance">Dystans (km)</label></th>
<td><input type="text" name="distance" id="distance" class="regular-text" placeholder="0,00" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float) $activity->distance, 2, ',', '' ) ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="duration">Czas (HH:MM:SS)</label></th>
<td><input type="time" name="duration" id="duration" step="1" value="<?php echo $is_edit_mode ? esc_attr( $activity->duration ) : '00:00:00'; ?>" class="regular-text"></td>
</tr>
<tr>
<th scope="row"><label for="calories">Kalorie (kcal)</label></th>
<td><input type="number" name="calories" id="calories" class="regular-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->calories ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="event_type_id">Typ wydarzenia</label></th>
<td>
<select name="event_type_id" id="event_type_id">
<option value="">-- Wybierz --</option>
<?php foreach ( $event_types as $type ) : ?>
<option value="<?php echo esc_attr( $type->id ); ?>" <?php echo $is_edit_mode ? selected( $activity->event_type_id, $type->id, false ) : selected( $type->name, 'Trening', false ); ?>><?php echo esc_html( $type->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="equipment_id">Sprzęt</label></th>
<td>
<select name="equipment_id" id="equipment_id">
<option value="">-- Wybierz --</option>
<?php foreach ( $equipment_list as $item ) : ?>
<option value="<?php echo esc_attr( $item->id ); ?>" <?php if ( $is_edit_mode ) {
selected( $activity->equipment_id, $item->id );} ?>><?php echo esc_html( $item->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr class="form-field">
<td colspan="2"><hr><h4>Dane szczegółowe (opcjonalne)</h4></td>
</tr>
<tr>
<th scope="row"><label for="avg_speed">Śr. prędkość (km/h)</label></th>
<td><input type="text" name="avg_speed" id="avg_speed" class="small-text" placeholder="0,0" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float) $activity->avg_speed, 2, ',', '' ) ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_speed">Maks. prędkość (km/h)</label></th>
<td><input type="text" name="max_speed" id="max_speed" class="small-text" placeholder="0,0" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float) $activity->max_speed, 2, ',', '' ) ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="avg_heart_rate">Śr. tętno</label></th>
<td><input type="number" name="avg_heart_rate" id="avg_heart_rate" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->avg_heart_rate ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_heart_rate">Maks. tętno</label></th>
<td><input type="number" name="max_heart_rate" id="max_heart_rate" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_heart_rate ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="avg_cadence">Śr. rytm pedałowania</label></th>
<td><input type="number" name="avg_cadence" id="avg_cadence" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->avg_cadence ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_cadence">Maks. rytm pedałowania</label></th>
<td><input type="number" name="max_cadence" id="max_cadence" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_cadence ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="total_elevation_gain">Całkowity wznios (m)</label></th>
<td><input type="number" name="total_elevation_gain" id="total_elevation_gain" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->total_elevation_gain ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="total_elevation_loss">Całkowity spadek (m)</label></th>
<td><input type="number" name="total_elevation_loss" id="total_elevation_loss" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->total_elevation_loss ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="min_altitude">Min. wysokość (m)</labe></th>
<td><input type="number" name="min_altitude" id="min_altitude" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->min_altitude ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="max_altitude">Maks. wysokość (m)</label></th>
<td><input type="number" name="max_altitude" id="max_altitude" class="small-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_altitude ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="comment">Komentarz</label></th>
<td><textarea name="comment" id="comment" rows="3" class="large-text"><?php echo $is_edit_mode ? esc_textarea( $activity->comment ) : ''; ?></textarea></td>
</tr>
<tr class="form-field">
<td colspan="2"><hr><h4>Linki zewnętrzne (opcjonalne)</h4></td>
</tr>
<tr>
<th scope="row"><label for="strava_url">Link do Strava</label></th>
<td><input type="url" name="strava_url" id="strava_url" class="large-text" placeholder="https://www.strava.com/activities/..." value="<?php echo $is_edit_mode ? esc_attr( $activity->strava_url ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="gpx_url">Link do pliku GPX</label></th>
<td>
<input type="text" name="gpx_url" id="gpx_url" class="large-text" placeholder="Wklej URL lub wgraj plik..." value="<?php echo $is_edit_mode ? esc_attr( $activity->gpx_url ) : ''; ?>">
<button type="button" class="button" id="upload_gpx_button" style="margin-top: 5px;">Wgraj lub wybierz plik</button>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="statpress_submit_activity" id="submit" class="button button-primary" value="<?php echo esc_attr( $button_text ); ?>">
</p>
</form>
<script>
jQuery(document).ready(function($) {
$('#upload_gpx_button').click(function(e) {
e.preventDefault();
var gpx_uploader = wp.media({
title: 'Wybierz plik GPX',
button: { text: 'Użyj tego pliku' },
multiple: false,
library: { type: ['application/gpx+xml', 'application/xml', 'text/plain'] }
})
.on('select', function() {
var attachment = gpx_uploader.state().get('selection').first().toJSON();
$('#gpx_url').val(attachment.url).trigger('change');
}).open();
});
});
</script>
</div>
</div>
<?php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function statpress_add_new_page() {
echo '<div class="wrap"><h1>Dodaj Nowy Trening</h1>';
// Obsługa zapisu formularza (musi być przed renderowaniem, aby wyświetlić komunikat)
statpress_handle_activity_form_submission();
// Formularz dodawania
statpress_render_add_form();
echo '</div>';
}
function statpress_edit_activity_page() {
global $wpdb;
$activity_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
if ( $activity_id === 0 ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie podano ID aktywności do edycji.</p></div>';
return;
}
// Handle form submission for update
statpress_handle_activity_form_submission();
$table_activities = $wpdb->prefix . 'statpress_activities';
$activity = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_activities WHERE id = %d", $activity_id ) );
if ( ! $activity ) {
echo '<div class="wrap"><h1>Błąd</h1><p>Nie znaleziono aktywności o podanym ID.</p></div>';
return;
}
echo '<div class="wrap"><h1>Edytuj Trening</h1>';
statpress_render_add_form( $activity );
echo '</div>';
}
/**
* Obsługa zapisu nowego lub edytowanego wpisu do bazy danych
*/
function statpress_handle_activity_form_submission() {
global $wpdb;
// Sprawdź czy formularz został wysłany
if ( ! isset( $_POST['statpress_submit_activity'] ) ) {
return;
}
$activity_id = isset( $_POST['activity_id'] ) ? intval( $_POST['activity_id'] ) : 0;
$nonce_action = $activity_id > 0 ? 'statpress_edit_entry_' . $activity_id : 'statpress_add_entry';
// Weryfikacja bezpieczeństwa (Nonce)
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], $nonce_action ) ) {
echo '<div class="notice notice-error"><p>Błąd weryfikacji bezpieczeństwa formularza.</p></div>';
return;
}
// Use the refactored function to save data.
// We can pass $_POST directly as the function will sanitize it.
$result = statpress_save_activity_data( $_POST, $activity_id );
if ( $activity_id > 0 ) {
$message = 'Trening zaktualizowany pomyślnie!';
} else {
$message = 'Trening dodany pomyślnie!';
}
if ( $result ) {
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $message ) . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>Wystąpił błąd podczas zapisu do bazy.</p></div>';
}
}
/**
* Renderowanie formularza HTML
*/
function statpress_render_add_form( $activity = null ) {
// Enqueue media scripts for the uploader
wp_enqueue_media();
global $wpdb;
$table_categories = $wpdb->prefix . 'statpress_categories';
$table_event_types = $wpdb->prefix . 'statpress_event_types';
$table_equipment = $wpdb->prefix . 'statpress_equipment';
$categories = $wpdb->get_results( "SELECT * FROM $table_categories ORDER BY name ASC" );
$event_types = $wpdb->get_results( "SELECT * FROM $table_event_types ORDER BY name ASC" );
$equipment_list = $wpdb->get_results( "SELECT * FROM $table_equipment ORDER BY name ASC" );
$is_edit_mode = ! is_null( $activity );
$nonce_action = $is_edit_mode ? 'statpress_edit_entry_' . $activity->id : 'statpress_add_entry';
$form_title = $is_edit_mode ? 'Edytuj Aktywność' : 'Dodaj Nową Aktywność';
$button_text = $is_edit_mode ? 'Zaktualizuj Trening' : 'Zapisz Trening';
?>
<div class="postbox">
<div class="postbox-header"><h2 class="hndle"><?php echo esc_html( $form_title ); ?></h2></div>
<div class="inside">
<form method="post" action="">
<input type="hidden" name="activity_id" value="<?php echo $is_edit_mode ? esc_attr( $activity->id ) : '0'; ?>">
<?php wp_nonce_field( $nonce_action, '_wpnonce' ); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="title">Tytuł</label></th>
<td><input type="text" name="title" id="title" class="large-text" placeholder="np. Poranny trening w lesie" value="<?php echo $is_edit_mode ? esc_attr( $activity->title ) : ''; ?>" required></td>
</tr>
<tr>
<th scope="row"><label for="gpx_url">Link do pliku GPX</label></th>
<td>
<input type="text" name="gpx_url" id="gpx_url" class="large-text" placeholder="Wklej URL lub wgraj plik..." value="<?php echo $is_edit_mode ? esc_attr( $activity->gpx_url ) : ''; ?>" style="width: calc(100% - 150px); margin-right: 10px;">
<span id="gpx_parse_status"></span>
<button type="button" class="button" id="upload_gpx_button" style="margin-top: 5px;">Wgraj lub wybierz plik</button>
<p class="description">Wklej link lub wgraj plik GPX, aby automatycznie uzupełnić pola poniżej.</p>
</td>
</tr>
<tr>
<td colspan="2" style="padding: 15px 0 5px;"><hr></td>
</tr>
<tr>
<th scope="row"><label for="category_id">Kategoria</label></th>
<td>
<select name="category_id" id="category_id" required>
<?php foreach ( $categories as $cat ) : ?>
<option value="<?php echo esc_attr( $cat->id ); ?>" <?php echo $is_edit_mode ? selected( $activity->category_id, $cat->id, false ) : selected( $cat->name, 'Rower', false ); ?>><?php echo esc_html( $cat->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="date">Data</label></th>
<td><input type="date" name="date" id="date" value="<?php echo $is_edit_mode ? esc_attr( $activity->date ) : current_time( 'Y-m-d' ); ?>" class="regular-text" required></td>
</tr>
<tr>
<th scope="row"><label for="distance">Dystans (km)</label></th>
<td><input type="text" name="distance" id="distance" class="regular-text" placeholder="0,00" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float) $activity->distance, 2, ',', '' ) ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="duration">Czas (HH:MM:SS)</label></th>
<td><input type="time" name="duration" id="duration" step="1" value="<?php echo $is_edit_mode ? esc_attr( $activity->duration ) : '00:00:00'; ?>" class="regular-text"></td>
</tr>
<tr>
<th scope="row"><label for="calories">Kalorie (kcal)</label></th>
<td><input type="number" name="calories" id="calories" class="regular-text" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->calories ) : ''; ?>"></td>
</tr>
<tr>
<th scope="row"><label for="event_type_id">Typ wydarzenia</label></th>
<td>
<select name="event_type_id" id="event_type_id">
<option value="">-- Wybierz --</option>
<?php foreach ( $event_types as $type ) : ?>
<option value="<?php echo esc_attr( $type->id ); ?>" <?php echo $is_edit_mode ? selected( $activity->event_type_id, $type->id, false ) : selected( $type->name, 'Trening', false ); ?>><?php echo esc_html( $type->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="equipment_id">Sprzęt</label></th>
<td>
<select name="equipment_id" id="equipment_id">
<option value="">-- Wybierz --</option>
<?php foreach ( $equipment_list as $item ) : ?>
<option value="<?php echo esc_attr( $item->id ); ?>" <?php if ( $is_edit_mode ) {
selected( $activity->equipment_id, $item->id );} ?>><?php echo esc_html( $item->name ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<td colspan="2" style="padding: 20px 0;">
<hr>
<h3 style="font-size: 1.2em; margin: 1em 0;">Dane szczegółowe (opcjonalne)</h3>
<div class="statpress-form-grid">
<div class="statpress-form-group">
<h3><span class="dashicons dashicons-dashboard" style="vertical-align: middle;"></span> Prędkość</h3>
<p><label for="avg_speed">Średnia (km/h)</label><input type="text" name="avg_speed" id="avg_speed" placeholder="0,0" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float) $activity->avg_speed, 2, ',', '' ) ) : ''; ?>"></p>
<p><label for="max_speed">Maksymalna (km/h)</label><input type="text" name="max_speed" id="max_speed" placeholder="0,0" value="<?php echo $is_edit_mode ? esc_attr( number_format( (float) $activity->max_speed, 2, ',', '' ) ) : ''; ?>"></p>
</div>
<div class="statpress-form-group">
<h3><span class="dashicons dashicons-heart" style="vertical-align: middle;"></span> Tętno</h3>
<p><label for="avg_heart_rate">Średnie (bpm)</label><input type="number" name="avg_heart_rate" id="avg_heart_rate" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->avg_heart_rate ) : ''; ?>"></p>
<p><label for="max_heart_rate">Maksymalne (bpm)</label><input type="number" name="max_heart_rate" id="max_heart_rate" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_heart_rate ) : ''; ?>"></p>
</div>
<div class="statpress-form-group">
<h3><span class="dashicons dashicons-update" style="vertical-align: middle;"></span> Rytm</h3>
<p><label for="avg_cadence">Średni (rpm)</label><input type="number" name="avg_cadence" id="avg_cadence" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->avg_cadence ) : ''; ?>"></p>
<p><label for="max_cadence">Maksymalny (rpm)</label><input type="number" name="max_cadence" id="max_cadence" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_cadence ) : ''; ?>"></p>
</div>
<div class="statpress-form-group">
<h3><span class="dashicons dashicons-chart-area" style="vertical-align: middle;"></span> Wysokość</h3>
<p><label for="total_elevation_gain">Suma wzniosów (m)</label><input type="number" name="total_elevation_gain" id="total_elevation_gain" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->total_elevation_gain ) : ''; ?>"></p>
<p><label for="total_elevation_loss">Suma spadków (m)</label><input type="number" name="total_elevation_loss" id="total_elevation_loss" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->total_elevation_loss ) : ''; ?>"></p>
<p><label for="min_altitude">Min. wysokość (m n.p.m.)</label><input type="number" name="min_altitude" id="min_altitude" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->min_altitude ) : ''; ?>"></p>
<p><label for="max_altitude">Maks. wysokość (m n.p.m.)</label><input type="number" name="max_altitude" id="max_altitude" placeholder="0" value="<?php echo $is_edit_mode ? esc_attr( $activity->max_altitude ) : ''; ?>"></p>
</div>
</div>
<hr style="margin-top: 2em;">
</td>
</tr>
<tr>
<th scope="row"><label for="comment">Komentarz</label></th>
<td><textarea name="comment" id="comment" rows="3" class="large-text"><?php echo $is_edit_mode ? esc_textarea( $activity->comment ) : ''; ?></textarea></td>
</tr>
<tr>
<th scope="row"><label for="strava_url">Link do Strava</label></th>
<td><input type="url" name="strava_url" id="strava_url" class="large-text" placeholder="https://www.strava.com/activities/..." value="<?php echo $is_edit_mode ? esc_attr( $activity->strava_url ) : ''; ?>"></td>
</tr>
</table>
<p class="submit">
<input type="submit" name="statpress_submit_activity" id="submit" class="button button-primary" value="<?php echo esc_attr( $button_text ); ?>">
</p>
</form>
<script>
jQuery(document).ready(function($) {
$('#upload_gpx_button').click(function(e) {
e.preventDefault();
var gpx_uploader = wp.media({
title: 'Wybierz plik GPX',
button: { text: 'Użyj tego pliku' },
multiple: false,
library: { type: ['application/gpx+xml', 'application/xml', 'text/plain'] }
})
.on('select', function() {
var attachment = gpx_uploader.state().get('selection').first().toJSON();
$('#gpx_url').val(attachment.url).trigger('change');
}).open();
});
// --- GPX Auto-fill Feature ---
const gpxUrlInput = $('#gpx_url');
const statusEl = $('#gpx_parse_status');
let debounceTimer;
gpxUrlInput.on('input change', function() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(function() {
const url = gpxUrlInput.val();
if (url && url.toLowerCase().endsWith('.gpx')) {
parseGpx(url);
}
}, 500); // 500ms delay to avoid firing on every keystroke
});
function parseGpx(url) {
statusEl.html('<span class="spinner is-active" style="float:none; vertical-align: middle;"></span> Analizuję plik...');
wp.apiFetch({
path: '/statpress/v1/gpx/parse-summary',
method: 'POST',
data: { gpx_url: url }
})
.then(function(data) {
statusEl.html('<span class="dashicons dashicons-yes-alt" style="color: #46b450;"></span> Dane wczytane!');
const fieldsToCheck = [
'#distance', '#duration', '#calories', '#avg_speed', '#max_speed',
'#avg_heart_rate', '#max_heart_rate', '#avg_cadence', '#max_cadence',
'#total_elevation_gain', '#total_elevation_loss', '#min_altitude', '#max_altitude'
];
let hasExistingData = false;
fieldsToCheck.forEach(function(selector) {
const field = $(selector);
if (field.val() && field.val() !== '00:00:00' && field.val() !== '0' && field.val() !== '0,00') {
hasExistingData = true;
}
});
let proceed = true;
if (hasExistingData) {
proceed = confirm('Niektóre pola formularza zawierają już dane. Czy na pewno chcesz je nadpisać danymi z pliku GPX?');
}
if (proceed) {
const formatNum = (num) => String(num).replace('.', ',');
if (data.distance) $('#distance').val(formatNum(data.distance));
if (data.duration) $('#duration').val(data.duration);
if (data.avg_speed) $('#avg_speed').val(formatNum(data.avg_speed));
if (data.max_speed) $('#max_speed').val(formatNum(data.max_speed));
if (data.avg_heart_rate) $('#avg_heart_rate').val(data.avg_heart_rate);
if (data.max_heart_rate) $('#max_heart_rate').val(data.max_heart_rate);
if (data.avg_cadence) $('#avg_cadence').val(data.avg_cadence);
if (data.max_cadence) $('#max_cadence').val(data.max_cadence);
if (data.total_elevation_gain) $('#total_elevation_gain').val(data.total_elevation_gain);
if (data.total_elevation_loss) $('#total_elevation_loss').val(data.total_elevation_loss);
if (data.min_altitude) $('#min_altitude').val(data.min_altitude);
if (data.max_altitude) $('#max_altitude').val(data.max_altitude);
}
setTimeout(() => statusEl.html(''), 4000);
})
.catch(function(error) {
const errorMsg = error.message || 'Nieznany błąd.';
statusEl.html('<span class="dashicons dashicons-warning" style="color: #d63638;"></span> Błąd: ' + errorMsg);
});
}
});
</script>
</div>
</div>
<?php
}