]> _ Git - fluidbook-toolbox.git/commitdiff
wait #6947 @12
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 17 Jun 2024 14:17:44 +0000 (16:17 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 17 Jun 2024 14:17:44 +0000 (16:17 +0200)
app/Jobs/WebflowPublish.php
app/Models/ToolWebflow.php
app/Services/Webflow.php
resources/views/fields/webflow/images.blade.php

index 299781d7a37536b3cdef38dbb7fa11d68b4ae4b6..b5748629274cac2163d6e7f8d66c15dc0ea8e885 100644 (file)
@@ -27,11 +27,13 @@ class WebflowPublish extends Base
 
     public function handle()
     {
+        start_measure('Webflow Publish '.$this->id.' '.$this->mode);
         /** @var ToolWebflow $wf */
         $wf = ToolWebflow::withoutGlobalScopes()->find($this->id);
         $subject = __('Site :name publié', ['name' => $wf->name]);
         if ($this->mode === 'webflow') {
-            $wf->mirror(false, rand(1, 10) === 10);
+            $wf->refreshFormDataFromAPI();
+            $wf->mirror(false);
             $text = __('Le site vient d\'être républié suite à une mise à jour de webflow');
         } else if ($this->mode === 'auto') {
             $text = __('Le site vient d\'être républié suite à une mise à jour des contenus');
@@ -46,10 +48,11 @@ class WebflowPublish extends Base
             $actions[__('Visiter [:locale]', ['locale' => $locale['locale']])] = 'https://' . $locale['url'];
         }
         if ($this->user === null) {
-           // Slack::send($wf->slack, $subject, $text, $actions, false);
+            // Slack::send($wf->slack, $subject, $text, $actions, false);
         } else {
             $this->user->notify(new ToolboxNotification($subject, $text, $actions, true));
         }
+        stop_measure('Webflow Publish '.$this->id.' '.$this->mode);
     }
 
 }
index e29e42530aacdf3fb89809abd972f7aa0f1c24c7..f7ecfba07474d89d34635cc1edd98d19efa19c23 100644 (file)
@@ -120,8 +120,11 @@ class ToolWebflow extends ToolboxTranslatableModel
         }
         $seo = $this->getTranslations('seo');
         $images = $this->getTranslations('images');
+        $locales = $this->getLocalesCodes();
+        $translateAPI = new Api(env('AZURE_TRANSLATE_API_KEY'));
 
         $hasChanged = false;
+        $imagesChanged = false;
 
         foreach ($seo[$mainLocale] as $page) {
 
@@ -140,14 +143,32 @@ class ToolWebflow extends ToolboxTranslatableModel
         }
 
         foreach ($images[$mainLocale] as $id => $alt) {
-            if ($api['images'][$id] !== $alt) {
-                $api['images'][$id] = $alt;
+            $apiAlt = $api['images'][$id]['alt'] ?? str_starts_with($api['images'][$id]['alt'], '__wf_') ? '' : $api['images'][$id]['alt'];
+            if ($apiAlt != $alt) {
+                $api['images'][$id]['alt'] = $alt;
                 Webflow::saveImageAlt($id, $alt);
+                $hasChanged = true;
+            }
+
+            if ($alt) {
+                foreach ($locales as $locale) {
+                    if ($locale === $mainLocale) {
+                        continue;
+                    }
+                    if (!$images[$locale][$id]) {
+                        $images[$locale][$id] = $translateAPI->translate($alt, $locale, $mainLocale);
+                        $imagesChanged = true;
+                        $hasChanged = true;
+                    }
+                }
             }
         }
 
         if ($hasChanged) {
             $this->api = json_encode($api);
+            if ($imagesChanged) {
+                $this->setTranslations('images', $images);
+            }
             $this->saveQuietly();
         }
 
@@ -175,11 +196,12 @@ class ToolWebflow extends ToolboxTranslatableModel
         return Cache::lock('webflow_api_refresh_' . $this->id);
     }
 
-    protected function refreshFormDataFromAPI()
+    public function refreshFormDataFromAPI()
     {
+        start_measure("Webflow refresh data from api");
         $lock = $this->getLock();
         try {
-            $lock->block(360);
+            // $lock->block(360);
 
             Webflow::clearCache();
             $this->api = Webflow::getEditableData($this->webflow);
@@ -262,8 +284,10 @@ class ToolWebflow extends ToolboxTranslatableModel
         } catch (LockTimeoutException $e) {
 
         } finally {
-            $lock?->release();
+            $lock?->forceRelease();
         }
+
+        stop_measure("Webflow refresh data from api");
     }
 
     /**
@@ -273,8 +297,9 @@ class ToolWebflow extends ToolboxTranslatableModel
      */
     public function mirror($slow = true, $force = false)
     {
+        start_measure('Webflow mirror');
+
         Webflow::setToken($this->webflow_api_token);
-        $this->refreshFormDataFromAPI();
 
         $path = $this->getMirrorPath();
         if ($force) {
@@ -310,6 +335,8 @@ class ToolWebflow extends ToolboxTranslatableModel
         }
         $wget->setArg(null, 'https://' . $this->webflow . '.webflow.io');
         $wget->execute();
+
+        stop_measure('Webflow mirror');
         return $wget;
 
     }
@@ -390,7 +417,7 @@ class ToolWebflow extends ToolboxTranslatableModel
 
         $lock = $this->getLock();
         try {
-            $lock->block(1200);
+            $lock->block(360);
 
             $locales = $this->locales_domains;
             if (is_string($locales)) {
@@ -408,7 +435,7 @@ class ToolWebflow extends ToolboxTranslatableModel
         } catch (LockTimeoutException $e) {
 
         } finally {
-            $lock?->release();
+            $lock?->forceRelease();
         }
 
 
index a9775ab6f587b083a07dbc71c8a39284494bc527..c8a1616921460a39fb760128f046f33553e38826 100644 (file)
@@ -78,6 +78,7 @@ class Webflow
 
     public static function request($url, $data = [], $method = 'get', $ttl = 86400, $force = false)
     {
+        start_measure('Webflow API : ' . $url);
         $cacheKey = self::getCacheKey($url, $data, $method);
         if ($force) {
             Cache::forget($cacheKey);
@@ -97,6 +98,7 @@ class Webflow
         if (null === $res && !$force) {
             return self::request($url, $data, $method, $ttl, true);
         }
+        stop_measure('Webflow API : ' . $url);
         return $res;
     }
 
@@ -240,11 +242,13 @@ class Webflow
 
     public static function saveImageAlt($id, $alt)
     {
-
+        return self::request('assets/' . $id, ['altText' => $alt], 'post', 0, true);
     }
 
     public static function getEditableData($shortname)
     {
+        start_measure('Webflow : get editable data');
+
         $key = self::getToken() . '_' . $shortname;
         if (!isset(self::$_editableData[$key])) {
             $data = self::getAllData($shortname);
@@ -306,7 +310,7 @@ class Webflow
                         if (isset($res['images'][$assetId])) {
                             continue;
                         }
-                        $res['images'][$pageID . '_' . $assetId] = ['page' => $pageID, 'id' => $assetId, 'alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']];
+                        $res['images'][$assetId] = ['page' => $pageID, 'id' => $assetId, 'alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']];
                     }
                 }
             }
@@ -324,6 +328,8 @@ class Webflow
 
             self::$_editableData[$key] = $res;
         }
+
+        stop_measure('Webflow : get editable data');
         return self::$_editableData[$key];
     }
 }
index 04d65ea8aa2b25fc1d2d8b830c08c30511378957..55558ffad65bfa68ea328e9e472e6acc451767be 100644 (file)
         <th>Texte alternatif</th>
     </tr>
     @foreach($images as $id=>$image)
+        @if(is_string($image))
+            @continue
+        @endif
         @php
+
             $e=explode('/',$image['url']);
             $ee=array_pop($e);
             $e=explode('_',$ee,2);