Shortcode
This commit is contained in:
@@ -822,3 +822,203 @@ function mystat_render_history_table() {
|
|||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
// --- 3. SHORTCODE DO WYŚWIETLANIA NA FRONCIE ---
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rejestruje shortcode [moje_statystyki].
|
||||||
|
*/
|
||||||
|
function mystat_register_shortcode() {
|
||||||
|
add_shortcode( 'moje_statystyki', 'mystat_shortcode_handler' );
|
||||||
|
add_shortcode( 'moje_statystyki_wpis', 'mystat_single_activity_shortcode_handler' );
|
||||||
|
}
|
||||||
|
add_action( 'init', 'mystat_register_shortcode' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Funkcja obsługująca shortcode.
|
||||||
|
* @param array $atts Atrybuty shortcode'u (np. year, month).
|
||||||
|
* @return string HTML do wyświetlenia.
|
||||||
|
*/
|
||||||
|
function mystat_shortcode_handler( $atts ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
// Ustawienie domyślnych atrybutów (bieżący rok i miesiąc)
|
||||||
|
$atts = shortcode_atts( array(
|
||||||
|
'year' => current_time( 'Y' ),
|
||||||
|
'month' => current_time( 'n' ),
|
||||||
|
), $atts, 'moje_statystyki' );
|
||||||
|
|
||||||
|
$year = intval( $atts['year'] );
|
||||||
|
$month = intval( $atts['month'] );
|
||||||
|
|
||||||
|
// Pobieranie danych z bazy
|
||||||
|
$table_activities = $wpdb->prefix . 'mystat_activities';
|
||||||
|
$sql = $wpdb->prepare("
|
||||||
|
SELECT a.*, c.name as category_name, eq.name as equipment_name
|
||||||
|
FROM $table_activities a
|
||||||
|
LEFT JOIN {$wpdb->prefix}mystat_categories c ON a.category_id = c.id
|
||||||
|
LEFT JOIN {$wpdb->prefix}mystat_equipment eq ON a.equipment_id = eq.id
|
||||||
|
WHERE YEAR(a.date) = %d AND MONTH(a.date) = %d
|
||||||
|
ORDER BY a.date ASC
|
||||||
|
", $year, $month);
|
||||||
|
|
||||||
|
$activities = $wpdb->get_results( $sql );
|
||||||
|
|
||||||
|
// Obliczanie podsumowań
|
||||||
|
$total_distance = 0;
|
||||||
|
$total_seconds = 0;
|
||||||
|
foreach ($activities as $activity) {
|
||||||
|
$total_distance += $activity->distance;
|
||||||
|
if ( ! empty( $activity->duration ) ) {
|
||||||
|
list($h, $m, $s) = explode(':', $activity->duration);
|
||||||
|
$total_seconds += $h * 3600 + $m * 60 + $s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$hours = floor($total_seconds / 3600);
|
||||||
|
$minutes = floor(($total_seconds % 3600) / 60);
|
||||||
|
$total_duration_formatted = sprintf('%d godz. %d min.', $hours, $minutes);
|
||||||
|
|
||||||
|
// Rozpoczęcie buforowania wyjścia
|
||||||
|
ob_start();
|
||||||
|
?>
|
||||||
|
<div class="mystats-shortcode-container">
|
||||||
|
|
||||||
|
<h3>Podsumowanie miesiąca</h3>
|
||||||
|
<table class="mystats-summary-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Całkowity dystans</th>
|
||||||
|
<th>Całkowity czas</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo number_format( $total_distance, 2, ',', ' ' ); ?> km</td>
|
||||||
|
<td><?php echo esc_html( $total_duration_formatted ); ?></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h3>Lista aktywności</h3>
|
||||||
|
<table class="mystats-activity-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Data</th>
|
||||||
|
<th>Tytuł</th>
|
||||||
|
<th>Kategoria</th>
|
||||||
|
<th>Dystans</th>
|
||||||
|
<th>Czas</th>
|
||||||
|
<th>Sprzęt</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php if ( ! empty( $activities ) ) : ?>
|
||||||
|
<?php foreach ( $activities as $row ) : ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo esc_html( date_i18n( 'd.m.Y', strtotime( $row->date ) ) ); ?></td>
|
||||||
|
<td><?php echo esc_html( $row->title ); ?></td>
|
||||||
|
<td><?php echo esc_html( $row->category_name ); ?></td>
|
||||||
|
<td><?php echo number_format( $row->distance, 2, ',', ' ' ); ?> km</td>
|
||||||
|
<td><?php echo esc_html( $row->duration ); ?></td>
|
||||||
|
<td><?php echo esc_html( $row->equipment_name ); ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php else : ?>
|
||||||
|
<tr>
|
||||||
|
<td colspan="6">Brak aktywności w tym miesiącu.</td>
|
||||||
|
</tr>
|
||||||
|
<?php endif; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<style>
|
||||||
|
.mystats-shortcode-container table { width: 100%; border-collapse: collapse; margin-bottom: 2em; }
|
||||||
|
.mystats-shortcode-container th, .mystats-shortcode-container td { padding: 8px 12px; border: 1px solid #ddd; text-align: left; }
|
||||||
|
.mystats-shortcode-container th { background-color: #f4f4f4; }
|
||||||
|
.mystats-activity-table td:nth-child(4), .mystats-activity-table td:nth-child(5) { text-align: right; }
|
||||||
|
</style>
|
||||||
|
<?php
|
||||||
|
// Zwrócenie zawartości bufora
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Funkcja obsługująca shortcode [moje_statystyki_wpis].
|
||||||
|
* @param array $atts Atrybuty shortcode'u (np. id).
|
||||||
|
* @return string HTML do wyświetlenia.
|
||||||
|
*/
|
||||||
|
function mystat_single_activity_shortcode_handler( $atts ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$atts = shortcode_atts( array(
|
||||||
|
'id' => 0,
|
||||||
|
), $atts, 'moje_statystyki_wpis' );
|
||||||
|
|
||||||
|
$activity_id = intval( $atts['id'] );
|
||||||
|
|
||||||
|
if ( $activity_id === 0 ) {
|
||||||
|
return '<p><strong>Błąd:</strong> Nie podano ID wpisu w shortcode.</p>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pobieranie danych z bazy
|
||||||
|
$table_activities = $wpdb->prefix . 'mystat_activities';
|
||||||
|
$sql = $wpdb->prepare("
|
||||||
|
SELECT a.*, c.name as category_name, et.name as event_type_name, eq.name as equipment_name
|
||||||
|
FROM $table_activities a
|
||||||
|
LEFT JOIN {$wpdb->prefix}mystat_categories c ON a.category_id = c.id
|
||||||
|
LEFT JOIN {$wpdb->prefix}mystat_event_types et ON a.event_type_id = et.id
|
||||||
|
LEFT JOIN {$wpdb->prefix}mystat_equipment eq ON a.equipment_id = eq.id
|
||||||
|
WHERE a.id = %d
|
||||||
|
", $activity_id);
|
||||||
|
|
||||||
|
$activity = $wpdb->get_row( $sql );
|
||||||
|
|
||||||
|
if ( ! $activity ) {
|
||||||
|
return '<p><strong>Błąd:</strong> Nie znaleziono wpisu o ID ' . esc_html($activity_id) . '.</p>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funkcja pomocnicza do wyświetlania wiersza
|
||||||
|
$render_row = function($label, $value, $unit = '') {
|
||||||
|
if ( ! is_null($value) && $value !== '' && $value != 0) {
|
||||||
|
echo '<tr>';
|
||||||
|
echo '<th>' . esc_html($label) . '</th>';
|
||||||
|
echo '<td>' . esc_html($value) . ($unit ? ' ' . esc_html($unit) : '') . '</td>';
|
||||||
|
echo '</tr>';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
?>
|
||||||
|
<div class="mystat-single-activity-shortcode">
|
||||||
|
<h4><?php echo esc_html( $activity->title ); ?></h4>
|
||||||
|
<p><em><?php echo esc_html( date_i18n( 'j F Y', strtotime( $activity->date ) ) ); ?></em></p>
|
||||||
|
|
||||||
|
<table class="mystat-single-summary-table">
|
||||||
|
<tbody>
|
||||||
|
<?php $render_row('Dystans', number_format($activity->distance, 2, ',', ' '), 'km'); ?>
|
||||||
|
<?php $render_row('Czas trwania', $activity->duration); ?>
|
||||||
|
<?php $render_row('Średnia prędkość', number_format($activity->avg_speed, 1, ',', ' '), 'km/h'); ?>
|
||||||
|
<?php $render_row('Suma wzniosów', $activity->total_elevation_gain, 'm'); ?>
|
||||||
|
<?php $render_row('Kategoria', $activity->category_name); ?>
|
||||||
|
<?php $render_row('Sprzęt', $activity->equipment_name); ?>
|
||||||
|
<?php if ( ! empty( $activity->strava_url ) ) : ?>
|
||||||
|
<tr><th>Strava</th><td><a href="<?php echo esc_url( $activity->strava_url ); ?>" target="_blank" rel="noopener noreferrer">Zobacz aktywność</a></td></tr>
|
||||||
|
<?php endif; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<style>
|
||||||
|
.mystat-single-activity-shortcode { border: 1px solid #eee; padding: 15px; margin-bottom: 1.5em; border-radius: 5px; background: #f9f9f9; }
|
||||||
|
.mystat-single-activity-shortcode h4 { margin-top: 0; }
|
||||||
|
.mystat-single-activity-shortcode p em { color: #777; font-size: 0.9em; }
|
||||||
|
.mystat-single-summary-table { width: 100%; border-collapse: collapse; }
|
||||||
|
.mystat-single-summary-table th, .mystat-single-summary-table td { padding: 4px 0; border: none; text-align: left; vertical-align: top; }
|
||||||
|
.mystat-single-summary-table th { font-weight: bold; width: 150px; }
|
||||||
|
</style>
|
||||||
|
<?php
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user