From 01f5603fb6607e417ff9967f3a4514c916ace783 Mon Sep 17 00:00:00 2001
From: btopro <btopro@outlook.com>
Date: Thu, 27 Jul 2023 10:33:04 -0400
Subject: [PATCH] https://github.com/elmsln/issues/issues/1633

---
 system/backend/php/lib/HAXCMSSite.php | 203 ++++++++++++++------------
 system/backend/php/lib/Operations.php |   8 +-
 system/coreConfig/siteFields.json     |   4 +-
 3 files changed, 114 insertions(+), 101 deletions(-)

diff --git a/system/backend/php/lib/HAXCMSSite.php b/system/backend/php/lib/HAXCMSSite.php
index 029f3889a4..042793eca9 100644
--- a/system/backend/php/lib/HAXCMSSite.php
+++ b/system/backend/php/lib/HAXCMSSite.php
@@ -356,11 +356,14 @@ public function getManagedTemplateFiles() {
      * Reprocess the files that twig helps set in their static
      * form that the user is not in control of.
      */
-    public function rebuildManagedFiles() {
-      $templates = $this->getManagedTemplateFiles();
-      // this can't be there by default since it's a dynamic file and we only
-      // want to update this when we are refreshing the managed files directly
-      $templates['indexphp'] = 'index.php';
+    public function rebuildManagedFiles($templates = array()) {
+      // support for calling with a set of predefined templates
+      if (count($templates) === 0) {
+        $templates = $this->getManagedTemplateFiles();
+        // this can't be there by default since it's a dynamic file and we only
+        // want to update this when we are refreshing the managed files directly
+        $templates['indexphp'] = 'index.php';
+      }
       $siteDirectoryPath = $this->directory . '/' . $this->manifest->metadata->site->name;
       $boilerPath = HAXCMS_ROOT . '/system/boilerplate/site/';
       foreach ($templates as $file) {
@@ -682,98 +685,104 @@ public function save() {
      */
     public function updateAlternateFormats($format = NULL)
     {
-            $siteDirectory = $this->directory . '/' . $this->manifest->metadata->site->name . '/';
-            if (is_null($format) || $format == 'rss') {
-                try {
-                    // rip changes to feed urls
-                    $rss = new FeedMe();
-                    $siteDirectory =
-                        $this->directory . '/' . $this->manifest->metadata->site->name . '/';
-                    @file_put_contents($siteDirectory . 'rss.xml', $rss->getRSSFeed($this));
-                    @file_put_contents(
-                        $siteDirectory . 'atom.xml',
-                        $rss->getAtomFeed($this)
-                    );
-                } catch (Exception $e) {
-                    // some of these XML parsers are a bit unstable
-                }
-            }
-            // build a sitemap if we have a domain, kinda required...
-            if (is_null($format) || $format == 'sitemap') {
-                try {
-                    if (isset($this->manifest->metadata->site->domain)) {
-                        $domain = $this->manifest->metadata->site->domain;
-                        $generator = new \Icamys\SitemapGenerator\SitemapGenerator(
-                            $domain,
-                            $siteDirectory
-                        );
-                        // will create also compressed (gzipped) sitemap
-                        $generator->createGZipFile = true;
-                        // determine how many urls should be put into one file
-                        // according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html)
-                        $generator->maxURLsPerSitemap = 50000;
-                        // sitemap file name
-                        $generator->sitemapFileName = "sitemap.xml";
-                        // sitemap index file name
-                        $generator->sitemapIndexFileName = "sitemap-index.xml";
-                        // adding url `loc`, `lastmodified`, `changefreq`, `priority`
-                        foreach ($this->manifest->items as $key => $item) {
-                            if ($item->parent == null) {
-                                $priority = '1.0';
-                            } elseif ($item->indent == 2) {
-                                $priority = '0.7';
-                            } else {
-                                $priority = '0.5';
-                            }
-                            $updatedTime = new DateTime();
-                            $updatedTime->setTimestamp($item->metadata->updated);
-                            $updatedTime->format(DateTime::ATOM);
-                            $generator->addUrl(
-                                $domain .
-                                    '/' .
-                                    str_replace(
-                                        'pages/',
-                                        '',
-                                        str_replace('/index.html', '', $item->location)
-                                    ),
-                                $updatedTime,
-                                'daily',
-                                $priority
-                            );
-                        }
-                        // generating internally a sitemap
-                        $generator->createSitemap();
-                        // writing early generated sitemap to file
-                        $generator->writeSitemap();
-                    }
-                } catch (Exception $e) {
-                    // some of these XML parsers are a bit unstable
-                }
-            }
-            if (is_null($format) || $format == 'legacy') {
-                // now generate a static list of links. This is so we can have legacy fail-back iframe mode in tact
-                @file_put_contents(
-                    $siteDirectory . 'legacy-outline.html',
-                    '<!DOCTYPE html>
-                    <html lang="' . $this->getLanguage() . '">
-                        <head>
-                            <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
-                            <meta content="utf-8" http-equiv="encoding">
-                            <link rel="stylesheet" type="text/css" href="assets/legacy-outline.css">
-                        </head>
-                        <body>' .
-                        $this->treeToNodes($this->manifest->items) .
-                        '</body>
-                    </html>'
-                );
-            }
-            if (is_null($format) || $format == 'search') {
-                // now generate the search index
-                @file_put_contents(
-                    $siteDirectory . 'lunrSearchIndex.json',
-                        json_encode($this->lunrSearchIndex($this->manifest->items))
-                );
-            }
+      $siteDirectory = $this->directory . '/' . $this->manifest->metadata->site->name . '/';
+      if (is_null($format) || $format == 'rss') {
+          try {
+              // rip changes to feed urls
+              $rss = new FeedMe();
+              $siteDirectory =
+                  $this->directory . '/' . $this->manifest->metadata->site->name . '/';
+              @file_put_contents($siteDirectory . 'rss.xml', $rss->getRSSFeed($this));
+              @file_put_contents(
+                  $siteDirectory . 'atom.xml',
+                  $rss->getAtomFeed($this)
+              );
+          } catch (Exception $e) {
+              // some of these XML parsers are a bit unstable
+          }
+      }
+      // build a sitemap if we have a domain, kinda required...
+      if (is_null($format) || $format == 'sitemap') {
+          try {
+              if (isset($this->manifest->metadata->site->domain)) {
+                  $domain = $this->manifest->metadata->site->domain;
+                  $generator = new \Icamys\SitemapGenerator\SitemapGenerator(
+                      $domain,
+                      $siteDirectory
+                  );
+                  // will create also compressed (gzipped) sitemap
+                  $generator->createGZipFile = true;
+                  // determine how many urls should be put into one file
+                  // according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html)
+                  $generator->maxURLsPerSitemap = 50000;
+                  // sitemap file name
+                  $generator->sitemapFileName = "sitemap.xml";
+                  // sitemap index file name
+                  $generator->sitemapIndexFileName = "sitemap-index.xml";
+                  // adding url `loc`, `lastmodified`, `changefreq`, `priority`
+                  foreach ($this->manifest->items as $key => $item) {
+                      if ($item->parent == null) {
+                          $priority = '1.0';
+                      } elseif ($item->indent == 2) {
+                          $priority = '0.7';
+                      } else {
+                          $priority = '0.5';
+                      }
+                      $updatedTime = new DateTime();
+                      $updatedTime->setTimestamp($item->metadata->updated);
+                      $updatedTime->format(DateTime::ATOM);
+                      $generator->addUrl(
+                          $domain .
+                              '/' .
+                              str_replace(
+                                  'pages/',
+                                  '',
+                                  str_replace('/index.html', '', $item->location)
+                              ),
+                          $updatedTime,
+                          'daily',
+                          $priority
+                      );
+                  }
+                  // generating internally a sitemap
+                  $generator->createSitemap();
+                  // writing early generated sitemap to file
+                  $generator->writeSitemap();
+              }
+          } catch (Exception $e) {
+              // some of these XML parsers are a bit unstable
+          }
+      }
+      if (is_null($format) || $format == 'legacy') {
+          // now generate a static list of links. This is so we can have legacy fail-back iframe mode in tact
+          @file_put_contents(
+              $siteDirectory . 'legacy-outline.html',
+              '<!DOCTYPE html>
+              <html lang="' . $this->getLanguage() . '">
+                  <head>
+                      <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
+                      <meta content="utf-8" http-equiv="encoding">
+                      <link rel="stylesheet" type="text/css" href="assets/legacy-outline.css">
+                  </head>
+                  <body>' .
+                  $this->treeToNodes($this->manifest->items) .
+                  '</body>
+              </html>'
+          );
+      }
+      if (is_null($format) || $format == 'search') {
+          // now generate the search index
+          @file_put_contents(
+              $siteDirectory . 'lunrSearchIndex.json',
+                  json_encode($this->lunrSearchIndex($this->manifest->items))
+          );
+      }
+      // rebuild the service worker's hashed cache index because this file updated
+      // this way users getting cached copies from local device will be informed
+      // that this page updated since their last visit
+      if (is_null($format) || $format == 'service-worker') {
+        $this->rebuildManagedFiles(array('sw' => 'service-worker.js'));
+      }
     }
     /**
      * Create Lunr.js style search index
diff --git a/system/backend/php/lib/Operations.php b/system/backend/php/lib/Operations.php
index 54312cc54b..32756e4c9b 100644
--- a/system/backend/php/lib/Operations.php
+++ b/system/backend/php/lib/Operations.php
@@ -950,7 +950,9 @@ public function createNode() {
           }
         }
       }
-    $site->gitCommit('Page added:' . $item->title . ' (' . $item->id . ')'); 
+      $site->gitCommit('Page added:' . $item->title . ' (' . $item->id . ')');
+      // update the alternate formats as a new page exists
+      $site->updateAlternateFormats();
     }
     return array(
       'status' => 200,
@@ -2070,7 +2072,9 @@ public function createSite() {
           $cssvar = '--simple-colors-default-theme-light-blue-7';
       }
       $schema->metadata->theme->variables->cssVariable = $cssvar;
-      $schema->metadata->site->settings->lang = 'en';
+      $schema->metadata->site->settings = new stdClass();
+      $schema->metadata->site->settings->lang = 'en-US';
+      $schema->metadata->site->settings->publishPagesOn = true;
       $schema->metadata->site->created = time();
       $schema->metadata->site->updated = time();
       // check for publishing settings being set globally in HAXCMS
diff --git a/system/coreConfig/siteFields.json b/system/coreConfig/siteFields.json
index 0f41b9053e..2fbde8d18e 100644
--- a/system/coreConfig/siteFields.json
+++ b/system/coreConfig/siteFields.json
@@ -150,7 +150,7 @@
         {
           "property": "manifest-metadata-site-settings-publishPagesOn",
           "title": "Hide unpublished pages",
-          "description": "Check the box to hide unpublished pages",
+          "description": "Uncheck this box if you want to show unpublished pages to the public",
           "inputMethod": "boolean",
           "icon": "icons:link"
         },
@@ -164,7 +164,7 @@
         {
           "property": "manifest-metadata-site-settings-forceUpgrade",
           "title": "Force browser upgrade",
-          "description": "Force users viewing the site to have evergreen browsers (not recommended)",
+          "description": "Force users viewing the site to have evergreen browsers and ask to upgrade if they don't",
           "inputMethod": "boolean",
           "icon": "icons:link"
         }