Update repo
This commit is contained in:
@@ -1,195 +1,233 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
function statpress_dashboard_page() {
|
||||
echo '<div class="wrap"><h1>StatPress Dashboard</h1>';
|
||||
statpress_render_history_table();
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
function statpress_render_history_table() {
|
||||
global $wpdb;
|
||||
|
||||
// Definicje nazw tabel (z uwzględnieniem prefixu WP, jeśli był użyty przy tworzeniu)
|
||||
// Zakładam, że tabele nazywają się dokładnie tak jak w dokumentacji, ale dobrą praktyką jest $wpdb->prefix
|
||||
// Jeśli tabele są "sztywne" (bez prefixu wp_), usuń $wpdb->prefix.
|
||||
$table_activities = $wpdb->prefix . 'statpress_activities';
|
||||
$table_categories = $wpdb->prefix . 'statpress_categories';
|
||||
|
||||
// --- 1. OBSŁUGA USUWANIA (DELETE) ---
|
||||
if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && 'statpress_delete' === $_GET['action'] ) {
|
||||
$activity_id = intval( $_GET['id'] );
|
||||
|
||||
// Weryfikacja bezpieczeństwa (Nonce)
|
||||
if ( wp_verify_nonce( $_GET['_wpnonce'], 'statpress_delete_' . $activity_id ) ) {
|
||||
$result = $wpdb->delete(
|
||||
$table_activities,
|
||||
array( 'id' => $activity_id ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
if ( $result ) {
|
||||
echo '<div class="notice notice-success is-dismissible"><p>Aktywność została usunięta.</p></div>';
|
||||
} else {
|
||||
echo '<div class="notice notice-error is-dismissible"><p>Wystąpił błąd podczas usuwania.</p></div>';
|
||||
}
|
||||
} else {
|
||||
echo '<div class="notice notice-error is-dismissible"><p>Błąd weryfikacji bezpieczeństwa (Nonce).</p></div>';
|
||||
}
|
||||
}
|
||||
|
||||
// --- 2. USTAWIENIA PAGINACJI ---
|
||||
$items_per_page = 20; // Ile wpisów na stronę
|
||||
$current_page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
|
||||
$offset = ( $current_page - 1 ) * $items_per_page;
|
||||
|
||||
// --- 3. POBIERANIE DANYCH (SELECT) ---
|
||||
// Pobranie całkowitej liczby wpisów do paginacji
|
||||
$total_items = $wpdb->get_var( "SELECT COUNT(id) FROM $table_activities" );
|
||||
$total_pages = ceil( $total_items / $items_per_page );
|
||||
|
||||
// Pobieramy wpisy dla bieżącej strony
|
||||
$sql = $wpdb->prepare(
|
||||
"
|
||||
SELECT a.*, c.name as category_name, c.icon, c.color, et.name as event_type_name, eq.name as equipment_name
|
||||
FROM $table_activities a
|
||||
LEFT JOIN $table_categories c ON a.category_id = c.id
|
||||
LEFT JOIN {$wpdb->prefix}statpress_event_types et ON a.event_type_id = et.id
|
||||
LEFT JOIN {$wpdb->prefix}statpress_equipment eq ON a.equipment_id = eq.id
|
||||
ORDER BY a.date DESC, a.id DESC
|
||||
LIMIT %d OFFSET %d
|
||||
",
|
||||
$items_per_page,
|
||||
$offset
|
||||
);
|
||||
|
||||
$activities = $wpdb->get_results( $sql );
|
||||
|
||||
// --- 4. WIDOK TABELI (HTML) ---
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h2>Historia Aktywności</h2>
|
||||
|
||||
<?php if ( $total_pages > 1 ) : ?>
|
||||
<div class="tablenav top">
|
||||
<div class="tablenav-pages">
|
||||
<span class="displaying-num"><?php echo esc_html( $total_items ); ?> aktywności</span>
|
||||
<?php
|
||||
echo paginate_links(
|
||||
array(
|
||||
'base' => add_query_arg( 'paged', '%#%' ),
|
||||
'format' => '',
|
||||
'total' => $total_pages,
|
||||
'current' => $current_page,
|
||||
'prev_text' => '« Poprzednia',
|
||||
'next_text' => 'Następna »',
|
||||
)
|
||||
);
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<table class="wp-list-table widefat fixed striped table-view-list">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col" style="width: 50px;">Ikona</th>
|
||||
<th scope="col">Data</th>
|
||||
<th scope="col">Tytuł</th>
|
||||
<th scope="col" style="width: 120px;">Kategoria</th>
|
||||
<th scope="col">Typ</th>
|
||||
<th scope="col">Sprzęt</th>
|
||||
<th scope="col">Dystans (km)</th>
|
||||
<th scope="col">Czas</th>
|
||||
<th scope="col">Śr. prędkość</th>
|
||||
<th scope="col" style="width: 180px;">Akcja</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if ( ! empty( $activities ) ) : ?>
|
||||
<?php foreach ( $activities as $row ) : ?>
|
||||
<?php
|
||||
// Generowanie URL-i akcji z zachowaniem paginacji
|
||||
$delete_url = wp_nonce_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'action' => 'statpress_delete',
|
||||
'id' => $row->id,
|
||||
)
|
||||
),
|
||||
'statpress_delete_' . $row->id
|
||||
);
|
||||
|
||||
$edit_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'statpress-edit-activity',
|
||||
'id' => $row->id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
$details_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'statpress-view-activity',
|
||||
'id' => $row->id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
?>
|
||||
<tr>
|
||||
<td>
|
||||
<?php if ( ! empty( $row->icon ) ) : ?>
|
||||
<span class="dashicons <?php echo esc_attr( $row->icon ); ?>" style="color: <?php echo esc_attr( $row->color ); ?>;"></span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo esc_html( $row->date ); ?></td>
|
||||
<td><strong><a href="<?php echo esc_url( $details_url ); ?>"><?php echo esc_html( $row->title ? wp_trim_words( $row->title, 6 ) : '(bez tytułu)' ); ?></a></strong></td>
|
||||
<td><?php echo esc_html( $row->category_name ); ?></td>
|
||||
<td><?php echo esc_html( $row->event_type_name ); ?></td>
|
||||
<td><?php echo esc_html( $row->equipment_name ); ?></td>
|
||||
<td><?php echo number_format( $row->distance, 2, ',', ' ' ); ?></td>
|
||||
<td><?php echo esc_html( $row->duration ); ?></td>
|
||||
<td><?php echo $row->avg_speed ? number_format( $row->avg_speed, 1, ',', ' ' ) . ' km/h' : '-'; ?></td>
|
||||
<td>
|
||||
<a href="<?php echo esc_url( $edit_url ); ?>" class="button button-small">Edytuj</a>
|
||||
<a href="<?php echo esc_url( $details_url ); ?>" class="button button-small">Szczegóły</a>
|
||||
<a href="<?php echo esc_url( $delete_url ); ?>"
|
||||
class="button button-small button-link-delete"
|
||||
onclick="return confirm('Czy na pewno chcesz usunąć ten wpis?');">
|
||||
Usuń
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php else : ?>
|
||||
<tr>
|
||||
<td colspan="10">Brak zarejestrowanych aktywności. Dodaj pierwszy trening powyżej!</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<?php if ( $total_pages > 1 ) : ?>
|
||||
<div class="tablenav bottom">
|
||||
<div class="tablenav-pages">
|
||||
<span class="displaying-num"><?php echo esc_html( $total_items ); ?> aktywności</span>
|
||||
<?php
|
||||
echo paginate_links(
|
||||
array(
|
||||
'base' => add_query_arg( 'paged', '%#%' ),
|
||||
'format' => '',
|
||||
'total' => $total_pages,
|
||||
'current' => $current_page,
|
||||
'prev_text' => '« Poprzednia',
|
||||
'next_text' => 'Następna »',
|
||||
)
|
||||
);
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
function statpress_dashboard_page() {
|
||||
echo '<div class="wrap"><h1>StatPress Dashboard</h1>';
|
||||
|
||||
// --- MIGRATION NOTICE ---
|
||||
// Show the migration button if it hasn't been completed yet.
|
||||
if ( ! get_option( 'statpress_migration_complete' ) ) {
|
||||
$migration_url = wp_nonce_url(
|
||||
admin_url( 'admin.php?page=statpress-dashboard&action=statpress_migrate_data' ),
|
||||
'statpress_migration_nonce'
|
||||
);
|
||||
echo '<div class="notice notice-warning is-dismissible" style="padding-bottom: 10px;">';
|
||||
echo '<h4>Migracja danych StatPress</h4>';
|
||||
echo '<p>Wygląda na to, że istnieją dane w starych tabelach (<code>mystat_*</code>), które można przenieść. Kliknij przycisk poniżej, aby rozpocząć proces.</p>';
|
||||
echo '<p><strong>Ważne:</strong> Ta operacja jest jednorazowa i powinna być wykonana tylko raz, na pustej instalacji wtyczki StatPress.</p>';
|
||||
echo '<a href="' . esc_url( $migration_url ) . '" class="button button-primary">Rozpocznij migrację danych</a>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// Show the results of the migration after it's done.
|
||||
$migration_results = get_transient( 'statpress_migration_results' );
|
||||
if ( $migration_results ) {
|
||||
echo '<div class="notice notice-success is-dismissible">';
|
||||
echo '<h4>Migracja zakończona!</h4>';
|
||||
echo '<ul>';
|
||||
foreach ( $migration_results as $table => $result ) {
|
||||
if ( 'success' === $result['status'] ) {
|
||||
echo '<li>Tabela <strong>' . esc_html( $table ) . '</strong>: Przeniesiono <strong>' . esc_html( $result['count'] ) . '</strong> wierszy.</li>';
|
||||
} elseif ( 'skipped' === $result['status'] ) {
|
||||
echo '<li>Tabela <strong>' . esc_html( $table ) . '</strong>: Pominięto, ponieważ nowa tabela zawiera już dane (' . esc_html( $result['count'] ) . ' wierszy).</li>';
|
||||
} elseif ( 'failure' === $result['status'] ) {
|
||||
echo '<li>Tabela <strong>' . esc_html( $table ) . '</strong>: <strong style="color:red;">Migracja nieudana.</strong> Błąd bazy danych: <pre style="display:inline;white-space:pre-wrap;">' . esc_html( $result['error'] ) . '</pre></li>';
|
||||
}
|
||||
}
|
||||
echo '</ul>';
|
||||
echo '<p>Twoje dane powinny być teraz widoczne. Stare tabele (<code>' . esc_html( $GLOBALS['wpdb']->prefix ) . 'mystat_*</code>) wciąż istnieją w bazie danych, ale nie są już używane. Możesz je usunąć ręcznie (np. przez phpMyAdmin), jeśli wszystko działa poprawnie.</p>';
|
||||
echo '</div>';
|
||||
delete_transient( 'statpress_migration_results' );
|
||||
}
|
||||
// --- END MIGRATION NOTICE ---
|
||||
|
||||
statpress_render_history_table();
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
function statpress_render_history_table() {
|
||||
global $wpdb;
|
||||
|
||||
// Definicje nazw tabel (z uwzględnieniem prefixu WP, jeśli był użyty przy tworzeniu)
|
||||
// Zakładam, że tabele nazywają się dokładnie tak jak w dokumentacji, ale dobrą praktyką jest $wpdb->prefix
|
||||
// Jeśli tabele są "sztywne" (bez prefixu wp_), usuń $wpdb->prefix.
|
||||
$table_activities = $wpdb->prefix . 'statpress_activities';
|
||||
$table_categories = $wpdb->prefix . 'statpress_categories';
|
||||
|
||||
// --- 1. OBSŁUGA USUWANIA (DELETE) ---
|
||||
if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && 'statpress_delete' === $_GET['action'] ) {
|
||||
$activity_id = intval( $_GET['id'] );
|
||||
|
||||
// Weryfikacja bezpieczeństwa (Nonce)
|
||||
if ( wp_verify_nonce( $_GET['_wpnonce'], 'statpress_delete_' . $activity_id ) ) {
|
||||
$result = $wpdb->delete(
|
||||
$table_activities,
|
||||
array( 'id' => $activity_id ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
if ( $result ) {
|
||||
echo '<div class="notice notice-success is-dismissible"><p>Aktywność została usunięta.</p></div>';
|
||||
} else {
|
||||
echo '<div class="notice notice-error is-dismissible"><p>Wystąpił błąd podczas usuwania.</p></div>';
|
||||
}
|
||||
} else {
|
||||
echo '<div class="notice notice-error is-dismissible"><p>Błąd weryfikacji bezpieczeństwa (Nonce).</p></div>';
|
||||
}
|
||||
}
|
||||
|
||||
// --- 2. USTAWIENIA PAGINACJI ---
|
||||
$items_per_page = 20; // Ile wpisów na stronę
|
||||
$current_page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
|
||||
$offset = ( $current_page - 1 ) * $items_per_page;
|
||||
|
||||
// --- 3. POBIERANIE DANYCH (SELECT) ---
|
||||
// Pobranie całkowitej liczby wpisów do paginacji
|
||||
$total_items = $wpdb->get_var( "SELECT COUNT(id) FROM $table_activities" );
|
||||
$total_pages = ceil( $total_items / $items_per_page );
|
||||
|
||||
// Pobieramy wpisy dla bieżącej strony
|
||||
$sql = $wpdb->prepare(
|
||||
"
|
||||
SELECT a.*, c.name as category_name, c.icon, c.color, et.name as event_type_name, eq.name as equipment_name
|
||||
FROM $table_activities a
|
||||
LEFT JOIN $table_categories c ON a.category_id = c.id
|
||||
LEFT JOIN {$wpdb->prefix}statpress_event_types et ON a.event_type_id = et.id
|
||||
LEFT JOIN {$wpdb->prefix}statpress_equipment eq ON a.equipment_id = eq.id
|
||||
ORDER BY a.date DESC, a.id DESC
|
||||
LIMIT %d OFFSET %d
|
||||
",
|
||||
$items_per_page,
|
||||
$offset
|
||||
);
|
||||
|
||||
$activities = $wpdb->get_results( $sql );
|
||||
|
||||
// --- 4. WIDOK TABELI (HTML) ---
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h2>Historia Aktywności</h2>
|
||||
|
||||
<?php if ( $total_pages > 1 ) : ?>
|
||||
<div class="tablenav top">
|
||||
<div class="tablenav-pages">
|
||||
<span class="displaying-num"><?php echo esc_html( $total_items ); ?> aktywności</span>
|
||||
<?php
|
||||
echo paginate_links(
|
||||
array(
|
||||
'base' => add_query_arg( 'paged', '%#%' ),
|
||||
'format' => '',
|
||||
'total' => $total_pages,
|
||||
'current' => $current_page,
|
||||
'prev_text' => '« Poprzednia',
|
||||
'next_text' => 'Następna »',
|
||||
)
|
||||
);
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<table class="wp-list-table widefat fixed striped table-view-list">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col" style="width: 50px;">Ikona</th>
|
||||
<th scope="col">Data</th>
|
||||
<th scope="col">Tytuł</th>
|
||||
<th scope="col" style="width: 120px;">Kategoria</th>
|
||||
<th scope="col">Typ</th>
|
||||
<th scope="col">Sprzęt</th>
|
||||
<th scope="col">Dystans (km)</th>
|
||||
<th scope="col">Czas</th>
|
||||
<th scope="col">Śr. prędkość</th>
|
||||
<th scope="col" style="width: 180px;">Akcja</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if ( ! empty( $activities ) ) : ?>
|
||||
<?php foreach ( $activities as $row ) : ?>
|
||||
<?php
|
||||
// Generowanie URL-i akcji z zachowaniem paginacji
|
||||
$delete_url = wp_nonce_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'action' => 'statpress_delete',
|
||||
'id' => $row->id,
|
||||
)
|
||||
),
|
||||
'statpress_delete_' . $row->id
|
||||
);
|
||||
|
||||
$edit_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'statpress-edit-activity',
|
||||
'id' => $row->id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
$details_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'statpress-view-activity',
|
||||
'id' => $row->id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
?>
|
||||
<tr>
|
||||
<td>
|
||||
<?php if ( ! empty( $row->icon ) ) : ?>
|
||||
<span class="dashicons <?php echo esc_attr( $row->icon ); ?>" style="color: <?php echo esc_attr( $row->color ); ?>;"></span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo esc_html( $row->date ); ?></td>
|
||||
<td><strong><a href="<?php echo esc_url( $details_url ); ?>"><?php echo esc_html( $row->title ? wp_trim_words( $row->title, 6 ) : '(bez tytułu)' ); ?></a></strong></td>
|
||||
<td><?php echo esc_html( $row->category_name ); ?></td>
|
||||
<td><?php echo esc_html( $row->event_type_name ); ?></td>
|
||||
<td><?php echo esc_html( $row->equipment_name ); ?></td>
|
||||
<td><?php echo number_format( $row->distance, 2, ',', ' ' ); ?></td>
|
||||
<td><?php echo esc_html( $row->duration ); ?></td>
|
||||
<td><?php echo $row->avg_speed ? number_format( $row->avg_speed, 1, ',', ' ' ) . ' km/h' : '-'; ?></td>
|
||||
<td>
|
||||
<a href="<?php echo esc_url( $edit_url ); ?>" class="button button-small">Edytuj</a>
|
||||
<a href="<?php echo esc_url( $details_url ); ?>" class="button button-small">Szczegóły</a>
|
||||
<a href="<?php echo esc_url( $delete_url ); ?>"
|
||||
class="button button-small button-link-delete"
|
||||
onclick="return confirm('Czy na pewno chcesz usunąć ten wpis?');">
|
||||
Usuń
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php else : ?>
|
||||
<tr>
|
||||
<td colspan="10">Brak zarejestrowanych aktywności. Dodaj pierwszy trening powyżej!</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<?php if ( $total_pages > 1 ) : ?>
|
||||
<div class="tablenav bottom">
|
||||
<div class="tablenav-pages">
|
||||
<span class="displaying-num"><?php echo esc_html( $total_items ); ?> aktywności</span>
|
||||
<?php
|
||||
echo paginate_links(
|
||||
array(
|
||||
'base' => add_query_arg( 'paged', '%#%' ),
|
||||
'format' => '',
|
||||
'total' => $total_pages,
|
||||
'current' => $current_page,
|
||||
'prev_text' => '« Poprzednia',
|
||||
'next_text' => 'Następna »',
|
||||
)
|
||||
);
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
Reference in New Issue
Block a user