<?php
/**
 * Featured News section.
 *
 * @package Rubik News
 */

$enable_featured_news = get_theme_mod( 'enable_featured_news', true );

if ( ! $enable_featured_news ) {
	return;
}

$category_id 		= absint( get_theme_mod( 'featured_news_category', 0 ) );
$show_category 		= get_theme_mod( 'featured_news_show_category', true );
$show_excerpt 		= get_theme_mod( 'featured_news_show_excerpt', true );
$excerpt_length 	= absint( get_theme_mod( 'featured_news_excerpt_length', 18 ) );
$show_author 		= get_theme_mod( 'featured_news_show_author', true );
$show_date 			= get_theme_mod( 'featured_news_show_date', true );
$show_comments 		= get_theme_mod( 'featured_news_show_comments', true );

$query_args = array(
	'post_type'           => 'post',
	'posts_per_page'      => 5,
	'post_status'         => 'publish',
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( $category_id ) {
	$query_args['cat'] = $category_id;
}

$featured_news_query = new WP_Query( $query_args );

if ( $featured_news_query->have_posts() ) :
	$post_index = 0;
	?>
	<section class="featured-news-section">
		<div class="featured-news-grid">

			<?php
			while ( $featured_news_query->have_posts() ) :
				$featured_news_query->the_post();

				$is_main_post = 0 === $post_index;
				$article_class = $is_main_post
					? 'featured-news-main'
					: 'featured-news-small';
				?>
				<article
					id="post-<?php the_ID(); ?>"
					<?php post_class( 'featured-news-card ' . $article_class ); ?>
				>
					<a
						class="featured-news-image"
						href="<?php the_permalink(); ?>"
						aria-label="<?php echo esc_attr( get_the_title() ); ?>"
					>
						<?php
						if ( has_post_thumbnail() ) {
							the_post_thumbnail(
								$is_main_post ? 'large' : 'medium_large',
								array(
									'loading' => 0 === $post_index
										? 'eager'
										: 'lazy',
								)
							);
						} else {
							?>
							<span class="featured-news-placeholder"></span>
							<?php
						}
						?>
					</a>

					<div class="featured-news-overlay"></div>

					<div class="featured-news-content">
						<?php if ( $show_category ) : ?>
							<?php
							$categories = get_the_category();

							if ( ! empty( $categories ) ) :
								?>
								<a
									class="featured-news-category"
									href="<?php echo esc_url(
										get_category_link(
											$categories[0]->term_id
										)
									); ?>"
								>
									<?php
									echo esc_html(
										$categories[0]->name
									);
									?>
								</a>
							<?php endif; ?>
						<?php endif; ?>

						<?php
						if ( $is_main_post ) :
							?>
							<h2 class="featured-news-title">
								<a href="<?php the_permalink(); ?>">
									<?php the_title(); ?>
								</a>
							</h2>
						<?php else : ?>
							<h3 class="featured-news-title">
								<a href="<?php the_permalink(); ?>">
									<?php the_title(); ?>
								</a>
							</h3>
						<?php endif; ?>

						<?php if ( $is_main_post && $show_excerpt ) : ?>
							<div class="featured-news-excerpt">
								<?php
								echo esc_html(
									wp_trim_words(
										get_the_excerpt(),
										$excerpt_length,
										'...'
									)
								);
								?>
							</div>
						<?php endif; ?>

						<?php
						$show_meta = $show_author
							|| $show_date
							|| $show_comments;

						if ( $is_main_post && $show_meta ) :
							?>
							<div class="featured-news-meta">
								<?php if ( $show_author ) : ?>
									<span class="featured-news-author">
										<?php
										printf(
											/* translators: %s: author name. */
											esc_html__( 'By %s', 'rubik-news' ),
											esc_html(
												get_the_author()
											)
										);
										?>
									</span>
								<?php endif; ?>

								<?php if ( $show_date ) : ?>
									<span class="featured-news-date">
										<time
											datetime="<?php echo esc_attr(
												get_the_date( DATE_W3C )
											); ?>"
										>
											<?php
											echo esc_html(
												get_the_date()
											);
											?>
										</time>
									</span>
								<?php endif; ?>

								<?php if ( $show_comments ) : ?>
									<span class="featured-news-comments">
										<i
											class="far fa-comment"
											aria-hidden="true"
										></i>

										<?php
										echo esc_html(
											get_comments_number()
										);
										?>
									</span>
								<?php endif; ?>
							</div>
						<?php endif; ?>
					</div>
				</article>
				<?php
				$post_index++;
			endwhile;
			?>

		</div>
	</section>
	<?php
	wp_reset_postdata();
endif;