<?php
/**
 * Template part for displaying front page introduction.
 *
 * @package Adore News
 */

// Posts Grid Section.
$posts_grid_section = get_theme_mod( 'adore_news_posts_grid_section_enable', false );

if ( false === $posts_grid_section ) {
	return;
}

$content_ids = array();

$posts_grid_content_type = get_theme_mod( 'adore_news_posts_grid_content_type', 'post' );

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

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

	$args = array(
		'post_type'           => $posts_grid_content_type,
		'post__in'            => array_filter( $content_ids ),
		'orderby'             => 'post__in',
		'posts_per_page'      => absint( 3 ),
		'ignore_sticky_posts' => true,
	);

} else {
	$cat_content_id = get_theme_mod( 'adore_news_posts_grid_category' );
	$args           = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 3 ),
	);
}

$query = new WP_Query( $args );
if ( $query->have_posts() ) {

	$section_title    = get_theme_mod( 'adore_news_posts_grid_title', __( 'Latest Posts', 'adore-news' ) );
	$view_all_btn     = get_theme_mod( 'adore_news_posts_grid_view_all_button_label', __( 'View All', 'adore-news' ) );
	$view_all_btn_url = get_theme_mod( 'adore_news_posts_grid_view_all_button_url', '#' );
	$read_more_btn    = get_theme_mod( 'adore_news_posts_grid_read_more_button_label', __( 'Read More', 'adore-news' ) );
	?>
	<div id="adore_news_posts_grid_section" class="frontpage post-grid-section">
		<div class="theme-wrapper">
			<div class="widget-header">
				<?php if ( ! empty( $section_title ) ) : ?>
					<h3 class="widget-title"><?php echo esc_html( $section_title ); ?></h3>
				<?php endif; ?>
				<?php if ( ! empty( $view_all_btn ) ) : ?>
				<a href="<?php echo esc_url( $view_all_btn_url ); ?>" class="adore-view-all"><?php echo esc_html( $view_all_btn ); ?></a>
				<?php endif; ?>
			</div>
			<div class="post-grid-wrapper">
				<?php
				while ( $query->have_posts() ) :
					$query->the_post();
					?>
					<div class="post-item post-grid">
						<div class="post-item-image">
							<a href="<?php the_permalink(); ?>">
								<?php the_post_thumbnail( 'post-thumbnail' ); ?>
							</a>
						</div>
						<div class="post-item-content">
							<div class="entry-cat">
								<?php the_category( '', '', get_the_ID() ); ?>
							</div>
							<h3 class="entry-title">
								<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
							</h3>  
							<ul class="entry-meta">
								<li class="post-author"> <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php echo esc_html( get_the_author() ); ?></a></li>
								<li class="post-date"> <span class="line"></span><?php echo esc_html( get_the_date() ); ?></li>
							</ul>
							<div class="post-exerpt">
								<p><?php echo wp_kses_post( wp_trim_words( get_the_content(), 15 ) ); ?></p>
							</div>
							<?php if ( ! empty( $read_more_btn ) ) { ?>
								<div class="post-btn">
									<a href="<?php the_permalink(); ?>" class="btn-read-more"><?php echo esc_html( $read_more_btn ); ?><i></i></a>
								</div>
							<?php } ?>
						</div>
					</div>
					<?php
				endwhile;
				wp_reset_postdata();
				?>
			</div>
		</div>
	</div>

	<?php
}
