Shortcodes: add home blocks and global custom CSS setting

This commit is contained in:
AudioCore Bot
2026-03-05 17:09:01 +00:00
parent 93e829bd19
commit af6bb638ac
7 changed files with 273 additions and 227 deletions

View File

@@ -39,18 +39,9 @@ Shortcodes::register('sale-chart', static function (array $attrs = []): string {
}
$rows = [];
try {
$latestPaid = (string)($db->query("SELECT MAX(updated_at) FROM ac_store_orders WHERE status = 'paid'")->fetchColumn() ?? '');
$latestCache = (string)($db->query("SELECT MAX(updated_at) FROM ac_store_sales_chart_cache")->fetchColumn() ?? '');
if ($latestPaid !== '' && ($latestCache === '' || strcmp($latestPaid, $latestCache) > 0)) {
(new StoreController())->rebuildSalesChartCache();
}
} catch (\Throwable $e) {
}
try {
$stmt = $db->prepare("
SELECT item_key, item_label AS title, units, revenue
SELECT item_label AS title, units, revenue
FROM ac_store_sales_chart_cache
WHERE chart_scope = :scope
AND chart_window = :window
@@ -66,16 +57,19 @@ Shortcodes::register('sale-chart', static function (array $attrs = []): string {
$rows = [];
}
if (!$rows && $scope === 'tracks') {
if (!$rows) {
try {
$controller = new StoreController();
$controller->rebuildSalesChartCache();
$stmt = $db->prepare("
SELECT item_key, item_label AS title, units, revenue
SELECT item_label AS title, units, revenue
FROM ac_store_sales_chart_cache
WHERE chart_scope = 'releases'
WHERE chart_scope = :scope
AND chart_window = :window
ORDER BY rank_no ASC
LIMIT :limit
");
$stmt->bindValue(':scope', $scope, \PDO::PARAM_STR);
$stmt->bindValue(':window', $window, \PDO::PARAM_STR);
$stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
$stmt->execute();
@@ -89,98 +83,33 @@ Shortcodes::register('sale-chart', static function (array $attrs = []): string {
return '<div class="ac-shortcode-empty">No sales yet.</div>';
}
$releaseIds = [];
$trackIds = [];
foreach ($rows as $row) {
$itemKey = trim((string)($row['item_key'] ?? ''));
if (preg_match('/^release:(\d+)$/', $itemKey, $m)) {
$releaseIds[] = (int)$m[1];
} elseif (preg_match('/^track:(\d+)$/', $itemKey, $m)) {
$trackIds[] = (int)$m[1];
}
}
$releaseIds = array_values(array_unique(array_filter($releaseIds)));
$trackIds = array_values(array_unique(array_filter($trackIds)));
$releaseMap = [];
if ($releaseIds) {
try {
$in = implode(',', array_fill(0, count($releaseIds), '?'));
$stmt = $db->prepare("
SELECT id, title, slug, cover_url, COALESCE(artist_name, '') AS artist_name
FROM ac_releases
WHERE id IN ({$in})
");
foreach ($releaseIds as $i => $rid) {
$stmt->bindValue($i + 1, $rid, \PDO::PARAM_INT);
}
$stmt->execute();
$rels = $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
foreach ($rels as $rel) {
$releaseMap['release:' . (int)$rel['id']] = $rel;
}
} catch (\Throwable $e) {
}
}
if ($trackIds) {
try {
$in = implode(',', array_fill(0, count($trackIds), '?'));
$stmt = $db->prepare("
SELECT t.id AS track_id, r.id, r.title, r.slug, r.cover_url, COALESCE(r.artist_name, '') AS artist_name
FROM ac_release_tracks t
JOIN ac_releases r ON r.id = t.release_id
WHERE t.id IN ({$in})
");
foreach ($trackIds as $i => $tid) {
$stmt->bindValue($i + 1, $tid, \PDO::PARAM_INT);
}
$stmt->execute();
$rels = $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
foreach ($rels as $rel) {
$releaseMap['track:' . (int)$rel['track_id']] = $rel;
}
} catch (\Throwable $e) {
}
$currency = strtoupper(trim((string)Settings::get('store_currency', 'GBP')));
if (!preg_match('/^[A-Z]{3}$/', $currency)) {
$currency = 'GBP';
}
$list = '';
$position = 1;
foreach ($rows as $row) {
$itemKey = trim((string)($row['item_key'] ?? ''));
$rel = $releaseMap[$itemKey] ?? null;
$titleRaw = $rel ? (string)($rel['title'] ?? '') : (string)($row['title'] ?? '');
$artistRaw = $rel ? trim((string)($rel['artist_name'] ?? '')) : '';
$slugRaw = $rel ? trim((string)($rel['slug'] ?? '')) : '';
$coverRaw = $rel ? trim((string)($rel['cover_url'] ?? '')) : '';
$title = htmlspecialchars($titleRaw !== '' ? $titleRaw : ((string)($row['title'] ?? 'Release')), ENT_QUOTES, 'UTF-8');
$artist = htmlspecialchars($artistRaw, ENT_QUOTES, 'UTF-8');
$href = $slugRaw !== '' ? '/release?slug=' . rawurlencode($slugRaw) : '#';
$thumbHtml = $coverRaw !== ''
? '<img src="' . htmlspecialchars($coverRaw, ENT_QUOTES, 'UTF-8') . '" alt="" loading="lazy">'
: '<div class="ac-shortcode-cover-fallback">AC</div>';
$copy = '<span class="ac-shortcode-sale-copy">'
$title = htmlspecialchars((string)($row['title'] ?? ''), ENT_QUOTES, 'UTF-8');
$units = (int)($row['units'] ?? 0);
$revenue = number_format((float)($row['revenue'] ?? 0), 2);
$list .= '<li class="ac-shortcode-sale-item">'
. '<span class="ac-shortcode-sale-rank">#' . $position . '</span>'
. '<span class="ac-shortcode-sale-title">' . $title . '</span>'
. ($artist !== '' ? '<span class="ac-shortcode-sale-artist">' . $artist . '</span>' : '')
. '</span>'
. '<span class="ac-shortcode-sale-rank">' . $position . '</span>';
$content = '<span class="ac-shortcode-sale-thumb">' . $thumbHtml . '</span>' . $copy;
if ($href !== '#') {
$content = '<a class="ac-shortcode-sale-link" href="' . htmlspecialchars($href, ENT_QUOTES, 'UTF-8') . '">' . $content . '</a>';
}
$list .= '<li class="ac-shortcode-sale-item">' . $content . '</li>';
. '<span class="ac-shortcode-sale-meta">' . $units . ' sold - ' . htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') . ' ' . $revenue . '</span>'
. '</li>';
$position++;
}
$heading = htmlspecialchars((string)($attrs['title'] ?? 'Top Sellers'), ENT_QUOTES, 'UTF-8');
return '<section class="ac-shortcode-sale-chart"><header class="ac-shortcode-sale-head"><h3>' . $heading . '</h3></header><ol class="ac-shortcode-sale-list">' . $list . '</ol></section>';
return '<section class="ac-shortcode-sale-chart"><ol class="ac-shortcode-sale-list">' . $list . '</ol></section>';
});
Shortcodes::register('top-sellers', static function (array $attrs = []): string {
$type = trim((string)($attrs['type'] ?? 'tracks'));
$window = trim((string)($attrs['window'] ?? 'latest'));
$limit = max(1, min(50, (int)($attrs['limit'] ?? 10)));
return Shortcodes::render('[sale-chart type="' . $type . '" window="' . $window . '" limit="' . $limit . '"]');
});
Shortcodes::register('login-link', static function (array $attrs = []): string {