<?php

    if ( citymag_setting('citymag_enable_trending_posts', true) == true ) :

        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 4,
            'ignore_sticky_posts' => true,
            'orderby' => citymag_setting('citymag_trending_posts_orderby', 'comment_count'),
            'order' => citymag_setting('citymag_trending_posts_sort_order', 'desc'),
        );

        if ( is_numeric(citymag_setting('citymag_trending_posts_category')) ) :
			$args['cat'] = citymag_setting('citymag_trending_posts_category');
		endif;

        $trending_posts = new WP_Query($args);
        $trending_posts_enabled = citymag_setting('citymag_enable_trending_posts_only_first_page_pagination', true);

        if (
            $trending_posts->have_posts() &&
            (
                (
                    $trending_posts_enabled == true && 
                    get_query_var('paged') <= 1
                ) ||
                $trending_posts_enabled == false
            )
    
        ) :

        $trending_articles_additional_css = ( citymag_setting('citymag_trending_posts_truncate_post_titles') == true ) ? ' trending-articles-truncate-post-titles' : '';

    ?>

        <section class="trending-articles-wrapper">

            <div class="container">
                
                <div class="row">
                    
                    <div class="col-md-12">

                        <div class="trending-articles-section">
                            
                            <?php if ( citymag_setting('citymag_enable_trending_posts_section_title', true) == true ) : ?>

                                <h2 class="trending-articles-section-title site-section-title"><?php echo esc_html(citymag_setting('citymag_trending_posts_section_title', __( 'Trending Posts', 'citymag' ))) ;?></h2>
                            
                            <?php endif; ?>

                            <div class="trending-articles-grid <?php echo esc_attr($trending_articles_additional_css);?>">

                                <?php

                                    while ($trending_posts->have_posts()) : $trending_posts->the_post();
                                        
                                        global $post;

                                        $post_count = $trending_posts->current_post + 1;
                                        $image_layout = citymag_setting('citymag_trending_posts_images_layout', 'square');

                                ?>

                                        <div class="trending-article trending-article-<?php echo $post_count;?>">

                                            <div class="trending-article-inner <?php echo esc_attr($image_layout);?>_images">
                                                
                                                <div class="trending-article-image-link">

                                                    <a href="<?php echo esc_url(get_permalink()); ?>">

                                                        <?php

                                                            if ('' != get_the_post_thumbnail() ) : 

                                                                the_post_thumbnail('citymag_trending_image');
                                                            
                                                            else :

                                                                $thumbnailIMG = get_stylesheet_directory_uri() . '/assets/images/placeholders/trending-posts/placeholder-140x140.jpg';
                                                                echo '<img src="' . esc_url($thumbnailIMG) . '" alt="' . esc_attr(get_the_title()) . '">';
                                            
                                                            endif;
                                                        ?>
                                                        
                                                    </a>
                                                    
                                                </div>

                                                <div class="trending-article-content">
                                                    <h3><a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_the_title()); ?></a></h3>
                                                    <p class="trending-article-author"><?php echo esc_html(citymag_setting('citymag_trending_posts_post_author_label', __( 'By', 'citymag' ))) . '&nbsp;' . get_the_author_posts_link(); ?></p>
                                                </div>

                                            </div>

                                        </div>

                                <?php

                                    endwhile;
                                    wp_reset_postdata();
                                    
                                ?>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </section>

<?php

        endif;

    endif;

?>