Created
August 14, 2019 18:18
-
-
Save wpmudev-sls/1fe5212104f97ce7bb33c68826c380af to your computer and use it in GitHub Desktop.
[Smush] - Bakery Carousel Excludes. Alter Smush's buffer for pages with Bakery Carousel
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] - Bakery Carousel Excludes | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Alter Smush's buffer for pages with Bakery Carousel | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Smush_Bakery_Carousel_Excludes' ) ) { | |
class WPMUDEV_Smush_Bakery_Carousel_Excludes { | |
private static $_instance = null; | |
private $lazy_load = false; | |
private $cdn = false; | |
private $background_images = false; | |
protected $settings = null; | |
protected $options = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Smush_Bakery_Carousel_Excludes(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
$this->init(); | |
add_action( 'template_redirect', array( $this, 'attempt_alter_buffer' ) ); | |
} | |
private function init() { | |
$this->settings = WP_Smush_Settings::get_instance(); | |
$background_images = $this->settings->get( 'background_images' ); | |
if ( $background_images ) { | |
$this->background_images = true; | |
} | |
//$this->parser->enable( 'cdn' ); | |
//$this->cdn = true; | |
$this->lazy_load = true; | |
} | |
public function attempt_alter_buffer() { | |
ob_start( array( $this, 'maybe_alter_buffer' ) ); | |
} | |
public function maybe_alter_buffer( $content, $flags ) { | |
$content = ob_get_contents(); | |
if ( strpos( $content, 'wpb_images_carousel' ) === false ) { | |
return $content; | |
} | |
$content = $this->process_images( $content ); | |
if ( $this->background_images ) { | |
$content = $this->process_background_images( $content ); | |
} | |
return $content; | |
} | |
protected function process_images( $content ) { | |
$images = WP_Smush_Page_Parser::get_images_from_content( $content ); | |
if ( empty( $images ) ) { | |
return $content; | |
} | |
foreach ( $images[0] as $key => $image ) { | |
$img_src = $images['img_url'][ $key ]; | |
$new_image = $image; | |
// Then update the image with correct CDN links. | |
if ( $this->cdn ) { | |
$new_image = WP_Smush::get_instance()->core()->mod->cdn->parse_image( $img_src, $new_image ); | |
} | |
// First prepare for lazy-loading, as that does not require any URL rewrites. | |
if ( $this->lazy_load ) { | |
$new_image = $this->parse_image( $img_src, $new_image ); | |
} | |
$content = str_replace( $image, $new_image, $content ); | |
} | |
return $content; | |
} | |
public function parse_image( $src, $image ) { | |
/** | |
* Filter to skip a single image from lazy load. | |
* | |
* @param bool $skip Should skip? Default: false. | |
* @param string $src Image url. | |
*/ | |
if ( apply_filters( 'smush_skip_image_from_lazy_load', false, $src ) ) { | |
return $image; | |
} | |
$this->options = $this->settings->get_setting( WP_SMUSH_PREFIX . 'lazy_load' ); | |
// Avoid conflicts if attributes are set (another plugin, for example). | |
if ( false !== strpos( $image, 'data-src' ) ) { | |
return $image; | |
} | |
/** | |
* Check if some image formats are excluded. | |
*/ | |
if ( in_array( false, $this->options['format'], true ) ) { | |
$ext = strtolower( pathinfo( $src, PATHINFO_EXTENSION ) ); | |
$ext = 'jpg' === $ext ? 'jpeg' : $ext; | |
if ( isset( $this->options['format'][ $ext ] ) && ! $this->options['format'][ $ext ] ) { | |
return $image; | |
} | |
} | |
if ( $this->has_excluded_class_or_id( $image ) ) { | |
return $image; | |
} | |
$new_image = $image; | |
$src = WP_Smush_Page_Parser::get_attribute( $new_image, 'src' ); | |
WP_Smush_Page_Parser::remove_attribute( $new_image, 'src' ); | |
WP_Smush_Page_Parser::add_attribute( $new_image, 'data-src', $src ); | |
// Change srcset to data-srcset attribute. | |
//$new_image = preg_replace( '/<img(.*?)(srcset=)(.*?)>/i', '<img$1data-$2$3>', $new_image ); | |
// Add .lazyload class. | |
$class = WP_Smush_Page_Parser::get_attribute( $new_image, 'class' ); | |
if ( $class ) { | |
$class .= ' lazyload'; | |
} else { | |
$class = 'lazyload'; | |
} | |
WP_Smush_Page_Parser::remove_attribute( $new_image, 'class' ); | |
WP_Smush_Page_Parser::add_attribute( $new_image, 'class', apply_filters( 'wp_smush_lazy_load_classes', $class ) ); | |
WP_Smush_Page_Parser::add_attribute( $new_image, 'src', 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' ); | |
// Use noscript element in HTML to load elements normally when JavaScript is disabled in browser. | |
$new_image .= '<noscript>' . $image . '</noscript>'; | |
return $new_image; | |
} | |
private function has_excluded_class_or_id( $image ) { | |
$image_classes = WP_Smush_Page_Parser::get_attribute( $image, 'class' ); | |
$image_classes = explode( ' ', $image_classes ); | |
$image_id = '#' . WP_Smush_Page_Parser::get_attribute( $image, 'id' ); | |
if ( in_array( $image_id, $this->options['exclude-classes'], true ) ) { | |
return true; | |
} | |
foreach ( $image_classes as $class ) { | |
// Skip Revolution Slider images. | |
if ( 'rev-slidebg' === $class ) { | |
return true; | |
} | |
// Internal class to skip images. | |
if ( 'no-lazyload' === $class ) { | |
return true; | |
} | |
if ( in_array( ".{$class}", $this->options['exclude-classes'], true ) ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
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 ]; | |
$new_image = $image; | |
// Update the image with correct CDN links. | |
$new_image = WP_Smush::get_instance()->core()->mod->cdn->parse_background_image( $img_src, $new_image ); | |
$content = str_replace( $image, $new_image, $content ); | |
} | |
return $content; | |
} | |
private static function get_background_images( $content ) { | |
$images = array(); | |
if ( preg_match_all( '/(?:background-image:?\surl\([\'|"](?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; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_smush_bakery_carousel_excludes' ) ) { | |
function wpmudev_smush_bakery_carousel_excludes() { | |
return WPMUDEV_Smush_Bakery_Carousel_Excludes::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_smush_bakery_carousel_excludes', 99 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment