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

Fix (Dynamic Content): prevent blurry images when using image optimizer #3369

Open
wants to merge 3 commits into
base: develop
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
1 change: 1 addition & 0 deletions .config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ module.exports = [
'frontend_block_progress_bar': path.resolve( __dirname, '../src/block/progress-bar/frontend-progress-bar.js' ),
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
'frontend_image_optimizer_polyfill': path.resolve( __dirname, '../src/block-components/image/image-optimizer-polyfill.js' ),
},

output: {
Expand Down
1 change: 1 addition & 0 deletions .config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ module.exports = [
'frontend_block_progress_bar': path.resolve( __dirname, '../src/block/progress-bar/frontend-progress-bar.js' ),
'frontend_block_horizontal_scroller': path.resolve( __dirname, '../src/block/horizontal-scroller/frontend-horizontal-scroller.js' ),
'frontend_block_tabs': path.resolve( __dirname, '../src/block/tabs/frontend-tabs.js' ),
'frontend_image_optimizer_polyfill': path.resolve( __dirname, '../src/block-components/image/image-optimizer-polyfill.js' ),
},

output: {
Expand Down
1 change: 1 addition & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ function is_frontend() {
require_once( plugin_dir_path( __FILE__ ) . 'src/block/columns/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block/timeline/index.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block/icon-label/deprecated.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/block-components/image/index.php' );
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/block-components/image/image-optimizer-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import domReady from '@wordpress/dom-ready'

class ImageOptimizerPolyfill {
bfintal marked this conversation as resolved.
Show resolved Hide resolved
/**
* This script is loaded when EWWW Image Optimizer plugin is activated
* If Easy IO setting is activated for EWWW Image Optimizer plugin, dynamic images becomes blurry.
* This script fixes the issue by removing the &fit parameter from the srcset and src attributes
*/
init = () => {
const imgs = document.querySelectorAll( '.stk-block img' )
imgs.forEach( img => {
if ( img.hasAttribute( 'srcset' ) ) {
let srcset = img.getAttribute( 'srcset' )
const pattern = /https?:\/\/[^\s,]+/g
const urls = srcset.match( pattern )

urls.forEach( url => {
const index = url.indexOf( '&fit' )
if ( index !== -1 ) {
const newUrl = url.slice( 0, index )
srcset = srcset.replace( url, newUrl )
}
} )

img.setAttribute( 'srcset', srcset )
}

if ( img.getAttribute( 'src' ).indexOf( '&fit' ) !== -1 ) {
const src = img.getAttribute( 'src' )
const index = src.indexOf( '&fit' )
const newSrc = src.slice( 0, index )
img.setAttribute( 'src', newSrc )
}
} )
}
}

window.ImageOptimizerPolyfill = new ImageOptimizerPolyfill()
domReady( window.ImageOptimizerPolyfill.init )
30 changes: 30 additions & 0 deletions src/block-components/image/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

if ( ! function_exists( 'stackable_load_image_optimizer_polyfill_frontend_script' ) ) {
function stackable_load_image_optimizer_polyfill_frontend_script( $block_content, $block ) {
bfintal marked this conversation as resolved.
Show resolved Hide resolved
// If Easy IO setting is activated for EWWW Image Optimizer, dynamic images becomes blurry.
// Load the script to fix the issue.
if ( ! is_admin() ) {
bfintal marked this conversation as resolved.
Show resolved Hide resolved
wp_enqueue_script(
'stk-frontend-image-optimizer-polyfill',
plugins_url( 'dist/frontend_image_optimizer_polyfill.js', STACKABLE_FILE ),
array(),
STACKABLE_VERSION,
true
);

// Only do this once.
remove_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10 );
}
}

if ( ! is_admin() && defined( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' )) {
// Load the script in the frontend if EWWW Image Optimizer is active.
add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10, 2 );
}
}
Loading