Skip to content

Commit 4e685f3

Browse files
authored
Merge pull request #16 from magepal/clockworkgeek-mage-init
Add individual data-mage-init attributes
2 parents ee77e07 + f293e60 commit 4e685f3

File tree

11 files changed

+44
-370
lines changed

11 files changed

+44
-370
lines changed

Block/Adminhtml/System/Config/Form/Composer/Version.php

Lines changed: 0 additions & 136 deletions
This file was deleted.

Block/Adminhtml/System/Config/Form/Module/Version.php

Lines changed: 0 additions & 88 deletions
This file was deleted.

Block/Js.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

Plugin/Block/Product/ImagePlugin.php

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
use Magento\Catalog\Block\Product\Image;
1212
use MagePal\CatalogLazyLoad\Helper\Data;
1313

14-
/**
15-
* Class ImagePlugin
16-
* @package MagePal\CatalogLazyLoad\Plugin\Block\Product
17-
*/
1814
class ImagePlugin
1915
{
2016
/** @var Data */
@@ -31,46 +27,43 @@ public function __construct(
3127

3228
/**
3329
* @param Image $subject
34-
* @param Closure $proceed
35-
* @return mixed
30+
* @param string $result
31+
* @return string
3632
*/
37-
public function aroundToHtml(Image $subject, Closure $proceed)
33+
public function afterToHtml(Image $subject, $result)
3834
{
3935
if ($this->helper->isEnabled() && $this->helper->applyLazyLoad()) {
40-
$orgImageUrl = $subject->getImageUrl();
41-
$subject->setImageUrl('');
42-
43-
//Magento 2.4.0
44-
if (is_array($subject->getCustomAttributes())) {
45-
$customAttributes = array_merge(
46-
$subject->getCustomAttributes(),
47-
['magepal-data-original' => 'placeholder']
48-
);
49-
} else {
50-
$customAttributes = trim(
51-
$subject->getCustomAttributes() . 'magepal-data-original'
52-
);
53-
}
5436

55-
$subject->setCustomAttributes($customAttributes);
37+
$result = preg_replace_callback(
38+
'#<img(?:\s+[-\w]+=(?:"[^"]*"|\'[^\']*\'))+\s*/>#mu',
39+
function ($matches) {
40+
$img = $matches[0];
41+
$search = [' src="'];
42+
$replace = [' data-original="'];
5643

57-
$result = $proceed();
44+
if (strpos($img, 'class=') === false) {
45+
$search[] = '/>';
46+
$replace[] = ' class="swatch-option-loading" />';
47+
} else {
48+
$search[] = ' class="';
49+
$replace[] = ' class="swatch-option-loading ';
50+
}
5851

59-
$find = [
60-
'img class="',
61-
'magepal-data-original="placeholder"',
62-
'magepal-data-original'
63-
];
52+
if (strpos($img, 'data-mage-init=') === false) {
53+
$search[] = '/>';
54+
$replace[] = ' data-mage-init=\'{"MagePalLazyLoad":{}}\' />';
55+
} else {
56+
$search[] = ' data-mage-init=\'{';
57+
$replace[] = ' data-mage-init=\'{"MagePalLazyLoad":{},';
58+
}
6459

65-
$replace = [
66-
'img class="lazy swatch-option-loading ',
67-
sprintf(' data-original="%s"', $orgImageUrl),
68-
sprintf(' data-original="%s"', $orgImageUrl)
69-
];
70-
71-
return str_replace($find, $replace, $result);
72-
} else {
73-
return $proceed();
60+
return str_replace($search, $replace, $img);
61+
},
62+
$result
63+
);
7464
}
65+
66+
return $result;
7567
}
7668
}
69+

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Unlike other Lazy Load extensions for Magento, our extension take into considera
2323
This is important because:
2424

2525
1. Your site will appear to load much faster to your customer because they won't have to wait for Javascript to activate your images.
26-
2. Your products images will start to display immediately as your browser fetches them, instead of waiting for Javascript to activate them which will normally cause 1-5 seconds delay and largely depended on the speed of your customer's computer.
26+
2. Your products image will start to display immediately as your browser fetches them, instead of waiting for Javascript to activate them which will normally cause 1-5 seconds delay and largely depended on the speed of your customer's computer.
2727
3. On subsequences visit, your top 'x' images just appear because they are already cached, while other extensions will still delay loading.
2828

2929
You can quickly and easily configure the number of images to show instantly base on your site need in our configurable in system configuration.
@@ -78,4 +78,4 @@ Magento 2 Extensions
7878
- [Order Confirmation Page Miscellaneous Scripts](https://www.magepal.com/magento2/extensions/order-confirmation-miscellaneous-scripts-for-magento-2.html)
7979
- [HTML Minifier for Magento2](https://www.magepal.com/magento2/extensions/html-minifier.html)
8080

81-
© MagePal LLC. | [www.magepal.com](https:/www.magepal.com)
81+
© MagePal LLC. | [www.magepal.com](https://www.magepal.com)

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"php": "~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0|~7.1.3|~7.2.0|~7.3.0|~7.4.0",
2020
"magento/module-backend": "100.0.*|100.1.*|100.2.*|101.0.*|102.0.*",
2121
"magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*|103.0.*",
22-
"magepal/magento2-core":">1.1.0"
22+
"magepal/magento2-core":">=1.1.9"
2323
},
2424
"type": "magento2-module",
25-
"version": "1.7.2",
25+
"version": "1.8.0",
2626
"license": [
2727
"proprietary"
2828
],

0 commit comments

Comments
 (0)