esc_html__( 'Small (VP)', 'visual-portfolio' ),
'vp_md' => esc_html__( 'Medium (VP)', 'visual-portfolio' ),
'vp_lg' => esc_html__( 'Large (VP)', 'visual-portfolio' ),
'vp_xl' => esc_html__( 'Extra Large (VP)', 'visual-portfolio' ),
)
);
}
/**
* Get blocked attributes to prevent our images lazy loading.
*/
public static function get_image_blocked_attributes() {
$blocked_attributes = array(
'data-skip-lazy',
'data-no-lazy',
'data-src',
'data-srcset',
'data-lazy-original',
'data-lazy-src',
'data-lazysrc',
'data-lazyload',
'data-bgposition',
'data-envira-src',
'fullurl',
'lazy-slider-img',
);
/**
* Allow plugins and themes to tell lazy images to skip an image with a given attribute.
*/
return apply_filters( 'vpf_lazyload_images_blocked_attributes', $blocked_attributes );
}
/**
* Init Lazyload
*/
public static function init_lazyload() {
// Don't lazy load for feeds, previews and admin side.
if ( is_feed() || is_preview() || is_admin() ) {
return;
}
// Don't add on AMP endpoint.
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
return;
}
// Disable using filter.
// Same filter used in `class-assets.php`.
if ( ! apply_filters( 'vpf_images_lazyload', true ) ) {
return;
}
self::$allow_vp_lazyload = ! ! Visual_Portfolio_Settings::get_option( 'lazy_loading', 'vp_images' );
self::$allow_wp_lazyload = 'full' === Visual_Portfolio_Settings::get_option( 'lazy_loading', 'vp_images' );
// Check for plugin settings.
if ( ! self::$allow_vp_lazyload && ! self::$allow_wp_lazyload ) {
return;
}
$lazyload_exclusions = Visual_Portfolio_Settings::get_option( 'lazy_loading_excludes', 'vp_images' );
if ( $lazyload_exclusions ) {
self::$lazyload_user_exclusions = explode( "\n", $lazyload_exclusions );
add_filter( 'vpf_lazyload_skip_image_with_attributes', 'Visual_Portfolio_Images::add_lazyload_exclusions', 10, 2 );
}
if ( self::$allow_wp_lazyload ) {
add_filter( 'the_content', 'Visual_Portfolio_Images::add_image_placeholders', 9999 );
add_filter( 'post_thumbnail_html', 'Visual_Portfolio_Images::add_image_placeholders', 9999 );
add_filter( 'get_avatar', 'Visual_Portfolio_Images::add_image_placeholders', 9999 );
add_filter( 'widget_text', 'Visual_Portfolio_Images::add_image_placeholders', 9999 );
add_filter( 'get_image_tag', 'Visual_Portfolio_Images::add_image_placeholders', 9999 );
}
add_action( 'wp_kses_allowed_html', 'Visual_Portfolio_Images::allow_lazy_attributes' );
add_action( 'wp_head', 'Visual_Portfolio_Images::add_nojs_fallback' );
}
/**
* Get the URL of an image attachment.
*
* @param int $attachment_id Image attachment ID.
* @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array
* of width and height values in pixels (in that order). Default 'thumbnail'.
* @param bool $icon Optional. Whether the image should be treated as an icon. Default false.
*
* @return string|false Attachment URL or false if no image is available.
*/
public static function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
$mime_type = get_post_mime_type( $attachment_id );
// Prevent usage of resized GIFs, since GIFs animated only in full size.
if ( $mime_type && 'image/gif' === $mime_type ) {
$size = 'full';
}
return wp_get_attachment_image_url( $attachment_id, $size, $icon );
}
/**
* Allow attributes of Lazy Load for wp_kses.
*
* @param array $allowed_tags The allowed tags and their attributes.
*
* @return array
*/
public static function allow_lazy_attributes( $allowed_tags ) {
if ( ! isset( $allowed_tags['img'] ) ) {
return $allowed_tags;
}
// But, if images are allowed, ensure that our attributes are allowed!
$img_attributes = array_merge(
$allowed_tags['img'],
array(
'data-src' => 1,
'data-sizes' => 1,
'data-srcset' => 1,
'data-no-lazy' => 1,
'loading' => 1,
)
);
$allowed_tags['img'] = $img_attributes;
return $allowed_tags;
}
/**
* Fix img src attribute correction in wp_kses.
*
* @param array $protocols protocols array.
*
* @return array
*/
public static function kses_allowed_protocols( $protocols ) {
$protocols[] = 'data';
return $protocols;
}
/**
* Add image placeholders.
*
* @param string $content Content.
* @return string
*/
public static function add_image_placeholders( $content ) {
// This is a pretty simple regex, but it works.
//
// 1. Find tags
// 2. Exclude tags, placed inside