get( 'disable_on_wptouch' ) && self::is_wptouch() ) {
return;
}
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
$skip_classes = $options->get( 'skip_classes' );
if ( strlen( trim( $skip_classes ) ) ) {
$this->_skip_classes = array_map( 'trim', explode( ',', $options->get( 'skip_classes' ) ) );
}
$this->_placeholder_url = $options->get( 'placeholder_url' );
if ( ! strlen( $this->_placeholder_url ) ) {
$this->_placeholder_url = plugins_url( '/img/placeholder.gif', __FILE__ );
}
if ( $options->get( 'filter_content' ) == 'yes' ) {
add_filter( 'the_content', array( $this, 'filter' ), 200 );
}
if ( $options->get( 'filter_post_thumbnails' ) == 'yes' ) {
add_filter( 'post_thumbnail_html', array( $this, 'filter' ), 200 );
}
if ( $options->get( 'filter_gravatars' ) == 'yes' ) {
add_filter( 'get_avatar', array( $this, 'filter' ), 200 );
}
}
static function singleton() {
if ( ! isset( self::$_instance ) ) {
$className = __CLASS__;
self::$_instance = new $className;
}
return self::$_instance;
}
static function enqueue_scripts() {
$in_footer = true;
$options = self::_get_options();
$theme_loader_function = $options->get( 'theme_loader_function' );
if ( $theme_loader_function == 'wp_head' ) {
$in_footer = false;
}
wp_enqueue_script( 'jquery.sonar', plugins_url( '/js/jquery.sonar.min.js', __FILE__ ), array( 'jquery' ), self::version, $in_footer );
if ( defined( 'SCRIPT_DEBUG') && SCRIPT_DEBUG ) {
wp_enqueue_script( 'BJLL', plugins_url( '/js/bj-lazy-load.js', __FILE__ ), array( 'jquery', 'jquery.sonar' ), self::version, $in_footer );
} else {
wp_enqueue_script( 'BJLL', plugins_url( '/js/bj-lazy-load.min.js', __FILE__ ), array( 'jquery', 'jquery.sonar' ), self::version, $in_footer );
}
$bjll_options = array();
if ( $options->get('load_hidpi') == 'yes' || $options->get('load_responsive') == 'yes' ) {
$bjll_options['thumb_base'] = plugins_url( '/thumb.php', __FILE__ ) . '?src=';
$bjll_options['load_hidpi'] = $options->get('load_hidpi');
$bjll_options['load_responsive'] = $options->get('load_responsive');
}
if ( $options->get('infinite_scroll') == 'yes' ) {
$bjll_options['infinite_scroll'] = $options->get('infinite_scroll');
}
if ( count( $bjll_options ) ) {
wp_localize_script( 'BJLL', 'BJLL', $bjll_options );
}
}
static function filter( $content ) {
$BJLL = BJLL::singleton();
$options = self::_get_options();
if ( $options->get('lazy_load_images') == 'yes' ) {
$content = $BJLL->_filter_images( $content );
}
if ( $options->get('lazy_load_iframes') == 'yes' ) {
$content = $BJLL->_filter_iframes( $content );
}
return $content;
}
protected function _filter_images( $content ) {
$matches = array();
preg_match_all( '/
/', $content, $matches );
$search = array();
$replace = array();
if ( is_array( $this->_skip_classes ) ) {
$skip_images_preg_quoted = array_map( 'preg_quote', $this->_skip_classes );
$skip_images_regex = sprintf( '/class=".*(%s).*"/', implode( '|', $skip_images_preg_quoted ) );
}
foreach ( $matches[0] as $imgHTML ) {
// don't to the replacement if a skip class is provided and the image has the class
if ( ! ( is_array( $this->_skip_classes ) && preg_match( $skip_images_regex, $imgHTML ) ) ) {
// replace the src and add the data-src attribute
$replaceHTML = preg_replace( '/
_placeholder_url . '" data-lazy-type="image" data-lazy-src=', $imgHTML );
// add the lazy class to the img element
if ( preg_match( '/class="/i', $replaceHTML ) ) {
$replaceHTML = preg_replace( '/class="(.*?)"/i', 'class="lazy lazy-hidden $1"', $replaceHTML );
} else {
$replaceHTML = preg_replace( '/
';
array_push( $search, $imgHTML );
array_push( $replace, $replaceHTML );
}
}
$content = str_replace( $search, $replace, $content );
return $content;
}
protected function _filter_iframes( $content ) {
$matches = array();
preg_match_all( '/