Siteβ―Builder
Editing:
helhhhpers.php
writable 0666
<?php /************************************************************************** * helpers.php β autoβdetect βsocialβ or βbusinessβ folder, load JSON * -------------------------------------------------------------------- * β’ Keeps the classic wrapper names: get_profile(), get_prompts(), β¦ * β’ No external calls, no Cloudflare, no global cache bundle * β’ Every JSON file is read at most *once per page request* (static cache) *************************************************************************/ /* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ * 1. Decide which folder weβre in * Returns /full/path/to/.../social/<slug> * or /full/path/to/.../ph/<10βdigit> * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */ function helpers_base_dir(): string { static $dir; // memoised after first call if ($dir) return $dir; $root = $_SERVER['DOCUMENT_ROOT']; /* CaseΒ A βΒ the script already lives inside /social/β¦ or /ph/β¦ */ $here = realpath(__DIR__.'/..'); // /pages/lib β one up if (preg_match('#/(social|ph)/([^/]+)$#', $here, $m)) { return $dir = $here; } /* CaseΒ B βΒ ?user=<slug> or ?ph=<10digits> passed in URL */ $slug = preg_replace('/[^a-z0-9_]/i', '', $_GET['user'] ?? ''); if ($slug) return $dir = "$root/social/$slug"; $ph = preg_replace('/\D/', '', $_GET['ph'] ?? ''); if (strlen($ph) === 10) return $dir = "$root/ph/$ph"; /* Nothing matched βΒ throw so the problem is obvious in logs */ throw new RuntimeException('helpers.php: cannot determine base directory'); } /* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ * 2. Core loader β reads one JSON file exactly once per request * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */ function load_json(string $filename, $default = []) { static $cache = []; $abs = helpers_base_dir()."/$filename"; if (isset($cache[$abs])) return $cache[$abs]; if (!is_readable($abs)) return $cache[$abs] = $default; $data = json_decode(file_get_contents($abs), true); return $cache[$abs] = ($data ?? $default); } /* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ * 3. Wrapper functions β keep the same names your code already uses * ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */ function get_profile () { return load_json('profile.json', []); } function get_prompts () { return load_json('prompts.json', []); } function get_coupon () { return load_json('coupon.json', []); } function get_hours () { return load_json('hours.json', []); } function get_links () { return load_json('links.json', []); } function get_rss_cache() { return load_json('rss-cache.json',[]); } /* Add more as needed: * function get_testimonials() { return load_json('testimonials.json', []); } */ ?>
Save changes
Create folder
writable 0777
Create
Cancel