Last active
February 13, 2020 17:43
-
-
Save wpmudev-sls/fda66b3344e8d6347f5b37cec186e740 to your computer and use it in GitHub Desktop.
Fix Images not render from CDN while change upload path on MU site
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 older "blogs.dir" multisite | |
* Description: [Smush Pro] - Support CDN for older "blogs.dir" multisite - required Smush >= 3.5 | |
* Task: 0/11289012348292/1161674459914571 - 1151490191545756 | |
* 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_older_blogs_mu_site_func', 100 ); | |
function wpmudev_smpro_support_cdn_for_older_blogs_mu_site_func() { | |
if( defined('WP_SMUSH_VERSION') && ( class_exists( 'Smush\WP_Smush' ) || class_exists( 'WP_Smush' ) ) ){ | |
if( ! Smush\Core\Settings::get_instance()->get('cdn') || ! is_multisite() ) return; | |
class WPMUDEV_Smush_Fix_Blog_Dir{ | |
private static $_original_domains; | |
public function __construct(){ | |
add_filter( 'smush_filter_generate_cdn_url', array( $this, 'add_site_path_to_image_source') ); | |
} | |
public function add_site_path_to_image_source( $src ){ | |
return str_replace(network_site_url(), '', str_replace(get_site_url(), rtrim($this->get_original_siteurl(),'/'), $src) ); | |
} | |
/** | |
* Get original domain from siteurl value. | |
* @return bool|null|string | |
*/ | |
public function get_original_siteurl( $blog_id = false ) { | |
// If no blog id is passed, then take current one | |
if ( ! $blog_id ) { | |
$blog_id = get_current_blog_id(); | |
} | |
// Check if we have already found original domain for the blog. | |
if ( isset( self::$_original_domains[ $blog_id ] ) ) { | |
return self::$_original_domains[ $blog_id ]; | |
} else { | |
global $wpdb; | |
// Get the site url. | |
$site_url = $wpdb->get_var( sprintf( | |
"SELECT option_value FROM %s WHERE option_name = 'siteurl'", | |
$wpdb->options | |
) ); | |
// If a value found, store the value and return. | |
if ( ! empty( $site_url ) ) { | |
// Store to cache. | |
self::$_original_domains[ $blog_id ] = $site_url; | |
return $site_url; | |
} | |
} | |
return false; | |
} | |
} | |
$run = new WPMUDEV_Smush_Fix_Blog_Dir(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment