<?php
/**
* Images functions
*
* @package Katerio
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* -----------------------------------------------------------------------------
* Check Lazyload
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_lazyload_check' ) ) {
function th90_lazyload_check() {
// Check if we are in an AMP page
if ( th90_is_amp() ) {
return;
}
return true;
}
}
/**
* -----------------------------------------------------------------------------
* Max scrset image width
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_max_srcset_image_width' ) ) {
function th90_max_srcset_image_width() {
return 1920;
}
}
add_filter( 'max_srcset_image_width', 'th90_max_srcset_image_width', 10 );
/**
* -----------------------------------------------------------------------------
* Image Ratio
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_image_ratio' ) ) {
function th90_image_ratio( $thumb = 'post-thumbnail' ) {
if ( strpos( $thumb, '1_1' ) !== false ) {
$img_ratio = '100';
} elseif ( strpos( $thumb, '16_9' ) !== false ) {
$img_ratio = '56';
} elseif ( strpos( $thumb, '3_2' ) !== false ) {
$img_ratio = '67';
} elseif ( strpos( $thumb, '4_3' ) !== false ) {
$img_ratio = '75';
} elseif ( strpos( $thumb, '2_1' ) !== false ) {
$img_ratio = '50';
} elseif ( strpos( $thumb, '4_5' ) !== false ) {
$img_ratio = '125';
} elseif ( strpos( $thumb, '2_3' ) !== false ) {
$img_ratio = '150';
} elseif ( strpos( $thumb, '3_4' ) !== false ) {
$img_ratio = '133';
} else {
$img_ratio = 'custom';
}
return $img_ratio;
}
}
/**
* -----------------------------------------------------------------------------
* Post thumbnail URL
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_post_thumbnail_url' ) ) {
function th90_post_thumbnail_url( $thumb = 'large' ) {
if ( has_post_thumbnail() && get_the_post_thumbnail() ) {
return wp_get_attachment_image_src( get_post_thumbnail_id(), $thumb )[0];
}
}
}
/**
* -----------------------------------------------------------------------------
* Post thumbnail
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_post_thumbnail' ) ) {
function th90_post_thumbnail( $thumb_ratio = 'ori', $args = array() ) {
$args_default = array(
'first_cat' => false,
'trending' => false,
'format_icon' => true,
'format_pos' => 'center',
'first_cat_style' => 'text',
'first_cat_position' => 'thumbnail',
'before' => '',
'after' => '',
'post_number' => '',
'is_link' => true,
'block_style' => '',
);
$args = wp_parse_args( $args, $args_default );
if ( get_the_post_thumbnail() ) {
echo wp_kses_post( $args['before'] );
if ( $args['is_link'] ) {
echo '<a class="src-'. esc_attr( $thumb_ratio ) .'" href="' . esc_url( get_permalink() ) . '" title="' . esc_attr( the_title_attribute( 'echo=0' ) ) . '">';
}
if ( 'custom' == th90_image_ratio( $thumb_ratio ) ) {
$image_id = get_post_thumbnail_id();
$image = wp_get_attachment_image_src( $image_id, 'full' );
$ratio_value = ( $image[2] / $image[1] ) * 100;
echo '<div class="thumb-container" style="padding-bottom:' . $ratio_value . '%;">';
} else {
echo '<div class="thumb-container thumb-'. esc_attr( th90_image_ratio( $thumb_ratio ) ) .'">';
}
echo get_the_post_thumbnail();
echo '</div>';
if ( $args['post_number'] ) {
echo '<div class="thumb-count">' . th90_number_post_parse( $args['post_number'] ) . '</div>';
}
if ( $args['is_link'] ) {
echo '</a>';
}
if ( $args['format_icon'] ) {
th90_post_format_icon( $args['format_pos'] );
}
if ( $args['first_cat'] ) {
echo '<span class="cat-thumb cat-thumb-' . $args['first_cat_style'] . ' first-cat-' . $args['first_cat_position'] . '">';
echo th90_get_category( $args['first_cat_style'], th90_opt( 'post_first_cat_only' ) );
echo '</span>';
}
echo wp_kses_post( $args['after'] );
}
}
}
/**
* -----------------------------------------------------------------------------
* Single Featured Image
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_single_featured_image' ) ) {
function th90_single_featured_image( $have_featured_image = true ) {
if( ! $have_featured_image && ! has_post_format( 'video' ) && ! has_post_format( 'audio' ) && ! has_post_format( 'gallery' ) ) {
return;
}
$video = th90_field_single( 'video_embed' );
if ( ! $have_featured_image && has_post_format( 'video' ) && $video && th90_check_youtube_vimeo( $video ) ) {
return;
}
if ( is_singular( 'post' ) ) {
$thumb_ratio = th90_opt_override( 'override_post_layout', 'post_featured_ratio' );
} else {
$thumb_ratio = 'ori';
}
$wrap_before = '<div class="entry-featured">';
$wrap_after = '</div>';
if ( has_post_format( 'video' ) && th90_field_single( 'video_embed' ) ) {
echo wp_kses_post( $wrap_before );
th90_video_featured();
echo wp_kses_post( $wrap_after );
} elseif ( has_post_format( 'audio' ) && th90_field_single( 'audio_embed' ) ) {
echo wp_kses_post( $wrap_before );
th90_audio_featured();
echo wp_kses_post( $wrap_after );
} elseif ( has_post_format( 'gallery' ) && ! empty( th90_field_single( 'post_gallery' ) ) ) {
echo wp_kses_post( $wrap_before );
th90_gallery();
echo wp_kses_post( $wrap_after );
} else {
if ( has_post_thumbnail() ) {
echo wp_kses_post( $wrap_before );
echo '<div class="media-holder">';
echo '<a class="venobox" href="' . wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' )[0] . '">';
if ( 'custom' == th90_image_ratio( $thumb_ratio ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
$ratio_value = ( $image[2] / $image[1] ) * 100;
echo '<div class="thumb-container" style="padding-bottom:' . $ratio_value . '%;">';
} else {
echo '<div class="thumb-container thumb-'. esc_attr( th90_image_ratio( $thumb_ratio ) ) .'">';
}
the_post_thumbnail( 'large' );
echo '</div>';
echo '</a>';
echo '</div>';
echo wp_kses_post( $wrap_after );
}
}
}
}
/**
* -----------------------------------------------------------------------------
* Single Featured Image Box
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_single_featured_image_box' ) ) {
function th90_single_featured_image_box() {
$thumb_ratio = th90_opt_override( 'override_post_layout', 'post_featured_ratio' );
$classes = array(
'entry-featured',
'featured-box',
'no-image-effect',
th90_opt_override( 'override_post_layout', 'post_title_vcenter' ) ? 'box-vertical-center' : '',
);
?>
<div class="<?php echo esc_attr( implode( ' ', array_filter( $classes ) ) ); ?>">
<?php
if ( 'custom' == th90_image_ratio( $thumb_ratio ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
$ratio_value = ( $image[2] / $image[1] ) * 100;
echo '<div class="bg-dark box-holder" style="padding-bottom:' . $ratio_value . '%;">';
} else {
echo '<div class="bg-dark box-holder thumb-'. esc_attr( th90_image_ratio( $thumb_ratio ) ) .'">';
}
?>
<div class="box-holder-img">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'large' );
}
?>
</div>
<?php
// Youtube & Vimeo Bg
$video = th90_field_single( 'video_embed' );
if ( has_post_format( 'video' ) && $video && th90_check_youtube_vimeo( $video ) ) {
echo '<div class="box-holder-video">';
th90_video_bg( $video );
echo '</div>';
}
?>
<div class="post-desc">
<div class="post-desc-inner">
<?php get_template_part( 'template-parts/article/article', 'title', array( 'show' => 'only_title' ) ); ?>
</div>
</div>
</div>
</div>
<?php
}
}
/**
* -----------------------------------------------------------------------------
* Article Gallery
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_gallery' ) ) {
function th90_gallery() {
$wrap_before = '<div class="block-slider media-holder post-gallery nav-hover nav-bottom_right">';
$wrap_after = '</div>';
$gallery = array();
if ( ! empty( th90_field_single( 'post_gallery' ) ) ) {
$gallery = th90_field_single( 'post_gallery' );
}
$gallery_columns = th90_field_single( 'gallery_columns' );
$gallery_spaces = th90_field_single( 'gallery_spaces' );
$gallery_autoplay = th90_field_single( 'gallery_autoplay' );
$thumb_ratio = th90_field_single( 'gallery_ratio' );
if ( th90_is_amp() ) {
?>
<amp-carousel loop width="450" height="300" layout="responsive" type="slides" role="region">
<?php
foreach ( $gallery as $gallery_image ) {
if ( 'custom' == th90_image_ratio( $thumb_ratio ) ) {
$image = wp_get_attachment_image_src( $gallery_image['ID'], 'full' );
$image_ratio = ( $image[2] / $image[1] ) * 100;
echo '<div class="thumb-container" style="padding-bottom:' . $image_ratio . '%;">';
} else {
echo '<div class="thumb-container thumb-'. esc_attr( th90_image_ratio( $thumb_ratio ) ) .'">';
}
wp_get_attachment_image( $gallery_image['ID'], 'large' );
echo '</div>';
}
?>
</amp-carousel>
<?php
} else {
/* Slider Atts */
$slider_atts = array(
'class' => 'th90-slider',
'id' => 'fgallery-' . get_the_ID(),
'dir' => is_rtl() ? 'rtl' : 'ltr',
);
if ( ! empty( $gallery ) ) {
/* Slider Config */
$slider_config = array(
'navigation' => true,
'autoplay' => $gallery_autoplay,
'delay' => 3000,
'autoHeight' => true,
'responsive' => true,
'slidesPerView' => $gallery_columns['desktop'],
'tSlidesPerView' => $gallery_columns['tablet'],
'mSlidesPerView' => $gallery_columns['mobile'],
'spaceBetween' => $gallery_spaces['desktop'],
'tSpaceBetween' => $gallery_spaces['tablet'],
'mSpaceBetween' => $gallery_spaces['mobile'],
);
echo wp_kses_post( $wrap_before );
?>
<div <?php echo th90_stringify_attributes( $slider_atts ); ?> data-settings='<?php echo esc_attr( wp_json_encode( th90_slider_config_default( $slider_config ) ) ); ?>'>
<div class="swiper-container">
<div class="swiper-wrapper">
<?php
foreach ( $gallery as $gallery_image ) {
echo '<div class="swiper-slide">';
echo '<a class="venobox" data-gall="post-' . get_the_ID() . '" href="'.wp_get_attachment_image_src( $gallery_image['ID'], 'full' )[0].'">';
if ( 'custom' == th90_image_ratio( $thumb_ratio ) ) {
$image = wp_get_attachment_image_src( $gallery_image['ID'], 'full' );
$image_ratio = ( $image[2] / $image[1] ) * 100;
echo '<div class="thumb-container" style="padding-bottom:' . $image_ratio . '%;">';
} else {
echo '<div class="thumb-container thumb-'. esc_attr( th90_image_ratio( $thumb_ratio ) ) .'">';
}
th90_the_add_lazyload( wp_get_attachment_image( $gallery_image['ID'], 'large' ), $gallery_image['ID'] );
echo '</div>';
echo '</a>';
echo '</div>';
}
?>
</div>
</div>
</div>
<?php
echo '<div class="swiper-nav">';
echo '<div class="slider-arrow swiper-arrow swiper-prev swiper-prev-fgallery-' . get_the_ID() . '"></div>';
echo '<div class="slider-arrow swiper-arrow swiper-next swiper-next-fgallery-' . get_the_ID() . '"></div>';
echo '</div>';
echo wp_kses_post( $wrap_after );
}
}
}
}
/**
* -----------------------------------------------------------------------------
* Article Audio
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_audio_featured' ) ) {
function th90_audio_featured() {
$wrap_before = '<div class="media-holder">';
$wrap_after = '</div>';
$embed_code = th90_field_single( 'audio_embed' );
if ( $embed_code ) {
echo wp_kses_post( $wrap_before );
th90_the_add_lazyload_iframe( $embed_code );
echo wp_kses_post( $wrap_after );
}
}
}
/**
* -----------------------------------------------------------------------------
* Article Video
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_video_featured' ) ) {
function th90_video_featured() {
$wrap_before = '<div class="media-holder">';
$wrap_after = '</div>';
$embed_code = th90_field_single( 'video_embed' );
if ( $embed_code ) {
echo wp_kses_post( $wrap_before );
echo '<div class="video-container">';
th90_the_add_lazyload_iframe( $embed_code );
echo '</div>';
echo wp_kses_post( $wrap_after );
}
}
}
/**
* -----------------------------------------------------------------------------
* Get URL src of embed
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_get_url_embed' ) ) {
function th90_get_url_embed( $embed ) {
if ( $embed ) {
preg_match('/src=(["\'])(.*?)\1/', $embed, $match);
return $match[2];
}
return;
}
}
/**
* -----------------------------------------------------------------------------
* Check iframe is youtube or vimeo
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_check_youtube_vimeo' ) ) {
function th90_check_youtube_vimeo( $embed ) {
if ( $embed ) {
if ( strpos( th90_get_url_embed( $embed ), 'yout' ) !== false || strpos( th90_get_url_embed( $embed ), 'vim' ) !== false ) {
return true;
}
}
return false;
}
}
/**
* -----------------------------------------------------------------------------
* Build Youtube URL
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_build_youtube_url' ) ) {
function th90_build_youtube_url( $url, $args = array() ) {
// Defaults ----------
$args = wp_parse_args( $args, array(
'autoplay' => 1,
'loop' => 1,
'modestbranding' => 0,
'rel' => 0,
'controls' => 0,
'showinfo' => 0,
'mute' => 1,
));
$matches = array();
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $matches);
if ( ! empty( $matches ) ) {
return add_query_arg( $args, 'https://www.youtube-nocookie.com/embed/' . $matches[1] );
}
return;
}
}
/**
* -----------------------------------------------------------------------------
* Build Vimeo URL
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_build_vimeo_url' ) ) {
function th90_build_vimeo_url( $url, $args = array() ) {
// Defaults ----------
$args = wp_parse_args( $args, array(
'autoplay' => 1,
'loop' => 1,
'byline' => 0,
'title' => 0,
'background' => 1,
'dnt' => 1,
'muted' => 1,
));
$matches = array();
preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/video_url\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $matches);
if ( ! empty( $matches ) ) {
return add_query_arg( $args, 'https://player.vimeo.com/video/' . $matches[3] );
}
return;
}
}
/**
* -----------------------------------------------------------------------------
* Build video iframe
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_build_video_iframe' ) ) {
function th90_build_video_iframe( $url ) {
return '<iframe width="560" height="315" src="' . esc_url( $url ) . '" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
}
}
if ( ! function_exists( 'th90_video_bg' ) ) {
function th90_video_bg( $iframe ) {
$url_result = '';
$url_iframe = th90_get_url_embed( $iframe );
if ( strpos( $url_iframe, 'yout' ) !== false ) {
$url_result = th90_build_youtube_url( $url_iframe );
} elseif ( strpos( $url_iframe, 'vim' ) !== false ) {
$url_result = th90_build_vimeo_url( $url_iframe );
}
if ( $url_result ) {
echo '<div class="video-container">';
th90_the_add_lazyload_iframe( th90_build_video_iframe( $url_result ) );
echo '</div>';
}
}
}
/**
* -----------------------------------------------------------------------------
* Cat thumbnail
* -----------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_cat_thumbnail' ) ) {
function th90_cat_thumbnail( $thumb_ratio = 'ori', $image_id = null, $size = 'thumbnail', $before= '', $after= '' ) {
if ( $image_id ) {
echo wp_kses_post( $before );
$image = wp_get_attachment_image_src( $image_id, 'full' );
if ( 'custom' == th90_image_ratio( $thumb_ratio ) ) {
$ratio_value = ( $image[2] / $image[1] ) * 100;
echo '<div class="thumb-container" style="padding-bottom:' . $ratio_value . '%;">';
} else {
echo '<div class="thumb-container thumb-'. esc_attr( th90_image_ratio( $thumb_ratio ) ) .'">';
}
th90_the_add_lazyload( wp_get_attachment_image( $image_id, $size ), $image_id );
echo '</div>';
echo wp_kses_post( $after );
}
}
}