Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/54e6a65ac7a4e7a9c7407bd84529b64d to your computer and use it in GitHub Desktop.
Save wpmudev-sls/54e6a65ac7a4e7a9c7407bd84529b64d to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Smush] - Beaver fix image not serving via CDN.
* Plugin URI: https://wpmudev.com/
* Description: Beaver Builder not serving images via CDN.
* Author: Abdoljabbar Bakhshande
* Author URI: https://wpmudev.com/
* Task: SLS-3724
* License: GPLv2 or later
*
* @package WordPress
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
add_action(
'after_setup_theme',
function() {
if ( class_exists( 'WP_Smush' ) && WP_Smush::is_pro() && WP_Smush::get_instance()->core()->mod->cdn->is_active() ) {
add_filter( 'fl_builder_render_js', 'smush_mu_fix_beaver_builder_cdn' );
add_filter( 'fl_builder_render_css', 'smush_mu_fix_beaver_builder_cdn' );
}
}
);
function smush_mu_fix_beaver_builder_cdn( $source ) {
$source = preg_replace_callback(
'#https?://[^"].+.(?:jpe?g|png|gif|webp)#',
function( $match ) {
if ( ! empty( $match[0] ) ) {
$cdn = WP_Smush::get_instance()->core()->mod->cdn;
if ( $cdn->is_supported_path( $match[0] ) ) {
$match[0] = $cdn->generate_cdn_url( $match[0] );
}
}
return $match[0];
},
$source
);
return $source;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment