| 1 |
<?php |
| 2 |
|
| 3 |
use Optimole\Sdk\Resource\ImageProperty\ResizeTypeProperty; |
| 4 |
use Optimole\Sdk\ValueObject\Position; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class Optml_shortcode_ultimate. |
| 8 |
* |
| 9 |
* @reason The gallery output contains a different src attribute used for lazyload |
| 10 |
* which prevented optimole to parse the tag. |
| 11 |
*/ |
| 12 |
class Optml_envira extends Optml_compatibility { |
| 13 |
|
| 14 |
/** |
| 15 |
* Should we load the integration logic. |
| 16 |
* |
| 17 |
* @return bool Should we load. |
| 18 |
*/ |
| 19 |
public function should_load() { |
| 20 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 21 |
|
| 22 |
return ( is_plugin_active( 'envira-gallery-lite/envira-gallery-lite.php' ) || is_plugin_active( 'envira-gallery/envira-gallery.php' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register integration details. |
| 27 |
*/ |
| 28 |
public function register() { |
| 29 |
add_filter( 'optml_possible_lazyload_flags', [ $this, 'add_lazyflag' ], 10 ); |
| 30 |
add_filter( 'optml_parse_resize_from_tag', [ $this, 'check_resize_tag' ], 10, 2 ); |
| 31 |
add_filter( 'envira_gallery_image_src', [ $this, 'revert_src' ], 10 ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Revert the optimole url to the original state in |
| 36 |
* order to allow to be parsed by the image tag parser. |
| 37 |
* |
| 38 |
* @param string $image Image url. |
| 39 |
* |
| 40 |
* @return string Original url. |
| 41 |
*/ |
| 42 |
public function revert_src( $image ) { |
| 43 |
$pos = strpos( $image, '/http' ); |
| 44 |
if ( $pos !== false ) { |
| 45 |
return ltrim( substr( $image, $pos ), '/' ); |
| 46 |
} |
| 47 |
|
| 48 |
return $image; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Alter default resize for image tag parsing. |
| 53 |
* |
| 54 |
* @param array $old_resize Old array, if any. |
| 55 |
* @param string $tag Image tag. |
| 56 |
* |
| 57 |
* @return array Resize conf. |
| 58 |
*/ |
| 59 |
public function check_resize_tag( $old_resize, $tag ) { |
| 60 |
if ( preg_match( '/(_c)\.(?:' . implode( '|', array_keys( Optml_Config::$image_extensions ) ) . ')/i', $tag, $match ) ) { |
| 61 |
return [ |
| 62 |
'type' => ResizeTypeProperty::FILL, |
| 63 |
'gravity' => Position::CENTER, |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
return []; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Add envira lazyload flag. |
| 72 |
* |
| 73 |
* @param array $strings Old strings. |
| 74 |
* |
| 75 |
* @return array New flags. |
| 76 |
*/ |
| 77 |
public function add_lazyflag( $strings = [] ) { |
| 78 |
|
| 79 |
$strings[] = 'envira-gallery-image'; |
| 80 |
|
| 81 |
return $strings; |
| 82 |
} |
| 83 |
} |
| 84 |
|