]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6937 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 27 May 2024 17:06:32 +0000 (19:06 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 27 May 2024 17:06:32 +0000 (19:06 +0200)
app/Models/ToolWebflow.php
app/Services/Webflow.php

index e63859a17c78e302287c2d2763dc5ad3fb801fae..28cfea5c8eed399e7d0296bc9d00983c1f580313 100644 (file)
@@ -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);
index e1d59ec76a8d1b815caf4d669d18d6af64ca31c3..2bf0a653c9da2066db162f1e186074c5dc310b38 100644 (file)
@@ -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) {