<?php
if ( ! get_theme_mod( 'zenith_blog_enable_list_posts_section', false ) ) {
	return;
}

$content_ids  = array();
$content_type = get_theme_mod( 'zenith_blog_list_posts_content_type', 'post' );

if ( $content_type === 'post' ) {

	for ( $i = 1; $i <= 6; $i++ ) {
		$content_ids[] = get_theme_mod( 'zenith_blog_list_posts_content_post_' . $i );
	}

	$args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 6 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $content_ids ) ) ) {
		$args['post__in'] = array_filter( $content_ids );
		$args['orderby']  = 'post__in';
	} else {
		$args['orderby'] = 'date';
	}
} else {
	$cat_content_id = get_theme_mod( 'zenith_blog_list_posts_content_category' );
	$args           = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 6 ),
	);
}

$args = apply_filters( 'zenith_blog_list_posts_section_args', $args );

zenith_blog_render_list_posts_section( $args );

/**
 * Render List Posts Section.
 */
function zenith_blog_render_list_posts_section( $args ) {
	$query = new WP_Query( $args );
	if ( $query->have_posts() ) :

		$section_title    = get_theme_mod( 'zenith_blog_list_posts_title', __( 'List Posts', 'zenith-blog' ) );
		$section_subtitle = get_theme_mod( 'zenith_blog_list_posts_subtitle', '' );
		?>

		<section id="zenith_blog_list_posts_section" class="zenith-blog-frontpage-section zenith-blog-grid-list-section style-1">
			<?php
			if ( is_customize_preview() ) :
				zenith_blog_section_link( 'zenith_blog_list_posts_section' );
			endif;
			?>
			<div class="ascendoor-wrapper">
				<?php if ( ! empty( $section_title || $section_subtitle ) ) : ?>
					<div class="section-header">
						<h3 class="section-title"><?php echo esc_html( $section_title ); ?></h3>
						<p class="section-subtitle"><?php echo esc_html( $section_subtitle ); ?></p>
					</div>
				<?php endif; ?>
				<div class="zenith-blog-section-body">
					<div class="zenith-blog-grid-list-section-wrapper">
						<?php
						while ( $query->have_posts() ) :
							$query->the_post();
							?>
							<div class="blog-post-single <?php echo esc_attr( has_post_thumbnail() ? 'has-post-thumbnail' : '' ); ?> small-list-design">
								<?php if ( has_post_thumbnail() ) { ?>
									<div class="blog-img">
										<a class="post-thumbnail" href="<?php the_permalink(); ?>">
											<?php the_post_thumbnail( 'post-thumbnail' ); ?>
										</a>
									</div>
									<div class="blog-details">
									<?php } ?>
									<div class="mag-post-category">
										<?php zenith_blog_categories_list(); ?>
									</div>
									<h3 class="mag-post-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<div class="mag-post-meta">
										<?php
										zenith_blog_posted_by();
										zenith_blog_posted_on();
										?>
										<span class="post-views">
											<i class="far fa-eye"></i>
											<?php echo getPostViews( get_the_ID() ); ?>
										</span>
									</div>
									<?php if ( has_post_thumbnail() ) { ?>
									</div>
								<?php } ?>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			</div>
		</section>

		<?php
	endif;
}
