Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to use dynamic blocks in AJAX requests with JSON. #324

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/code/community/Lesti/Fpc/Helper/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Lesti_Fpc_Helper_Block extends Lesti_Fpc_Helper_Abstract
{
const DYNAMIC_BLOCKS_XML_PATH = 'system/fpc/dynamic_blocks';
const LAZY_BLOCKS_XML_PATH = 'system/fpc/lazy_blocks';
const JSON_ENCODE_DYNAMIC_BLOCKS_XML_PATH = 'system/fpc/json_encode_dynamic_blocks';
const LAZY_BLOCKS_VALID_SESSION_PARAM = 'fpc_lazy_blocks_valid';
const USE_RECENTLY_VIEWED_PRODUCTS_XML_PATH =
'system/fpc/use_recently_viewed_products';
Expand All @@ -38,6 +39,14 @@ public function getLazyBlocks()
return $this->getCSStoreConfigs(self::LAZY_BLOCKS_XML_PATH);
}

/**
* @return array
*/
public function getJsonEncodeDynamicBlocks()
{
return $this->getCSStoreConfigs(self::JSON_ENCODE_DYNAMIC_BLOCKS_XML_PATH);
}

/**
* @return bool
*/
Expand Down
25 changes: 25 additions & 0 deletions app/code/community/Lesti/Fpc/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,29 @@ public function getContentType(\Mage_Core_Controller_Response_Http $response)

return 'text/html; charset=UTF-8';
}

/**
* Use is_ajax as AJAX parameter
* @return bool
*/
protected function _isAjaxRequest()
{
$params = Mage::app()->getRequest()->getParams();
return Mage::app()->getRequest()->isAjax() || (isset($params['is_ajax']) && $params['is_ajax'] == 1);
}

/**
* @param $blockName
* @param $html
* @return string
*/
public function parseResponseBlockHtml($blockName, $html)
{
$jsonEncodeDynamicBlocks = Mage::helper('fpc/block')->getJsonEncodeDynamicBlocks();
if (in_array($blockName, $jsonEncodeDynamicBlocks) && $this->_isAjaxRequest()) {
return trim(Mage::helper('core')->jsonEncode($html), '"');
} else {
return $html;
}
}
}
4 changes: 2 additions & 2 deletions app/code/community/Lesti/Fpc/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function coreBlockAbstractToHtmlAfter($observer)
$placeholder = Mage::helper('fpc/block')
->getPlaceholderHtml($blockName);
$html = $observer->getTransport()->getHtml();
$this->_html[] = $html;
$this->_html[] = Mage::helper('fpc')->parseResponseBlockHtml($blockName, $html);
$this->_placeholder[] = $placeholder;
$observer->getTransport()->setHtml($placeholder);
}
Expand Down Expand Up @@ -266,7 +266,7 @@ protected function _insertDynamicBlocks(
if (in_array($blockName, $lazyBlocks)) {
$session->setData('fpc_lazy_block_' . $blockName, $html);
}
$this->_html[] = $html;
$this->_html[] = Mage::helper('fpc')->parseResponseBlockHtml($blockName, $html);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Lesti/Fpc/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<config>
<modules>
<Lesti_Fpc>
<version>1.4.8</version>
<version>1.4.9</version>
</Lesti_Fpc>
</modules>
<global>
Expand Down
9 changes: 9 additions & 0 deletions app/code/community/Lesti/Fpc/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</dynamic_blocks>
<json_encode_dynamic_blocks>
<label>Dynamic Blocks to JSON Encode in AJAX Response</label>
<frontend_type>textarea</frontend_type>
<comment>All dynamic Blocks which needs to be encoded as JSON in the AJAX response, separated with Comma</comment>
<sort_order>25</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</json_encode_dynamic_blocks>
<refresh_actions>
<label>Refresh Actions</label>
<frontend_type>textarea</frontend_type>
Expand Down