<?php
/**
 * Template part for displaying featured content in the front page.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Blogbuster
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$blogbuster_front_featured_enable           = blogbuster_get_customizer_option_value( 'blogbuster_front_featured_enable' );
$blogbuster_front_featured_items            = blogbuster_get_customizer_option_value( 'blogbuster_front_featured_items' );
$front_featured_items                       = json_decode( $blogbuster_front_featured_items );
$blogbuster_front_featured_section_title    = blogbuster_get_customizer_option_value( 'blogbuster_front_featured_section_title' );

if ( false == $blogbuster_front_featured_enable || empty( $front_featured_items ) ) {
    return ;
}

?>
<div class="mt-front-featured-wrapper">

    <h2 class="mt-content-title"><?php echo esc_html( $blogbuster_front_featured_section_title ); ?></h2>

    <?php
        foreach ( $front_featured_items as $featured_item ) {
            if ( 'show' === $featured_item->item_visible ) {
                $category_slug  = $featured_item->item_category;
                $category_image = $featured_item->item_image;

                $get_category = get_category_by_slug( $category_slug );
                
                if ( $get_category ) {
    ?>
                    <div class="mt-single-featured-wrapper featured-cat-<?php echo esc_attr( $get_category->term_id ); ?>">
                        <?php if ( ! empty( $category_image ) ) { ?>
                            <figure><img src="<?php echo esc_url( $category_image ); ?>" /> </figure>
                        <?php } ?>
                        
                        <div class="featured-info-wrap">
                            <a class="item-title" href="<?php echo esc_url( get_category_link( $get_category->term_id ) ); ?>"><?php echo esc_html( $get_category->name ); ?>
                                <span class="item-count">(<?php echo esc_html( $get_category->count ); ?>)</span>
                            </a>
                        </div><!-- .featured-info-wrap -->
                    </div><!-- .mt-single-featured-wrapper -->
    <?php
                }
            }
        }
    ?>
</div><!-- .mt-front-featured-wrapper -->