Last active
May 12, 2021 08:06
-
-
Save wpmudev-sls/f417a3fa8a1df641f5df6a92a60e19ae to your computer and use it in GitHub Desktop.
[Smush] - Background Images Fix. Add unquoted background image 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 | |
/** | |
* Plugin Name: [Smush] - Background Images Fix | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Add unquoted background image support | |
* Author: Alessandro Kaounas & Tho Bui @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if (! defined('ABSPATH')) { | |
exit; | |
} | |
add_action('after_setup_theme', 'wpmudev_smpro_fix_background_issue_func', 100); | |
function wpmudev_smpro_fix_background_issue_func() | |
{ | |
if (defined('WP_SMUSH_VERSION') && class_exists('WP_Smush') && class_exists('Smush\Core\Settings')) { | |
if (! class_exists('WPMUDEV_Smush_Background_Images_Fix')) { | |
class WPMUDEV_Smush_Background_Images_Fix | |
{ | |
private static $_instance = null; | |
private $background_images = false; | |
protected $settings = null; | |
public static function get_instance() | |
{ | |
if (is_null(self::$_instance)) { | |
self::$_instance = new WPMUDEV_Smush_Background_Images_Fix(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() | |
{ | |
$this->init(); | |
} | |
private function init() | |
{ | |
if (! class_exists('WP_Smush')) { | |
return; | |
} | |
add_action('template_redirect', array( $this, 'attempt_alter_buffer' )); | |
$this->settings = Smush\Core\Settings::get_instance(); | |
$this->background_images = intval($this->settings->get('background_images')); | |
} | |
public function attempt_alter_buffer() | |
{ | |
ob_start(array( $this, 'maybe_alter_buffer' )); | |
} | |
public function maybe_alter_buffer($content, $flags) | |
{ | |
$content = ob_get_contents(); | |
if ($this->background_images) { | |
$content = $this->process_background_images($content); | |
} | |
return $content; | |
} | |
private function process_background_images($content) | |
{ | |
$images = self::get_background_images($content); | |
if (empty($images)) { | |
return $content; | |
} | |
foreach ($images[0] as $key => $image) { | |
$img_src = $images['img_url'][ $key ]; | |
// Update the image with correct CDN links. | |
$new_image = WP_Smush::get_instance()->core()->mod->cdn->generate_cdn_url($img_src); | |
$content = str_replace($img_src, $new_image, $content); | |
} | |
return $content; | |
} | |
private static function get_background_images($content) | |
{ | |
$images = array(); | |
if (preg_match_all('/(?:background(-image)?:[^:;]*?url\([\'|"]?(?P<img_url>http?s?:?\/\/[^"\']*\.(?:png|jpg|jpeg|gif))[\'|"]?\)?;){1}/is', $content, $images)) { | |
foreach ($images as $key => $unused) { | |
// Simplify the output as much as possible, mostly for confirming test results. | |
if (is_numeric($key) && $key > 0) { | |
unset($images[ $key ]); | |
} | |
} | |
} | |
return $images; | |
} | |
} | |
$run = WPMUDEV_Smush_Background_Images_Fix::get_instance(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment