Site Builder
Editing:
helpersh.php
writable 0666
<?php /*************************************************************************** * /pages/lib/helpers.php – tiny one‑call JSON cache * -------------------------------------------------- * • load_json_once($file) – core routine (static cache) * • Thin wrappers: get_profile(), get_coupon(), …, get_rss_cache() * • Add new helpers by copying one wrapper line. * * The helpers file itself is always here: /pages/lib/helpers.php * Each microsite’s JSON lives in the **same folder as the front‑end * index.php that gets hit by the browser**. * * Usage in any front‑end PHP: * * require_once $_SERVER['DOCUMENT_ROOT'] . '/pages/lib/helpers.php'; * $profile = get_profile(); // cached array * $hours = get_hours(); // cached array ***************************************************************************/ /** * Read & decode a JSON file **once per request**. * * @param string $fileName e.g. 'profile.json' * @return array decoded JSON, or [] on error / not found */ function load_json_once(string $fileName): array { static $cache = []; /* Folder of the *entry script* (index.php), no matter where modules live */ $rootDir = dirname($_SERVER['SCRIPT_FILENAME']); $abs = $rootDir . '/' . $fileName; if (isset($cache[$abs])) { return $cache[$abs]; // instant 0 ms hit } $data = []; if (is_readable($abs)) { $raw = file_get_contents($abs); $json = json_decode($raw, true); if (is_array($json)) { $data = $json; } } return $cache[$abs] = $data; // cache & return } /* ---------- Thin wrappers (copy/paste to extend) ---------- */ function get_profile () { return load_json_once('profile.json'); } function get_coupon () { return load_json_once('coupon.json'); } function get_cta () { return load_json_once('cta.json'); } function get_links () { return load_json_once('links.json'); } function get_hours () { return load_json_once('hours.json'); } function get_prompts () { return load_json_once('prompts.json'); } /** * RSS proxy cache – expected structure: * [ 'items' => [...], 'fetched' => 1712700000 ] */ function get_rss_cache() { return load_json_once('rss-cache.json'); } /* Example for future files: * function get_testimonials() { return load_json_once('testimonials.json'); } */
Save changes
Create folder
writable 0777
Create
Cancel