Files
CodePress/plugins/Statistics/Statistics.php
T
E.Noorlander 5edc929c13 v2.6.1d (Lyra): Plugin i18n, plugin editor vernieuwd, media invoegen
Plugin internationalisatie: plugins hebben eigen language/ mappen. Systeem
plugins volgen admin taal (admin.php), content plugins volgen content taal
(site.php). Fallback chain: geselecteerd -> plugin default_language -> CMS
default. PluginManager/AdminPluginAPI/CMSAPI uitgebreid met
getPluginTranslations()/t(). plugin.json settings ondersteunen
label_key/help_key/option_label_key.

Plugin uniformiteit: alle 6 plugins hebben uniforme structuur (README.md,
assets/.gitkeep, language/nl|en/). plugins/README.md herschreven.
guide plugin-development.md (NL+EN) volledig herschreven.

Plugin editor vernieuwd: geneste bestandsbrowser zijbalk, nieuw bestand
aanmaken, uploaden naar assets/, verwijderen en verplaatsen. Nieuwe routes:
plugins-file-upload, plugins-file-delete, plugins-file-move. Path-traversal
bescherming + protected plugins geblokkeerd.

Media invoegen in editor: nieuw /admin/media-list JSON endpoint + herbruikbare
_media-modal.twig include. Plugin-context scant assets/ map. editor-toolbar.js
modeMap uitgebreid voor css/scss/js/json.

Plugin overzicht knoppen: alleen iconen met title/aria-label.

Tests: pentest 30/30, WCAG 2.1 AA 25/25.
2026-08-18 13:49:52 +00:00

196 lines
7.9 KiB
PHP

<?php
class Statistics
{
private ?PluginAPIInterface $api = null;
public function setAPI(PluginAPIInterface $api): void
{
$this->api = $api;
}
public function getConfig(): array
{
return [
'title' => 'Statistics',
'viewable' => false,
'type' => 'system',
];
}
public function getAdminMenu(): array
{
$config = $this->getPluginConfig();
$requiredRoles = $config['required_roles'] ?? ['bi-manager', 'site-admin', 'admin'];
return [
[
'plugin' => 'Statistics',
'route' => 'statistics',
'label' => 'Statistieken',
'label_key' => 'menu_label',
'icon' => 'bi-bar-chart',
'section' => 'general',
'permission' => 'statistics',
'required_roles' => $requiredRoles,
],
];
}
/**
* Get this plugin's runtime config (defaults + overrides).
*/
private function getPluginConfig(): array
{
$pluginDir = dirname(__DIR__);
$pluginJsonFile = $pluginDir . '/plugin.json';
$configJsonFile = $pluginDir . '/config.json';
$defaults = [];
if (file_exists($pluginJsonFile)) {
$pluginJson = json_decode(file_get_contents($pluginJsonFile), true) ?? [];
foreach ($pluginJson['settings'] ?? [] as $setting) {
if (isset($setting['key'])) {
$defaults[$setting['key']] = $setting['default'] ?? null;
}
}
}
$overrides = [];
if (file_exists($configJsonFile)) {
$overrides = json_decode(file_get_contents($configJsonFile), true) ?? [];
}
return array_merge($defaults, $overrides);
}
public function handleAdminRoute(string $action): ?string
{
// System plugin: translations resolve against the admin language.
$t = $this->api ? $this->api->getPluginTranslations('Statistics') : [];
$tr = function (string $key) use ($t): string {
return $t[$key] ?? $key;
};
$analytics = $this->getAnalytics();
if ($analytics === null) {
return '<div class="alert alert-warning">' . htmlspecialchars($tr('analytics_disabled')) . '</div>';
}
$config = $this->getPluginConfig();
$showCountries = $config['show_countries'] ?? true;
$showTopPages = $config['show_top_pages'] ?? true;
$stats = $analytics->getStats();
$totals = $stats['totals'] ?? ['views' => 0, 'uniques' => 0, 'pages' => []];
$countries = $stats['countries'] ?? [];
$topPages = $stats['top_pages'] ?? $totals['pages'] ?? [];
ob_start();
?>
<h2 class="mb-4"><i class="bi bi-bar-chart"></i> <?= htmlspecialchars($tr('page_title')) ?></h2>
<div class="row g-4 mb-4">
<div class="col-md-4">
<div class="card stat-card shadow-sm">
<div class="card-body d-flex justify-content-between align-items-center">
<div>
<h6 class="text-muted mb-1"><?= htmlspecialchars($tr('total_views')) ?></h6>
<h3 class="mb-0"><?= number_format($totals['views'] ?? 0, 0, ',', '.') ?></h3>
</div>
<i class="bi bi-eye stat-icon text-primary"></i>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card stat-card shadow-sm">
<div class="card-body d-flex justify-content-between align-items-center">
<div>
<h6 class="text-muted mb-1"><?= htmlspecialchars($tr('unique_visitors')) ?></h6>
<h3 class="mb-0"><?= number_format($totals['uniques'] ?? 0, 0, ',', '.') ?></h3>
</div>
<i class="bi bi-people stat-icon text-success"></i>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card stat-card shadow-sm">
<div class="card-body d-flex justify-content-between align-items-center">
<div>
<h6 class="text-muted mb-1"><?= htmlspecialchars($tr('page_views')) ?></h6>
<h3 class="mb-0"><?= number_format($totals['page_views'] ?? ($totals['views'] ?? 0), 0, ',', '.') ?></h3>
</div>
<i class="bi bi-file-earmark-text stat-icon text-info"></i>
</div>
</div>
</div>
</div>
<?php if ($showCountries || $showTopPages): ?>
<div class="row g-4">
<?php if ($showCountries): ?>
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header"><i class="bi bi-globe"></i> <?= htmlspecialchars($tr('countries')) ?></div>
<div class="card-body">
<?php if (empty($countries)): ?>
<p class="text-muted mb-0"><?= htmlspecialchars($tr('no_data')) ?></p>
<?php else: ?>
<table class="table table-sm mb-0">
<thead><tr><th><?= htmlspecialchars($tr('col_country')) ?></th><th><?= htmlspecialchars($tr('col_views')) ?></th></tr></thead>
<tbody>
<?php foreach ($countries as $country => $count): ?>
<tr>
<td><?= GeoIP::getCountryFlagEmoji($country) ?> <?= htmlspecialchars(GeoIP::getCountryName($country)) ?></td>
<td><?= number_format($count, 0, ',', '.') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($showTopPages): ?>
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header"><i class="bi bi-file-earmark-bar"></i> <?= htmlspecialchars($tr('top_pages')) ?></div>
<div class="card-body">
<?php if (empty($topPages)): ?>
<p class="text-muted mb-0"><?= htmlspecialchars($tr('no_data')) ?></p>
<?php else: ?>
<table class="table table-sm mb-0">
<thead><tr><th><?= htmlspecialchars($tr('col_page')) ?></th><th><?= htmlspecialchars($tr('col_views')) ?></th></tr></thead>
<tbody>
<?php foreach ($topPages as $page => $count): ?>
<tr>
<td><?= htmlspecialchars($page) ?></td>
<td><?= number_format($count, 0, ',', '.') ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<?php
return ob_get_clean();
}
private function getAnalytics(): ?Analytics
{
if ($this->api === null) {
return null;
}
$analyticsConfig = $this->api->getConfig('analytics', []);
if (!is_array($analyticsConfig) || empty($analyticsConfig['enabled'])) {
return null;
}
return new Analytics($analyticsConfig);
}
}