Przygotwanie do REST
This commit is contained in:
@@ -57,76 +57,17 @@ function mystat_handle_activity_form_submission() {
|
||||
return;
|
||||
}
|
||||
|
||||
$table_activities = $wpdb->prefix . 'mystat_activities';
|
||||
|
||||
// Przygotowanie danych (zamiana przecinka na kropkę w dystansie)
|
||||
$distance = isset( $_POST['distance'] ) ? floatval( str_replace( ',', '.', $_POST['distance'] ) ) : 0;
|
||||
|
||||
// Funkcja pomocnicza do zamiany pustych wartości na NULL, aby poprawnie zapisać je w bazie
|
||||
$null_if_empty = function( $value ) {
|
||||
return $value !== '' ? $value : null;
|
||||
};
|
||||
|
||||
$data = array(
|
||||
'category_id' => intval( $_POST['category_id'] ),
|
||||
'date' => sanitize_text_field( $_POST['date'] ),
|
||||
'title' => sanitize_text_field( $_POST['title'] ),
|
||||
'distance' => $distance,
|
||||
'duration' => sanitize_text_field( $_POST['duration'] ),
|
||||
'calories' => intval( $_POST['calories'] ),
|
||||
'comment' => sanitize_textarea_field( $_POST['comment'] ),
|
||||
'strava_url' => $null_if_empty( esc_url_raw( $_POST['strava_url'] ) ),
|
||||
'avg_heart_rate' => $null_if_empty( intval( $_POST['avg_heart_rate'] ) ),
|
||||
'max_heart_rate' => $null_if_empty( intval( $_POST['max_heart_rate'] ) ),
|
||||
'avg_speed' => $null_if_empty( floatval( str_replace( ',', '.', $_POST['avg_speed'] ) ) ),
|
||||
'max_speed' => $null_if_empty( floatval( str_replace( ',', '.', $_POST['max_speed'] ) ) ),
|
||||
'avg_cadence' => $null_if_empty( intval( $_POST['avg_cadence'] ) ),
|
||||
'max_cadence' => $null_if_empty( intval( $_POST['max_cadence'] ) ),
|
||||
'total_elevation_gain' => $null_if_empty( intval( $_POST['total_elevation_gain'] ) ),
|
||||
'total_elevation_loss' => $null_if_empty( intval( $_POST['total_elevation_loss'] ) ),
|
||||
'min_altitude' => $null_if_empty( intval( $_POST['min_altitude'] ) ),
|
||||
'max_altitude' => $null_if_empty( intval( $_POST['max_altitude'] ) ),
|
||||
'equipment_id' => $null_if_empty( intval( $_POST['equipment_id'] ) ),
|
||||
'gpx_url' => $null_if_empty( esc_url_raw( $_POST['gpx_url'] ) ),
|
||||
'event_type_id' => $null_if_empty( intval( $_POST['event_type_id'] ) ),
|
||||
);
|
||||
|
||||
// Format danych dla $wpdb->insert
|
||||
$format = array(
|
||||
'%d',
|
||||
'%s',
|
||||
'%s',
|
||||
'%f',
|
||||
'%s',
|
||||
'%d',
|
||||
'%s', // Pola podstawowe
|
||||
'%s',
|
||||
'%d',
|
||||
'%d',
|
||||
'%f',
|
||||
'%f',
|
||||
'%d',
|
||||
'%d', // Tętno, prędkość, kadencja
|
||||
'%d',
|
||||
'%d',
|
||||
'%d',
|
||||
'%d',
|
||||
'%d',
|
||||
'%s',
|
||||
'%d', // Wysokość, sprzęt, linki, typ wydarzenia
|
||||
);
|
||||
// Use the refactored function to save data.
|
||||
// We can pass $_POST directly as the function will sanitize it.
|
||||
$result = mystat_save_activity_data( $_POST, $activity_id );
|
||||
|
||||
if ( $activity_id > 0 ) {
|
||||
// UPDATE
|
||||
$result = $wpdb->update( $table_activities, $data, array( 'id' => $activity_id ), $format, array( '%d' ) );
|
||||
$message = 'Trening zaktualizowany pomyślnie!';
|
||||
} else {
|
||||
// INSERT
|
||||
$result = $wpdb->insert( $table_activities, $data, $format );
|
||||
$message = 'Trening dodany pomyślnie!';
|
||||
}
|
||||
|
||||
if ( false !== $result ) {
|
||||
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>';
|
||||
|
||||
Reference in New Issue
Block a user