Skip to content

Commit

Permalink
support ignore classes and ids
Browse files Browse the repository at this point in the history
  • Loading branch information
dangscaleflex committed Mar 4, 2024
1 parent 875e745 commit ed4114d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 7 deletions.
36 changes: 36 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Config extends AbstractHelper
const XML_PATH_SCALEFLEX_CLOUDIMAGE_ADVANCED_DEVICEPIXELRATIO = 'scaleflex_cloudimage/advanced/devicepixelratio';
const XML_PATH_SCALEFLEX_CLOUDIMAGE_OPTIONS_ORG_IF_SML = 'scaleflex_cloudimage/advanced/orgifsml';
const XML_PATH_SCALEFLEX_CLOUDIMAGE_IGNORE_BLOCKS = 'scaleflex_cloudimage/advanced/ignore_blocks';
const XML_PATH_SCALEFLEX_CLOUDIMAGE_IGNORE_HTML_ID_ACTIVATE = 'scaleflex_cloudimage/advanced/ignore_html_id_active';
const XML_PATH_SCALEFLEX_CLOUDIMAGE_IGNORE_HTML_IDS = 'scaleflex_cloudimage/advanced/ignore_html_ids';

/**
* @return bool
Expand Down Expand Up @@ -143,6 +145,27 @@ public function getIgnoreBlocks()
return $ignoreList;
}

public function getIgnoreHtmlIds()
{
$ignoreList = [];

if ($this->isIgnoreHtmlIdActive()) {
$ignoreHtmlIds = $this->scopeConfig->getValue(
self::XML_PATH_SCALEFLEX_CLOUDIMAGE_IGNORE_HTML_IDS,
ScopeInterface::SCOPE_STORE
);
if (!empty($ignoreHtmlIds)) {
$ignoreHtmlIds = trim($ignoreHtmlIds);
$explodedHtmlIds = explode(",", $ignoreHtmlIds);
foreach ($explodedHtmlIds as $item) {
$ignoreList[] = trim($item);
}
}
}

return $ignoreList;
}

/**
* Get Scaleflex Cloudimage plugin configuration
*
Expand Down Expand Up @@ -224,6 +247,19 @@ public function isActive()
);
}

/**
* Is Ignore HTML ID Active
*
* @return bool
*/
public function isIgnoreHtmlIdActive()
{
return (bool)$this->scopeConfig->getValue(
self::XML_PATH_SCALEFLEX_CLOUDIMAGE_IGNORE_HTML_ID_ACTIVATE,
ScopeInterface::SCOPE_STORE
);
}

/**
* Get custom function
*
Expand Down
56 changes: 50 additions & 6 deletions Helper/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,46 @@ public function __construct(
*/
public function processHtml($html)
{
$htmlAttributes = [];

$dom = new domDocument();
$useErrors = libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $html);
libxml_use_internal_errors($useErrors);
$dom->preserveWhiteSpace = false;

foreach ($dom->getElementsByTagName('*') as $element) {
// Check if the element has an "id" attribute
if ($element->hasAttribute('id')) {
// Get the value of the "id" attribute and add it to the array
$htmlAttributes[] = $element->getAttribute('id');
}

// Check if the element has an "id" attribute
if ($element->hasAttribute('class')) {
// Get the value of the "class" attribute
$classValue = $element->getAttribute('class');

// Split the class value into individual class names
$classNames = explode(' ', $classValue);

// Add each class name to the $htmlClasses array
foreach ($classNames as $className) {
// Ignore empty class names
if (!empty($className)) {
$htmlAttributes[] = $className;
}
}
}
}

$htmlAttributes = array_unique($htmlAttributes);
if ($this->isIgnoreHtmlIds($htmlAttributes)) {
return $html;
}

if (stripos($html, '<img') !== false) {
$dom = new domDocument();
$useErrors = libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $html);
libxml_use_internal_errors($useErrors);
$dom->preserveWhiteSpace = false;
$replaceHtml = false;

$quality = '';
if ($this->config->getImageQuality() < 100) {
$quality = '?q=' . $this->config->getImageQuality();
Expand Down Expand Up @@ -108,4 +140,16 @@ public function processHtml($html)
}
return $html;
}

private function isIgnoreHtmlIds($htmlIds)
{
$ignoreHtmlIds = $this->config->getIgnoreHtmlIds();

foreach ($htmlIds as $htmlId) {
if (in_array($htmlId, $ignoreHtmlIds)) {
return true;
}
}
return false;
}
}
1 change: 1 addition & 0 deletions Observer/ProcessImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function execute(Observer $observer)

if (stripos($transport->getHtml(), '<img') !== false) {
$newHtml = $this->images->processHtml($transport->getHtml());

if ($transport->getHtml() !== $newHtml) {
$transport->setData('html', $newHtml);
}
Expand Down
15 changes: 15 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@
<comment>
<![CDATA[Cloudimage will not affect on these blocks, separate by comma. For example: product.info.description, product.info.attribute]]></comment>
</field>
<field id="ignore_html_id_active" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Ignore HTML Active</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[]]></comment>
</field>
<field id="ignore_html_ids" translate="label" type="text" sortOrder="95" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Ignore HTML ID</label>
<comment>
<![CDATA[Cloudimage will not affect on these HTML ID(s), CLASS(es), separate by comma.]]></comment>
<depends>
<field id="ignore_html_id_active">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<devicepixelratio>2</devicepixelratio>
<orgifsml>1</orgifsml>
<remove_v7>1</remove_v7>
<ignore_html_id_active>0</ignore_html_id_active>
</advanced>
</scaleflex_cloudimage>
</default>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Scaleflex_Cloudimage" setup_version="2.0.9">
<module name="Scaleflex_Cloudimage" setup_version="2.0.10">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
Expand Down

0 comments on commit ed4114d

Please sign in to comment.