diff --git a/inc/app_replacer.php b/inc/app_replacer.php index 475ee557..38fff042 100644 --- a/inc/app_replacer.php +++ b/inc/app_replacer.php @@ -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, [ '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 ); } } diff --git a/inc/compatibilities/envira.php b/inc/compatibilities/envira.php new file mode 100644 index 00000000..9f1ef296 --- /dev/null +++ b/inc/compatibilities/envira.php @@ -0,0 +1,80 @@ + 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; + } +} diff --git a/inc/compatibilities/foogallery.php b/inc/compatibilities/foogallery.php index a99d0994..5c74dd0b 100644 --- a/inc/compatibilities/foogallery.php +++ b/inc/compatibilities/foogallery.php @@ -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 ); } /** diff --git a/inc/lazyload_replacer.php b/inc/lazyload_replacer.php index 037c3e4e..eefb5894 100644 --- a/inc/lazyload_replacer.php +++ b/inc/lazyload_replacer.php @@ -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, '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,6 +153,7 @@ 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']; @@ -159,6 +161,8 @@ private function parse_dimensions_from_tag( $tag, $image_sizes, $args = array() 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 ); } diff --git a/inc/url_replacer.php b/inc/url_replacer.php index bea33fc9..88c32f5d 100644 --- a/inc/url_replacer.php +++ b/inc/url_replacer.php @@ -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'];