From: Vincent Vanwaelscappel Date: Thu, 20 Jun 2024 15:22:39 +0000 (+0200) Subject: wip #6944 @7 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=aff2ac6a8a62de7d063a3c06f4d7fcd742887ee5;p=fluidbook-toolbox.git wip #6944 @7 --- diff --git a/app/Jobs/WebflowPublish.php b/app/Jobs/WebflowPublish.php index b57486292..5117b6831 100644 --- a/app/Jobs/WebflowPublish.php +++ b/app/Jobs/WebflowPublish.php @@ -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); } } diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index f7ecfba07..7ffc3e756 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -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('', '' . "\n" . '', $html); $html = str_replace('', '' . "\n" . '', $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>Nimp', $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", '
', htmlspecialchars(base64_decode($text))); + $text = str_replace("'", ''', $text); + $translation = str_replace("\n", '
', $translation); + $res[$text] = $translation; + $res[str_replace('
', '
', $text)] = $translation; + } + + $this->_translationsForCompilation[$locale] = $res; + return $res; + } + public function onRetrieved(): bool { diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index c8a161692..969d38d0a 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -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'])) {