Siteβ―Builder
Editing:
modulesjhkjk-rss.php
writable 0666
<?php /************************************************************************** * RSSΒ MODULE (v7Β βΒ robust slug logic + skip empty cache) * ---------------------------------------------------------------------- * β’ First build: fetches the RSS/JSON feed, renders <ul>, caches it as * /ph/<phone>/rss-cache.html βorβ /social/<handle>/rss-cache.html * β’ Later builds: serves the cached HTML. If the feed produced zero * items, we donβt create the cache and we output nothing * so the outer wrapper is skipped (no blank card). **************************************************************************/ if (!function_exists('get_profile')) { return; // hardβexit if helper missing } $profile = get_profile(); /* ---------- 2. Load links.json ---------- */ /* βββ identify business vs. social & slug βββββββββββββββββββββββββββ */ $root = rtrim($_SERVER['DOCUMENT_ROOT'], '/'); $isBiz = isset($profile['name']) && isset($profile['phone']); if ($isBiz) { // business profile $slug = preg_replace('/\D/', '', $profile['phone']); // digits only $dir = "$root/ph/$slug"; } else { $slug = ltrim($profile['handle'], '@'); if (!$slug) return; $dir = "$root/social/$slug"; } /* βββ feed URL & cache path βββββββββββββββββββββββββββββββββββββββββ */ $feedUrl = $profile['website_rss'] ?? ''; if (!$feedUrl) return; // no feed configured $htmlFile = "$dir/rss-cache.html"; $maxItems = 10; /* βββ helper: fetch & build <ul> markup βββββββββββββββββββββββββββββ */ function build_feed_html(string $url, int $max): string { $ctx = stream_context_create(['http'=>['timeout'=>5],'https'=>['timeout'=>5]]); $raw = @file_get_contents($url, false, $ctx); if (!$raw) return ''; $items = []; /* try XML first */ libxml_use_internal_errors(true); if ($xml = simplexml_load_string($raw, 'SimpleXMLElement', LIBXML_NOCDATA)) { foreach ($xml->channel->item ?? [] as $it) { $items[] = [ 'title'=>(string)$it->title, 'link' =>(string)$it->link, 'date' =>(string)$it->pubDate ]; if (count($items) >= $max) break; } } /* fallback: JSON feed */ if (!$items && ($json = json_decode($raw, true)) && isset($json['items'])) { foreach ($json['items'] as $it) { $items[] = [ 'title'=>$it['title'] ?? '', 'link' =>$it['url'] ?? $it['external_url'] ?? '', 'date' =>$it['date_published'] ?? '' ]; if (count($items) >= $max) break; } } if (!$items) return ''; // nothing useful $out = '<ul class="rs-list">'; foreach ($items as $it) { $t = htmlspecialchars($it['title'] ?: 'Untitled'); $l = htmlspecialchars($it['link'] ?: '#'); $d = $it['date'] ? date('M j, Y', strtotime($it['date'])) : ''; $out .= "<li><a class=\"rs-title\" href=\"$l\" target=\"_blank\" rel=\"noopener\">$t</a>"; if ($d) $out .= " <span class=\"rs-date\">$d</span>"; $out .= "</li>"; } return $out.'</ul>'; } /* βββ ensure cache exists (only if we have content) βββββββββββββββββ */ if (!is_readable($htmlFile)) { $html = build_feed_html($feedUrl, $maxItems); if ($html) { // only cache if nonβempty file_put_contents($htmlFile, $html, LOCK_EX); } else { /* no items: skip output so the outer wrapper vanishes */ return; } } /* βββ serve cached HTML (guaranteed nonβempty) βββββββββββββββββββββ */ $cachedHtml = file_get_contents($htmlFile) ?: ''; if (!$cachedHtml) return; // safety β shouldnβt occur ?> <section class="rs-shell"> <div class="rs-card"> <h2>π° Latest Posts</h2> <?= $cachedHtml ?> </div> </section> <style> .rs-shell{margin:2.4rem auto;max-width:clamp(480px,70vw,720px);width:100%; font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif; text-align:center} .rs-card{background:#fff;padding:1.6rem 1.4rem;border:3.5px solid #ffb63b; border-radius:27px;box-shadow:0 8px 22px rgba(0,0,0,.05)} .rs-card h2{margin:0 0 1.1rem;font-size:1.33rem;font-weight:700;color:#102a66} .rs-list{list-style:none;padding:0;margin:0 auto;max-width:430px;text-align:left} .rs-list li{margin:1.05em 0} .rs-title{font-size:1.04rem;color:#1a0dab;text-decoration:none} .rs-title:hover{text-decoration:underline} .rs-date{display:block;font-size:.82rem;color:#555;margin-top:.18em} @media(prefers-color-scheme:dark){ .rs-card{background:#1a1f2b;border-color:#ffb63b;box-shadow:0 6px 18px #0006} .rs-title{color:#96b5ff} .rs-date{color:#aaa} } </style>
Save changes
Create folder
writable 0777
Create
Cancel