Skip to content

Commit

Permalink
fix: compatibility with Revolution slider, adds support for backgroun…
Browse files Browse the repository at this point in the history
…d images lazyload and exact match
  • Loading branch information
selul committed Mar 8, 2019
1 parent 0d09efc commit 0bbd254
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 195 deletions.
22 changes: 18 additions & 4 deletions inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public function __construct() {
public function inline_bootstrap_script() {
$domain = 'https://' . OPTML_JS_CDN;

$min = ! OPTML_DEBUG ? '.min' : '';
$min = ! OPTML_DEBUG ? '.min' : '';
$bgclasses = Optml_Lazyload_Replacer::get_lazyload_bg_classes();
$watcher_classes = Optml_Lazyload_Replacer::get_watcher_lz_classes();

$bgclasses = empty( $bgclasses ) ? '' : sprintf( '"%s"', implode( '","', (array) $bgclasses ) );
$watcher_classes = empty( $watcher_classes ) ? '' : sprintf( '"%s"', implode( '","', (array) $watcher_classes ) );

$output = sprintf(
'
Expand All @@ -72,22 +77,31 @@ public function inline_bootstrap_script() {
opacity: .75;
filter: blur(5px);
}
</style>
<script type="application/javascript">
(function(w, d){
var b = d.getElementsByTagName("head")[0];
var s = d.createElement("script");
var v = ("IntersectionObserver" in w) ? "_no_poly" : "";
s.async = true;
s.src = "%s/v2/latest/optimole_lib" + v + "%s.js";
s.src = "%s" + v + "%s.js?sdaassss=abc";
b.appendChild(s);
w.optimoleData = {
backgroundReplaceClasses: [%s],
watchClasses: [%s],
quality: %d
}
}(window, document));
document.addEventListener( "DOMContentLoaded", function() { document.body.className = document.body.className.replace("optimole-no-script",""); } );
</script>',
esc_url( $domain ),
$min
esc_url( 'https://d5jmkjjpb7yfg.cloudfront.net/tags/fix_36/optimole_lib' ),
$min,
$bgclasses,
$watcher_classes,
$this->settings->get_numeric_quality()
);
echo $output;
}
Expand Down
74 changes: 24 additions & 50 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ abstract class Optml_App_Replacer {
* @var array
*/
protected static $ignore_lazyload_strings = null;
/**
* Holds flags that should ignore the data-opt-tag format.
*
* @var array
*/
protected static $ignore_data_opt_attribute = null;
/**
* Settings handler.
*
Expand Down Expand Up @@ -85,12 +91,6 @@ abstract class Optml_App_Replacer {
* @var bool Domains.
*/
protected $is_allowed_site = array();
/**
* Possible integrations with different plugins.
*
* @var array Integrations classes.
*/
private $compatibilities = array( 'shortcode_ultimate', 'foogallery', 'envira' );

/**
* Returns possible src attributes.
Expand Down Expand Up @@ -124,6 +124,22 @@ public static function possible_lazyload_flags() {
return array_merge( self::$possible_src_attributes, [ '<noscript' ] );
}

/**
* Returns possible data-opt-src ignore flags attributes.
*
* @return array
*/
public static function possible_data_ignore_flags() {

if ( null != self::$ignore_data_opt_attribute && is_array( self::$ignore_data_opt_attribute ) ) {
return self::$ignore_data_opt_attribute;
}

self::$ignore_data_opt_attribute = apply_filters( 'optml_ignore_data_opt_flag', [] );

return self::$ignore_data_opt_attribute;
}

/**
* Size to crop maping.
*
Expand Down Expand Up @@ -199,46 +215,16 @@ protected static function image_sizes() {
*/
public function init() {
$this->settings = new Optml_Settings();

if ( ! $this->should_replace() ) {
return false; // @codeCoverageIgnore
}
$this->set_properties();

return true;
}

/**
* Check if we should rewrite the urls.
*
* @return bool If we can replace the image.
*/
public function should_replace() {
if ( Optml_Manager::is_ajax_request() ) {
return true;
}
if ( is_admin() || ! $this->settings->is_connected() || ! $this->settings->is_enabled() || is_customize_preview() ) {
return false; // @codeCoverageIgnore
}

if ( array_key_exists( 'preview', $_GET ) && 'true' == $_GET['preview'] ) {
return false; // @codeCoverageIgnore
}

if ( array_key_exists( 'optml_off', $_GET ) && 'true' == $_GET['optml_off'] ) {
return false; // @codeCoverageIgnore
}
if ( array_key_exists( 'elementor-preview', $_GET ) && ! empty( $_GET['elementor-preview'] ) ) {
return false; // @codeCoverageIgnore
}

return true;
}

/**
* Set the cdn url based on the current connected user.
*/
public function set_properties() {

$upload_data = wp_upload_dir();
$this->upload_resource = array(
'url' => str_replace( array( 'https://', 'http://' ), '', $upload_data['baseurl'] ),
Expand Down Expand Up @@ -278,20 +264,8 @@ public function set_properties() {
$this->max_height = $this->settings->get( 'max_height' );
$this->max_width = $this->settings->get( 'max_width' );

foreach ( $this->compatibilities as $compatibility_class ) {
$compatibility_class = 'Optml_' . $compatibility_class;
$compatibility = new $compatibility_class;

/**
* Check if we should load compatibility.
*
* @var Optml_compatibility $compatibility Class to register.
*/
if ( $compatibility->should_load() ) {
$compatibility->register();
}
}
add_filter( 'optml_strip_image_size_from_url', [ $this, 'strip_image_size_from_url' ], 10, 1 );

}

/**
Expand Down
55 changes: 55 additions & 0 deletions inc/compatibilities/revslider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Class Optml_revslider.
*
* @reason The slider output dont needs the data-opt-src and uses a background lazyload approach.
*/
class Optml_revslider extends Optml_compatibility {

/**
* Should we load the integration logic.
*
* @return bool Should we load.
*/
function should_load() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

return is_plugin_active( 'revslider/revslider.php' );
}

/**
* Register integration details.
*/
public function register() {
add_filter( 'optml_ignore_data_opt_flag', [ $this, 'add_data_ignore' ], 10, 3 );
add_filter( 'optml_lazyload_bg_classes', [ $this, 'add_bg_class' ], 10, 1 );
}

/**
* Add classes for lazyload on background.
*
* @param string $classes Old classes.
*
* @return array New classes.
*/
public function add_bg_class( $classes = array() ) {
$classes[] = 'tp-bgimg';

return $classes;
}

/**
* Adds flag that should ignore applying the data-opt-src
*
* @param string $flags Flag that should ignore.
*
* @return array New flags.
*/
public function add_data_ignore( $flags = array() ) {
$flags[] = 'rev-slidebg';

return $flags;
}

}
100 changes: 82 additions & 18 deletions inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ final class Optml_Lazyload_Replacer extends Optml_App_Replacer {
* @var Optml_Tag_Replacer
*/
protected static $instance = null;
/**
* Holds classes for listening to lazyload on background.
*
* @var array Lazyload background classes.
*/
private static $lazyload_background_classes = null;
/**
* Holds classes responsabile for watching lazyload behaviour.
*
* @var array Lazyload classes.
*/
private static $lazyload_watcher_classes = null;

/**
* Class instance method.
Expand All @@ -27,32 +39,63 @@ final class Optml_Lazyload_Replacer extends Optml_App_Replacer {
* @return Optml_Tag_Replacer
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
if ( null === self::$instance ) {
self::$instance = new self();
add_action( 'after_setup_theme', array( self::$instance, 'init' ) );
add_action( 'optml_replacer_setup', array( self::$instance, 'init' ) );
}

return self::$instance;
}

/**
* Returns background classes for lazyload.
*
* @return array
*/
public static function get_lazyload_bg_classes() {

if ( null != self::$lazyload_background_classes && is_array( self::$lazyload_background_classes ) ) {
return self::$lazyload_background_classes;
}

self::$lazyload_background_classes = apply_filters( 'optml_lazyload_bg_classes', [] );

return self::$lazyload_background_classes;
}

/**
* Returns classes for lazyload additional watch.
*
* @return array
*/
public static function get_watcher_lz_classes() {

if ( null != self::$lazyload_watcher_classes && is_array( self::$lazyload_watcher_classes ) ) {
return self::$lazyload_watcher_classes;
}

self::$lazyload_watcher_classes = apply_filters( 'optml_watcher_lz_classes', [] );

return self::$lazyload_watcher_classes;
}

/**
* The initialize method.
*/
public function init() {
parent::init();

if ( ! parent::init() ) {
return; // @codeCoverageIgnore
if ( ! $this->settings->use_lazyload() ) {
return;
}
add_filter(
'max_srcset_image_width',
function () {
return 1;
}
);
add_filter( 'optml_tag_replace', array( $this, 'lazyload_tag_replace' ), 2, 5 );

if ( $this->settings->use_lazyload() ) {
add_filter(
'max_srcset_image_width',
function () {
return 1;
}
);
add_filter( 'optml_tag_replace', array( $this, 'lazyload_tag_replace' ), 2, 5 );
}
}

/**
Expand All @@ -72,13 +115,17 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
return Optml_Tag_Replacer::instance()->regular_tag_replace( $new_tag, $original_url, $new_url, $optml_args, $is_slashed );
}
$optml_args['quality'] = 'eco';
$low_url = apply_filters( 'optml_content_url', $is_slashed ? stripslashes( $original_url ) : $original_url, $optml_args );
$low_url = $is_slashed ? addcslashes( $low_url, '/' ) : $low_url;

$opt_format = ' data-opt-src="%s" ';
$low_url = apply_filters( 'optml_content_url', $is_slashed ? stripslashes( $original_url ) : $original_url, $optml_args );
$low_url = $is_slashed ? addcslashes( $low_url, '/' ) : $low_url;
$opt_format = '';

if ( $this->should_add_data_tag( $new_tag ) ) {
$opt_format = ' data-opt-src="%s" ';
$opt_format = $is_slashed ? addslashes( $opt_format ) : $opt_format;
}

$opt_format = $is_slashed ? addslashes( $opt_format ) : $opt_format;
$new_url = $is_slashed ? addcslashes( $new_url, '/' ) : $new_url;
$new_url = $is_slashed ? addcslashes( $new_url, '/' ) : $new_url;

$opt_src = sprintf( $opt_format, $new_url );

Expand Down Expand Up @@ -138,6 +185,23 @@ public function can_lazyload_for( $url, $tag = '' ) {
return false;
}

/**
* Check if we should add the data-opt-tag.
*
* @param string $tag Html tag.
*
* @return bool Should add?
*/
public function should_add_data_tag( $tag ) {
foreach ( self::possible_data_ignore_flags() as $banned_string ) {
if ( strpos( $tag, $banned_string ) !== false ) {
return false;
}
}

return true;
}

/**
* Throw error on object clone
*
Expand Down
Loading

0 comments on commit 0bbd254

Please sign in to comment.