]> _ Git - fluidbook-toolbox.git/commitdiff
wip #7048 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 21 Aug 2024 15:49:12 +0000 (17:49 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 21 Aug 2024 15:49:12 +0000 (17:49 +0200)
app/Models/ToolWebflow.php
app/Services/Webflow.php

index 25186f95eafc61869cbe16bfc2a1ac09d1a020d6..133f9d7a523a293c8b48ac1a1eb5e7d0bfe81fdc 100644 (file)
@@ -108,6 +108,7 @@ class ToolWebflow extends ToolboxTranslatableModel
 
     public function onSaving(): bool
     {
+
         $this->saveDataInWebflow();
 
         $change = false;
@@ -158,6 +159,7 @@ class ToolWebflow extends ToolboxTranslatableModel
         $hasChanged = false;
         $imagesChanged = false;
 
+
         foreach ($seo[$mainLocale] as $page) {
 
             if ($page['og_title_copied']) {
@@ -240,7 +242,6 @@ class ToolWebflow extends ToolboxTranslatableModel
     {
         start_measure("Webflow refresh data from api");
         $lock = $this->getLock();
-        //dd(Webflow::getAllData($this->webflow)['cms']);
         try {
             Webflow::clearCache();
             $this->api = $this->getEditableData();
@@ -487,7 +488,7 @@ class ToolWebflow extends ToolboxTranslatableModel
         $urlmaps = $this->getURLMaps();
         $urlmap = $urlmaps[$locale];
         $origRelative = $relative;
-        $strippedSlashRelative=ltrim($relative,'/');
+        $strippedSlashRelative = ltrim($relative, '/');
 
         $html = file_get_contents($f->getPathname());
         $regex = '/("https:\/\/' . $this->webflow . '.webflow.io\/\\\\&quot;)(.*)(\\\\&quot;")/';
@@ -701,10 +702,10 @@ class ToolWebflow extends ToolboxTranslatableModel
         return parent::onRetrieved();
     }
 
-    public function getEditableData()
+    public function getEditableData($force = false)
     {
         Webflow::setToken($this->webflow_api_token);
-        return Webflow::getEditableData($this->webflow, $this->getMirrorPath());
+        return Webflow::getEditableData($this->webflow, $this->getMirrorPath(), $force);
     }
 
     public function getAvailableLocales()
index a602428fe0bf002bb09d4e602ec3c2dd74d09a3c..5b1c4beb9fc6d90b4dfd6f89d42f4787b92933d2 100644 (file)
@@ -253,18 +253,22 @@ class Webflow
         return self::request('assets/' . $id, ['altText' => $alt], 'post', 0, true);
     }
 
-    public static function getEditableData($shortname, $mirrorPath)
+    public static function getEditableData($shortname, $mirrorPath, $force = false)
     {
         start_measure('Webflow : get editable data');
 
         $key = self::getToken() . '_' . $shortname;
-        if (!isset(self::$_editableData[$key])) {
+
+        if ($force || !isset(self::$_editableData[$key])) {
+
             $data = self::getAllData($shortname);
+
+
             $res = ['texts' => [], 'images' => [], 'seo' => []];
             foreach ($data['pages'] as $pageID => $page) {
                 $details = $page['details'];
 
-                $url = trim($details['slug']??'');
+                $url = trim($details['slug'] ?? '');
                 $c = $page;
                 while (true) {
                     if (null === $c['details']['parentId']) {
@@ -285,6 +289,7 @@ class Webflow
                     }
                 }
 
+
                 $url = '/' . $url;
 
 
@@ -304,6 +309,7 @@ class Webflow
 
                 $res['seo'][$pageID] = $seo;
 
+
                 foreach ($page['contents'] as $node) {
                     if ($node['type'] === 'text') {
                         if (!$node['text']['text'] || !$node['text']['html']) {
@@ -312,12 +318,7 @@ class Webflow
                         $html = str_replace('<br />', "\n", $node['text']['html']);
                         $html = str_replace('<br>', "\n", $html);
                         $texts = Html::getTextNodes($html);
-                        foreach ($texts as $t) {
-                            if (!isset($res['texts'][$t])) {
-                                $res['texts'][$t] = ['key' => base64_encode($t), 'occurences' => 0];
-                            }
-                            $res['texts'][$t]['occurences']++;
-                        }
+                        self::addTexts($texts, $res['texts']);
 
                     } else if ($node['type'] === 'image') {
 
@@ -331,6 +332,11 @@ class Webflow
                         $res['images'][$assetId] = ['page' => $pageID, 'id' => $assetId, 'alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']];
                     }
                 }
+
+                if (is_file($mirrorPath . $url)) {
+                    $texts = Html::getTextNodes(file_get_contents($mirrorPath . $url));
+                    self::addTexts($texts, $res['texts']);
+                }
             }
 
             $ignoreNames = ['slug', 'json'];
@@ -359,21 +365,13 @@ class Webflow
                         } else if ($type === 'RichText') {
                             $texts = Html::getTextNodes($t);
                         }
-                        foreach ($texts as $t) {
-                            if (!isset($res['texts'][$t])) {
-                                $res['texts'][$t] = ['key' => base64_encode($t), 'occurences' => 0];
-                            }
-                            $res['texts'][$t]['occurences']++;
-                        }
+                        self::addTexts($texts, $res['texts']);
                     }
                 }
             }
 
-            $res['seo'] = static::getPagesFromHTMLFiles($res['seo'], $mirrorPath);
-
-
-
 
+            $res['seo'] = static::getPagesFromHTMLFiles($res['seo'], $mirrorPath);
             self::$_editableData[$key] = $res;
         }
 
@@ -381,6 +379,23 @@ class Webflow
         return self::$_editableData[$key];
     }
 
+    protected static function addTexts($texts, &$array)
+    {
+        foreach ($texts as $t) {
+            $t = trim($t);
+            if (!$t) {
+                continue;
+            }
+            $paragraphs = preg_split('/\v{2,}/', $t);
+            foreach ($paragraphs as $p) {
+                if (!isset($array[$p])) {
+                    $array[$p] = ['key' => base64_encode($p), 'occurences' => 0];
+                }
+                $array[$p]['occurences']++;
+            }
+        }
+    }
+
     public static function getPagesFromHTMLFiles($seo, $mirrorPath)
     {
         foreach (Files::getRecursiveDirectoryIterator($mirrorPath) as $f) {