get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
// 'caption' => $attachment->post_excerpt,
// 'description' => $attachment->post_content,
// 'href' => get_permalink( $attachment->ID ),
// 'src' => $attachment->guid,
// 'title' => $attachment->post_title
$img_post = get_post( $item['img'] );
if ( is_int( $item['img'] ) ) {
$img_post = get_post( $item['img'] );
if ( !is_wp_error( $img_post ) && is_object( $img_post ) && 'attachment' === $img_post->post_type ) {
switch( $requested_img_attr ) {
case 'caption' :
$img_attr = $img_post->post_excerpt;
break;
case 'description' :
$img_attr = $img_post->post_content;
break;
case 'title' :
$img_attr = $img_post->post_title;
break;
}
}
}
return $img_attr;
}
}
if ( !function_exists( 'Nimble\sek_slider_parse_template_tags') ) {
// fired @filter 'nimble_parse_template_tags'
function sek_slider_parse_template_tags( $val, $item = array() ) {
//the pattern could also be '!\{\{(\w+)\}\}!', but adding \s? allows us to allow spaces around the term inside curly braces
//see https://stackoverflow.com/questions/959017/php-regex-templating-find-all-occurrences-of-var#comment71815465_959026
return is_string( $val ) ? preg_replace_callback( '!\{\{\s?(\w+)\s?\}\}!', function( $matches ) use( $item ) {
return sek_slider_find_pattern_match( $matches, $item );
}, $val) : $val;
}
}
if ( !function_exists('Nimble\sek_maybe_parse_slider_img_html_for_lazyload') ) {
// @return html string
function sek_maybe_parse_slider_img_html_for_lazyload( $attachment_id, $is_first_img, $lazy_load_on, $size = 'thumbnail' ) {
// Skip when :
// - is customizing
// - slider lazy loading is not active
// - global Nimble lazy load is active, and this is the first image ( in this case we want to lazy load the first image of the slider if offscreen )
if ( skp_is_customizing() || !$lazy_load_on || ( sek_is_img_smartload_enabled() && $is_first_img ) ) {
// Nov 2020 : removes any additional styles added by a theme ( Twenty Twenty one ) or a plugin to the image
add_filter( 'wp_get_attachment_image_attributes', '\Nimble\sek_remove_image_style_attr', 999 );
$img_html = wp_get_attachment_image( $attachment_id, $size );
remove_filter( 'wp_get_attachment_image_attributes', '\Nimble\sek_remove_image_style_attr', 999 );
return $img_html;
}
// If lazy loaded, preprocess the image like wp_get_attachment_image()
// added in dec 2019 for https://github.com/presscustomizr/nimble-builder/issues/570
$html = '';
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon = false );
if ( $image ) {
list($src, $width, $height) = $image;
$hwstring = image_hwstring( $width, $height );
$size_class = $size;
if ( is_array( $size_class ) ) {
$size_class = join( 'x', $size_class );
}
$attachment = get_post( $attachment_id );
$default_attr = array(
'src' => $src,
'class' => "attachment-$size_class size-$size_class swiper-lazy",// add swiper class for lazyloading @see https://swiperjs.com/api/#lazy
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
);
$attr = $default_attr;
// Generate 'srcset' and 'sizes' if not already present.
if ( empty( $attr['srcset'] ) ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
if ( is_array( $image_meta ) ) {
$size_array = array( absint( $width ), absint( $height ) );
$srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
$sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
if ( $srcset && ( $sizes || !empty( $attr['sizes'] ) ) ) {
$attr['srcset'] = $srcset;
if ( empty( $attr['sizes'] ) ) {
$attr['sizes'] = $sizes;
}
}
}
}
/**
* Filters the list of attachment image attributes.
*
* @since 2.8.0
*
* @param array $attr Attributes for the image markup.
* @param WP_Post $attachment Image attachment post.
* @param string|array $size Requested size. Image size or array of width and height values
* (in that order). Default 'thumbnail'.
*/
// Nov 2020 : removes any additional styles added by a theme ( Twenty Twenty one ) or a plugin to the image
add_filter( 'wp_get_attachment_image_attributes', '\Nimble\sek_remove_image_style_attr', 999 );
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
remove_filter( 'wp_get_attachment_image_attributes', '\Nimble\sek_remove_image_style_attr', 999 );
// add swiper data-* stuffs for lazyloading now, after all filters
// @see https://swiperjs.com/api/#lazy
if ( !empty( $attr['srcset'] ) ) {
$attr['data-srcset'] = $attr['srcset'];
unset( $attr['srcset'] );
}
// april 22 : deactivated when implementing late escape for #885 because it breaks data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
// No idea how to escape this without breaking it for now
// if ( !empty( $attr['src'] ) ) {
// $attr['data-src'] = $attr['src'];
// $attr['src'] = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
// //unset( $attr['src'] );
// }
if ( !empty( $attr['sizes'] ) ) {
$attr['data-sek-img-sizes'] = $attr['sizes'];
unset( $attr['sizes'] );
}
$attr = array_map( 'esc_attr', $attr );
$html = rtrim( " $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
}
return $html;
}
}
if ( !function_exists( 'Nimble\sek_get_img_slider_module_img_html') ) {
function sek_get_img_slider_module_img_html( $item, $lazy_load_on, $index ) {
$html = '';
$is_first_img = 0 == $index;
if ( is_int( $item['img'] ) ) {
// don't parse the first image of the carousel for lazyloading
// @see https://github.com/presscustomizr/nimble-builder/issues/596 ( Lazy load break layout of first slide )
// if ( $lazy_load_on && !$is_first_img ) {
// $html = sek_maybe_parse_slider_img_html_for_lazyload(
// $item['img'],
// empty( $item['img-size'] ) ? 'large' : $item['img-size'],
// $is_first_img//<= // when lazy load is active, we want to lazy load the first image of the slider if offscreen by adding 'data-sek-src' attribute
// );
// $html .= '
%1$s