From 2b3233ac53afc6c7e293c7f8c9479f35ea423e86 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 3 Jun 2024 16:39:52 +0200 Subject: [PATCH] wip #6937 @4 --- app/Models/ToolWebflow.php | 66 +++++++++++++++++++++++--------- app/Services/Webflow.php | 4 +- app/SubForms/Webflow/SEOPage.php | 3 +- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index 1ef0030a7..c29e84d57 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -92,10 +92,16 @@ class ToolWebflow extends ToolboxTranslatableModel WebflowPublish::dispatch($this->id, $mode); } + public function onSaving(): bool + { + $this->saveDataInWebflow(); + return parent::onSaving(); + } + public function onSaved(): bool { + $this->onPublish('auto'); - $this->saveDataInWebflow(); return parent::onSaved(); } @@ -104,27 +110,56 @@ class ToolWebflow extends ToolboxTranslatableModel Webflow::setToken($this->webflow_api_token); $mainLocale = $this->getMainLocale(); - $api = json_decode($this->api, true); + if (is_string($this->api)) { + $api = json_decode($this->api, true); + } + $seo = $this->getTranslations('seo'); - $clearCache = false; - if (!$this->_compareObjects($this->api['seo'], $this->seo[$mainLocale])) { - dd('seo has changed'); - $clearCache = true; + $hasChanged = false; + + foreach ($seo[$mainLocale] as $page) { + + if ($page['og_title_copied']) { + $page['og_title'] = $page['seo_title']; + } + if ($page['og_description_copied']) { + $page['og_description'] = $page['seo_description']; + } + + if (!$this->_compareArrays($api['seo'][$page['id']], $page)) { + $api['seo'][$page['id']] = $page; + $hasChanged = true; + Webflow::savePageMeta($page); + } } - if ($clearCache) { - Webflow::clearCache(); - $this->refreshFormDataFromAPI(); + if ($hasChanged) { + $this->api = json_encode($api); + $this->saveQuietly(); } + } - protected function _compareObjects($a, $b) + protected function _compareArrays($a, $b, $params = null) { - return $a === $b; + if (null === $params) { + $params = array_keys($a); + } + foreach ($params as $p) { + $va = $a[$p] ?? null; + $vb = $b[$p] ?? null; + + if ($va != $vb) { + return false; + } + } + + return true; } protected function refreshFormDataFromAPI() { + Webflow::clearCache(); $this->api = Webflow::getEditableData($this->webflow); $mainLocale = $this->getMainLocale(); @@ -137,13 +172,12 @@ class ToolWebflow extends ToolboxTranslatableModel if (!isset($seo[$locale])) { $seo[$locale] = []; } - foreach ($seo[$mainLocale] as $id => $s) { - $seo[$locale][$id] = $s; + foreach ($seo[$mainLocale] as $s) { + $seo[$locale][] = $s; } } $this->setTranslations('seo', $seo); - $this->saveQuietly(); } @@ -155,7 +189,6 @@ class ToolWebflow extends ToolboxTranslatableModel public function mirror($slow = true, $force = false) { Webflow::setToken($this->webflow_api_token); - Webflow::clearCache(); $this->refreshFormDataFromAPI(); $path = $this->getMirrorPath(); @@ -264,9 +297,6 @@ class ToolWebflow extends ToolboxTranslatableModel $locales = json_decode($locales, true); } Cache::put('webflow_' . $this->id . '_locales', $locales); - Webflow::setToken($this->webflow_api_token); - $this->refreshFormDataFromAPI(); - return parent::onRetrieved(); } diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index 2bf0a653c..a76a3562a 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -195,9 +195,7 @@ class Webflow ]; $url = 'pages/' . $page['id']; - $res = self::request($url, $data, 'put', 0, true); - - return $res; + return self::request($url, $data, 'put', 0, true); } protected static function _arrayKey($array) diff --git a/app/SubForms/Webflow/SEOPage.php b/app/SubForms/Webflow/SEOPage.php index 11f9d9929..01e9b41da 100644 --- a/app/SubForms/Webflow/SEOPage.php +++ b/app/SubForms/Webflow/SEOPage.php @@ -17,7 +17,8 @@ class SEOPage extends SubForm $this->addField('id', HiddenVisible::class, __('#')); $this->addField('url', HiddenVisible::class, __('URL')); - $this->addField('type', Hidden::class, __('Type')); + $this->addField('type', HiddenVisible::class, __('Type')); + $this->addField('draft', Checkbox::class, __('Brouillon')); $this->addField('slug', Text::class, __('Slug') . ' (' . __('slug') . ')', ['when' => ['url' => ['operator' => '!', 'value' => '/index.html']]]); $this->addField('seo_title', Text::class, __('Titre'), ['when' => ['type' => 'page']]); $this->addField('og_title_copied', Checkbox::class, __('Utiliser le titre ci-dessus pour les réseaux sociaux'), ['when' => ['type' => 'page']]); -- 2.39.5