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

$rising_news_grid_content_ids  = array();
$rising_news_grid_content_type = get_theme_mod( 'rising_news_grid_content_type', 'post' );

if ( $rising_news_grid_content_type === 'post' ) {
	for ( $rising_news_i = 1; $rising_news_i <= 5; $rising_news_i++ ) {
		$rising_news_grid_content_ids[] = get_theme_mod( 'rising_news_grid_content_post_' . $rising_news_i );
	}
	$rising_news_grid_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 5 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $rising_news_grid_content_ids ) ) ) {
		$rising_news_grid_args['post__in'] = array_filter( $rising_news_grid_content_ids );
		$rising_news_grid_args['orderby']  = 'post__in';
	} else {
		$rising_news_grid_args['orderby'] = 'date';
	}
} else {
	$rising_news_cat_content_id = get_theme_mod( 'rising_news_grid_content_category' );
	$rising_news_grid_args      = array(
		'cat'            => $rising_news_cat_content_id,
		'posts_per_page' => absint( 5 ),
	);
}
$rising_news_grid_args = apply_filters( 'rising_news_grid_section_args', $rising_news_grid_args );

rising_news_render_grid_section( $rising_news_grid_args );

/**
 * Render Grid Section.
 */
function rising_news_render_grid_section( $rising_news_grid_args ) {

	$rising_news_query = new WP_Query( $rising_news_grid_args );
	if ( $rising_news_query->have_posts() ) {
		$rising_news_section_title       = get_theme_mod( 'rising_news_grid_title', __( 'Grid Posts', 'rising-news' ) );
		$rising_news_view_all_button     = get_theme_mod( 'rising_news_grid_button_label', __( 'View All', 'rising-news' ) );
		$rising_news_grid_content_type   = get_theme_mod( 'rising_news_grid_content_type', 'post' );
		$rising_news_view_all_button_url = get_theme_mod( 'rising_news_grid_button_link' );
		if ( 'category' === $rising_news_grid_content_type ) {
			$rising_news_grid_posts_category = get_theme_mod( 'rising_news_grid_content_category' );
			$rising_news_view_all_button_url = ! empty( $rising_news_view_all_button_url ) ? $rising_news_view_all_button_url : get_category_link( $rising_news_grid_posts_category );
		} else {
			$rising_news_view_all_button_url = ! empty( $rising_news_view_all_button_url ) ? $rising_news_view_all_button_url : '#';
		}

		?>
		<section id="rising_news_grid_section" class="grid-section section-splitter column-5">
			<?php
			if ( is_customize_preview() ) :
				rising_news_section_link( 'rising_news_grid_section' );
			endif;
			?>
			<div class="section-wrapper">
				<div class="main-container-wrap">
					<?php if ( ! empty( $rising_news_section_title || $rising_news_view_all_button ) ) : ?>
						<div class="title-heading">
							<?php if ( ! empty( $rising_news_section_title ) ) : ?>
								<h3 class="section-title">
									<?php echo esc_html( $rising_news_section_title ); ?>
								</h3>
							<?php endif; ?>
							<?php if ( ! empty( $rising_news_view_all_button ) ) { ?>
								<a href="<?php echo esc_url( $rising_news_view_all_button_url ); ?>" class="view-all"><?php echo esc_html( $rising_news_view_all_button ); ?></a>
							<?php } ?>
						</div>
					<?php endif; ?>
					<div class="grid-wrap">
						<?php
						while ( $rising_news_query->have_posts() ) :
							$rising_news_query->the_post();
							?>
							<div class="blog-post-container grid-layout">
								<div class="blog-post-inner">
									<?php if ( has_post_thumbnail() ) { ?>
										<div class="blog-post-image">
											<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'post-thumbnail' ); ?></a>
										</div>
										<?php } ?>
									<div class="blog-post-detail">
										<?php rising_news_categories_list(); ?>
										<h2 class="entry-title">
											<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
										</h2>

										<div class="post-meta">
											<?php rising_news_posted_by(); ?>
											<?php rising_news_posted_on(); ?>
										</div>
										<?php if ( ! has_post_thumbnail() ) { ?>
											<p class="post-excerpt">
												<?php echo wp_kses_post( wp_trim_words( get_the_content(), 20 ) ); ?>
											</p>
										<?php } ?>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			</div>
		</section>
		<?php
	}
}
