Last active
December 2, 2019 16:54
-
-
Save wpmudev-sls/f40ade2cfc9b540a801ceebf5430709c to your computer and use it in GitHub Desktop.
[Smush Pro] - Smart Slider 3 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 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.3.2) | |
* Author: Alessandro Kaounas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
use Smush\WP_Smush; | |
use Smush\Core\Settings; | |
use Smush\Core\Modules\Helpers\Parser; | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Smush_Smart_Slider' ) ) { | |
class WPMUDEV_Smush_Smart_Slider { | |
private static $_instance = null; | |
private $cdn = false; | |
private $lazy_load = false; | |
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( '\\Smush\\WP_Smush' ) ){ | |
return; | |
} | |
$this->init(); | |
} | |
private function init() { | |
$settings = Settings::get_instance(); | |
if ( $settings->get( 'lazy_load' ) ) { | |
$this->lazy_load = $settings->get( 'lazy_load' ); | |
} | |
if ( $settings->get( 'cdn' ) ) { | |
$this->cdn = $settings->get( 'cdn' ); | |
} | |
// Do not parse page if CDN and Lazy load modules are disabled. | |
if ( ! $this->cdn && ! $this->lazy_load ) { | |
return $content; | |
} | |
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 ) { | |
// Match elements with $this->data_attr attribute | |
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 ) { | |
$content = str_replace( $image, WP_Smush::get_instance()->core()->mod->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