Last active
July 15, 2020 15:53
-
-
Save Pross/5a20eb0ff57c888e72d7dc29637544a1 to your computer and use it in GitHub Desktop.
Replace Pagely urls in Beaver Builder with optional webp support
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 | |
/** | |
* Replace Pagely urls in Beaver Builder | |
* @author Simon Prosser | |
* @license MIT | |
*/ | |
class PagelyCDNReplace { | |
static $site_url = 'https://example.com'; // Your site url | |
static $cdn_url = 'https://cdn.example.io'; // Your CDN url | |
static $webp = true; | |
public static function init() { | |
add_filter( 'fl_builder_render_css', array( __CLASS__, 'filter' ), 10, 4 ); | |
add_filter( 'fl_builder_render_js', array( __CLASS__, 'filter' ), 10, 4 ); | |
} | |
public static function filter( $input, $nodes, $global_settings, $include_global ) { | |
// fix for CDN | |
$input = str_replace( PagelyCDNReplace::$site_url, PagelyCDNReplace::$cdn_url, $input ); | |
// if webp replace is no enabled bail. | |
if ( ! PagelyCDNReplace::$webp ) { | |
return $input; | |
} | |
// find any images we need to replace and populate urls array | |
preg_match_all( '@(\/wp-content\/.+(jpe?g|png))(?!\.webp)@', $input, $matches ); | |
$urls = array(); | |
if( ! empty( $matches ) ) { | |
foreach ( $matches as $match ) { | |
if( isset( $match[0] ) ) { | |
$urls[] = $match[0]; | |
} | |
} | |
} | |
// loop through urls and do replace | |
foreach( array_unique( $urls ) as $url ) { | |
$input = str_replace( $url, $url . '.webp', $input ); | |
} | |
return $input; | |
} | |
} | |
PagelyCDNReplace::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment