Advertisement
kasiaswiderska

wpmudev_smushcdn_bg_fix

Nov 21st, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: [Smush Pro] - data background support
  4. * Plugin URI: https://premium.wpmudev.org/
  5. * Description:
  6. * Author: Alessandro Kaounas @ WPMUDEV
  7. * Author URI: https://premium.wpmudev.org/
  8. * License: GPLv2 or later
  9. */
  10.  
  11. if ( ! defined( 'ABSPATH' ) ) {
  12.     exit;
  13. }
  14.  
  15. use Smush\WP_Smush;
  16. use Smush\Core\Settings;
  17. use Smush\Core\Modules\Helpers\Parser;
  18.  
  19. if ( ! class_exists( 'WPMUDEV_Smush_Smart_Slider' ) ) {
  20.    
  21.     class WPMUDEV_Smush_Smart_Slider {
  22.  
  23.         private static $_instance       = null;
  24.        
  25.         private $cdn                    = false;
  26.         private $lazy_load              = false;
  27.  
  28.         public static function get_instance() {
  29.  
  30.             if( is_null( self::$_instance ) ){
  31.                 self::$_instance = new WPMUDEV_Smush_Smart_Slider();
  32.             }
  33.             return self::$_instance;
  34.            
  35.         }
  36.  
  37.         private function __construct() {
  38.  
  39.             $this->init();
  40.            
  41.         }
  42.  
  43.         private function init() {
  44.  
  45.             $settings = Settings::get_instance();
  46.  
  47.             if ( $settings->get( 'lazy_load' ) ) {
  48.                 $this->lazy_load = $settings->get( 'lazy_load' );
  49.             }
  50.    
  51.             if ( $settings->get( 'cdn' ) ) {
  52.                 $this->cdn = $settings->get( 'cdn' );
  53.             }
  54.  
  55.             // Do not parse page if CDN and Lazy load modules are disabled.
  56.             if ( ! $this->cdn && ! $this->lazy_load ) {
  57.                 return $content;
  58.             }
  59.  
  60.             add_action( 'template_redirect', function () { ob_start( array( $this, 'parse_page' ) ); }, 1 );
  61.            
  62.         }
  63.  
  64.         public function parse_page( $content ) {
  65.  
  66.             if ( empty( $content ) || apply_filters( 'wp_smush_should_skip_parse', false ) ) {
  67.                 return $content;
  68.             }
  69.    
  70.             $content = $this->process_custom_images( $content );
  71.  
  72.             return $content;
  73.  
  74.         }
  75.  
  76.         private function process_custom_images( $content ) {
  77.  
  78.             // Match elements with $this->data_attr attribute
  79.             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 ) ) {
  80.                 foreach ( $images as $key => $unused ) {
  81.                     // Simplify the output as much as possible, mostly for confirming test results.
  82.                     if ( is_numeric( $key ) && $key > 0 ) {
  83.                         unset( $images[ $key ] );
  84.                     }
  85.                 }
  86.             }
  87.    
  88.             if ( empty( $images ) ) {
  89.                 return $content;
  90.             }
  91.    
  92.             foreach ( $images['img_url'] as $key => $image ) {
  93.    
  94.                 $content = str_replace( $image, WP_Smush::get_instance()->core()->mod->cdn->generate_cdn_url( $image ), $content );
  95.  
  96.             }
  97.    
  98.             return $content;
  99.  
  100.         }
  101.  
  102.     }
  103.  
  104.     if ( ! function_exists( 'wpmudev_smush_smart_slider' ) ) {
  105.  
  106.         function wpmudev_smush_smart_slider() {
  107.             return WPMUDEV_Smush_Smart_Slider::get_instance();
  108.         };
  109.  
  110.         add_action( 'plugins_loaded', 'wpmudev_smush_smart_slider', 99 );
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement