diff options
Diffstat (limited to 'plugins/jetpack/modules/lazy-images/lazy-images.php')
-rw-r--r-- | plugins/jetpack/modules/lazy-images/lazy-images.php | 71 |
1 files changed, 46 insertions, 25 deletions
diff --git a/plugins/jetpack/modules/lazy-images/lazy-images.php b/plugins/jetpack/modules/lazy-images/lazy-images.php index 59c64a1e..e77d2e10 100644 --- a/plugins/jetpack/modules/lazy-images/lazy-images.php +++ b/plugins/jetpack/modules/lazy-images/lazy-images.php @@ -162,7 +162,7 @@ class Jetpack_Lazy_Images { * @return string The image with updated lazy attributes */ static function process_image( $matches ) { - $old_attributes_str = $matches[2]; + $old_attributes_str = $matches[2]; $old_attributes_kses_hair = wp_kses_hair( $old_attributes_str, wp_allowed_protocols() ); if ( empty( $old_attributes_kses_hair['src'] ) ) { @@ -170,16 +170,13 @@ class Jetpack_Lazy_Images { } $old_attributes = self::flatten_kses_hair_data( $old_attributes_kses_hair ); - $new_attributes = self::process_image_attributes( $old_attributes ); // If we didn't add lazy attributes, just return the original image source. - if ( empty( $new_attributes['data-lazy-src'] ) ) { + if ( ! empty( $old_attributes['class'] ) && false !== strpos( $old_attributes['class'], 'jetpack-lazy-image' ) ) { return $matches[0]; } - // Ensure we add the jetpack-lazy-image class to this image. - $new_attributes['class'] = sprintf( '%s jetpack-lazy-image', empty( $new_attributes['class'] ) ? '' : $new_attributes['class'] ); - + $new_attributes = self::process_image_attributes( $old_attributes ); $new_attributes_str = self::build_attributes_string( $new_attributes ); return sprintf( '<img %1$s><noscript>%2$s</noscript>', $new_attributes_str, $matches[0] ); @@ -209,6 +206,8 @@ class Jetpack_Lazy_Images { * * @module-lazy-images * + * @deprecated 6.5.0 Use jetpack_lazy_images_skip_image_with_attributes instead. + * * @since 5.9.0 * * @param bool Default to not skip processing the current image. @@ -218,23 +217,42 @@ class Jetpack_Lazy_Images { return $attributes; } - $old_attributes = $attributes; + /** + * Allow plugins and themes to conditionally skip processing an image via its attributes. + * + * @module-lazy-images + * + * @since 6.5.0 Filter name was updated from jetpack_lazy_images_skip_image_with_atttributes to correct typo. + * @since 5.9.0 + * + * @param bool Default to not skip processing the current image. + * @param array An array of attributes via wp_kses_hair() for the current image. + */ + if ( apply_filters( 'jetpack_lazy_images_skip_image_with_attributes', false, $attributes ) ) { + return $attributes; + } - // Set placeholder and lazy-src - $attributes['src'] = self::get_placeholder_image(); - $attributes['data-lazy-src'] = $old_attributes['src']; + $old_attributes = $attributes; - // Handle `srcset` - if ( ! empty( $attributes['srcset'] ) ) { - $attributes['data-lazy-srcset'] = $old_attributes['srcset']; - unset( $attributes['srcset'] ); + // Stash srcset and sizes in data attributes. + foreach ( array( 'srcset', 'sizes' ) as $attribute ) { + if ( isset( $old_attributes[ $attribute ] ) ) { + $attributes[ "data-lazy-$attribute" ] = $old_attributes[ $attribute ]; + unset( $attributes[ $attribute ] ); + } } - // Handle `sizes` - if ( ! empty( $attributes['sizes'] ) ) { - $attributes['data-lazy-sizes'] = $old_attributes['sizes']; - unset( $attributes['sizes'] ); - } + // We set this, adding the query arg so that it doesn't exactly equal the src attribute, so that photon JavaScript + // will hold off on processing this image. + $attributes['data-lazy-src'] = esc_url_raw( add_query_arg( 'is-pending-load', true, $attributes['src'] ) ); + + $attributes['srcset'] = self::get_placeholder_image(); + $attributes['class'] = sprintf( + '%s jetpack-lazy-image', + empty( $old_attributes['class'] ) + ? '' + : $old_attributes['class'] + ); /** * Allow plugins and themes to override the attributes on the image before the content is updated. @@ -263,21 +281,23 @@ class Jetpack_Lazy_Images { public function add_nojs_fallback() { ?> <style type="text/css"> - .jetpack-lazy-image { + html:not( .jetpack-lazy-images-js-enabled ) .jetpack-lazy-image { display: none; } - .jetpack-lazy-images-js .jetpack-lazy-image { - display: inline-block; - } </style> <script> document.documentElement.classList.add( - 'jetpack-lazy-images-js' + 'jetpack-lazy-images-js-enabled' ); </script> <?php } + /** + * Retrieves the placeholder image after running it through the lazyload_images_placeholder_image filter. + * + * @return string The placeholder image source. + */ private static function get_placeholder_image() { /** * Allows plugins and themes to modify the placeholder image. @@ -288,12 +308,13 @@ class Jetpack_Lazy_Images { * @module lazy-images * * @since 5.6.0 + * @since 6.5.0 Default image is now a base64 encoded transparent gif. * * @param string The URL to the placeholder image */ return apply_filters( 'lazyload_images_placeholder_image', - plugins_url( 'modules/lazy-images/images/1x1.trans.gif', JETPACK__PLUGIN_FILE ) + 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' ); } |