: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Smush integration with various plugins: Common class
* @package Smush\Core\Integrations
* @author Anton Vanyukov <anton@incsub.com>
* @copyright (c) 2018, Incsub (http://incsub.com)
namespace Smush\Core\Integrations;
use Smush\Core\CDN\CDN_Helper;
use Smush\Core\Modules\Helpers\Parser;
use Smush\Core\Modules\Smush;
if ( ! defined( 'WPINC' ) ) {
* Singleton class class Common
public function __construct() {
// AJAX Thumbnail Rebuild integration.
add_filter( 'wp_smush_media_image', array( $this, 'skip_images' ), 10, 2 );
// Optimise WP retina 2x images.
add_action( 'wr2x_retina_file_added', array( $this, 'smush_retina_image' ), 20, 3 );
// Remove any pre_get_posts_filters added by WP Media Folder plugin.
add_action( 'wp_smush_remove_filters', array( $this, 'remove_filters' ) );
add_action( 'wpml_updated_attached_file', array( $this, 'wpml_undo_ignore_attachment' ) );
add_action( 'wpml_after_duplicate_attachment', array( $this, 'wpml_ignore_duplicate_attachment' ), 10, 2 );
add_action( 'wpml_after_copy_attached_file_postmeta', array( $this, 'wpml_ignore_duplicate_attachment' ), 10, 2 );
add_filter( 'smush_skip_iframe_from_lazy_load', array( $this, 'exclude_recaptcha_iframe' ), 10, 2 );
// Soliloquy slider CDN support.
add_filter( 'soliloquy_image_src', array( $this, 'soliloquy_image_src' ) );
// Translate Press integration.
add_filter( 'smush_skip_image_from_lazy_load', array( $this, 'trp_translation_editor' ) );
// WP Maintenance Plugin integration.
add_action( 'template_redirect', array( $this, 'wp_maintenance_mode' ) );
// Buddyboss theme and its platform plugin integration.
add_filter( 'wp_smush_cdn_before_process_src', array( $this, 'buddyboss_platform_modify_image_src' ), 10, 2 );
// GiveWP donation form load lazyload images in iframe.
add_action( 'give_donation_form_top', array( $this, 'givewp_skip_image_lazy_load' ), 0 );
// Thumbnail regeneration handler.
add_filter( 'wp_generate_attachment_metadata', array( $this, 'maybe_handle_thumbnail_generation' ) );
$this->cdn_helper = CDN_Helper::get_instance();
add_filter( 'wp_smush_should_transform_page', array( $this, 'should_transform_page' ) );
* Remove any pre_get_posts_filters added by WP Media Folder plugin.
public function remove_filters() {
// Remove any filters added b WP media Folder plugin to get the all attachments.
if ( class_exists( 'Wp_Media_Folder' ) ) {
if ( is_object( $wp_media_folder ) ) {
remove_filter( 'pre_get_posts', array( $wp_media_folder, 'wpmf_pre_get_posts1' ) );
remove_filter( 'pre_get_posts', array( $wp_media_folder, 'wpmf_pre_get_posts' ), 0, 1 );
global $wpml_query_filter;
// If WPML is not installed, return.
if ( ! is_object( $wpml_query_filter ) ) {
// Remove language filter and let all the images be smushed at once.
if ( has_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ) ) ) {
remove_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10, 2 );
remove_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10, 2 );
/**************************************
* AJAX Thumbnail Rebuild integration.
* If this is a thumbnail regeneration - only continue for selected thumbs
* (no need to regenerate everything else).
* @param string $smush_image Image size.
* @param string $size_key Thumbnail size.
public function skip_images( $smush_image, $size_key ) {
if ( empty( $_POST['regen'] ) || ! is_array( $_POST['regen'] ) ) { // Input var ok.
$smush_sizes = wp_unslash( $_POST['regen'] ); // Input var ok.
if ( in_array( $size_key, $smush_sizes, true ) ) {
// Do not regenerate other thumbnails for regenerate action.
/**************************************
* Smush Retina images for WP Retina 2x, Update Stats.
* @param int $id Attachment ID.
* @param string $retina_file Retina image.
* @param string $image_size Image size.
public function smush_retina_image( $id, $retina_file, $image_size ) {
$smush = WP_Smush::get_instance()->core()->mod->smush;
* Allows to Enable/Disable WP Retina 2x Integration
$smush_retina_images = apply_filters( 'smush_retina_images', true );
// Check if Smush retina images is enabled.
if ( ! $smush_retina_images ) {
// Check for Empty fields.
if ( empty( $id ) || empty( $retina_file ) || empty( $image_size ) ) {
// Do not smush if auto smush is turned off.
if ( ! $smush->should_auto_smush( $id ) ) {
* Allows to skip a image from smushing
* @param bool $smush_image Smush image or not
* @param string $image_size Size of image being smushed
* @param string $retina_file Retina file path.
* @param int $id Attachment ID.
* @since 3.9.6 Add two parameters for the filter.
$smush_image = apply_filters( 'wp_smush_media_image', true, $image_size, $retina_file, $id );
$stats = $smush->do_smushit( $retina_file );
// If we squeezed out something, Update stats.
if ( ! is_wp_error( $stats ) && ! empty( $stats['data'] ) && isset( $stats['data'] ) && $stats['data']->bytes_saved > 0 ) {
$image_size = $image_size . '@2x';
$this->update_smush_stats_single( $id, $stats, $image_size );
* Updates the smush stats for a single image size.
* @param int $id Attachment ID.
* @param array $smush_stats Smush stats.
* @param string $image_size Image size.
private function update_smush_stats_single( $id, $smush_stats, $image_size = '' ) {
// Return, if we don't have image id or stats for it.
if ( empty( $id ) || empty( $smush_stats ) || empty( $image_size ) ) {
$smush = WP_Smush::get_instance()->core()->mod->smush;
$data = $smush_stats['data'];
$stats = get_post_meta( $id, Smush::$smushed_meta_key, true );
// Update existing Stats.
if ( ! empty( $stats ) ) {
// Update stats for each size.
if ( isset( $stats['sizes'] ) ) {
// if stats for a particular size doesn't exists.
if ( empty( $stats['sizes'][ $image_size ] ) ) {
// Update size wise details.
$stats['sizes'][ $image_size ] = (object) $smush->array_fill_placeholders( $smush->get_size_signature(), (array) $data );
// Update compression percent and bytes saved for each size.
$stats['sizes'][ $image_size ]->bytes = $stats['sizes'][ $image_size ]->bytes + $data->bytes_saved;
$stats['sizes'][ $image_size ]->percent = $stats['sizes'][ $image_size ]->percent + $data->compression;
$smush->get_size_signature(),
$stats['stats']['api_version'] = $data->api_version;
$stats['stats']['lossy'] = $data->lossy;
$stats['stats']['keep_exif'] = ! empty( $data->keep_exif ) ? $data->keep_exif : 0;
// Update size wise details.
$stats['sizes'][ $image_size ] = (object) $smush->array_fill_placeholders( $smush->get_size_signature(), (array) $data );
// Calculate the total compression.
$stats = WP_Smush::get_instance()->core()->total_compression( $stats );
update_post_meta( $id, Smush::$smushed_meta_key, $stats );
/**************************************
* Ignore WPML duplicated images from Smush.
* If WPML is duplicating images, we need to mark them as ignored for Smushing
* because the image is same for all those duplicated attachment posts. This is
* required to avoid wrong Smush stats.
* @param int $attachment_id Original attachment ID.
* @param int $duplicated_attachment_id Duplicated attachment ID.
public function wpml_ignore_duplicate_attachment( $attachment_id, $duplicated_attachment_id ) {
// Ignore the image from Smush if duplicate.
update_post_meta( $duplicated_attachment_id, 'wp-smush-ignore-bulk', true );
* Remove an image from the ignored list.
* When a new image is added instead of duplicate, we need to remove it
* from the ignored list to make it available for Smushing.
* @param int $attachment_id Attachment ID.
public function wpml_undo_ignore_attachment( $attachment_id ) {
delete_post_meta( $attachment_id, 'wp-smush-ignore-bulk' );
* Skip ReCaptcha iframes from lazy loading.
* @param bool $skip Should skip? Default: false.
* @param string $src Iframe url.
public function exclude_recaptcha_iframe( $skip, $src ) {
return false !== strpos( $src, 'recaptcha/api' );
/**************************************
* Replace slider image links with CDN links.
* @param string $src Image source.
public function soliloquy_image_src( $src ) {
if ( ! $this->cdn_helper->is_cdn_active() || empty( $src ) ) {
if ( $this->cdn_helper->is_supported_url( $src ) ) {
return $this->cdn_helper->generate_cdn_url( $src );
/**************************************
* Disables "Lazy Load" on Translate Press translate editor
* @param bool $skip Should skip? Default: false.
public function trp_translation_editor( $skip ) {
if ( ! class_exists( '\TRP_Translate_Press' ) || ! isset( $_GET['trp-edit-translation'] ) ) {
/**************************************
* Disable page parsing when "Maintenance" is enabled
public function wp_maintenance_mode() {
if ( ! class_exists( '\MTNC' ) ) {
if ( ! is_user_logged_in() && ! empty( $mt_options['state'] ) ) {
add_filter( 'wp_smush_should_skip_lazy_load', '__return_true' );
/**************************************
* CDN compatibility with Buddyboss platform
* @param string $src Image source.
* @param string $image Actual image element.
* @return string Original or modified image source.
public function buddyboss_platform_modify_image_src( $src, $image ) {
if ( ! defined( 'BP_PLATFORM_VERSION' ) ) {
* Compatibility with buddyboss theme and it's platform plugin.
* Buddyboss platform plugin uses the placeholder image as it's main src.
* And process_src() method below uses the same placeholder.png to create
* the srcset when "Automatic resizing" options is enabled for CDN.
* Replacing the placeholder with actual image source as early as possible.
* 1. The image source contains buddyboss-platform in its string
* 2. The image source contains placeholder.png and is crucial because there are other
* images as well which doesn't uses placeholder.
if ( false !== strpos( $src, 'buddyboss-platform' ) && false !== strpos( $src, 'placeholder.png' ) ) {
$new_src = Parser::get_attribute( $image, 'data-src' );
if ( ! empty( $new_src ) ) {
* Skip images from lazy loading on GiveWP forms.
public function givewp_skip_image_lazy_load() {
add_filter( 'wp_smush_should_skip_lazy_load', '__return_true' );
* Add method to handle thumbnail generation.
* We use this trick to call self::thumbnail_regenerate_handler()
* to avoid it called several times while calling wp_generate_attachment_metadata().
* 1. wp_generate_attachment_metadata -> wp_create_image_subsizes -> wp_update_attachment_metadata().
* 2. wp_generate_attachment_metadata -> _wp_make_subsizes -> wp_update_attachment_metadata().
* 3. After calling wp_generate_attachment_metadata() => We should only add our filter here.
* @param array $metadata Image metadata.
* @return array The provided metadata.
public function maybe_handle_thumbnail_generation( $metadata ) {
* Add filter to handle thumbnail generation.
* We use a big priority because it seems WP has an issue for this case,
* after we remove this filter, all registered filters after this priority of this hook will not call.
add_filter( 'wp_update_attachment_metadata', array( $this, 'thumbnail_regenerate_handler' ), 99999, 2 ); // S3 is using priority 110.
* Callback for 'wp_update_attachment_metadata' WordPress hook used by Smush to detect
* regenerated thumbnails and mark them as pending for (re)smush.
* @param array $new_meta New metadata.
* @param int $attachment_id The attachment ID.
* @since 3.9.6 Disable this filter while async uploading,
* and update compatible with S3, and only call it after generated metadata.
public function thumbnail_regenerate_handler( $new_meta, $attachment_id ) {
// Remove the filter as we are no longer need it.
remove_filter( 'wp_update_attachment_metadata', array( $this, 'thumbnail_regenerate_handler' ), 99999 );
* Skip if there is WP uploading a new image,
* or the attachment is not an image or does not have thumbnails.
empty( $new_meta['sizes'] )
|| isset( $_POST['post_id'] ) || isset( $_FILES['async-upload'] )
// Smushed it, don't need to check it again.
|| did_action( 'wp_smush_before_smush_file' )
// Disable when restoring.
|| did_action( 'wp_smush_before_restore_backup' )
|| ! Helper::is_smushable( $attachment_id )
// Skip if the attachment has an active smush operation or in being restored by Smush or ignored.
if ( get_transient( 'smush-in-progress-' . $attachment_id ) || get_transient( 'wp-smush-restore-' . $attachment_id ) || get_post_meta( $attachment_id, 'wp-smush-ignore-bulk', true ) ) {
$smush_meta = get_post_meta( $attachment_id, Smush::$smushed_meta_key, true );