<?php
    $popular_title       = magazine_bell_get_option( 'popular_title' );
    $popular_subtitle       = magazine_bell_get_option( 'popular_subtitle' );
    $popular_content_type     = magazine_bell_get_option( 'popular_content_type' );
    $enable_category     = magazine_bell_get_option( 'popular_category_enable' );
    $enable_content     = magazine_bell_get_option( 'popular_content_enable' );
    $enable_author     = magazine_bell_get_option( 'popular_author_enable' );
    $enable_posted_on     = magazine_bell_get_option( 'popular_posted_on_enable' );
    $number_of_popular_items  = magazine_bell_get_option( 'number_of_popular_items' );
    $popular_category = magazine_bell_get_option( 'popular_category' );
    $header_font_size = magazine_bell_get_option( 'popular_font_size');
    $number_of_popular_column = magazine_bell_get_option('number_of_popular_column');
    $content_align = magazine_bell_get_option('popular_content_align');
    $excerpt_length =magazine_bell_get_option( 'popular_excerpt_length');
    $popular_post_count =magazine_bell_get_option( 'popular_post_count_enable');

    $see_more_txt     = magazine_bell_get_option( 'popular_see_all_txt' );
    $see_more_url     = magazine_bell_get_option( 'popular_see_all_url' );

    for( $i=1; $i<=$number_of_popular_items; $i++ ) :
        $popular_page_posts[] = absint(magazine_bell_get_option( 'popular_page_'.$i ) );
        $popular_post_posts[] = absint(magazine_bell_get_option( 'popular_post_'.$i ) );
    endfor;

?> 
<style>
    <?php if ($header_font_size != 0): ?>
        #popular .entry-title{
            font-size:<?php echo esc_attr($header_font_size); ?>px;
        }
    <?php endif ?>
</style>
<?php if( !empty($popular_title) ):?>
    <div class="section-header">
        <?php if (!empty($popular_title)): ?>
            <h2 class="section-title"><?php echo esc_html($popular_title);?></h2>
        <?php endif; ?>
    </div>       
<?php endif;?> 
<div class="must-read-wrapper clear col-2">
    <?php 
        $args = array (
            'posts_per_page' =>absint( $number_of_popular_items ),              
            'post_type' => 'post',
            'post_status' => 'publish',
            'paged' => 1,
            'ignore_sticky_posts' => true, 
            );
            if ( absint( $popular_category ) > 0 ) {
                $args['cat'] = absint( $popular_category );
            }
        $loop = new WP_Query($args);                        
        if ( $loop->have_posts() ) :
            $i=0;  
            while ($loop->have_posts()) : $loop->the_post(); $i++;?>      
            <?php $col_class='';
            if ($i==1) {
                 $col_class='full-width';
             } else{
                $col_class='half-width';
             } ?>  
                <article class="<?php echo $col_class; ?> <?php echo has_post_thumbnail() ? 'has-post-thumbnail' : 'no-post-thumbnail' ; ?>">
                    <?php if ($popular_post_count==true && !has_post_thumbnail()): ?>
                        <span class="post-num"><?php echo $i; ?></span>
                    <?php endif ?>
                    <div class="popular-item-wrapper">
                        <?php if (has_post_thumbnail()) : ?>
                            <div class="post-featured-image">
                                <div class="featured-image" style="background-image: url('<?php the_post_thumbnail_url();?>');">
                                    <a href="<?php the_permalink();?>" class="post-thumbnail-link"></a>
                                    <?php $homepage_video_url = get_post_meta( get_the_ID(), 'magazine-bell-video-url', true ); ?>
                                    <?php if (!empty($homepage_video_url)): ?>
                                       <a href="<?php the_permalink();?>"> <div class="homepage-video-icon"><i class="fa fa-play"></i></div></a>
                                    <?php endif ?>
                                    <?php if ($i==1): ?>
                                        <div class="overlay"></div>
                                    <?php endif ?>
                                    <?php if ($popular_post_count==true): ?>
                                        <span class="post-num"><?php echo $i; ?></span>
                                    <?php endif ?>
                                </div><!-- .featured-image -->
                            </div>
                        <?php endif; ?>
                        <div class="entry-container <?php echo esc_attr($content_align); ?>">
                            <?php if ( ($popular_content_type !== 'popular_page') && ($enable_category==true)) : ?>
                                <div class="entry-meta">
                                    <?php magazine_bell_entry_meta(); ?>
                                </div><!-- .entry-meta -->
                            <?php endif; ?>
                            <header class="entry-header">
                                <h2 class="entry-title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
                            </header>
                            <?php if ((($enable_posted_on==true) || ($enable_author==true)) && ($i==1)) : ?>
                                <div class="entry-meta">
                                    <?php 
                                        if (($enable_posted_on==true)) {
                                            magazine_bell_posted_on();
                                        } 
                                        if (($enable_author==true)) {
                                            magazine_bell_author();
                                        }
                                     ?>
                                </div><!-- .entry-meta -->
                            <?php endif; ?>
                            <?php if (($enable_content==true)) : ?>
                                <div class="entry-content">
                                    <?php if ($i==1) {
                                            $excerpt = magazine_bell_the_excerpt( $excerpt_length );
                                            echo wp_kses_post( wpautop( $excerpt ) );
                                        } else{
                                            $excerpt = magazine_bell_the_excerpt(5);
                                            echo wp_kses_post( wpautop( $excerpt ) );
                                        }
                                    ?>
                                </div><!-- .entry-content -->
                            <?php endif; ?>  
                            <?php if ((($enable_posted_on==true) || ($enable_author==true)) && ($i!=1)) : ?>
                                <div class="entry-meta">
                                    <?php 
                                        if (($enable_posted_on==true)) {
                                            magazine_bell_posted_on();
                                        }
                                     ?>
                                </div><!-- .entry-meta -->
                            <?php endif; ?>
                        </div><!-- .entry-container -->
                    </div>
                </article>
            <?php endwhile;?>
        <?php endif; ?>
        <?php wp_reset_postdata(); ?>
</div>
<?php 
    //ads
    $number_of_popular_ads = magazine_bell_get_option( 'number_of_popular_ads' );
    $popular_col = magazine_bell_get_option( 'popular_column_option' ); 
    $popular_ads_enable = magazine_bell_get_option( 'disable_popular_ads_section' ); 
?>
<?php if ($popular_ads_enable==true): ?>
    <div class="site-advertisement <?php echo esc_attr($popular_col); ?>">
        <?php for ($i=1; $i <= $number_of_popular_ads ; $i++) { 
            $popular_ads_img = magazine_bell_get_option( 'popular_ads_img_'.$i ); 
            $popular_ads_url = magazine_bell_get_option( 'popular_ads_url_'.$i );
            ?>
            <span class="ads-info">
                <a target="_blank" href="<?php echo esc_url($popular_ads_url) ?>"><img  src="<?php echo esc_url($popular_ads_img) ?>"></a>
            </span>
        <?php } ?>
    </div>
<?php endif; ?>