Skip to content

Commit

Permalink
fix: compatibility with Foogallery plugin when the gallery uses lazyl…
Browse files Browse the repository at this point in the history
…oad too
  • Loading branch information
selul committed Mar 7, 2019
1 parent 164ba35 commit 67991dc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
24 changes: 23 additions & 1 deletion inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ abstract class Optml_App_Replacer {
* @var array
*/
protected static $size_to_crop = array();
/**
* Holds possible src attributes.
*
* @var array
*/
protected static $possible_src_attributes = null;
/**
* Settings handler.
*
Expand Down Expand Up @@ -78,7 +84,23 @@ abstract class Optml_App_Replacer {
*
* @var array Integrations classes.
*/
private $compatibilities = array( 'shortcode_ultimate' );
private $compatibilities = array( 'shortcode_ultimate', 'foogallery' );

/**
* Returns possible src attributes.
*
* @return array
*/
public static function possible_src_attributes() {

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

self::$possible_src_attributes = apply_filters( 'optml_possible_src_attributes', [] );

return self::$possible_src_attributes;
}

/**
* Size to crop maping.
Expand Down
42 changes: 42 additions & 0 deletions inc/compatibilities/foogallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Class Optml_shortcode_ultimate.
*
* @reason The gallery output contains a different src attribute used for lazyload
* which prevented optimole to parse the tag.
*/
class Optml_foogallery 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( 'foogallery/foogallery.php' );
}

/**
* Register integration details.
*/
public function register() {
add_filter( 'optml_possible_src_attributes', [ $this, 'add_lazysrc' ], 10, 3 );
}

/**
* Add foogallery lazysrc attribute.
*
* @param array $attributes Old src attributes.
*
* @return array New src attributes.
*/
function add_lazysrc( $attributes = array() ) {

$attributes[] = 'data-src-fg';

return $attributes;
}
}
6 changes: 6 additions & 0 deletions inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public function can_lazyload_for( $url, $tag = '' ) {
if ( strpos( $tag, '<noscript' ) !== false ) {
return false;
}
foreach ( self::possible_src_attributes() as $banned_src ) {
if ( strpos( $tag, $banned_src ) !== false ) {
return false;
}
}

if ( ! defined( 'OPTML_DISABLE_PNG_LAZYLOAD' ) ) {
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ public function process_images_from_content( $content ) {
return $content;
}
$images = self::parse_images_from_html( $content );

if ( empty( $images ) ) {
return $content;
}
Expand Down Expand Up @@ -333,7 +332,7 @@ public static function parse_images_from_html( $content ) {
$images = array();

$content = self::strip_header_from_content( $content );
if ( preg_match_all( '/(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag>(?:<\s*noscript\s*>\s*)?<img[^>]*?\s+?src=\\\\?["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:\s*<\/a>)?/ism', $content, $images ) ) {
if ( preg_match_all( '/(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag>(?:<\s*noscript\s*>\s*)?<img[^>]*?\s+?(?:' . implode( '|', array_merge( [ 'src' ], Optml_Tag_Replacer::possible_src_attributes() ) ) . ')=\\\\?["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:\s*<\/a>)?/ism', $content, $images ) ) {

foreach ( $images as $key => $unused ) {
// Simplify the output as much as possible, mostly for confirming test results.
Expand Down

0 comments on commit 67991dc

Please sign in to comment.