Skip to content
Permalink
Browse files

fix: adds full compatibility with Envira gallery

  • Loading branch information...
selul committed Mar 7, 2019
1 parent 67991dc commit 7fd618fb36b98f41e67c9eb98ab440715c8a1859
@@ -26,6 +26,12 @@ abstract class Optml_App_Replacer {
* @var array
*/
protected static $possible_src_attributes = null;
/**
* Holds possible lazyload flags where we should ignore our lazyload.
*
* @var array
*/
protected static $ignore_lazyload_strings = null;
/**
* Settings handler.
*
@@ -84,7 +90,7 @@ abstract class Optml_App_Replacer {
*
* @var array Integrations classes.
*/
private $compatibilities = array( 'shortcode_ultimate', 'foogallery' );
private $compatibilities = array( 'shortcode_ultimate', 'foogallery', 'envira' );
/**
* Returns possible src attributes.
@@ -102,6 +108,22 @@ public static function possible_src_attributes() {
return self::$possible_src_attributes;
}
/**
* Returns possible src attributes.
*
* @return array
*/
public static function possible_lazyload_flags() {
if ( null != self::$ignore_lazyload_strings && is_array( self::$ignore_lazyload_strings ) ) {
return self::$ignore_lazyload_strings;
}
self::$possible_src_attributes = apply_filters( 'optml_possible_lazyload_flags', [] );
return array_merge( self::$possible_src_attributes, [ '<noscript' ] );
}
/**
* Size to crop maping.
*
@@ -269,6 +291,7 @@ public function set_properties() {
$compatibility->register();
}
}
add_filter( 'optml_strip_image_size_from_url', [ $this, 'strip_image_size_from_url' ], 10, 1 );
}
/**
@@ -322,6 +345,7 @@ public function can_replace_url( $url ) {
if ( ! isset( $url['host'] ) ) {
return false;
}
return isset( $this->possible_sources[ $url['host'] ] ) || isset( $this->allowed_sources[ $url['host'] ] );
}
@@ -332,9 +356,9 @@ public function can_replace_url( $url ) {
*
* @return string
**/
protected function strip_image_size_from_url( $url ) {
public function strip_image_size_from_url( $url ) {
if ( preg_match( '#(-\d+x\d+)\.(' . implode( '|', array_keys( Optml_Config::$extensions ) ) . '){1}$#i', $url, $src_parts ) ) {
if ( preg_match( '#(-\d+x\d+(?:_c)?)\.(' . implode( '|', array_keys( Optml_Config::$extensions ) ) . '){1}$#i', $url, $src_parts ) ) {
$stripped_url = str_replace( $src_parts[1], '', $url );
// Extracts the file path to the image minus the base url
$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 ) {
protected function parse_dimensions_from_filename( $src ) {
$width_height_string = array();
$extensions = array_keys( Optml_Config::$extensions );
if ( preg_match( '#-(\d+)x(\d+)\.(?:' . implode( '|', $extensions ) . '){1}$#i', $src, $width_height_string ) ) {
if ( preg_match( '#-(\d+)x(\d+)(:?_c)?\.(?:' . implode( '|', $extensions ) . '){1}$#i', $src, $width_height_string ) ) {
$width = (int) $width_height_string[1];
$height = (int) $width_height_string[2];
$crop = ( isset( $width_height_string[3] ) && $width_height_string[3] === '_c' );
if ( $width && $height ) {
return array( $width, $height );
return array( $width, $height, $crop );
}
}
return array( false, false );
return array( false, false, false );
}
}
@@ -0,0 +1,80 @@
<?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_envira 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( 'envira-gallery-lite/envira-gallery-lite.php' ) || is_plugin_active( 'envira-gallery/envira-gallery.php' ) );
}
/**
* Register integration details.
*/
public function register() {
add_filter( 'optml_possible_src_attributes', [ $this, 'add_lazysrc' ], 10, 3 );
add_filter( 'optml_possible_lazyload_flags', [ $this, 'add_lazyflag' ], 10, 3 );
add_filter( 'optml_parse_resize_from_tag', [ $this, 'check_resize_tag' ], 10, 2 );
add_filter( 'envira_gallery_image_src', [ $this, 'revert_src' ], 10, 1 );
}
function revert_src( $image ) {
if ( ( $pos = strpos( $image, '/http' ) ) !== false ) {
return ltrim( substr( $image, $pos ), '/' );
}
return $image;
}
function check_resize_tag( $old_resize, $tag ) {
error_log( $tag );
if ( preg_match( '/(_c)\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')/i', $tag, $match ) ) {
return [
'type' => Optml_Resize::RESIZE_FILL,
'gravity' => Optml_Resize::GRAVITY_CENTER,
];
}
return [];
}
/**
* Add envira lazyload flag.
*
* @param array $strings Old strings.
*
* @return array New flags.
*/
function add_lazyflag( $strings = array() ) {
$strings[] = 'envira-lazy';
return $strings;
}
/**
* Add Envira lazysrc attribute.
*
* @param array $attributes Old src attributes.
*
* @return array New src attributes.
*/
function add_lazysrc( $attributes = array() ) {
$attributes[] = 'data-envira-src';
return $attributes;
}
}
@@ -24,6 +24,7 @@ function should_load() {
*/
public function register() {
add_filter( 'optml_possible_src_attributes', [ $this, 'add_lazysrc' ], 10, 3 );
add_filter( 'optml_possible_lazyload_flags', [ $this, 'add_lazysrc' ], 10, 3 );
}
/**
@@ -113,11 +113,8 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
* @return bool We can lazyload?
*/
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 ) {
foreach ( self::possible_lazyload_flags() as $banned_string ) {
if ( strpos( $tag, $banned_string ) !== false ) {
return false;
}
}
@@ -96,11 +96,12 @@ public function process_image_tags( $content, $images = array() ) {
)
);
if ( false === $width && false === $height ) {
list( $width, $height ) = $this->parse_dimensions_from_filename( $tmp );
list( $width, $height, $crop ) = $this->parse_dimensions_from_filename( $tmp );
}
if ( empty( $resize ) && isset( $sizes2crop[ $width . $height ] ) ) {
$resize = $this->to_optml_crop( $sizes2crop[ $width . $height ] );
} elseif ( isset( $crop ) ) {
$resize = $this->to_optml_crop( $crop );
}
$optml_args = [ 'width' => $width, 'height' => $height, 'resize' => $resize ];
@@ -152,13 +153,16 @@ private function parse_dimensions_from_tag( $tag, $image_sizes, $args = array()
}
if ( preg_match( '#class=["|\']?[^"\']*size-([^"\'\s]+)[^"\']*["|\']?#i', $tag, $size ) ) {
$size = array_pop( $size );
if ( false === $args['width'] && false === $args['height'] && 'full' != $size && array_key_exists( $size, $image_sizes ) ) {
$args['width'] = (int) $image_sizes[ $size ]['width'];
$args['height'] = (int) $image_sizes[ $size ]['height'];
}
if ( 'full' != $size && array_key_exists( $size, $image_sizes ) ) {
$args['resize'] = $this->to_optml_crop( $image_sizes[ $size ]['crop'] );
}
} else {
$args['resize'] = apply_filters( 'optml_parse_resize_from_tag', [], $tag );
}
return array( $args['width'], $args['height'], $args['resize'] );
@@ -205,9 +209,10 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $
$sizes = self::size_to_crop();
$cropping = isset( $sizes[ $size_array[0] . $size_array[1] ] ) ? $this->to_optml_crop( $sizes[ $size_array[0] . $size_array[1] ] ) : null;
}
foreach ( $sources as $i => $source ) {
$url = $source['url'];
list( $width, $height ) = $this->parse_dimensions_from_filename( $url );
list( $width, $height, $file_crop ) = $this->parse_dimensions_from_filename( $url );
if ( empty( $width ) ) {
$width = $image_meta['width'];
@@ -216,6 +221,7 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $
if ( empty( $height ) ) {
$height = $image_meta['height'];
}
if ( $original_url === null ) {
if ( ! empty( $attachment_id ) ) {
$original_url = wp_get_attachment_url( $attachment_id );
@@ -234,6 +240,8 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $
}
if ( $cropping !== null ) {
$args['resize'] = $cropping;
} else {
$args['resize'] = $this->to_optml_crop( $file_crop );
}
$sources[ $i ]['url'] = apply_filters( 'optml_content_url', $original_url, $args );
}
@@ -64,9 +64,9 @@ public function init() {
*/
public function build_image_url(
$url, $args = array(
'width' => 'auto',
'height' => 'auto',
)
'width' => 'auto',
'height' => 'auto',
)
) {
if ( apply_filters( 'optml_dont_replace_url', false, $url ) ) {
@@ -104,7 +104,10 @@ public function build_image_url(
$new_url = $this->strip_image_size_from_url( $url );
if ( $new_url !== $url ) {
list( $args['width'], $args['height'] ) = $this->parse_dimensions_from_filename( $url );
list( $args['width'], $args['height'], $crop ) = $this->parse_dimensions_from_filename( $url );
if ( $crop ) {
$args['resize'] = $this->to_optml_crop( $crop );
}
$url = $new_url;
}
$args['width'] = (int) $args['width'];

0 comments on commit 7fd618f

Please sign in to comment.
You can’t perform that action at this time.