-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix (Dynamic Content): prevent blurry images when using image optimiz…
…er (#3369) * fix blurry dynamic content images * load when plugin is active, add comments * use constant to check if plugin is active
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import domReady from '@wordpress/dom-ready' | ||
|
||
class ImageOptimizerPolyfill { | ||
/** | ||
* 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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) { | ||
// If Easy IO setting is activated for EWWW Image Optimizer, dynamic images becomes blurry. | ||
// Load the script to fix the issue. | ||
if ( ! is_admin() ) { | ||
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 ); | ||
} | ||
} |