Skip to content

Commit

Permalink
fix: compatibility with Metaslider, adds full support for all slider …
Browse files Browse the repository at this point in the history
…types available
  • Loading branch information
selul committed Mar 22, 2019
1 parent f407a10 commit a09d1fd
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
2 changes: 1 addition & 1 deletion inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function inline_bootstrap_script() {
-moz-transition: .2s filter linear, .2s opacity linear, .2s border-radius linear;
-o-transition: .2s filter linear, .2s opacity linear, .2s border-radius linear;
}
img[data-opt-src].optml_lazyload_img {
img[data-opt-src]:not([data-opt-lazy-loaded]) {
opacity: .75;
filter: blur(5px);
}
Expand Down
85 changes: 85 additions & 0 deletions inc/compatibilities/metaslider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* Class Optml_metaslider.
*
* @reason Metaslider behaves strange when the noscript tag is present near the image tag.
*/
class Optml_metaslider 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( 'ml-slider/ml-slider.php' ) || is_plugin_active( 'ml-slider-pro/ml-slider-pro.php' );
}

/**
* Register integration details.
*/
public function register() {
add_filter( 'optml_ignore_noscript_on', [ $this, 'add_noscript_flags' ], PHP_INT_MAX, 1 );
add_filter( 'optml_possible_lazyload_flags', [ $this, 'add_ignore_lazyload' ], PHP_INT_MAX, 1 );
add_filter( 'optml_watcher_lz_classes', [ $this, 'add_watcher_class' ], 10, 1 );
add_filter( 'metaslider_coin_slider_image_attributes', [ $this, 'setup_listner' ], PHP_INT_MAX, 1 );
}

/**
* Disable lazyload on coinslider type.
*
* @param array $attributes Old attributes.
*
* @return mixed Slider attributes.
*/
public function setup_listner( $attributes ) {
$attributes['class'] .= 'no-optml-lazyload';

return $attributes;
}

/**
* Add ignore lazyload flag.
*
* @param array $flags Old flags.
*
* @return array New flags.
*/
public function add_ignore_lazyload( $flags = array() ) {
$flags[] = 'no-optml-lazyload';

return $flags;
}

/**
* Add nive slider watcher class.
*
* @param array $classes Old watcher.
*
* @return array New watchers.
*/
public function add_watcher_class( $classes ) {
$classes[] = 'nivo-main-image';

return $classes;
}


/**
* Return metaslider flags.
*
* @param array $flags Old flags.
*
* @return array New flags.
*/
public function add_noscript_flags( $flags = array() ) {
$flags[] = 'slide-';

return $flags;
}

}
44 changes: 43 additions & 1 deletion inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ final class Optml_Lazyload_Replacer extends Optml_App_Replacer {
* @var array Lazyload background classes.
*/
private static $lazyload_background_classes = null;
/**
* Holds flags which remove noscript tag bundle causing issues on render, i.e slider plugins.
*
* @var array Noscript flags.
*/
private static $ignore_no_script_flags = null;
/**
* Holds classes responsabile for watching lazyload behaviour.
*
Expand Down Expand Up @@ -148,8 +154,11 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
1
);

$new_tag = str_replace( 'srcset=', 'old-srcset=', $new_tag );
$new_tag = str_replace( 'srcset=', 'old-srcset=', $new_tag );

if ( ! $this->should_add_noscript( $new_tag ) ) {
return $new_tag;
}
return $new_tag . '<noscript>' . $no_script_tag . '</noscript>';
}

Expand Down Expand Up @@ -204,6 +213,39 @@ public function should_add_data_tag( $tag ) {
return true;
}

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

return true;
}

/**
* Returns flags for ignoring noscript tag additional watch.
*
* @return array
*/
public static function get_ignore_noscript_flags() {

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

self::$ignore_no_script_flags = apply_filters( 'optml_ignore_noscript_on', [] );

return self::$ignore_no_script_flags;
}

/**
* Throw error on object clone
*
Expand Down
1 change: 1 addition & 0 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class Optml_Manager {
'envira',
'beaver_builder',
'revslider',
'metaslider',
'woocommerce',
);

Expand Down

0 comments on commit a09d1fd

Please sign in to comment.