Skip to content

Commit

Permalink
block names
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaziuk committed Mar 1, 2017
1 parent dd7e3b4 commit b9b55d5
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class TLaziuk_LazyLoad_Block_System_Config_Form_Field_Block
extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
{
protected function _prepareToRender()
{
$this->addColumn('name', array(
'label' => $this->__('Name in Layout'),
));

$this->addColumn('type', array(
'label' => $this->__('Type'),
'renderer' => $this->_getTypeRenderer(),
));

return parent::_prepareToRender();
}

protected $_typeRenderer = null;

protected function _getTypeRenderer()
{
if (is_null($this->_typeRenderer)) {
$this->_typeRenderer = $this->getLayout()->createBlock(
'tlaziuk_lazyload/system_config_form_field_type',
'',
array(
'is_render_to_js_template' => true,
)
);
}
return $this->_typeRenderer;
}

protected function _prepareArrayRow(Varien_Object $row)
{
$row->setData(
'option_extra_attr_' . $this->_getTypeRenderer()->calcOptionHash($row->getData('type')),
'selected="selected"'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class TLaziuk_LazyLoad_Block_System_Config_Form_Field_Type
extends Mage_Core_Block_Html_Select
{
protected function _beforeToHtml()
{
$this->setOptions(Mage::getSingleton('tlaziuk_lazyload/system_config_source_type')->toOptionArray());

return parent::_beforeToHtml();
}
}
149 changes: 149 additions & 0 deletions app/code/local/TLaziuk/LazyLoad/Helper/Abstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
abstract class TLaziuk_LazyLoad_Helper_Abstract
extends Mage_Core_Helper_Abstract
{
const XML_PATH_ALL = 'advanced/tlaziuk_lazyload/all';
const XML_PATH_ATTRIBUTE = 'advanced/tlaziuk_lazyload/attribute';
const XML_PATH_BLOCK = 'advanced/tlaziuk_lazyload/block';
const XML_PATH_ENABLED = 'advanced/tlaziuk_lazyload/enabled';
const XML_PATH_MODULE = 'advanced/tlaziuk_lazyload/module';
const XML_PATH_PATTERN = 'advanced/tlaziuk_lazyload/pattern';
const XML_PATH_PLACEHOLDER = 'advanced/tlaziuk_lazyload/placeholder';

/**
* @return string
*/
public function getAttribute()
{
return Mage::getStoreConfig(self::XML_PATH_ATTRIBUTE);
}

/**
* @return string
*/
public function getPlaceholder()
{
return Mage::getStoreConfig(self::XML_PATH_PLACEHOLDER);
}

/** @var string */
protected $_pattern = null;

/**
* @return string
*/
public function getPattern()
{
if (!is_string($this->_pattern)) {
$this->_pattern = Mage::getStoreConfig(self::XML_PATH_PATTERN);
}
return $this->_pattern;
}

/**
* @param string $pattern
*
* @return self
*/
public function setPattern($pattern)
{
$this->_pattern = $pattern;
return $this;
}

/** @var callable */
protected $_callback = null;

/**
* @return callable
*/
public function getCallback()
{
if (!is_callable($this->_callback)) {
$this->_callback = function ($matches) {
if (strpos($matches[0], $this->getAttribute()) === false) {
return "<img{$matches[1]}{$this->getAttribute()}={$matches[2]}{$matches[3]}{$matches[2]} src={$matches[2]}{$this->getPlaceholder()}{$matches[2]}{$matches[4]}>";
}
return $matches[0];
};
}
return $this->_callback;
}

/**
* @param callable $callback
*
* @return self
*/
public function setCallback(callable $callback)
{
$this->_callback = $callback;
return $this;
}

/**
* @return boolean
*/
public function isEnabled()
{
return Mage::getStoreConfigFlag(self::XML_PATH_ENABLED);
}

/**
* @return boolean
*/
public function isAll()
{
return Mage::getStoreConfigFlag(self::XML_PATH_ALL);
}

/**
* @return string[]
*/
public function getModule()
{
return explode(',', Mage::getStoreConfig(self::XML_PATH_MODULE));
}

/**
* @return array
*/
public function getBlock()
{
$res = array(
TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_EXCLUDE => array(),
TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_INCLUDE => array(),
);

try {
if (is_array($arr = Zend_Serializer::unserialize(Mage::getStoreConfig(self::XML_PATH_BLOCK)))) {
foreach ($arr as $row) {
if ($row['type'] == TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_EXCLUDE) {
$res[TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_EXCLUDE][] = $row['name'];
} elseif ($row['type'] == TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_INCLUDE) {
$res[TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_INCLUDE][] = $row['name'];
}
}
}
} catch (Exception $exception) {
Mage::logException($exception);
}

return $res;
}

/**
* overwrite default isModuleEnabled method
*
* @param string $moduleName (optional) full module name
*
* @return boolean
*/
public function isModuleEnabled($moduleName = null)
{
if ($moduleName === $this->_getModuleName()) {
return $this->isEnabled() && parent::isModuleEnabled($moduleName);
}
return parent::isModuleEnabled($moduleName);
}
}
106 changes: 32 additions & 74 deletions app/code/local/TLaziuk/LazyLoad/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<?php
class TLaziuk_LazyLoad_Helper_Data extends Mage_Core_Helper_Abstract
class TLaziuk_LazyLoad_Helper_Data
extends TLaziuk_LazyLoad_Helper_Abstract
{
const XML_PATH_ALL = 'advanced/tlaziuk_lazyload/all';
const XML_PATH_ATTRIBUTE = 'advanced/tlaziuk_lazyload/attribute';
const XML_PATH_ENABLED = 'advanced/tlaziuk_lazyload/enabled';
const XML_PATH_MODULE = 'advanced/tlaziuk_lazyload/module';
const XML_PATH_PLACEHOLDER= 'advanced/tlaziuk_lazyload/placeholder';

const CACHE_GROUP = Mage_Core_Block_Abstract::CACHE_GROUP;
const CACHE_KEY_PREFIX = self::CACHE_TAG;
const CACHE_LIFETIME = Mage_Core_Model_Cache::DEFAULT_LIFETIME;
Expand All @@ -26,74 +21,26 @@ public function saveCache($data, $key, array $tags = array(self::CACHE_TAG), $li
return $this;
}

protected $_attribute;

public function getAttribute() {
if (empty($this->_attribute)) {
$this->_attribute = Mage::getStoreConfig(self::XML_PATH_ATTRIBUTE);
}
return $this->_attribute;
}

protected $_placeholder;

public function getPlaceholder() {
if (empty($this->_placeholder)) {
$this->setPlaceholder(Mage::getStoreConfig(self::XML_PATH_PLACEHOLDER));
}
return $this->_placeholder;
}

public function setPlaceholder($value) {
$this->_placeholder = (string) $value;
return $this;
}

protected $_pattern;

public function getPattern() {
if (empty($this->_pattern)) {
$this->setPattern('|<img([^>]*?)src=([\'\"])(.*?)\2([^>]*?)>|i');
}
return $this->_pattern;
}

public function setPattern($pattern) {
$this->_pattern = $pattern;
return $this;
}

protected $_callback;

public function getCallback() {
if (empty($this->_callback)) {
$this->setCallback(function ($matches) {
if (strpos($matches[0], $this->getAttribute()) === false) {
return "<img{$matches[1]}{$this->getAttribute()}={$matches[2]}{$matches[3]}{$matches[2]} src={$matches[2]}{$this->getPlaceholder()}{$matches[2]}{$matches[4]}>";
}
return $matches[0];
});
}
return $this->_callback;
}

public function setCallback(callable $callback) {
$this->_callback = $callback;
return $this;
}

public function isEnabled() {
return Mage::getStoreConfigFlag(self::XML_PATH_ENABLED);
}

public function isAll() {
return Mage::getStoreConfigFlag(self::XML_PATH_ALL);
}

public function getModule() {
return explode(',', Mage::getStoreConfig(self::XML_PATH_MODULE));
/**
* check config conditions if block is lazy
*
* @param Mage_Core_Block_Abstract $block
*
* @return boolean
*/
public function isBlockLazy(Mage_Core_Block_Abstract $block)
{
$blk = $this->getBlock();
return in_array($block->getNameInLayout(), $blk[TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_INCLUDE])
|| (in_array($block->getModuleName(), $this->getModule()) && !in_array($block->getNameInLayout(), $blk[TLaziuk_LazyLoad_Model_System_Config_Source_Type::VAL_EXCLUDE]));
}

/**
* @param string $html
* @param boolean $skipScript (optional)
*
* @return string
*/
public function lazyHtml($html, $skipScript = true) {
Varien_Profiler::start('TLaziuk_LazyLoad::lazyHtml');
try {
Expand Down Expand Up @@ -124,6 +71,12 @@ public function lazyHtml($html, $skipScript = true) {
protected $_commentPattern = '|<!--(.*?)-->([\s\S]*?)<!--\1-->|i';
protected $_tmpHtml = array();

/**
* @param Mage_Core_Block_Abstract $block
* @param Varien_Object $transport
*
* @return Mage_Core_Block_Abstract
*/
public function lazyBlock(Mage_Core_Block_Abstract $block, Varien_Object $transport) {
try {
$name = $block->getModuleName() . '::' . get_class($block) . '::' . ($block->getNameInLayout() ? $block->getNameInLayout() : 'anonymous');
Expand All @@ -138,7 +91,7 @@ public function lazyBlock(Mage_Core_Block_Abstract $block, Varien_Object $transp
}
return '';
}, $html);
if (in_array($block->getModuleName(), $this->getModule())) {
if ($this->isBlockLazy($block)) {
$html = $this->lazyHtml($html);
}
$html = preg_replace_callback($this->_commentPattern, function($matches) {
Expand All @@ -162,6 +115,11 @@ public function lazyBlock(Mage_Core_Block_Abstract $block, Varien_Object $transp
return $block;
}

/**
* @param Zend_Controller_Response_Abstract $response
*
* @return Zend_Controller_Response_Abstract
*/
public function lazyResponse(Zend_Controller_Response_Abstract $response) {
Varien_Profiler::start('TLaziuk_LazyLoad::lazyResponse');
foreach ($response->getBody(true) as $name => $html) {
Expand Down
4 changes: 2 additions & 2 deletions app/code/local/TLaziuk/LazyLoad/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class TLaziuk_LazyLoad_Model_Observer
public function controllerFrontSendResponseBefore(Varien_Event_Observer $observer)
{
$helper = Mage::helper('tlaziuk_lazyload');
if ($helper->isEnabled() && $helper->isAll()) {
if ($helper->isModuleOutputEnabled() && $helper->isAll()) {

/* @var Mage_Core_Controller_Varien_Front */
$front = $observer->getEvent()->getFront();
Expand All @@ -16,7 +16,7 @@ public function controllerFrontSendResponseBefore(Varien_Event_Observer $observe
public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $observer)
{
$helper = Mage::helper('tlaziuk_lazyload');
if ($helper->isEnabled() && !$helper->isAll()) {
if ($helper->isModuleOutputEnabled() && !$helper->isAll()) {

/* @var Varien_Event */
$event = $observer->getEvent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

class TLaziuk_LazyLoad_Model_System_Config_Source_Type
{
const VAL_INCLUDE = 'include';
const VAL_EXCLUDE = 'exclude';

public function toOptionArray()
{
$helper = Mage::helper('tlaziuk_lazyload');

return array(
array('value' => self::VAL_INCLUDE, 'label' => $helper->__('Include')),
array('value' => self::VAL_EXCLUDE, 'label' => $helper->__('Exclude')),
);
}
public function toArray()
{
$res = array();

foreach ($this->toOptionArray() as $opt) {
$res[$opt['value']] = $opt['label'];
}

return $res;
}
}
Loading

0 comments on commit b9b55d5

Please sign in to comment.