updated widgets

This commit is contained in:
2026-02-25 17:35:01 +09:00
parent 787724b09c
commit e4d2966377
43 changed files with 3648 additions and 180 deletions

View File

@@ -13,6 +13,9 @@ const server = Bun.serve({
const url = new URL(req.url);
const page = Math.max(1, Number(url.searchParams.get("page") ?? 1));
const UPLOADS_BASE_URL =
process.env.WP_UPLOADS_URL ?? "https://new.jkhsakha.ru/wp-content/uploads";
// 1. Load grid config post
const gridPost = await connection.run(`
SELECT ID, post_title
@@ -22,16 +25,15 @@ const server = Bun.serve({
`, { id: gridId });
const [grid] = await gridPost.getRowObjectsJson();
if (!grid) {
return Response.json(null, { status: 404 });
}
if (!grid) return Response.json(null, { status: 404 });
// 2. Load grid meta
const metaRes = await connection.run(`
SELECT meta_key, meta_value
FROM wp_postmeta
WHERE post_id = $id;
`, { id: gridId });
SELECT meta_key, meta_value
FROM wp_postmeta
WHERE post_id = $id;
`, { id: gridId });
const meta = Object.fromEntries(
(await metaRes.getRowObjectsJson())
@@ -47,33 +49,32 @@ const server = Bun.serve({
// 3. Total count (for pagination)
const totalRes = await connection.run(`
SELECT COUNT(*)::int AS total
FROM wp_posts
WHERE post_status = 'publish'
AND post_type = $postType;
`, { postType });
SELECT COUNT(*)::int AS total
FROM wp_posts
WHERE post_status = 'publish'
AND post_type = $postType;
`, { postType });
const [{ total }] = await totalRes.getRowObjectsJson();
const totalPages = Math.max(1, Math.ceil(total / perPage));
// clamp page if out of range
const safePage = Math.min(page, totalPages);
const safeOffset = (safePage - 1) * perPage;
// 4. Fetch paginated posts
const postsRes = await connection.run(`
SELECT
ID,
post_title,
post_name,
post_excerpt,
post_date
FROM wp_posts
WHERE post_status = 'publish'
AND post_type = $postType
ORDER BY ${orderBy} ${order}
LIMIT $perPage OFFSET $offset;
`, {
SELECT
ID,
post_title,
post_name,
post_excerpt,
post_date
FROM wp_posts
WHERE post_status = 'publish'
AND post_type = $postType
ORDER BY ${orderBy} ${order}
LIMIT $perPage OFFSET $offset;
`, {
postType,
perPage,
offset: safeOffset