Last active
June 15, 2020 09:28
-
-
Save wpmudev-sls/c808372a66a006b61db64036fda5473d to your computer and use it in GitHub Desktop.
[Smush Pro] - Support CDN For Images Relative Path
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] - Support CDN For Images Relative Path | |
* Description: [Smush Pro] - Support CDN For Images Relative Path: /wp-content/uploads/...|/wp-content/themes/... | |
* Task: 0/11289012348292/1168821112494197 | |
* 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_support_cdn_for_real_path_func', 100 ); | |
function wpmudev_smpro_support_cdn_for_real_path_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; | |
class WPMUDEV_SMPro_Support_CDN_For_Image_Real_Path{ | |
private $current_src; | |
private $current_cdn_url; | |
public function __construct(){ | |
add_filter( 'smush_skip_image_from_cdn', array( $this, 'maybe_custom_content_url' ), 99999, 2 ); | |
add_filter( 'smush_image_src_after_cdn', array( $this, 'save_cdn_url' ) ); | |
add_filter( 'smush_cdn_image_tag', array( $this, 'maybe_restore_content_url') ); | |
} | |
public function maybe_custom_content_url( $skip, $src ){ | |
if( ! $skip && 0 === strpos( $src, '/wp-content/' ) ){ | |
$this->current_src = $src; | |
} | |
return $skip; | |
} | |
public function save_cdn_url( $src ){ | |
$this->current_cdn_url = $src; | |
return $src; | |
} | |
public function maybe_restore_content_url( $image ){ | |
if( $this->current_src && $this->current_cdn_url && false === strpos( $image, $this->current_cdn_url ) ){ | |
$image = preg_replace( '#(src=["|\'])' . $this->current_src . '(["|\'])#i', '\1' . $this->current_cdn_url . '\2', $image, 1 ); | |
} | |
return $image; | |
} | |
} | |
$run = new WPMUDEV_SMPro_Support_CDN_For_Image_Real_Path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment