Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function page_content() {
'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
),
array(
'title' => __( 'Apply to text widgets', 'bj-lazy-load' ),
'title' => __( 'Apply to sidebar widgets', 'bj-lazy-load' ),
'type' => 'radio',
'name' => 'filter_widget_text',
'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
'name' => 'filter_sidebar_widgets',
'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ), 'only_text_widgets' => __( 'Only Text Widgets', 'bj-lazy-load' ) ),
),
array(
'title' => __( 'Apply to post thumbnails', 'bj-lazy-load' ),
Expand All @@ -49,6 +49,12 @@ function page_content() {
'name' => 'lazy_load_iframes',
'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
),
array(
'title' => __( 'Lazy load twitter timeline', 'bj-lazy-load' ),
'type' => 'radio',
'name' => 'lazy_load_twitter_timeline',
'value' => array( 'yes' => __( 'Yes', 'bj-lazy-load' ), 'no' => __( 'No', 'bj-lazy-load' ) ),
),
array(
'title' => __( 'Theme loader function', 'bj-lazy-load' ),
'type' => 'select',
Expand Down
86 changes: 70 additions & 16 deletions bj-lazy-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ function __construct() {
if ( $options->get( 'filter_content' ) == 'yes' ) {
add_filter( 'the_content', array( $this, 'filter' ), 200 );
}
if ( $options->get( 'filter_widget_text' ) == 'yes' ) {
if ( $options->get( 'filter_sidebar_widgets' ) == 'yes' ) {
add_action( 'dynamic_sidebar_before', array( $this, 'observe_sidebar' ), 200 );
add_action( 'dynamic_sidebar_after', array( $this, 'stop_observing_sidebar_and_output_sidebar' ), 200 );
}
if ( $options->get( 'filter_sidebar_widgets' ) == 'only_text_widgets' ) {
add_filter( 'widget_text', array( $this, 'filter' ), 200 );
}
if ( $options->get( 'filter_post_thumbnails' ) == 'yes' ) {
Expand Down Expand Up @@ -165,6 +169,10 @@ static function filter( $content ) {
if ( $options->get('lazy_load_iframes') == 'yes' ) {
$content = $BJLL->_filter_iframes( $content );
}

if ( $options->get('lazy_load_twitter_timeline') == 'yes' ) {
$content = $BJLL->_filter_twitter_timeline( $content );
}

return $content;
}
Expand Down Expand Up @@ -237,23 +245,47 @@ protected function _filter_iframes( $content ) {
return $content;
}

protected function _filter_twitter_timeline( $content ) {

$matches = array();
preg_match_all( '/<a\s+[^>]*?class="twitter-timeline".*?<\/a>/s', $content, $matches );

$search = array();
$replace = array();

foreach ( $matches[0] as $twitterTimelineHTML ) {

$replaceHTML = '<img src="' . $this->_placeholder_url . '" class="lazy lazy-hidden" data-lazy-type="twitter" data-lazy-src="' . base64_encode($twitterTimelineHTML) . '" alt="">';

$replaceHTML .= '<noscript>' . $twitterTimelineHTML . '</noscript>';

array_push( $search, $twitterTimelineHTML );
array_push( $replace, $replaceHTML );
}

$content = str_replace( $search, $replace, $content );

return $content;
}

protected static function _get_options() {
return new scbOptions( 'bj_lazy_load_options', __FILE__, array(
'filter_content' => 'yes',
'filter_widget_text' => 'yes',
'filter_post_thumbnails' => 'yes',
'filter_gravatars' => 'yes',
'lazy_load_images' => 'yes',
'lazy_load_iframes' => 'yes',
'theme_loader_function' => 'wp_footer',
'placeholder_url' => '',
'skip_classes' => '',
'load_hidpi' => 'no',
'load_responsive' => 'no',
'disable_on_wptouch' => 'yes',
'disable_on_mobilepress' => 'yes',
'infinite_scroll' => 'no',
'threshold' => '200'
'filter_content' => 'yes',
'filter_sidebar_widgets' => 'yes',
'filter_post_thumbnails' => 'yes',
'filter_gravatars' => 'yes',
'lazy_load_images' => 'yes',
'lazy_load_iframes' => 'yes',
'lazy_load_twitter_timeline' => 'yes',
'theme_loader_function' => 'wp_footer',
'placeholder_url' => '',
'skip_classes' => '',
'load_hidpi' => 'no',
'load_responsive' => 'no',
'disable_on_wptouch' => 'yes',
'disable_on_mobilepress' => 'yes',
'infinite_scroll' => 'no',
'threshold' => '200'
) );
}

Expand Down Expand Up @@ -310,6 +342,28 @@ static function has_mobilepress() {
return false;
}

function observe_sidebar() {
if ( !is_admin() ) {
// Only start observing output if we're not in the admin area
ob_start();
}
}

function stop_observing_sidebar_and_output_sidebar() {
if ( !is_admin() ) {
// Only work with observer if we're not in the admin area

// Get the sidebar content and shut down the observer
$sidebar_contents = ob_get_clean();

// Process the sidebar content for iframes and images
$sidebar_contents = $this->filter($sidebar_contents);

// Output the sidebar content
echo $sidebar_contents;
}
}

}
}

Expand Down
11 changes: 10 additions & 1 deletion js/bj-lazy-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ var BJLL = BJLL || {};
$el.attr( 'data-lazy-src' )
)
);
} else if ( data_lazy_type == 'twitter' ) {
$el.replaceWith(
bj_lazy_load_base64_decode(
$el.attr( 'data-lazy-src' )
)
);
if (typeof twttr !== "undefined") {
twttr.widgets.load();
}
}
}).addClass( 'data-lazy-ready' );

Expand Down Expand Up @@ -115,4 +124,4 @@ var BJLL = BJLL || {};
}
$( window ).on( 'resize', function() { $( document ).trigger( 'scroll' ); } );

})(jQuery);
})(jQuery);
8 changes: 7 additions & 1 deletion js/combined.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.