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

$enable_section = get_theme_mod( 'enable_latest_featured_posts', true );

if ( ! $enable_section ) {
	return;
}

$latest_title 				= get_theme_mod( 'latest_news_title', __( 'Latest News', 'rubik-news' ));
$latest_category 			= absint( get_theme_mod( 'latest_news_category', 0 ) );
$latest_posts_number 		= absint( get_theme_mod( 'latest_news_posts_number', 4 ) );
$featured_title 			= get_theme_mod( 'featured_posts_title', __( 'Featured Posts', 'rubik-news' ) );
$featured_category 			= absint( get_theme_mod( 'featured_posts_category', 0 ) );
$featured_posts_number 		= absint( get_theme_mod( 'featured_posts_number', 5 ) );

/*
 * Latest News query.
 */
$latest_args = array(
	'post_type'           => 'post',
	'posts_per_page'      => $latest_posts_number,
	'post_status'         => 'publish',
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( $latest_category ) {
	$latest_args['cat'] = $latest_category;
}

$latest_query = new WP_Query( $latest_args );

/*
 * Featured Posts query.
 */
$featured_args = array(
	'post_type'           => 'post',
	'posts_per_page'      => $featured_posts_number,
	'post_status'         => 'publish',
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( $featured_category ) {
	$featured_args['cat'] = $featured_category;
}

$featured_query = new WP_Query( $featured_args );

if ( ! $latest_query->have_posts() && ! $featured_query->have_posts() ) {
	return;
}
?>

<section class="latest-featured-section">
	<div class="latest-featured-grid">

		<?php if ( $latest_query->have_posts() ) : ?>
			<div class="latest-news-column">
				<div class="news-section-header">
					<h2 class="news-section-title">
						<?php echo esc_html( $latest_title ); ?>
					</h2>

					<span class="news-section-line"></span>

				</div><!-- .news-section-header -->

				<div class="latest-news-list">
					<?php
					while ( $latest_query->have_posts() ) :
						$latest_query->the_post();

						$categories = get_the_category();
						?>
						<article
							id="latest-post-<?php the_ID(); ?>"
							<?php post_class( 'latest-news-item' ); ?>
						>
							<div class="latest-news-thumbnail">
								<a
									href="<?php the_permalink(); ?>"
									aria-label="<?php echo esc_attr( get_the_title() ); ?>"
								>
									<?php
									if ( has_post_thumbnail() ) {
										the_post_thumbnail(
											'medium',
											array(
												'loading' => 'lazy',
											)
										);
									} else {
										?>
										<span class="latest-news-placeholder"></span>
										<?php
									}
									?>
								</a>
							</div><!-- .latest-news-thumbnail -->

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

								<h3 class="latest-news-title">
									<a href="<?php the_permalink(); ?>">
										<?php the_title(); ?>
									</a>
								</h3>

								<div class="latest-news-excerpt">
									<?php
									echo wp_kses_post(
										wp_trim_words(
											get_the_excerpt(),
											16,
											'&hellip;'
										)
									);
									?>
								</div>

								<div class="news-post-meta">
									<span class="news-post-date">
										<?php echo esc_html( get_the_date() ); ?>
									</span>

									<span class="news-meta-separator">•</span>

									<span class="news-post-time">
										<?php
										printf(
											/* translators: %s: Human-readable post time. */
											esc_html__( '%s ago', 'rubik-news' ),
											esc_html(
												human_time_diff(
													get_the_time( 'U' ),
													current_time( 'timestamp' )
												)
											)
										);
										?>
									</span>

									<span class="news-meta-separator">•</span>

									<span class="news-comments">
										<i class="far fa-comment" aria-hidden="true"></i>
										<?php echo esc_html( get_comments_number() ); ?>
									</span>
								</div><!-- .news-post-meta -->
							</div><!-- .latest-news-content -->
						</article>
						<?php
					endwhile;
					?>
				</div><!-- .latest-news-list -->
			</div><!-- .latest-news-column -->

			<?php wp_reset_postdata(); ?>
		<?php endif; ?>

		<?php if ( $featured_query->have_posts() ) : ?>
			<div class="featured-posts-column">
				<div class="news-section-header">
					<h2 class="news-section-title">
						<?php echo esc_html( $featured_title ); ?>
					</h2>

					<span class="news-section-line"></span>
				</div><!-- .news-section-header -->

				<div class="featured-posts-list">
					<?php
					$featured_index = 0;

					while ( $featured_query->have_posts() ) :
						$featured_query->the_post();

						$categories = get_the_category();

						if ( 0 === $featured_index ) :
							?>
							<article
								id="featured-post-<?php the_ID(); ?>"
								<?php post_class( 'featured-main-post' ); ?>
							>
								<div class="featured-main-thumbnail">
									<a
										href="<?php the_permalink(); ?>"
										aria-label="<?php echo esc_attr( get_the_title() ); ?>"
									>
										<?php
										if ( has_post_thumbnail() ) {
											the_post_thumbnail(
												'large',
												array(
													'loading' => 'lazy',
												)
											);
										} else {
											?>
											<span class="featured-post-placeholder"></span>
											<?php
										}
										?>
									</a>

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

										<h3 class="featured-main-title">
											<a href="<?php the_permalink(); ?>">
												<?php the_title(); ?>
											</a>
										</h3>

										<div class="featured-main-meta">
											<span>
												<?php
												printf(
													/* translators: %s: Post date. */
													esc_html__( 'By %s', 'rubik-news' ),
													esc_html( get_the_date() )
												);
												?>
											</span>

											<span class="news-meta-separator">•</span>

											<span>
												<i class="far fa-comment" aria-hidden="true"></i>
												<?php echo esc_html( get_comments_number() ); ?>
											</span>
										</div>
									</div><!-- .featured-main-overlay -->
								</div><!-- .featured-main-thumbnail -->
							</article>
							<?php
						else :
							?>
							<article
								id="featured-small-post-<?php the_ID(); ?>"
								<?php post_class( 'featured-small-post' ); ?>
							>
								<div class="featured-small-thumbnail">
									<a
										href="<?php the_permalink(); ?>"
										aria-label="<?php echo esc_attr( get_the_title() ); ?>"
									>
										<?php
										if ( has_post_thumbnail() ) {
											the_post_thumbnail(
												'thumbnail',
												array(
													'loading' => 'lazy',
												)
											);
										} else {
											?>
											<span class="featured-post-placeholder"></span>
											<?php
										}
										?>
									</a>
								</div>

								<div class="featured-small-content">
									<h3 class="featured-small-title">
										<a href="<?php the_permalink(); ?>">
											<?php the_title(); ?>
										</a>
									</h3>

									<div class="news-post-meta">
										<span>
											<?php echo esc_html( get_the_date() ); ?>
										</span>

										<span class="news-meta-separator">•</span>

										<span>
											<i class="far fa-comment" aria-hidden="true"></i>
											<?php echo esc_html( get_comments_number() ); ?>
										</span>
									</div>
								</div>
							</article>
							<?php
						endif;

						$featured_index++;
					endwhile;
					?>
				</div><!-- .featured-posts-list -->
			</div><!-- .featured-posts-column -->

			<?php wp_reset_postdata(); ?>
		<?php endif; ?>

	</div><!-- .latest-featured-grid -->
</section>