From: Vincent Vanwaelscappel Date: Mon, 27 May 2024 17:06:32 +0000 (+0200) Subject: wip #6937 @1.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e41742699d46f9ab9cbe7a6d997eec4c6b812645;p=fluidbook-toolbox.git wip #6937 @1.5 --- diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index e63859a17..28cfea5c8 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -56,7 +56,7 @@ class ToolWebflow extends ToolboxModel $this->addField('css', Code::class, __('CSS complémentaire'), ['language' => 'css', 'tab' => __('Code'), 'hint' => __('Code ajouté à toutes les pages')]); $this->addField('texts', WebflowTexts::class, '', ['tab' => __('Textes'), 'translatable' => true]); $this->addField('images', WebflowImages::class, '', ['tab' => __('Images'), 'translatable' => true]); - $this->addField('seo', BunchOfFieldsMultiple::class, '', ['allows_add' => false, 'allows_delete' => false, 'allows_clone' => false, 'allows_reorder' => false, 'bunch' => SEOPage::class, 'tab' => __('SEO')]); + $this->addField('seo', BunchOfFieldsMultiple::class, '', ['edit_label' => '%url | %seo_title', 'allows_add' => false, 'allows_delete' => false, 'allows_clone' => false, 'allows_reorder' => false, 'bunch' => SEOPage::class, 'tab' => __('SEO')]); } protected function _parsePages() @@ -82,16 +82,22 @@ class ToolWebflow extends ToolboxModel return parent::onSaved(); } - protected function saveDataInWebflow(){ + protected function saveDataInWebflow() + { + Webflow::setToken($this->webflow_api_token); // SEO - dddd($this->seo); - foreach ($this->seo as $item) { - + foreach ($this->seo as $page) { + Webflow::savePageMeta($page); } + Webflow::clearCache(); + $this->refreshFormDataFromAPI(); } protected function refreshFormDataFromAPI() { + $extra_locales=$this->locales; + array_shift($extra_locales); + $editable = Webflow::getEditableData($this->webflow); $this->seo = $editable['seo']; $this->saveQuietly(); @@ -108,7 +114,6 @@ class ToolWebflow extends ToolboxModel Webflow::clearCache(); $this->refreshFormDataFromAPI(); - $path = $this->getMirrorPath(); if ($force) { $path = Files::emptyDir($path); diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index e1d59ec76..2bf0a653c 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -2,7 +2,6 @@ namespace App\Services; - use App\Services\Webflow\Excel; use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Cache; @@ -72,9 +71,14 @@ class Webflow return self::_arrayKey($res); } + public static function getCacheKey($url, $data = [], $method = 'get') + { + return 'webflow_' . static::getToken() . '_' . mb_strtolower($method) . '_' . $url . '_' . sha1(print_r($data, true)); + } + public static function request($url, $data = [], $method = 'get', $ttl = 86400, $force = false) { - $cacheKey = 'webflow_' . static::getToken() . '_' . $method . '_' . $url . '_' . sha1(print_r($data, true)); + $cacheKey = self::getCacheKey($url, $data, $method); if ($force) { Cache::forget($cacheKey); } @@ -115,13 +119,13 @@ class Webflow } } - public static function getPageMeta($shortname, $pageID, $fromAPI = false) + public static function getPageMeta($shortname, $pageID, $fromAPI = false, $force = false) { if ($fromAPI) { - return self::request('pages/' . $pageID); + return self::request('pages/' . $pageID, force: $force); } - foreach (self::listPages($shortname) as $page) { + foreach (self::listPages($shortname, $force) as $page) { if ($page['id'] == $pageID) { return $page; } @@ -177,6 +181,25 @@ class Webflow return self::paginatedRequest('collections/' . $collectionID . '/items', 'items'); } + public static function savePageMeta($page) + { + $data['slug'] = $page['slug']; + + $data['seo'] = ['title' => $page['seo_title'], 'description' => $page['seo_description']]; + + $data['openGraph'] = [ + 'title' => $page['og_title_copied'] ? $page['seo_title'] : $page['og_title'], + 'titleCopied' => $page['og_title_copied'] == '1', + 'description' => $page['og_description_copied'] ? $page['seo_description'] : $page['og_description'], + 'descriptionCopied' => $page['og_description_copied'] == '1', + ]; + + $url = 'pages/' . $page['id']; + $res = self::request($url, $data, 'put', 0, true); + + return $res; + } + protected static function _arrayKey($array) { $res = []; @@ -196,12 +219,12 @@ class Webflow return self::paginatedRequest('pages/' . $pageID . '/dom', 'nodes'); } - public static function listPages($shortname) + public static function listPages($shortname, $force = false) { - $res = self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/pages', 'pages'); + $res = self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/pages', 'pages', force: $force); foreach ($res as $page) { if (null !== $page['parentId'] && !isset($res[$page['parentId']])) { - $res[$page['parentId']] = self::getPageMeta($shortname, $page['parentId'], true); + $res[$page['parentId']] = self::getPageMeta($shortname, $page['parentId'], true, $force); } } return $res; @@ -278,7 +301,6 @@ class Webflow $res['images'][$assetId] = ['alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']]; } } - } uasort($res['seo'], function ($a, $b) {