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

$rising_news_content_ids  = array();
$rising_news_content_type = get_theme_mod( 'rising_news_flash_news_content_type', 'post' );

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

	for ( $rising_news_i = 1; $rising_news_i <= 5; $rising_news_i++ ) {
		$rising_news_content_ids[] = get_theme_mod( 'rising_news_flash_news_content_post_' . $rising_news_i );
	}

	$rising_news_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 5 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $rising_news_content_ids ) ) ) {
		$rising_news_args['post__in'] = array_filter( $rising_news_content_ids );
		$rising_news_args['orderby']  = 'post__in';
	} else {
		$rising_news_args['orderby'] = 'date';
	}
} else {
	$rising_news_cat_content_id = get_theme_mod( 'rising_news_flash_news_content_category' );
	$rising_news_args           = array(
		'cat'            => $rising_news_cat_content_id,
		'posts_per_page' => absint( 5 ),
	);
}

$rising_news_args = apply_filters( 'rising_news_flash_news_section_args', $rising_news_args );

rising_news_render_flash_news_section( $rising_news_args );

/**
 * Render Flash News Section.
 */
function rising_news_render_flash_news_section( $rising_news_args ) {
	$rising_news_section_title    = get_theme_mod( 'rising_news_flash_news_title', __( 'Flash News', 'rising-news' ) );
	$rising_news_speed_controller = get_theme_mod( 'rising_news_flash_news_speed_controller', 30 );

	$rising_news_query = new WP_Query( $rising_news_args );
	if ( $rising_news_query->have_posts() ) :
		?>
		<div id="rising_news_flash_news_section" class="rising-news-flash-news">
			<?php
			if ( is_customize_preview() ) :
				rising_news_section_link( 'rising_news_flash_news_section' );
			endif;
			?>
			<div class="section-wrapper">
				<div class="flash-news-section ascendoor-customizer-section">
					<div class="flash-news-wrapper">
						<?php if ( ! empty( $rising_news_section_title ) ) { ?>
							<div class="flash-news-title">
								<span class="title"><?php echo esc_html( $rising_news_section_title ); ?></span>
								<div class="flash-news-icon"></div>
							</div>
						<?php } ?>
						<div class="flash-news-area" dir="ltr">
							<div class="marquee flash-news" data-speed="<?php echo absint( $rising_news_speed_controller ); ?>">
								<div class="flash-news-posts-wrapper">
									<div class="flash-news-post">
										<?php
										while ( $rising_news_query->have_posts() ) :
											$rising_news_query->the_post();
											?>
											<div class="single-post-wrap">
												<div class="content-wrap">
													<?php if ( has_post_thumbnail() ) { ?>
														<div class="flash-image">
															<?php the_post_thumbnail( array( 40, 40 ) ); ?>
														</div>
													<?php } ?>
													<div class="flash-title">
														<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
													</div>
												</div>
											</div>
											<?php
										endwhile;
										wp_reset_postdata();
										?>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	endif;
}
