From: Vincent Vanwaelscappel Date: Wed, 21 Aug 2024 15:49:12 +0000 (+0200) Subject: wip #7048 @1.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=55373fd4cb470f951f5bce1b29565d17e467e26a;p=fluidbook-toolbox.git wip #7048 @1.5 --- diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index 25186f95e..133f9d7a5 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -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\/\\\\")(.*)(\\\\"")/'; @@ -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() diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index a602428fe..5b1c4beb9 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -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('
', "\n", $node['text']['html']); $html = str_replace('
', "\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) {