/* ───────────── LISTADO (home o categoría) ───────────── * Portada: página 1 = lo más visto (tabla `busquedas`); desde la 2 = lo más reciente. * Categorías: siempre lo más reciente. */ $lang = $site['lang']; $where = $cat ? $cat['where'] : '1=1'; $params = $cat ? $cat['params'] : []; $total = (int)(db_one($db, "SELECT COUNT(id) AS total FROM noticias WHERE $where", $params)['total'] ?? 0); $topRows = $cat === null ? pbn_top_rows($db, $perPage) : []; $topMode = $topRows !== []; // sin "más vistos" → portada clásica de recientes $totalPages = max(1, ($topMode ? 1 : 0) + (int)ceil($total / $perPage)); $base = $cat ? '/categoria/' . $cat['slug'] : ''; $pageUrl = function (int $p) use ($base): string { return $p > 1 ? $base . '/page/' . $p : ($base === '' ? '/' : $base); }; // URLs antiguas (?page=N, ?categoria=x) → URL limpia con 301 (evita duplicados en Google) if (!$isAmp && !$notFoundHome && (isset($_GET['page']) || isset($_GET['categoria']))) { pbn_redirect($pageUrl(min($page, $totalPages)), 301); } if ($page > $totalPages) pbn_redirect($isAmp ? pbn_amp_url($pageUrl($totalPages)) : $pageUrl($totalPages)); $isTopPage = $topMode && $page === 1; if ($isTopPage) { $rows = $topRows; $offset = 0; } else { $offset = ($page - ($topMode ? 2 : 1)) * $perPage; // con "más vistos", la página 2 es la 1.ª de recientes $rows = db_all($db, "SELECT titulo, tipo, link, fecha, imagen FROM noticias WHERE $where ORDER BY fecha DESC, id DESC LIMIT ? OFFSET ?", array_merge($params, [$perPage, $offset])); } if ($cat) { $h1 = $cat['aux2'] !== '' ? $cat['aux2'] : trim($siteTitle . ' ' . pbn_ucfirst($cat['last'])); $listDesc = $h1; } else { $h1 = $siteTitle; $listDesc = $siteDescription; } $listTitle = $page > 1 ? $h1 . ' - ' . pbn_t('page', $lang) . ' ' . $page : $h1; $countLabel = $isTopPage ? sprintf(pbn_t('top_n', $lang), count($rows)) : ''; $cardsTitle = $isTopPage ? pbn_t('most_viewed', $lang) : ($topMode ? pbn_t('latest', $lang) : ''); $hlHtml = ''; $hlFile = PBN_DIR . '/includes/' . slugdbname($host) . 'hl.php'; if (is_file($hlFile)) { ob_start(); require $hlFile; $hlHtml = ob_get_clean(); } $ldItems = []; foreach ($rows as $r) { $ldItems[] = ['url' => fullUrl(pbn_href((string)$r['link'])), 'name' => $r['titulo'], 'image' => pbn_abs(fixImage($r['imagen'], $r['titulo']))]; } $canonical = fullUrl($pageUrl($page)); $ctx['ld'] = ['kind' => $cat ? 'category' : 'home', 'items' => $ldItems]; if ($cat) { $crumbs = [[pbn_t('home', $lang), '/'], [$cat['name'], $page > 1 ? fullUrl($pageUrl(1)) : null]]; if ($page > 1) $crumbs[] = [pbn_t('page', $lang) . ' ' . $page, null]; $ctx['breadcrumb'] = array_map(fn($c) => [$c[0], $c[1] ? (str_starts_with((string)$c[1], 'http') ? parse_url($c[1], PHP_URL_PATH) : $c[1]) : null], $crumbs); $ctx['keywords'] = $cat['name'] . ', noticias, blog, artículos, información'; } $ctx = array_merge($ctx, [ 'activeCat' => $cat['slug'] ?? null, 'isHome' => ($cat === null && $page === 1), 'amazon' => (bool)preg_match('~amazon\.[a-z.]+/[^"\'\s>]*tag=|//amzn\.to/~i', $hltext . ' ' . $hlHtml), 'amphtml' => $site['amp'] ? pbn_amp_url($pageUrl($page)) : null, 'title' => $listTitle, 'description' => $listDesc, 'canonical' => $canonical, 'prev' => $page > 1 ? fullUrl($pageUrl($page - 1)) : null, 'next' => $page < $totalPages ? fullUrl($pageUrl($page + 1)) : null, ]); if ($isAmp) { // ── versión AMP del listado ── $ampExt = []; $ampBody = pbn_amp_body_list([ 'hl' => pbn_amp_clean(trim($hltext . "\n" . $hlHtml), $ampExt), 'trending' => pbn_trending_rows($db), 'h1' => $h1, 'description' => $listDesc, 'rows' => $rows, 'page' => $page, 'totalPages' => $totalPages, 'pageUrl' => fn(int $p): string => pbn_amp_url($pageUrl($p)), 'lang' => $lang, ]); pbn_send(pbn_amp_page($ctx, $ampBody, $ampExt), 200, $cacheFile); exit; } $body = pbn_body_list([ 'h1' => $h1, 'description' => $listDesc, 'total' => $total, 'offset' => $offset, 'perPage' => $perPage, 'rows' => $rows, 'page' => $page, 'totalPages' => $totalPages, 'pageUrl' => $pageUrl, 'siteName' => $siteName, 'hltext' => $hltext, 'hlHtml' => $hlHtml, 'slider' => $isTopPage ? '' : pbn_slider_html($db, $lang), // en "más vistos" el slider sería redundante 'countLabel' => $countLabel, 'cardsTitle' => $cardsTitle, 'lang' => $lang, 'kind' => $cat ? 'category' : 'home', 'query' => '', 'noResults' => false, ]); $html = pbn_page($ctx, $body); if ($DEBUG) $html .= '' . ''; pbn_send($html, 200, $cacheFile); exit;