Skip to content

Commit 67991dc

Browse files
committed
fix: compatibility with Foogallery plugin when the gallery uses lazyload too
1 parent 164ba35 commit 67991dc

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

inc/app_replacer.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ abstract class Optml_App_Replacer {
2020
* @var array
2121
*/
2222
protected static $size_to_crop = array();
23+
/**
24+
* Holds possible src attributes.
25+
*
26+
* @var array
27+
*/
28+
protected static $possible_src_attributes = null;
2329
/**
2430
* Settings handler.
2531
*
@@ -78,7 +84,23 @@ abstract class Optml_App_Replacer {
7884
*
7985
* @var array Integrations classes.
8086
*/
81-
private $compatibilities = array( 'shortcode_ultimate' );
87+
private $compatibilities = array( 'shortcode_ultimate', 'foogallery' );
88+
89+
/**
90+
* Returns possible src attributes.
91+
*
92+
* @return array
93+
*/
94+
public static function possible_src_attributes() {
95+
96+
if ( null != self::$possible_src_attributes && is_array( self::$possible_src_attributes ) ) {
97+
return self::$possible_src_attributes;
98+
}
99+
100+
self::$possible_src_attributes = apply_filters( 'optml_possible_src_attributes', [] );
101+
102+
return self::$possible_src_attributes;
103+
}
82104

83105
/**
84106
* Size to crop maping.

inc/compatibilities/foogallery.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* Class Optml_shortcode_ultimate.
5+
*
6+
* @reason The gallery output contains a different src attribute used for lazyload
7+
* which prevented optimole to parse the tag.
8+
*/
9+
class Optml_foogallery extends Optml_compatibility {
10+
11+
/**
12+
* Should we load the integration logic.
13+
*
14+
* @return bool Should we load.
15+
*/
16+
function should_load() {
17+
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
18+
19+
return is_plugin_active( 'foogallery/foogallery.php' );
20+
}
21+
22+
/**
23+
* Register integration details.
24+
*/
25+
public function register() {
26+
add_filter( 'optml_possible_src_attributes', [ $this, 'add_lazysrc' ], 10, 3 );
27+
}
28+
29+
/**
30+
* Add foogallery lazysrc attribute.
31+
*
32+
* @param array $attributes Old src attributes.
33+
*
34+
* @return array New src attributes.
35+
*/
36+
function add_lazysrc( $attributes = array() ) {
37+
38+
$attributes[] = 'data-src-fg';
39+
40+
return $attributes;
41+
}
42+
}

inc/lazyload_replacer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ public function can_lazyload_for( $url, $tag = '' ) {
116116
if ( strpos( $tag, '<noscript' ) !== false ) {
117117
return false;
118118
}
119+
foreach ( self::possible_src_attributes() as $banned_src ) {
120+
if ( strpos( $tag, $banned_src ) !== false ) {
121+
return false;
122+
}
123+
}
124+
119125
if ( ! defined( 'OPTML_DISABLE_PNG_LAZYLOAD' ) ) {
120126
return true;
121127
}

inc/manager.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ public function process_images_from_content( $content ) {
288288
return $content;
289289
}
290290
$images = self::parse_images_from_html( $content );
291-
292291
if ( empty( $images ) ) {
293292
return $content;
294293
}
@@ -333,7 +332,7 @@ public static function parse_images_from_html( $content ) {
333332
$images = array();
334333

335334
$content = self::strip_header_from_content( $content );
336-
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 ) ) {
335+
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 ) ) {
337336

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

0 commit comments

Comments
 (0)