PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 2.5.7
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v2.5.7
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / compatibilities / envira.php

envira.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 2.5.7, at inc/compatibilities/envira.php

81 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_lazyload_flags', [ $this, 'add_lazyflag' ], 10, 2 );
27 add_filter( 'optml_parse_resize_from_tag', [ $this, 'check_resize_tag' ], 10, 2 );
28 add_filter( 'envira_gallery_image_src', [ $this, 'revert_src' ], 10, 1 );
29 }
30
31 /**
32 * Revert the optimole url to the original state in
33 * order to allow to be parsed by the image tag parser.
34 *
35 * @param string $image Image url.
36 *
37 * @return string Original url.
38 */
39 function revert_src( $image ) {
40 $pos = strpos( $image, '/http' );
41 if ( $pos !== false ) {
42 return ltrim( substr( $image, $pos ), '/' );
43 }
44
45 return $image;
46 }
47
48 /**
49 * Alter default resize for image tag parsing.
50 *
51 * @param array $old_resize Old array, if any.
52 * @param string $tag Image tag.
53 *
54 * @return array Resize conf.
55 */
56 function check_resize_tag( $old_resize, $tag ) {
57 if ( preg_match( '/(_c)\.(?:' . implode( '|', array_keys( Optml_Config::$image_extensions ) ) . ')/i', $tag, $match ) ) {
58 return [
59 'type' => Optml_Resize::RESIZE_FILL,
60 'gravity' => Optml_Resize::GRAVITY_CENTER,
61 ];
62 }
63
64 return [];
65 }
66
67 /**
68 * Add envira lazyload flag.
69 *
70 * @param array $strings Old strings.
71 *
72 * @return array New flags.
73 */
74 function add_lazyflag( $strings = [] ) {
75
76 $strings[] = 'envira-gallery-image';
77
78 return $strings;
79 }
80 }
81