Skip to content

Commit 8fdc7d8

Browse files
authored
Fix (Dynamic Content): prevent blurry images when using image optimizer (#3369)
* fix blurry dynamic content images * load when plugin is active, add comments * use constant to check if plugin is active
1 parent 02a6427 commit 8fdc7d8

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

.config/webpack.config.dev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ module.exports = [
181181
'frontend_block_progress_bar': path.resolve( __dirname, '../src/block/progress-bar/frontend-progress-bar.js' ),
182182
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
183183
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
184+
'frontend_image_optimizer_polyfill': path.resolve( __dirname, '../src/block-components/image/image-optimizer-polyfill.js' ),
184185
},
185186

186187
output: {

.config/webpack.config.prod.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ module.exports = [
166166
'frontend_block_progress_bar': path.resolve( __dirname, '../src/block/progress-bar/frontend-progress-bar.js' ),
167167
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
168168
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
169+
'frontend_image_optimizer_polyfill': path.resolve( __dirname, '../src/block-components/image/image-optimizer-polyfill.js' ),
169170
},
170171

171172
output: {

plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ function is_frontend() {
252252
require_once( plugin_dir_path( __FILE__ ) . 'src/block/columns/index.php' );
253253
require_once( plugin_dir_path( __FILE__ ) . 'src/block/timeline/index.php' );
254254
require_once( plugin_dir_path( __FILE__ ) . 'src/block/icon-label/deprecated.php' );
255+
require_once( plugin_dir_path( __FILE__ ) . 'src/block-components/image/index.php' );
255256
}
256257

257258
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import domReady from '@wordpress/dom-ready'
5+
6+
class ImageOptimizerPolyfill {
7+
/**
8+
* This script is loaded when EWWW Image Optimizer plugin is activated
9+
* If Easy IO setting is activated for EWWW Image Optimizer plugin, dynamic images becomes blurry.
10+
* This script fixes the issue by removing the &fit parameter from the srcset and src attributes
11+
*/
12+
init = () => {
13+
const imgs = document.querySelectorAll( '.stk-block img' )
14+
imgs.forEach( img => {
15+
if ( img.hasAttribute( 'srcset' ) ) {
16+
let srcset = img.getAttribute( 'srcset' )
17+
const pattern = /https?:\/\/[^\s,]+/g
18+
const urls = srcset.match( pattern )
19+
20+
urls.forEach( url => {
21+
const index = url.indexOf( '&fit' )
22+
if ( index !== -1 ) {
23+
const newUrl = url.slice( 0, index )
24+
srcset = srcset.replace( url, newUrl )
25+
}
26+
} )
27+
28+
img.setAttribute( 'srcset', srcset )
29+
}
30+
31+
if ( img.getAttribute( 'src' ).indexOf( '&fit' ) !== -1 ) {
32+
const src = img.getAttribute( 'src' )
33+
const index = src.indexOf( '&fit' )
34+
const newSrc = src.slice( 0, index )
35+
img.setAttribute( 'src', newSrc )
36+
}
37+
} )
38+
}
39+
}
40+
41+
window.ImageOptimizerPolyfill = new ImageOptimizerPolyfill()
42+
domReady( window.ImageOptimizerPolyfill.init )
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
// Exit if accessed directly.
4+
if ( ! defined( 'ABSPATH' ) ) {
5+
exit;
6+
}
7+
8+
if ( ! function_exists( 'stackable_load_image_optimizer_polyfill_frontend_script' ) ) {
9+
function stackable_load_image_optimizer_polyfill_frontend_script( $block_content, $block ) {
10+
// If Easy IO setting is activated for EWWW Image Optimizer, dynamic images becomes blurry.
11+
// Load the script to fix the issue.
12+
if ( ! is_admin() ) {
13+
wp_enqueue_script(
14+
'stk-frontend-image-optimizer-polyfill',
15+
plugins_url( 'dist/frontend_image_optimizer_polyfill.js', STACKABLE_FILE ),
16+
array(),
17+
STACKABLE_VERSION,
18+
true
19+
);
20+
21+
// Only do this once.
22+
remove_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10 );
23+
}
24+
}
25+
26+
if ( ! is_admin() && defined( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' )) {
27+
// Load the script in the frontend if EWWW Image Optimizer is active.
28+
add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10, 2 );
29+
}
30+
}

0 commit comments

Comments
 (0)