Răsfoiți Sursa

Ticket #5383 - Embeds: Add revalidation period.

AntonLV 8 luni în urmă
părinte
comite
3b9fc66843

+ 18 - 2
inc/classes/BxDolEmbed.php

@@ -43,6 +43,8 @@ class BxDolEmbed extends BxDolFactoryObject
 
     protected $_sTableData = '';
 
+    protected $_iLifetime;
+
     protected $_bCssJsAdded = false;
         
     public function __construct ($aObject, $oTemplate = null)
@@ -52,6 +54,8 @@ class BxDolEmbed extends BxDolFactoryObject
         $this->_oDb->setParams([
             'table_data' => $this->_sTableData
         ]);
+
+        $this->_iLifetime = 86400 * (int)getParam('sys_embed_lifetime');
     }
 
     /**
@@ -99,8 +103,20 @@ class BxDolEmbed extends BxDolFactoryObject
     {
         $sUrl = $this->cleanYoutubeUrl($sUrl);
 
-        $sData = $this->_oDb->getLocal($sUrl, $sTheme);
-        if(!$sData)
+        $aLocal = $this->_oDb->getLocalInfo($sUrl, $sTheme);
+        
+        $bRevalidate = false;
+        if($this->_iLifetime && (int)$aLocal['added'] + $this->_iLifetime < time()) {
+            $this->_oDb->deleteLocal([
+                'url' => $sUrl,
+                'theme' => $sTheme
+            ]);
+
+            $bRevalidate = true;
+        }
+
+        $sData = false;
+        if($bRevalidate || (($sKey = 'data') && !($sData = $aLocal[$sKey] ?? false)))
             $sData = $this->{'_getData' . ($sData !== false ? 'Empty' : ($this->_bAsync ? 'Async' : ''))}($sUrl, $sTheme);
 
         return json_decode($sData, true);

+ 16 - 0
inc/classes/BxDolEmbedQuery.php

@@ -38,6 +38,14 @@ class BxDolEmbedQuery extends BxDolFactoryObjectQuery
         ]);
     }
 
+    public function getLocalInfo ($sUrl, $sTheme)
+    {
+        return $this->getRow("SELECT * FROM `" . $this->_sTableData . "` WHERE `url` = :url AND  `theme` = :theme", [
+            'url' => $sUrl, 
+            'theme' => $sTheme
+        ]);
+    }
+
     public function getLocalUnprocessed ()
     {
         return $this->getAll("SELECT * FROM `" . $this->_sTableData . "` WHERE `data` = ''");
@@ -60,6 +68,14 @@ class BxDolEmbedQuery extends BxDolFactoryObjectQuery
 
         return $this->query("UPDATE `{$this->_sTableData}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
     }
+    
+    public function deleteLocal($aParamsWhere)
+    {
+        if(empty($aParamsWhere))
+            return false;
+
+        return $this->query("DELETE FROM `{$this->_sTableData}` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
+    }
 }
 
 /** @} */

+ 3 - 1
install/sql/system.sql

@@ -590,7 +590,9 @@ INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `info`, `value`, `ty
 
 (@iCategoryId, 'useLikeOperator', '_adm_stg_cpt_option_use_like_operator', '', 'on', 'checkbox', '', '', '', 45),
 
-(@iCategoryId, 'sys_embed_default', '_adm_stg_cpt_option_sys_embed_default', '', 'sys_system', 'select', 'a:3:{s:6:"module";s:6:"system";s:6:"method";s:25:"get_options_embed_default";s:5:"class";s:13:"TemplServices";}', '', '', 79),
+(@iCategoryId, 'sys_embed_default', '_adm_stg_cpt_option_sys_embed_default', '', 'sys_system', 'select', 'a:3:{s:6:"module";s:6:"system";s:6:"method";s:25:"get_options_embed_default";s:5:"class";s:13:"TemplServices";}', '', '', 70),
+(@iCategoryId, 'sys_embed_lifetime', '_adm_stg_cpt_option_sys_embed_lifetime', '', '30', 'digit', '', '', '', 73),
+
 (@iCategoryId, 'sys_embedly_api_key', '_adm_stg_cpt_option_sys_embedly_api_key', '', '', 'digit', '', '', '', 80),
 
 (@iCategoryId, 'sys_iframely_api_key', '_adm_stg_cpt_option_sys_iframely_api_key', '', '', 'digit', '', '', '', 90),

+ 1 - 0
modules/boonex/english/data/langs/system/en.xml

@@ -1689,6 +1689,7 @@
     <string name="_adm_stg_cpt_option_sys_location_normalize_names"><![CDATA[Normalize geographic names]]></string>
     <string name="_adm_stg_cpt_option_sys_maps_api_key"><![CDATA[Google Maps API key]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_default"><![CDATA[Embeds provider]]></string>
+    <string name="_adm_stg_cpt_option_sys_embed_lifetime"><![CDATA[Generated embed lifetime in days (0 - do not track time)]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_system"><![CDATA[System]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_microlink"><![CDATA[Microlink]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_peekalink"><![CDATA[Peekalink]]></string>

+ 1 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -1664,6 +1664,7 @@
     <string name="_adm_stg_cpt_option_sys_location_normalize_names"><![CDATA[Нормализация географических названий]]></string>
     <string name="_adm_stg_cpt_option_sys_maps_api_key"><![CDATA[Ключ Google Maps API]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_default"><![CDATA[Поставщик встраиваемого кода]]></string>
+    <string name="_adm_stg_cpt_option_sys_embed_lifetime"><![CDATA[Срок действия сгенерированного встраивания в днях (0 - время не отслеживать)]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_system"><![CDATA[Системный]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_microlink"><![CDATA[Microlink]]></string>
     <string name="_adm_stg_cpt_option_sys_embed_peekalink"><![CDATA[Peekalink]]></string>

+ 1 - 0
template/images/embed.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-link-icon lucide-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>

+ 1 - 1
template/scripts/BxBaseEmbedSystem.php

@@ -53,7 +53,7 @@ class BxBaseEmbedSystem extends BxDolEmbed
             if($sLogo && is_numeric($sLogo))
                 $sLogo = $oStorage->getFileUrlById($sLogo);
         }
-        $sImage = $sImage ?: $sLogo;
+        $sImage = $sImage ?: ($sLogo ?: $this->_oTemplate->getImageUrl('embed.svg'));
 
         return $this->_oTemplate->parseHtmlByName('embed_system_link.html', [
             'link' => $aData['url'],