Shortcodes: add home blocks and global custom CSS setting
This commit is contained in:
@@ -2,10 +2,60 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
use Core\Http\Router;
|
||||
use Core\Services\Database;
|
||||
use Core\Services\Shortcodes;
|
||||
use Plugins\Artists\ArtistsController;
|
||||
|
||||
require_once __DIR__ . '/ArtistsController.php';
|
||||
|
||||
Shortcodes::register('new-artists', static function (array $attrs = []): string {
|
||||
$limit = max(1, min(20, (int)($attrs['limit'] ?? 6)));
|
||||
$db = Database::get();
|
||||
if (!($db instanceof \PDO)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
SELECT name, slug, country, avatar_url
|
||||
FROM ac_artists
|
||||
WHERE is_active = 1
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT :limit
|
||||
");
|
||||
$stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
|
||||
} catch (\Throwable $e) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!$rows) {
|
||||
return '<div class="ac-shortcode-empty">No artists available yet.</div>';
|
||||
}
|
||||
|
||||
$cards = '';
|
||||
foreach ($rows as $row) {
|
||||
$name = htmlspecialchars((string)($row['name'] ?? ''), ENT_QUOTES, 'UTF-8');
|
||||
$slug = rawurlencode((string)($row['slug'] ?? ''));
|
||||
$country = htmlspecialchars(trim((string)($row['country'] ?? '')), ENT_QUOTES, 'UTF-8');
|
||||
$avatar = trim((string)($row['avatar_url'] ?? ''));
|
||||
$avatarHtml = $avatar !== ''
|
||||
? '<img src="' . htmlspecialchars($avatar, ENT_QUOTES, 'UTF-8') . '" alt="" loading="lazy">'
|
||||
: '<div class="ac-shortcode-cover-fallback">AC</div>';
|
||||
|
||||
$cards .= '<a class="ac-shortcode-artist-card" href="/artist?slug=' . $slug . '">'
|
||||
. '<div class="ac-shortcode-artist-avatar">' . $avatarHtml . '</div>'
|
||||
. '<div class="ac-shortcode-artist-meta">'
|
||||
. '<div class="ac-shortcode-artist-name">' . $name . '</div>'
|
||||
. ($country !== '' ? '<div class="ac-shortcode-artist-country">' . $country . '</div>' : '')
|
||||
. '</div>'
|
||||
. '</a>';
|
||||
}
|
||||
|
||||
return '<section class="ac-shortcode-artists"><div class="ac-shortcode-artists-grid">' . $cards . '</div></section>';
|
||||
});
|
||||
|
||||
return function (Router $router): void {
|
||||
$controller = new ArtistsController();
|
||||
$router->get('/artists', [$controller, 'index']);
|
||||
|
||||
Reference in New Issue
Block a user