<?php
/**
 * Post template: List
 */

?>
<?php

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'cookandmeal-blog-thumb-grid');

if(has_post_thumbnail( $post->ID )) {
    $image_bg ='background-image: url('.esc_url($image[0]).');';
    $post_class = '';
}
else {
    $image_bg = '';
    $post_class = ' cookandmeal-post-no-image';
}

$categories_list = cookandmeal_get_the_category_list( $post->ID );

// Show post format
$current_post_format = get_post_format($post->ID) ? get_post_format($post->ID) : 'standard';
$post_format_icon = '';

if(in_array($current_post_format, cookandmeal_get_mediaformats())) {
    $post_format_icon = '<div class="cookandmeal-post-format-icon"></div>';
}

$post_class .= ' format-'.$current_post_format;

echo '<div class="cookandmeal-list-post cookandmeal-post'.esc_attr($post_class).'"'.cookandmeal_add_aos(false).'>';

if(has_post_thumbnail( $post->ID )) {
    echo '<div class="cookandmeal-post-image-wrapper">';

    // Background video
    if($current_post_format == 'video' && get_theme_mod('blog_posts_video_bg', true)) {

        $post_embed_video = get_post_meta( $post->ID, '_cookandmeal_video_embed', true );

        if(!empty($post_embed_video) && strpos($post_embed_video, 'youtu') !== false) {
            echo '<div class="cookandmeal-youtube-video-bg" data-youtube="'.esc_url($post_embed_video).'"></div>';

            $image_bg = '';
        }
    }

    if(get_theme_mod('blog_posts_review', true)) {
        do_action('cookandmeal_post_review_rating'); // this action called from plugin
    }

    echo '<a href="'.esc_url(get_permalink($post->ID)).'"><div class="cookandmeal-post-image" data-style="'.esc_attr($image_bg).'">'.wp_kses_post($post_format_icon).'</div></a></div>';
}

// Post details
echo '<div class="cookandmeal-post-details">';

echo '<div class="post-categories">'.wp_kses($categories_list, cookandmeal_esc_data()).'</div>';

echo '<h3 class="post-title entry-title"><a href="'.esc_url(get_permalink($post->ID)).'">'.wp_kses_post($post->post_title).'</a></h3>';
if(get_theme_mod('blog_posts_author', false)):
?>
<div class="post-author">
    <span class="vcard">
        <?php echo esc_html__('By', 'cookandmeal'); ?> <span class="fn"><?php echo get_the_author_posts_link(); ?></span>
    </span>
</div>
<div class="post-info-dot"></div>
<?php
endif;
?>
<div class="post-date"><?php echo cookandmeal_get_post_date($post->ID); ?></div>
<?php if(get_theme_mod('blog_posts_read_time', true)): ?>
<div class="post-info-dot"></div>
<div class="post-read-time"><?php echo cookandmeal_get_post_read_time($post->ID); ?></div>
<?php endif; ?>
<?php if(get_theme_mod('blog_posts_excerpt', 'excerpt') !== 'none'): ?>
<?php echo '<div class="post-excerpt">';

echo wp_kses_post( cookandmeal_get_the_excerpt($post->ID) );

?>
<div class="post-readmore"><a href="<?php the_permalink(); ?>" class="more-link btn"><?php esc_html_e('Read more', 'cookandmeal'); ?></a>
</div>

</div>
<?php endif; // after post-excerpt ?>
</div>

</div>
