Explorar o código

Merge branch 'master' of https://github.com/unaio/una

AntonLV %!s(int64=5) %!d(string=hai) anos
pai
achega
4207aa2ce7

+ 8 - 6
inc/classes/BxDolTemplate.php

@@ -495,17 +495,19 @@ class BxDolTemplate extends BxDolFactory implements iBxDolSingleton
         $fFilterCss = function ($a) use ($aExcludeCss) {
             if (in_array($a['url'], $aExcludeCss))
                 return false;
-            foreach ($this->aPageSnapshot['css_compiled'] as $r)
-                if ($r['url'] == $a['url'])
-                    return false;
+            if (isset($this->aPageSnapshot['css_compiled']))
+                foreach ($this->aPageSnapshot['css_compiled'] as $r)
+                    if ($r['url'] == $a['url'])
+                        return false;
             return true;
         };
         $fFilterJs = function ($a) use ($aExcludeJs) {
             if (in_array($a['url'], $aExcludeJs))
                 return false;
-            foreach ($this->aPageSnapshot['js_compiled'] as $r)
-                if ($r['url'] == $a['url'])
-                    return false;
+            if (isset($this->aPageSnapshot['js_compiled']))
+                foreach ($this->aPageSnapshot['js_compiled'] as $r)
+                    if ($r['url'] == $a['url'])
+                        return false;
             return true;
         };    
 

+ 1 - 0
install/sql/system.sql

@@ -5144,6 +5144,7 @@ CREATE TABLE IF NOT EXISTS `sys_pages_blocks` (
   `text` mediumtext NOT NULL,
   `text_updated` int(11) NOT NULL,
   `help` varchar(255) NOT NULL,
+  `cache_lifetime` int(11) NOT NULL DEFAULT '0',
   `deletable` tinyint(4) NOT NULL DEFAULT '1',
   `copyable` tinyint(4) NOT NULL DEFAULT '1',
   `active` tinyint(4) NOT NULL DEFAULT '1',

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

@@ -530,6 +530,7 @@
 	<string name="_adm_bp_scs_save"><![CDATA[The changes were successfully saved]]></string>
 	<string name="_adm_bp_txt_block_active"><![CDATA[Active]]></string>
 	<string name="_adm_bp_txt_block_async"><![CDATA[Asynchronous Mode]]></string>
+	<string name="_adm_bp_txt_block_cache_lifetime"><![CDATA[Block Cache Lifetime (in seconds)]]></string>
 	<string name="_adm_bp_txt_block_content_attachments_html"><![CDATA[Attachments]]></string>
 	<string name="_adm_bp_txt_block_content_image_align"><![CDATA[Alignment]]></string>
 	<string name="_adm_bp_txt_block_content_image_align_center"><![CDATA[center]]></string>

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

@@ -530,6 +530,7 @@ _sys_layout_1_column<?xml version="1.0" encoding="utf-8"?>
 	<string name="_adm_bp_scs_save"><![CDATA[Изменения успешно сохранены]]></string>
 	<string name="_adm_bp_txt_block_active"><![CDATA[Активный]]></string>
     <string name="_adm_bp_txt_block_async"><![CDATA[Асинхронный режим]]></string>
+    <string name="_adm_bp_txt_block_cache_lifetime"><![CDATA[Кэш для блока (в секундах)]]></string>
 	<string name="_adm_bp_txt_block_content_attachments_html"><![CDATA[Вложения]]></string>
 	<string name="_adm_bp_txt_block_content_image_align"><![CDATA[Выравнивание]]></string>
 	<string name="_adm_bp_txt_block_content_image_align_center"><![CDATA[по центру]]></string>

+ 1 - 0
studio/classes/BxDolStudioBuilderPageQuery.php

@@ -417,6 +417,7 @@ class BxDolStudioBuilderPageQuery extends BxDolStudioPageQuery
                 `tpb`.`title` AS `title`,
                 `tpb`.`designbox_id` AS `designbox_id`,
                 `tpb`.`async` AS `async`,
+                `tpb`.`cache_lifetime` AS `cache_lifetime`,
                 `tpb`.`submenu` AS `submenu`,
                 `tpb`.`tabs` AS `tabs`,
                 `tpb`.`hidden_on` AS `hidden_on`,

+ 11 - 0
studio/template/scripts/BxBaseStudioBuilderPage.php

@@ -795,6 +795,17 @@ class BxBaseStudioBuilderPage extends BxDolStudioBuilderPage
                         'pass' => 'Xss',
                     ),
                 ),
+                'cache_lifetime' => array(
+                    'type' => 'text',
+                    'name' => 'cache_lifetime',
+                    'caption' => _t('_adm_bp_txt_block_cache_lifetime'),
+                    'info' => '',
+                    'value' => isset($aBlock['cache_lifetime']) ? $aBlock['cache_lifetime'] : 0,
+                    'required' => '0',
+                    'db' => array (
+                        'pass' => 'Int',
+                    ),
+                ),
                 'active' => array(
                     'type' => 'switcher',
                     'name' => 'active',

+ 87 - 15
template/scripts/BxBasePage.php

@@ -19,6 +19,7 @@ class BxBasePage extends BxDolPage
 
     protected $_sStorage; //--- Storage object for page's images like custom cover, HTML block attachments, etc.
     protected $_oPageCacheObject = null;
+    protected $_oBlockCacheObject = null;
     
     protected $_bStickyColumns = false;
 
@@ -396,7 +397,7 @@ class BxBasePage extends BxDolPage
             foreach ($aCell as $aBlock) {
                 $this->processPageBlock($aBlock, false);
 
-                $sContentWithBox = $this->_getBlockCode($aBlock, $aBlock['async']);
+                $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
 
             	$sHiddenOn = '';
                 if(!empty($aBlock['hidden_on']))
@@ -448,7 +449,41 @@ class BxBasePage extends BxDolPage
     {
         if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
             return '';
-        return $this->_getBlockCode($aBlock, 0);
+        return $this->_getBlockCodeWithCache($aBlock, 0);
+    }
+
+
+    protected function _getBlockCodeWithCache($aBlock, $iAsync = 0)
+    {
+        if (!getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync) {
+            $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
+        }
+        else {
+            $oCache = $this->_getBlockCacheObject();
+            $sKey = $this->_getBlockCacheKey(false, $aBlock);
+            $sKeyCssJs = $sKey . 'cssjs';
+
+            $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
+            $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
+
+            if ($mixedRet !== null && $aRetCssJs !== null) {
+                BxDolTemplate::getInstance()->collectingStart();
+                BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
+                $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
+                $sBlockCode .= $mixedRet;
+            } else {
+                BxDolTemplate::getInstance()->collectingStart();
+
+                $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
+
+                $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
+
+                $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
+                $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
+            }
+        }
+
+        return $sBlockCode;
     }
 
     /**
@@ -774,14 +809,31 @@ class BxBasePage extends BxDolPage
      */
     protected function _getPageCacheObject ()
     {
-        if ($this->_oPageCacheObject != null) {
-            return $this->_oPageCacheObject;
+        return $this->_getCacheObject ();
+    }
+
+    /**
+     * Get block cache object.
+     * @return cache object instance
+     */
+    protected function _getBlockCacheObject ()
+    {
+        return $this->_getCacheObject ('Block', 'pb');
+    }
+
+    protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
+    {
+        $sObj = '_o' . $sSuffixObj . 'CacheObject';
+        $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
+
+        if ($this->{$sObj} != null) {
+            return $this->{$sObj};
         } else {
-            $sEngine = getParam('sys_page_cache_engine');
-            $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine);
-            if (!$this->_oPageCacheObject->isAvailable())
-                $this->_oPageCacheObject = bx_instance ('BxDolCacheFile');
-            return $this->_oPageCacheObject;
+            $sEngine = getParam($sParam);
+            $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
+            if (!$this->{$sObj}->isAvailable())
+                $this->{$sObj} = bx_instance ('BxDolCacheFile');
+            return $this->{$sObj};
         }
     }
 
@@ -800,6 +852,21 @@ class BxBasePage extends BxDolPage
         return $s;
     }
 
+    /**
+     * Get block cache key.
+     * @param $isPrefixOnly return key prefix only.
+     * @return string
+     */
+    protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
+    {
+        $s = $this->_getPageCacheKey(true) . 'block_';
+        if ($isPrefixOnly)
+            return $s;
+        $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
+        $s .= bx_site_hash() . '.php';
+        return $s;
+    }
+
     /**
      * Additional cache key. In the case of dynamic page.
      * For example - profile page, where each profile has own page.
@@ -817,13 +884,18 @@ class BxBasePage extends BxDolPage
      */
     protected function cleanCache ($isDelAllWithPagePrefix = false)
     {
-        $oCache = $this->_getPageCacheObject ();
-        $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix);
+        $a = ['Page', 'Block'];
+        $bRet = false;
+        foreach ($a as $s) {
+            $oCache = $this->{'_get' . $s . 'CacheObject'} ();
+            $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
 
-        if ($isDelAllWithPagePrefix)
-            return $oCache->removeAllByPrefix($sKey);
-        else
-            return $oCache->delData($sKey);
+            if ($isDelAllWithPagePrefix)
+                $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
+            else
+                $bRet = $bRet && $oCache->delData($sKey);
+        }
+        return $bRet;
     }
 }