Created
April 3, 2020 11:26
-
-
Save wpmudev-sls/5c74d5eb588a01237a8eb40585f0c4cb to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Plugin Name: [Smush] - Override calculate image sizes | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Overrides default filter `wp_calculate_image_sizes` | |
* Task: 0/11289012348292/1169468395067145 | |
* Version: 1.0.0 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
* | |
* @package Forminator | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( | |
'init', | |
function() { | |
// Remove the default `wp_calculate_image_sizes` filter. | |
global $wp_filter; | |
$tag = 'wp_calculate_image_sizes'; | |
$hook_method = 'update_image_sizes'; | |
$hook_class = 'Smush\\Core\\Modules\\CDN'; | |
if ( ! isset( $wp_filter[ $tag ] ) ) { | |
return; | |
} | |
foreach ( $wp_filter[ $tag ]->callbacks as $key => $callback_array ) { | |
foreach ( $callback_array as $c_key => $callback ) { | |
if ( substr_compare( $c_key, $hook_method, strlen( $c_key ) - strlen( $hook_method ), strlen( $hook_method ) ) === 0 ) { | |
if ( $callback['function'][0] instanceof $hook_class ) { | |
unset( $wp_filter[ $tag ]->callbacks[ $key ][ $c_key ] ); | |
} | |
} | |
} | |
} | |
add_filter( | |
'wp_calculate_image_sizes', | |
function( $sizes, $size ) { | |
$content_width = ( isset( $GLOBALS['content_width'] ) && ! empty( $GLOBALS['content_width'] ) ) ? (int) $GLOBALS['content_width'] : 1920; | |
$resize_sizes = get_option( 'wp-smush-resize_sizes', array() ); | |
if ( isset( $resize_sizes['width'] ) && $resize_sizes['width'] < $content_width ) { | |
$content_width = $resize_sizes['width']; | |
} | |
if ( is_array( $size ) && ! empty( $size ) && $size[0] < $content_width ) { | |
return $sizes; | |
} | |
return sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $content_width ); | |
}, | |
1, | |
2 | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment