]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6944 @7
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 20 Jun 2024 15:22:39 +0000 (17:22 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 20 Jun 2024 15:22:39 +0000 (17:22 +0200)
app/Jobs/WebflowPublish.php
app/Models/ToolWebflow.php
app/Services/Webflow.php

index b5748629274cac2163d6e7f8d66c15dc0ea8e885..5117b68313d0a2851661f3dd3b328516f469bee8 100644 (file)
@@ -27,11 +27,12 @@ class WebflowPublish extends Base
 
     public function handle()
     {
-        start_measure('Webflow Publish '.$this->id.' '.$this->mode);
+        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') {
+        $notify = true;
+        if ($this->mode === 'webflow' && $wf->webflow_refresh_on_publish) {
             $wf->refreshFormDataFromAPI();
             $wf->mirror(false);
             $text = __('Le site vient d\'être républié suite à une mise à jour de webflow');
@@ -39,6 +40,7 @@ class WebflowPublish extends Base
             $text = __('Le site vient d\'être républié suite à une mise à jour des contenus');
         } else {
             $text = __('Le site vient d\'être républié suite à une déclenchement manuel');
+            $notify=false;
         }
 
         $wf->compile();
@@ -47,12 +49,14 @@ class WebflowPublish extends Base
         foreach ($wf->getLocales() as $locale) {
             $actions[__('Visiter [:locale]', ['locale' => $locale['locale']])] = 'https://' . $locale['url'];
         }
-        if ($this->user === null) {
-            // Slack::send($wf->slack, $subject, $text, $actions, false);
-        } else {
-            $this->user->notify(new ToolboxNotification($subject, $text, $actions, true));
+        if ($notify) {
+            if ($this->user === null) {
+                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);
+        stop_measure('Webflow Publish ' . $this->id . ' ' . $this->mode);
     }
 
 }
index f7ecfba07474d89d34635cc1edd98d19efa19c23..7ffc3e7562092342f1cf8b8035e4ce718d260f32 100644 (file)
@@ -13,6 +13,7 @@ use App\SubForms\Webflow\Redirection;
 use App\SubForms\Webflow\SEOPage;
 use Cubist\Azure\Translate\Api;
 use Cubist\Backpack\Magic\Fields\BunchOfFieldsMultiple;
+use Cubist\Backpack\Magic\Fields\Checkbox;
 use Cubist\Backpack\Magic\Fields\Code;
 use Cubist\Backpack\Magic\Fields\Hidden;
 use Cubist\Backpack\Magic\Fields\SelectFromArray;
@@ -40,6 +41,8 @@ class ToolWebflow extends ToolboxTranslatableModel
 
     protected $_operations = [WebflowOperation::class];
 
+    protected $_translationsForCompilation = [];
+
     public function setFields()
     {
         parent::setFields();
@@ -48,6 +51,8 @@ class ToolWebflow extends ToolboxTranslatableModel
         $this->addField('webflow', Text::class, __('URL du projet webflow'), ['translatable' => false, 'prefix' => 'https://', 'suffix' => '.webflow.io', 'column' => true, 'hint' => __('ex : :url', ['url' => 'https://projet.webflow.io']), 'tab' => __('Paramètres')]);
         $this->addField('webflow_api_token', Text::class, __('Token de l\'API Webflow'), ['translatable' => false, 'tab' => __('Paramètres')]);
         $this->addField('webflow_api_login', LinkButton::class, __('Récupérer un token'), ['translatable' => false, 'when' => ["webflow_api_token" => ''], 'tab' => __('Paramètres'), 'value' => 'https://webflow.com/oauth/authorize?response_type=code&client_id=' . env('WEBFLOW_CLIENT_ID') . '&state=$id&scope=assets%3Aread%20assets%3Awrite%20authorized_user%3Aread%20cms%3Aread%20cms%3Awrite%20custom_code%3Aread%20custom_code%3Awrite%20forms%3Aread%20forms%3Awrite%20pages%3Aread%20pages%3Awrite%20sites%3Aread%20sites%3Awrite']);
+        $this->addField('webflow_refresh_on_publish', Checkbox::class, __('Recharger tous les contenus lors de la publication sur Webflow'), ['default' => true, 'tab' => __('Paramètres')]);
+        $this->addField('publish_on_save', Checkbox::class, __('Publier le site lors de la modification de contenus'), ['default' => true, 'tab' => __('Paramètres')]);
         $this->addField('domains', Textarea::class, __('Domaines à télécharger'), ['translatable' => false, 'tab' => __('Paramètres')]);
         $this->addField('locales_domains', Table::class, __('Langues'), ['translatable' => false, 'columns' => ['locale' => __('Code langue'), 'url' => __('URL')], 'tab' => __('Paramètres')]);
         $this->addField('slack', Text::class, __('Notification slack'), ['translatable' => false, 'tab' => __('Paramètres')]);
@@ -106,7 +111,9 @@ class ToolWebflow extends ToolboxTranslatableModel
     public function onSaved(): bool
     {
 
-        $this->onPublish('auto');
+        if ($this->publish_on_save) {
+            $this->onPublish('auto');
+        }
         return parent::onSaved();
     }
 
@@ -326,6 +333,8 @@ class ToolWebflow extends ToolboxTranslatableModel
                 'ajax.googleapis.com',
                 'uploads-ssl.webflow.com',
                 'uploads.webflow.com',
+                'cdn.prod.website-files.com',
+                'cdn.embedly.com',
             ] + \Cubist\Util\Text::splitLines($this->domains));
 
         $wget->setArg("domains", implode(',', $domains));
@@ -362,14 +371,19 @@ class ToolWebflow extends ToolboxTranslatableModel
 
     protected function getCustomCSS()
     {
-        $css = '';
+        $css = '.w-webflow-badge{display:none !important;}';
 
         return $css . "\n\n" . $this->css;
     }
 
     protected function getCustomJS()
     {
-        $js = '';
+        $code = '$(".w-webflow-badge").remove();' . "\n";
+        foreach ($this->getLocales() as $locale) {
+            $code .= '$("a#switch-' . $locale['locale'] . '").attr("href","https://' . $locale['url'] . '");' . "\n";
+        }
+
+        $js = '$(function(){' . $code . '});';
 
         return $js . ";\n\n" . $this->js;
     }
@@ -409,9 +423,51 @@ class ToolWebflow extends ToolboxTranslatableModel
         $html = str_replace('</head>', '<link href="/css/custom.css" rel="stylesheet">' . "\n" . '</head>', $html);
         $html = str_replace('</body>', '<script src="/js/custom.js"></script>' . "\n" . '</body>', $html);
 
+        $html = preg_replace('/lang=\"[a-zA-Z\-_]{2,6}\"/', 'lang="' . $locale . '"', $html);
+
+        // Texts
+        $texts = $this->getTextTranslationsForCompilation($locale);
+        foreach ($texts as $text => $translation) {
+            $html = str_replace($text, $translation, $html);
+        }
+
+        // Images
+        $images = $this->getTranslation('images', $locale);
+
+        // SEO
+        $html = preg_replace('/<title>[^<]*<\/title>/', '<title>Nimp</title>', $html);
+
         file_put_contents($f->getPathname(), $html);
     }
 
+    protected function getTextTranslationsForCompilation($locale)
+    {
+        if (isset($this->_translationsForCompilation[$locale])) {
+            return $this->_translationsForCompilation[$locale];
+        }
+
+        $res = [];
+        $texts = $this->getTranslation('texts', $locale);
+
+        uksort($texts, function ($a, $b) {
+            return mb_strlen($b) - mb_strlen($a);
+        });
+
+        foreach ($texts as $text => $translation) {
+            if (!$translation) {
+                continue;
+            }
+            $text = str_replace("\n", '<br />', htmlspecialchars(base64_decode($text)));
+            $text = str_replace("&#039;", '&#x27;', $text);
+            $translation = str_replace("\n", '<br />', $translation);
+            $res[$text] = $translation;
+            $res[str_replace('<br />', ' <br />', $text)] = $translation;
+        }
+
+        $this->_translationsForCompilation[$locale] = $res;
+        return $res;
+    }
+
     public function onRetrieved(): bool
     {
 
index c8a1616921460a39fb760128f046f33553e38826..969d38d0a3567e1700b859f9650b7f6e3046d2d0 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Services\Webflow\Excel;
+use Cubist\Util\Html;
 use Illuminate\Http\Client\Response;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Http;
@@ -297,10 +298,14 @@ class Webflow
                         if (!$node['text']['text']) {
                             continue;
                         }
-                        if (!isset($res['texts'][$node['text']['text']])) {
-                            $res['texts'][$node['text']['text']] = ['key' => base64_encode($node['text']['text']), 'occurences' => 0];
+                        $texts = Html::getTextNodes($node['text']['html']);
+                        foreach ($texts as $t) {
+                            if (!isset($res['texts'][$t])) {
+                                $res['texts'][$t] = ['key' => base64_encode($t), 'occurences' => 0];
+                            }
+                            $res['texts'][$t]['occurences']++;
                         }
-                        $res['texts'][$node['text']['text']]['occurences']++;
+
                     } else if ($node['type'] === 'image') {
 
                         if (!isset($node['image']['assetId'])) {