Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- use Smush\WP_Smush;
- use Smush\Core\Settings;
- use Smush\Core\Modules\Helpers\Parser;
- 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() {
- $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|ae-bg|desktop-retina|tablet|tablet-retina|mobile|mobile-retina)=[\'|"](?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 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement