Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/9e0bce90bc0efd00b94d048fde4ead71 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/9e0bce90bc0efd00b94d048fde4ead71 to your computer and use it in GitHub Desktop.
Fix Conflict With Brizy Builder
<?php
/**
* Plugin Name: [Smush Pro] - Fix Conflict With Brizy Builder
* Description: [Smush Pro] - Fix Conflict With Brizy Builder - Tested with Brizy 2.2.8
* Author: Thobk @ WPMUDEV
* Jira: SLS-1717
* Version: 1.2
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
add_action( 'plugins_loaded', 'wpmudev_smpro_fix_conflict_with_brizy_builder_on_enabled_cdn_func', 100 );
function wpmudev_smpro_fix_conflict_with_brizy_builder_on_enabled_cdn_func() {
if( defined('WP_SMUSH_VERSION') && class_exists('Brizy_Compatibilities_Init') && class_exists( 'WP_Smush' ) ){
// return if disabled cdn
if( ! Smush\Core\Settings::get_instance()->get('cdn') ) return;
class WPMUDEV_SMPro_Fix_Conflict_With_Brizy_Builder{
private $cdn_core;
private $is_front_end;
public function __construct(){
add_filter( 'brizy_content', array( $this, 'fix_brizy_conflict'), 9, 3 );
}
public function crop_local_asset( $attachment_hash, $filter, $post_id, $optimize = false ){
$attachment = $this->get_attachment( $attachment_hash );
if ( ! $attachment ) {
return false;
}
$media_url = get_attached_file( $attachment->ID );
$project = Brizy_Editor_Project::get();
//$brizy_post = Brizy_Editor_Post::get( $post_id );
$media_cache = new Brizy_Editor_CropCacheMedia( $project, $post_id );
$crop_media_path = $media_cache->crop_media( $media_url, $filter, true, $optimize );
return $crop_media_path;
}
public function get_attachment( $hash ) {
$attachment = null;
if ( is_numeric( $hash ) ) {
$attachment = get_post( (int) $hash );
} else {
$attachments = get_posts( array(
'meta_key' => 'brizy_attachment_uid',
'meta_value' => $hash,
'post_type' => 'attachment',
) );
if ( isset( $attachments[0] ) ) {
$attachment = $attachments[0];
}
}
return $attachment;
}
public function fix_brizy_conflict( $content, $project, $post ){
if( strpos( $content, '{@brizy_SITE_URL_PLACEHOLDER@}') !== false ){
$content = preg_replace_callback('/{@brizy_SITE_URL_PLACEHOLDER@}[^(\'|\")]+/im', function( $str ){
if( is_array( $str ) ){
$replacements = explode(',', $str[0]);
$wp_upload_dir = wp_upload_dir( null, false );
$change = 0;
foreach( $replacements as $key => $replacement ){
$replacement = trim( $replacement );
if( preg_match_all('/(brizy_media=[^ ]+)/i', $replacement, $matches) ){
if( ! empty( $matches[0] ) ){
foreach( $matches[0] as $brizy_vars ){
$brizy_vars_new = str_replace('&amp;', '&', $brizy_vars );
$params = wp_parse_args( $brizy_vars_new, [
'brizy_media' => '',
'brizy_crop' => '',
'brizy_post' => '',
'brizy_optimize' => false
] );
if( empty( $params['brizy_post'] ) ){
continue 2;
}
$params['brizy_post'] = (int) $params['brizy_post'];
if( ! empty( $params['brizy_crop'] ) ){
$params['brizy_crop'] = html_entity_decode( $params['brizy_crop'] );
}
$url = $this->crop_local_asset( $params['brizy_media'], $params['brizy_crop'], $params['brizy_post'], $params['brizy_optimize']);
if( $url ){
$url = str_replace( $wp_upload_dir['basedir'], $wp_upload_dir['baseurl'], $url);
$url = $this->generate_cdn_url( $url );
$replacements[ $key ] = str_replace('{@brizy_SITE_URL_PLACEHOLDER@}/?'. $brizy_vars, $url, $replacement);
$change = 1;
}
}
}
}
}
if( $change ){
$str[0] = join(', ', $replacements);
}
return $str[0];
}
return $str;
}, $content);
}
return $content;
}
public function cdn_core(){
if( is_null( $this->cdn_core ) ){
if( class_exists('Smush\WP_Smush') ){
$this->cdn_core = Smush\WP_Smush::get_instance()->core()->mod->cdn;
}else{
$this->cdn_core = WP_Smush::get_instance()->core()->mod->cdn;
}
}
return $this->cdn_core;
}
public function generate_cdn_url($image_url){
if( $this->is_supported_path( $image_url ) ){
$image_url = $this->cdn_core()->generate_cdn_url( $image_url );
}
return $image_url;
}
/**
* Check if the image path is supported by the CDN.
*
* @since 3.0
*
* @param string $src Image path.
*
* @return bool|string
*/
private function is_supported_path( $src ) {
$url_parts = wp_parse_url( $src );
// Unsupported scheme.
if ( isset( $url_parts['scheme'] ) && 'http' !== $url_parts['scheme'] && 'https' !== $url_parts['scheme'] ) {
return false;
}
if ( ! isset( $url_parts['scheme'] ) && 0 === strpos( $src, '//' ) ) {
$src = is_ssl() ? 'https:' : 'http:' . $src;
}
// This is a relative path, try to get the URL.
if ( ! isset( $url_parts['host'] ) && ! isset( $url_parts['scheme'] ) ) {
$src = site_url( $src );
}
$mapped_domain = $this->check_mapped_domain();
if ( false === strpos( $src, content_url() ) || ( is_multisite() && $mapped_domain && false === strpos( $src, $mapped_domain ) ) ) {
return false;
}
// Allow only these extensions in CDN.
$ext = strtolower( pathinfo( $src, PATHINFO_EXTENSION ) );
if ( ! in_array( $ext, array( 'gif', 'jpg', 'jpeg', 'png' ), true ) ) {
return false;
}
return $src;
}
/**
* Support for domain mapping plugin.
*
* @since 3.1.1
*/
private function check_mapped_domain() {
if ( ! is_multisite() ) {
return false;
}
if ( ! defined( 'DOMAINMAP_BASEFILE' ) ) {
return false;
}
$domain = wp_cache_get( 'smush_mapped_site_domain', 'smush' );
if ( ! $domain ) {
global $wpdb;
$domain = $wpdb->get_var(
$wpdb->prepare(
"SELECT domain FROM {$wpdb->base_prefix}domain_mapping WHERE blog_id = %d ORDER BY id ASC LIMIT 1",
get_current_blog_id()
)
); // Db call ok.
if ( null !== $domain ) {
wp_cache_add( 'smush_mapped_site_domain', $domain, 'smush' );
}
}
return $domain;
}
}
$run = new WPMUDEV_SMPro_Fix_Conflict_With_Brizy_Builder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment