Skip to content

Commit 7fd618f

Browse files
committed
fix: adds full compatibility with Envira gallery
1 parent 67991dc commit 7fd618f

File tree

6 files changed

+132
-19
lines changed

6 files changed

+132
-19
lines changed

inc/app_replacer.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ abstract class Optml_App_Replacer {
2626
* @var array
2727
*/
2828
protected static $possible_src_attributes = null;
29+
/**
30+
* Holds possible lazyload flags where we should ignore our lazyload.
31+
*
32+
* @var array
33+
*/
34+
protected static $ignore_lazyload_strings = null;
2935
/**
3036
* Settings handler.
3137
*
@@ -84,7 +90,7 @@ abstract class Optml_App_Replacer {
8490
*
8591
* @var array Integrations classes.
8692
*/
87-
private $compatibilities = array( 'shortcode_ultimate', 'foogallery' );
93+
private $compatibilities = array( 'shortcode_ultimate', 'foogallery', 'envira' );
8894

8995
/**
9096
* Returns possible src attributes.
@@ -102,6 +108,22 @@ public static function possible_src_attributes() {
102108
return self::$possible_src_attributes;
103109
}
104110

111+
/**
112+
* Returns possible src attributes.
113+
*
114+
* @return array
115+
*/
116+
public static function possible_lazyload_flags() {
117+
118+
if ( null != self::$ignore_lazyload_strings && is_array( self::$ignore_lazyload_strings ) ) {
119+
return self::$ignore_lazyload_strings;
120+
}
121+
122+
self::$possible_src_attributes = apply_filters( 'optml_possible_lazyload_flags', [] );
123+
124+
return array_merge( self::$possible_src_attributes, [ '<noscript' ] );
125+
}
126+
105127
/**
106128
* Size to crop maping.
107129
*
@@ -269,6 +291,7 @@ public function set_properties() {
269291
$compatibility->register();
270292
}
271293
}
294+
add_filter( 'optml_strip_image_size_from_url', [ $this, 'strip_image_size_from_url' ], 10, 1 );
272295
}
273296

274297
/**
@@ -322,6 +345,7 @@ public function can_replace_url( $url ) {
322345
if ( ! isset( $url['host'] ) ) {
323346
return false;
324347
}
348+
325349
return isset( $this->possible_sources[ $url['host'] ] ) || isset( $this->allowed_sources[ $url['host'] ] );
326350
}
327351

@@ -332,9 +356,9 @@ public function can_replace_url( $url ) {
332356
*
333357
* @return string
334358
**/
335-
protected function strip_image_size_from_url( $url ) {
359+
public function strip_image_size_from_url( $url ) {
336360

337-
if ( preg_match( '#(-\d+x\d+)\.(' . implode( '|', array_keys( Optml_Config::$extensions ) ) . '){1}$#i', $url, $src_parts ) ) {
361+
if ( preg_match( '#(-\d+x\d+(?:_c)?)\.(' . implode( '|', array_keys( Optml_Config::$extensions ) ) . '){1}$#i', $url, $src_parts ) ) {
338362
$stripped_url = str_replace( $src_parts[1], '', $url );
339363
// Extracts the file path to the image minus the base url
340364
$file_path = substr( $stripped_url, strpos( $stripped_url, $this->upload_resource['url'] ) + $this->upload_resource['url_length'] );
@@ -356,15 +380,15 @@ protected function strip_image_size_from_url( $url ) {
356380
protected function parse_dimensions_from_filename( $src ) {
357381
$width_height_string = array();
358382
$extensions = array_keys( Optml_Config::$extensions );
359-
if ( preg_match( '#-(\d+)x(\d+)\.(?:' . implode( '|', $extensions ) . '){1}$#i', $src, $width_height_string ) ) {
383+
if ( preg_match( '#-(\d+)x(\d+)(:?_c)?\.(?:' . implode( '|', $extensions ) . '){1}$#i', $src, $width_height_string ) ) {
360384
$width = (int) $width_height_string[1];
361385
$height = (int) $width_height_string[2];
362-
386+
$crop = ( isset( $width_height_string[3] ) && $width_height_string[3] === '_c' );
363387
if ( $width && $height ) {
364-
return array( $width, $height );
388+
return array( $width, $height, $crop );
365389
}
366390
}
367391

368-
return array( false, false );
392+
return array( false, false, false );
369393
}
370394
}

inc/compatibilities/envira.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_envira 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( 'envira-gallery-lite/envira-gallery-lite.php' ) || is_plugin_active( 'envira-gallery/envira-gallery.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+
add_filter( 'optml_possible_lazyload_flags', [ $this, 'add_lazyflag' ], 10, 3 );
28+
add_filter( 'optml_parse_resize_from_tag', [ $this, 'check_resize_tag' ], 10, 2 );
29+
add_filter( 'envira_gallery_image_src', [ $this, 'revert_src' ], 10, 1 );
30+
}
31+
32+
function revert_src( $image ) {
33+
34+
if ( ( $pos = strpos( $image, '/http' ) ) !== false ) {
35+
return ltrim( substr( $image, $pos ), '/' );
36+
}
37+
38+
return $image;
39+
}
40+
41+
function check_resize_tag( $old_resize, $tag ) {
42+
error_log( $tag );
43+
if ( preg_match( '/(_c)\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')/i', $tag, $match ) ) {
44+
return [
45+
'type' => Optml_Resize::RESIZE_FILL,
46+
'gravity' => Optml_Resize::GRAVITY_CENTER,
47+
];
48+
}
49+
50+
return [];
51+
}
52+
53+
/**
54+
* Add envira lazyload flag.
55+
*
56+
* @param array $strings Old strings.
57+
*
58+
* @return array New flags.
59+
*/
60+
function add_lazyflag( $strings = array() ) {
61+
62+
$strings[] = 'envira-lazy';
63+
64+
return $strings;
65+
}
66+
67+
/**
68+
* Add Envira lazysrc attribute.
69+
*
70+
* @param array $attributes Old src attributes.
71+
*
72+
* @return array New src attributes.
73+
*/
74+
function add_lazysrc( $attributes = array() ) {
75+
76+
$attributes[] = 'data-envira-src';
77+
78+
return $attributes;
79+
}
80+
}

inc/compatibilities/foogallery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function should_load() {
2424
*/
2525
public function register() {
2626
add_filter( 'optml_possible_src_attributes', [ $this, 'add_lazysrc' ], 10, 3 );
27+
add_filter( 'optml_possible_lazyload_flags', [ $this, 'add_lazysrc' ], 10, 3 );
2728
}
2829

2930
/**

inc/lazyload_replacer.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,8 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
113113
* @return bool We can lazyload?
114114
*/
115115
public function can_lazyload_for( $url, $tag = '' ) {
116-
if ( strpos( $tag, '<noscript' ) !== false ) {
117-
return false;
118-
}
119-
foreach ( self::possible_src_attributes() as $banned_src ) {
120-
if ( strpos( $tag, $banned_src ) !== false ) {
116+
foreach ( self::possible_lazyload_flags() as $banned_string ) {
117+
if ( strpos( $tag, $banned_string ) !== false ) {
121118
return false;
122119
}
123120
}

inc/tag_replacer.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ public function process_image_tags( $content, $images = array() ) {
9696
)
9797
);
9898
if ( false === $width && false === $height ) {
99-
list( $width, $height ) = $this->parse_dimensions_from_filename( $tmp );
99+
list( $width, $height, $crop ) = $this->parse_dimensions_from_filename( $tmp );
100100
}
101-
102101
if ( empty( $resize ) && isset( $sizes2crop[ $width . $height ] ) ) {
103102
$resize = $this->to_optml_crop( $sizes2crop[ $width . $height ] );
103+
} elseif ( isset( $crop ) ) {
104+
$resize = $this->to_optml_crop( $crop );
104105
}
105106

106107
$optml_args = [ 'width' => $width, 'height' => $height, 'resize' => $resize ];
@@ -152,13 +153,16 @@ private function parse_dimensions_from_tag( $tag, $image_sizes, $args = array()
152153
}
153154
if ( preg_match( '#class=["|\']?[^"\']*size-([^"\'\s]+)[^"\']*["|\']?#i', $tag, $size ) ) {
154155
$size = array_pop( $size );
156+
155157
if ( false === $args['width'] && false === $args['height'] && 'full' != $size && array_key_exists( $size, $image_sizes ) ) {
156158
$args['width'] = (int) $image_sizes[ $size ]['width'];
157159
$args['height'] = (int) $image_sizes[ $size ]['height'];
158160
}
159161
if ( 'full' != $size && array_key_exists( $size, $image_sizes ) ) {
160162
$args['resize'] = $this->to_optml_crop( $image_sizes[ $size ]['crop'] );
161163
}
164+
} else {
165+
$args['resize'] = apply_filters( 'optml_parse_resize_from_tag', [], $tag );
162166
}
163167

164168
return array( $args['width'], $args['height'], $args['resize'] );
@@ -205,9 +209,10 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $
205209
$sizes = self::size_to_crop();
206210
$cropping = isset( $sizes[ $size_array[0] . $size_array[1] ] ) ? $this->to_optml_crop( $sizes[ $size_array[0] . $size_array[1] ] ) : null;
207211
}
212+
208213
foreach ( $sources as $i => $source ) {
209214
$url = $source['url'];
210-
list( $width, $height ) = $this->parse_dimensions_from_filename( $url );
215+
list( $width, $height, $file_crop ) = $this->parse_dimensions_from_filename( $url );
211216

212217
if ( empty( $width ) ) {
213218
$width = $image_meta['width'];
@@ -216,6 +221,7 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $
216221
if ( empty( $height ) ) {
217222
$height = $image_meta['height'];
218223
}
224+
219225
if ( $original_url === null ) {
220226
if ( ! empty( $attachment_id ) ) {
221227
$original_url = wp_get_attachment_url( $attachment_id );
@@ -234,6 +240,8 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $
234240
}
235241
if ( $cropping !== null ) {
236242
$args['resize'] = $cropping;
243+
} else {
244+
$args['resize'] = $this->to_optml_crop( $file_crop );
237245
}
238246
$sources[ $i ]['url'] = apply_filters( 'optml_content_url', $original_url, $args );
239247
}

inc/url_replacer.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public function init() {
6464
*/
6565
public function build_image_url(
6666
$url, $args = array(
67-
'width' => 'auto',
68-
'height' => 'auto',
69-
)
67+
'width' => 'auto',
68+
'height' => 'auto',
69+
)
7070
) {
7171

7272
if ( apply_filters( 'optml_dont_replace_url', false, $url ) ) {
@@ -104,7 +104,10 @@ public function build_image_url(
104104
$new_url = $this->strip_image_size_from_url( $url );
105105

106106
if ( $new_url !== $url ) {
107-
list( $args['width'], $args['height'] ) = $this->parse_dimensions_from_filename( $url );
107+
list( $args['width'], $args['height'], $crop ) = $this->parse_dimensions_from_filename( $url );
108+
if ( $crop ) {
109+
$args['resize'] = $this->to_optml_crop( $crop );
110+
}
108111
$url = $new_url;
109112
}
110113
$args['width'] = (int) $args['width'];

0 commit comments

Comments
 (0)