<?php

    if ( urbamag_setting('urbamag_enable_recent_posts', true) == true ) :

        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 5,
            'ignore_sticky_posts' => true,
        );
        
        $recent_posts = new WP_Query($args);
        $recent_posts_enabled = urbamag_setting('urbamag_enable_recent_posts_only_first_page_pagination', true);
    
        if (
            $recent_posts->have_posts() &&
            (
                (
                    $recent_posts_enabled == true && 
                    get_query_var('paged') <= 1
                ) ||
                $recent_posts_enabled == false
            )
    
        ) :
    
    ?>

        <section class="recent-posts-section">

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

                        <div class="recent-posts-grid">

                            <div class="recent-posts-left-column">
                                
                                <?php

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

                                        $post_count = $recent_posts->current_post + 1;

                                        if ($post_count <= 2) :
                                
                                ?>

                                            <div class="recent-post">

                                                <a class="recent-posts-permalink" href="<?php echo esc_url(get_permalink()); ?>">

                                                    <?php

                                                        if ('' != get_the_post_thumbnail() ) : 

                                                            the_post_thumbnail('urbamag_recent_post_medium');
                                                        
                                                        else :

                                                            $thumbnailIMG = get_stylesheet_directory_uri() . '/assets/images/placeholders/recent-posts/placeholder-650x439.jpg';
                                                            echo '<img src="' . esc_url($thumbnailIMG) . '" alt="' . esc_attr(get_the_title()) . '">';
                                        
                                                        endif;
                                                    ?>

                                                </a>

                                                <p class="category"><?php the_category(' . ');?></p>
                                                <h2><a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_the_title()); ?></a></h2>
                                                <p class="author"><?php echo esc_html__('Written by ','urbamag') . get_the_author_posts_link(); ?></p>

                                            </div>
                                <?php

                                        endif;
                                        if ($post_count == 3) :
                                
                                ?>

                            </div>

                            <div class="recent-posts-center-column">

                                <a class="recent-posts-permalink" href="<?php echo esc_url(get_permalink()); ?>">

                                    <?php

                                        if ('' != get_the_post_thumbnail() ) : 

                                            the_post_thumbnail('urbamag_recent_post_large');
                                        
                                        else :

                                            $thumbnailIMG = get_stylesheet_directory_uri() . '/assets/images/placeholders/recent-posts/placeholder-700x490.jpg';
                                            echo '<img src="' . esc_url($thumbnailIMG) . '" alt="' . esc_attr(get_the_title()) . '">';
                        
                                        endif;

                                    ?>

                                </a>
                                
                                <div class="recent-posts-article-content">

                                    <p class="category"><?php the_category(' . ');?></p>
                                    <h2><a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_the_title()); ?></a></h2>
                                    <p class="author"><?php echo esc_html__('Written by ','urbamag') . get_the_author_posts_link(); ?></p>
                                    <p class="description"><?php echo urbamag_recent_post_more_tag(get_the_ID()); ?></p>

                                </div>

                            </div>

                            <div class="recent-posts-right-column">
                                
                                <?php
                                
                                    endif;
                                    if ($post_count > 3) :
                                
                                ?>

                                        <div class="recent-post">

                                            <a class="recent-posts-permalink" href="<?php echo esc_url(get_permalink()); ?>">

                                                <?php

                                                    if ('' != get_the_post_thumbnail() ) : 

                                                        the_post_thumbnail('urbamag_recent_post_medium');
                                                    
                                                    else :

                                                        $thumbnailIMG = get_stylesheet_directory_uri() . '/assets/images/placeholders/recent-posts/placeholder-650x439.jpg';
                                                        echo '<img src="' . esc_url($thumbnailIMG) . '" alt="' . esc_attr(get_the_title()) . '">';
                                    
                                                    endif;

                                                ?>

                                            </a>

                                            <p class="category"><?php the_category(' . ');?></p>
                                            <h2><a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_the_title()); ?></a></h2>
                                            <p class="author"><?php echo esc_html__('Written by ','urbamag') . get_the_author_posts_link(); ?></p>

                                        </div>

                            <?php

                                    endif;

                                endwhile;
                                wp_reset_postdata();
                                
                            ?>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </section>

<?php

        endif;


    endif;

?>