krz/php-blog

A PHP blogging system with built-in static comments.

clone: git clone https://gitbay.org/krz/php-blog.git

main: _functions/loadJSON.php · raw

  1<?php
  2
  3function loadHomepageJSON(string $fileName): string
  4{
  5    // Get the file contents and decode JSON
  6    $fileContents = file_get_contents($fileName);
  7    $data = json_decode($fileContents);
  8
  9    // Loop through the JSON object
 10    $stack = [];
 11    $metadata = '';
 12    foreach ($data as $postObject) {
 13        // Only load published posts
 14        if ($postObject->published == 'Yes') {
 15            // Add relevant metadata to the output template
 16            $year = date_format(date_create($postObject->created), "Y");
 17            if (!in_array($year, $stack)) {
 18                // If any posts have been created in a list, close the existing <ul> and open a new <ul> after the heading
 19                if (!empty($stack)) {
 20                    $metadata .= '</ul>';
 21                }
 22                $metadata .= '<h2 class="text-center">' . $year . '</h2><ul class="blog-posts">';
 23                array_push($stack, $year);
 24            }
 25            $metadata .= '<li><time datetime=' . date_format(date_create($postObject->created), "Y-m-d") . '>' . date_format(date_create($postObject->created), "Y-m-d") . '</time><a href=' . $postObject->link . ' aria-label="Read ' . $postObject->title . ' blog post">' . $postObject->title . '</a></li>';
 26        }
 27    }
 28    $metadata .= '</ul>';
 29    return $metadata;
 30}
 31
 32function loadCategoryJSON(string $fileName): string
 33{
 34    // Get the file contents and decode JSON
 35    $fileContents = file_get_contents($fileName);
 36    $data = json_decode($fileContents);
 37
 38    // Sort the JSON data by the `tag`, then by `create`.
 39    // I had to sort a -> b to get an "A-Z" sort for `tag`,
 40    // but b -> a to get a "first-last" sort for `create`.
 41    uasort($data, function ($a, $b) {
 42        return strcmp($a->tag, $b->tag) ?: strcmp($b->created, $a->created);
 43    });
 44
 45    // Loop through the JSON object
 46    $stack = [];
 47    $metadata = '';
 48    foreach ($data as $postObject) {
 49        // Only load published posts
 50        if ($postObject->published == 'Yes') {
 51            // Add relevant metadata to the output template
 52            $category = str_replace('-', ' ', $postObject->tag);
 53            if (!in_array($category, $stack)) {
 54                // If any posts have been created in a list, close the existing <ul> and open a new <ul> after the heading
 55                if (!empty($stack)) {
 56                    $metadata .= '</ul>';
 57                }
 58                $metadata .= '<h2 id=' . $category . ' class="text-center">' . ucwords($category) . '</h2><ul class="blog-posts">';
 59                array_push($stack, $category);
 60            }
 61
 62            $metadata .= '<li><time datetime=' . date_format(date_create($postObject->created), "Y-m-d") . '>' . date_format(date_create($postObject->created), "Y-m-d") . '</time><a href=' . $postObject->link . ' aria-label="Read ' . $postObject->title . ' blog post">' . $postObject->title . '</a></li>';
 63        }
 64    }
 65    $metadata .= '</ul>';
 66    return $metadata;
 67}
 68
 69function loadPostJSON(string $fileName, string $query)
 70{
 71    // Get the file contents and decode JSON
 72    $fileContents = file_get_contents($fileName);
 73    $data = json_decode($fileContents);
 74
 75    // Loop through the JSON object
 76    foreach ($data as $postObject) {
 77        // If this function is called to get a specific post's metadata, return the whole object
 78        if ($postObject->link == $GLOBALS['fullDomain'] . "/post/" . $query) {
 79            return $postObject;
 80        }
 81    }
 82}
 83
 84function loadCommentJSON(string $fileName, string $query = null): string
 85{
 86    // Set up an empty comment section
 87    $commentSection = '';
 88
 89    // Load the file and decode the JSON
 90    $fileContents = file_get_contents($fileName);
 91    $data = json_decode($fileContents);
 92
 93    // Sort comments by date - latest comments will appear at the top
 94    uasort($data, function ($a, $b) {
 95        return strcmp($b->timestamp, $a->timestamp);
 96    });
 97
 98    // Loop through all comments add to returned string if it matches criteria
 99    foreach ($data as $commentObject) {
100        if (($query !== null) && ($commentObject->postURL == $GLOBALS['fullDomain'] . "/post/" . $query)) {
101            $securedHTML = parseMarkdown(null, $commentObject->comment);
102            $commentSection .= '<div class="user-comment"><div class="row"><label>Timestamp:</label><p>' . $commentObject->timestamp . '</p></div><div class="row"><label>Name:</label><p>' . $commentObject->username . '</p></div><div class="row markdown"><label>Comment:</label><div class="comment-markdown">' . $securedHTML . '</div></div></div>';
103        } else if ($query == null) {
104            $securedHTML = parseMarkdown(null, $commentObject->comment);
105            $pageURL = str_replace($GLOBALS['fullDomain'] . '/post/', '', $commentObject->postURL);
106            $postObject = loadPostJSON('_data/metadata.json', $pageURL);
107            $commentSection .= '<div class="user-comment"><div class="row"><label>Post:</label><a href="' . $commentObject->postURL . '#comments">' . $postObject->title . '</a></div><div class="row"><label>Timestamp:</label><p>' . $commentObject->timestamp . '</p></div><div class="row"><label>Name:</label><p>' . $commentObject->username . '</p></div><div class="row markdown"><label>Comment:</label><div class="comment-markdown">' . $securedHTML . '</div></div></div>';
108        }
109    }
110    return $commentSection;
111}