|
| 1 | +<?php |
| 2 | + |
| 3 | +class Jetpack_Lazy_Images { |
| 4 | + private static $__instance = null; |
| 5 | + /** |
| 6 | + * Singleton implementation |
| 7 | + * |
| 8 | + * @return object |
| 9 | + */ |
| 10 | + public static function instance() { |
| 11 | + if ( is_null( self::$__instance ) ) { |
| 12 | + self::$__instance = new Jetpack_Lazy_Images(); |
| 13 | + } |
| 14 | + |
| 15 | + return self::$__instance; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Registers actions |
| 20 | + */ |
| 21 | + private function __construct() { |
| 22 | + // modify content |
| 23 | + add_action( 'wp_head', array( $this, 'setup_filters' ), 9999 ); // we don't really want to modify anything in <head> since it's mostly all metadata |
| 24 | + |
| 25 | + // js to do lazy loading |
| 26 | + add_action( 'init', array( $this, 'register_assets' ) ); |
| 27 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 28 | + } |
| 29 | + |
| 30 | + public function setup_filters() { |
| 31 | + add_filter( 'the_content', array( $this, 'add_image_placeholders' ), 99 ); // run this later, so other content filters have run, including image_add_wh on WP.com |
| 32 | + add_filter( 'post_thumbnail_html', array( $this, 'add_image_placeholders' ), 11 ); |
| 33 | + add_filter( 'get_avatar', array( $this, 'add_image_placeholders' ), 11 ); |
| 34 | + } |
| 35 | + |
| 36 | + public function add_image_placeholders( $content ) { |
| 37 | + // Don't lazyload for feeds, previews |
| 38 | + if ( is_feed() || is_preview() ) { |
| 39 | + return $content; |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + // Don't lazy-load if the content has already been run through previously |
| 44 | + if ( false !== strpos( $content, 'data-lazy-src' ) ) { |
| 45 | + return $content; |
| 46 | + } |
| 47 | + |
| 48 | + // This is a pretty simple regex, but it works |
| 49 | + $content = preg_replace_callback( '#<(img)([^>]+?)(>(.*?)</\\1>|[\/]?>)#si', array( __CLASS__, 'process_image' ), $content ); |
| 50 | + |
| 51 | + return $content; |
| 52 | + } |
| 53 | + |
| 54 | + function process_image( $matches ) { |
| 55 | + $old_attributes_str = $matches[2]; |
| 56 | + $old_attributes = wp_kses_hair( $old_attributes_str, wp_allowed_protocols() ); |
| 57 | + |
| 58 | + if ( empty( $old_attributes['src'] ) ) { |
| 59 | + return $matches[0]; |
| 60 | + } |
| 61 | + |
| 62 | + $image_src = $old_attributes['src']['value']; |
| 63 | + |
| 64 | + if ( isset( $old_attributes['srcset'] ) ) { |
| 65 | + $image_srcset = $old_attributes['srcset']['value']; |
| 66 | + } else { |
| 67 | + $image_srcset = ''; |
| 68 | + } |
| 69 | + |
| 70 | + // Remove src, lazy-src, srcset and lazy-srcset since we manually add them |
| 71 | + $new_attributes = $old_attributes; |
| 72 | + unset( $new_attributes['src'], $new_attributes['srcset'], $new_attributes['data-lazy-src'], $new_attributes['data-lazy-srcset'] ); |
| 73 | + |
| 74 | + $new_attributes_str = $this->build_attributes_string( $new_attributes ); |
| 75 | + |
| 76 | + return sprintf( |
| 77 | + '<img data-lazy-src="%1$s" data-lazy-srcset="%2$s" %3$s><noscript>%4$s</noscript>', |
| 78 | + esc_url( $image_src ), |
| 79 | + esc_attr( $image_srcset ), |
| 80 | + $new_attributes_str, |
| 81 | + $matches[0] |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + private function build_attributes_string( $attributes ) { |
| 86 | + $string = array(); |
| 87 | + foreach ( $attributes as $name => $attribute ) { |
| 88 | + $value = $attribute['value']; |
| 89 | + if ( '' === $value ) { |
| 90 | + $string[] = sprintf( '%s', $name ); |
| 91 | + } else { |
| 92 | + $string[] = sprintf( '%s="%s"', $name, esc_attr( $value ) ); |
| 93 | + } |
| 94 | + } |
| 95 | + return implode( ' ', $string ); |
| 96 | + } |
| 97 | + |
| 98 | + public function register_assets() { |
| 99 | + wp_register_script( |
| 100 | + 'jetpack-lazy-images', |
| 101 | + plugins_url( 'modules/lazy-images/assets/lazy-images.js', JETPACK__PLUGIN_FILE ), |
| 102 | + array(), |
| 103 | + '1.5', |
| 104 | + true |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public function enqueue_assets() { |
| 109 | + wp_enqueue_script( 'jetpack-lazy-images' ); |
| 110 | + } |
| 111 | +} |
0 commit comments