<?php
/**
 * Sticky Posts Frontpage Template
 *
 * @package PureNote
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$enable  = purenote_get_customizer_option_value( 'purenote_sticky_posts_enable' );
$cats    = purenote_get_customizer_option_value( 'purenote_sticky_posts_categories' );
$count   = purenote_get_customizer_option_value( 'purenote_sticky_posts_per_cat' );
$heading = purenote_get_customizer_option_value( 'purenote_sticky_posts_heading' );

if ( $cats === 'all' || empty( $cats ) ) {
    $cats = wp_list_pluck( get_categories(), 'term_id' );
}

$cats = (array) $cats;

if ( $enable && ! empty( $cats ) ) :

    $global_post_count = 0; 
    
?>
<div id="sticky-posts-section" class="sticky-posts-wrapper">

    <span class='divider'></span>
    <?php if ( ! empty( $heading ) ) : ?>
        <h2 class="sticky-posts-heading"><?php echo esc_html( $heading ); ?></h2>
    <?php endif; ?>

    <div id="posts" class="posts-wrapper">
        <?php foreach ( $cats as $cat_id ) :
            $category = get_category( $cat_id );
            if ( ! $category ) continue;

            $query = new WP_Query( array(
                'cat'                 => $cat_id,
                'posts_per_page'      => $count,
                'ignore_sticky_posts' => false,
            ));

            if ( $query->have_posts() ) :
                while ( $query->have_posts() ) : $query->the_post();
                    $global_post_count++;
                ?>
                <div class="sticky-post-item">
                    <div class="sticky-post-thumb">
                        <a href="<?php the_permalink(); ?>">
                            <?php
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail( 'thumbnail' );
                            } else {
                                echo '<img src="' . esc_url( get_template_directory_uri() . '/assets/images/no-thumb.jpg' ) . '" alt="">';
                            }
                            ?>
                            <span class="sticky-post-number"><?php echo $global_post_count; ?></span>
                        </a>
                    </div>
                    <div class="sticky-post-content">
                        <div class="sticky-post-cat">
                            <span><?php echo esc_html( $category->name ); ?></span>
                            <span class="sticky-post-count">(<?php echo $category->count; ?>)</span>
                        </div>
                        <h3 class="sticky-post-title">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h3>
                        <div class="sticky-post-meta">
                            <span class="sticky-post-author"><?php purenote_posted_by(); ?></span>
                            <span class="sticky-post-date"><?php purenote_posted_on(); ?></span>
                        </div>
                    </div>
                </div>
                <?php endwhile;
                wp_reset_postdata();
            endif;
        endforeach; ?>
    </div>
</div>
<?php endif; ?>
