settings = Settings::get_instance();
$this->helper = Lazy_Load_Helper::get_instance();
$this->register_action( 'wp_smush_content_transforms', array(
$this,
'register_lazy_load_transform',
), self::LAZY_LOAD_TRANSFORM_PRIORITY );
// Only run on front end and if lazy loading is enabled.
if ( is_admin() || ! $this->settings->is_module_active( 'lazy_load' ) ) {
return;
}
$this->options = $this->settings->get_setting( 'wp-smush-lazy_load' );
// Enabled without settings? Don't think so... Exit.
if ( ! $this->options ) {
return;
}
// Disable WordPress native lazy load.
$this->register_filter( 'wp_lazy_loading_enabled', array( $this, 'should_enable_wordpress_native_lazyload' ) );
// Load js file that is required in public facing pages.
$this->register_action( 'wp_head', array( $this, 'add_inline_styles' ) );
$this->register_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ), 99 );
if ( defined( 'WP_SMUSH_ASYNC_LAZY' ) && WP_SMUSH_ASYNC_LAZY ) {
$this->register_filter( 'script_loader_tag', array( $this, 'async_load' ), 10, 2 );
}
// Allow lazy load attributes in img tag.
$this->register_filter( 'wp_kses_allowed_html', array( $this, 'add_lazy_load_attributes' ) );
// Filter images.
if ( ! isset( $this->options['output']['content'] ) || ! $this->options['output']['content'] ) {
$this->register_filter( 'the_content', array( $this, 'exclude_from_lazy_loading' ), 100 );
}
if ( ! isset( $this->options['output']['thumbnails'] ) || ! $this->options['output']['thumbnails'] ) {
$this->register_filter( 'post_thumbnail_html', array( $this, 'exclude_from_lazy_loading' ), 100 );
}
if ( ! isset( $this->options['output']['gravatars'] ) || ! $this->options['output']['gravatars'] ) {
$this->register_filter( 'get_avatar', array( $this, 'exclude_from_lazy_loading' ), 100 );
}
if ( ! isset( $this->options['output']['widgets'] ) || ! $this->options['output']['widgets'] ) {
$this->register_action( 'dynamic_sidebar_before', array( $this, 'filter_sidebar_content_start' ), 0 );
$this->register_action( 'dynamic_sidebar_after', array( $this, 'filter_sidebar_content_end' ), 1000 );
}
}
/**
* Add inline styles at the top of the page for pre-loaders and effects.
*
* @since 3.2.0
*/
public function add_inline_styles() {
if ( $this->helper->should_skip_lazyload() ) {
return;
}
// Lazy load embed video styles.
$this->add_inline_embed_video_css();
// Fix for poorly coded themes that do not remove the no-js in the HTML class.
?>
options['animation']['selected'] ) || 'none' === $this->options['animation']['selected'] ) {
return;
}
// Spinner.
if ( 'spinner' === $this->options['animation']['selected'] ) {
$loader = WP_SMUSH_URL . 'app/assets/images/smush-lazyloader-' . $this->options['animation']['spinner']['selected'] . '.gif';
if ( isset( $this->options['animation']['spinner']['selected'] ) && 5 < (int) $this->options['animation']['spinner']['selected'] ) {
$loader = wp_get_attachment_image_src( $this->options['animation']['spinner']['selected'], 'full' );
$loader = $loader[0];
}
$background = 'rgba(255, 255, 255, 0)';
} else {
// Placeholder.
$loader = WP_SMUSH_URL . 'app/assets/images/smush-placeholder.png';
$background = '#FAFAFA';
if ( isset( $this->options['animation']['placeholder']['selected'] ) && 2 === (int) $this->options['animation']['placeholder']['selected'] ) {
$background = '#333333';
}
if ( isset( $this->options['animation']['placeholder']['selected'] ) && 2 < (int) $this->options['animation']['placeholder']['selected'] ) {
$loader = wp_get_attachment_image_src( (int) $this->options['animation']['placeholder']['selected'], 'full' );
// Can't find a loader on multisite? Try main site.
if ( ! $loader && is_multisite() ) {
switch_to_blog( 1 );
$loader = wp_get_attachment_image_src( (int) $this->options['animation']['placeholder']['selected'], 'full' );
restore_current_blog();
}
$loader = $loader[0];
}
if ( isset( $this->options['animation']['placeholder']['color'] ) ) {
$background = $this->options['animation']['placeholder']['color'];
}
}
// Fade in.
$fadein = isset( $this->options['animation']['fadein']['duration'] ) ? $this->options['animation']['fadein']['duration'] : 0;
$delay = isset( $this->options['animation']['fadein']['delay'] ) ? $this->options['animation']['fadein']['delay'] : 0;
?>
helper->should_lazy_load_embed_video() ) {
return;
}
?>
helper->should_skip_lazyload()
|| ( $this->helper->is_native_lazy_loading_enabled() && ! $this->helper->should_lazy_load_embed_video() )
) {
return;
}
$script = WP_SMUSH_URL . 'app/assets/js/smush-lazy-load.min.js';
$in_footer = isset( $this->options['footer'] ) ? $this->options['footer'] : true;
wp_enqueue_script(
'smush-lazy-load',
$script,
array(),
WP_SMUSH_VERSION,
$in_footer
);
$this->add_masonry_support();
if ( defined( 'WP_SMUSH_LAZY_LOAD_AVADA' ) && WP_SMUSH_LAZY_LOAD_AVADA ) {
$this->add_avada_support();
}
$this->add_divi_support();
$this->add_soliloquy_support();
}
/**
* Async load the lazy load scripts.
*
* @param string $tag The