Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Smush Pro] - Smart Slider 3 Support
<?php
/**
* Plugin Name: [Smush Pro] - Smart Slider 3 Support
* Plugin URI: https://premium.wpmudev.org/
* Description: Alter Smush's buffer for pages with Smart Slider 3 (as of 3.6.0)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/11289012348292/1163697725989640
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// No need to do anything if the request is via WP-CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Smush_Smart_Slider' ) ) {
class WPMUDEV_Smush_Smart_Slider {
private $cdn;
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Smush_Smart_Slider();
}
return self::$_instance;
}
private function __construct() {
if ( ! class_exists( 'WP_Smush' ) ) {
return;
}
$this->init();
}
private function init() {
$this->cdn = WP_Smush::get_instance()->core()->mod->cdn;
// Do not parse page if CDN is disabled.
if ( $this->cdn->status() !== 'enabled' ) {
return;
}
add_action(
'template_redirect',
function () {
ob_start( array( $this, 'parse_page' ) );
},
1
);
}
public function parse_page( $content ) {
if ( empty( $content ) || apply_filters( 'wp_smush_should_skip_parse', false ) ) {
return $content;
}
$content = $this->process_custom_images( $content );
return $content;
}
private function process_custom_images( $content ) {
// Add support for unquoted images
if ( preg_match_all( '/(?:data-(?:desktop|desktop-retina|tablet|tablet-retina|mobile|mobile-retina|thumbnail)=[\'|"](?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 ] );
}
}
}
if ( empty( $images ) ) {
return $content;
}
foreach ( $images['img_url'] as $key => $image ) {
if( ! $this->cdn->is_supported_path( $image ) ){
continue;
}
$content = str_replace( $image, $this->cdn->generate_cdn_url( $image ), $content );
}
return $content;
}
}
if ( ! function_exists( 'wpmudev_smush_smart_slider' ) ) {
function wpmudev_smush_smart_slider() {
return WPMUDEV_Smush_Smart_Slider::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_smush_smart_slider', 99 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.