Last active
March 17, 2020 16:13
-
-
Save wpmudev-sls/2c84342aefbf131ce8747e8e8cc43a0e to your computer and use it in GitHub Desktop.
[Smush Pro] - Fix Images not render from CDN on Ajax, for theme Newspaper and plugin Revoslider
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 Pro] - Fix Images not render from CDN | |
* Description: [Smush Pro] - Fix Images not render from CDN on Ajax, for theme Newspaper and plugin Revoslider - 1134740322826710/1138709545061167/1143766010143430 | |
* Version: 1.2 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; } | |
add_action( 'after_setup_theme', 'wpmudev_smpro_fix_image_not_render_from_cdn_func', 100 ); | |
function wpmudev_smpro_fix_image_not_render_from_cdn_func() { | |
if( defined('WP_SMUSH_VERSION') && ( class_exists( 'Smush\WP_Smush' ) || class_exists( 'WP_Smush' ) ) ){ | |
// return if disabled cdn | |
if( ! Smush\Core\Settings::get_instance()->get('cdn') ) return; | |
if( ! function_exists('wpmudev_check_front_end_ajax') ){ | |
function wpmudev_check_front_end_ajax(){ | |
if( ! ( is_admin() && wp_doing_ajax() ) ) return false; | |
$script_filename = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : ''; | |
//From wp-includes/functions.php, wp_get_referer() function. | |
//Required to fix: https://core.trac.wordpress.org/ticket/25294 | |
$ref = ''; | |
if ( ! empty( $_REQUEST['_wp_http_referer'] ) ){ | |
$ref = wp_unslash( $_REQUEST['_wp_http_referer'] ); | |
}elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ){ | |
$ref = wp_unslash( $_SERVER['HTTP_REFERER'] ); | |
} | |
//If referer does not contain admin URL and we are using the admin-ajax.php endpoint, this is likely a frontend AJAX request | |
if( strpos($ref, admin_url()) === false && basename($script_filename) === 'admin-ajax.php' ){ | |
return true; | |
} | |
return false; | |
} | |
} | |
class WPMUDEV_SMPro_Image_Not_Render_From_CDN{ | |
private $cdn_core; | |
private $filtered; | |
private static $_instance; | |
public function __construct(){ | |
/** | |
* for module template Newspaper/includes/modules | |
* Theme: Newspaper | |
*/ | |
if( defined('TD_THEME_NAME') && class_exists('td_config') ){ | |
add_filter( 'td_wp_booster_module_constructor', array( $this, 'add_hook_fix_image_not_render_from_cdn_on_tp_module_template' ) ); | |
// enabled render cdn on ajax | |
if( wpmudev_check_front_end_ajax() ){ | |
$cdn = $this->cdn_core(); | |
if( ! has_filter( 'wp_calculate_image_srcset', array( $cdn, 'update_image_srcset' ) ) ){ | |
// Update responsive image srcset and sizes if required. | |
add_filter( 'wp_calculate_image_srcset', array( $cdn, 'update_image_srcset' ), 99, 5 ); | |
add_filter( 'wp_calculate_image_sizes', array( $cdn, 'update_image_sizes' ), 10, 5 ); | |
// Add resizing arguments to image src. | |
add_filter( 'smush_image_cdn_args', array( $cdn, 'update_cdn_image_src_args' ), 99, 3 ); | |
} | |
} | |
} | |
/** | |
* nuclear_ape_gutenberg theme | |
*/ | |
if( function_exists('nuclear_ape_gutenberg_setup') ){ | |
add_action( 'woocommerce_before_single_product', array( $this, 'enable_render_image_from_cdn') ); | |
add_action( 'woocommerce_after_single_product', array( $this, 'disable_render_image_from_cdn') ); | |
} | |
/** | |
* Fix image from revoslider bg image type not render from cdn | |
* Plugin: Revoslider | |
*/ | |
if( class_exists('RevSliderFront') ){ | |
add_filter( 'revslider_slide_initByData', array( $this, 'revslider_slide_initByData_add_hook_render_image_from_cdn' ) ); | |
add_action( 'revslider_enable_static_layers', array( $this, 'disable_render_image_from_cdn') ); | |
} | |
} | |
public static function get_instance(){ | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
public function enable_render_image_from_cdn(){ | |
if( ! $this->filtered ){ | |
add_filter( 'wp_get_attachment_image_src', array( $this, 'render_image_from_cdn') ); | |
$this->filtered = true; | |
} | |
} | |
public function disable_render_image_from_cdn(){ | |
if( $this->filtered ){ | |
remove_filter( 'wp_get_attachment_image_src', array( $this, 'render_image_from_cdn') ); | |
$this->filtered = null; | |
} | |
} | |
public function add_hook_fix_image_not_render_from_cdn_on_tp_module_template($obj){ | |
add_filter( 'wp_get_attachment_image_src', array( $this, 'render_image_from_cdn') ); | |
return $obj; | |
} | |
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 render_image_from_cdn($image){ | |
if( ! empty( $image[0] ) ){ | |
$image[0] = $this->generate_cdn_url( $image[0] ); | |
} | |
if( ! $this->filtered ){ | |
// remove the hook if is tp_module_template | |
remove_filter( 'wp_get_attachment_image_src', array( $this, 'render_image_from_cdn') ); | |
} | |
return $image; | |
} | |
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; | |
} | |
// revoslider | |
public function revslider_slide_initByData_add_hook_render_image_from_cdn( $record ){ | |
if( empty( $record['params']['background_type'] ) || 'image' === $record['params']['background_type'] ){ | |
add_filter( 'wp_get_attachment_image_src', array( $this, 'render_image_from_cdn') ); | |
$this->filtered = true; | |
} | |
return $record; | |
} | |
// public function revslider_remove_hook_render_cdn_image_for_bg_type(){ | |
// if( $this->filtered ){ | |
// remove_filter( 'wp_get_attachment_image_src', array( $this, 'render_image_from_cdn') ); | |
// $this->filtered = null; | |
// } | |
// } | |
/** | |
* 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 = WPMUDEV_SMPro_Image_Not_Render_From_CDN::get_instance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment