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

$todays_pick_content_ids  = $most_recent_content_ids = array();
$todays_pick_content_type = get_theme_mod( 'macro_news_todays_pick_content_type', 'post' );
$most_recent_content_type = get_theme_mod( 'macro_news_most_recent_content_type', 'post' );

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

	for ( $i = 1; $i <= 5; $i++ ) {
		$todays_pick_content_ids[] = get_theme_mod( 'macro_news_todays_pick_content_post_' . $i );
	}

	$todays_pick_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 5 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $todays_pick_content_ids ) ) ) {
		$todays_pick_args['post__in'] = array_filter( $todays_pick_content_ids );
		$todays_pick_args['orderby']  = 'post__in';
	} else {
		$todays_pick_args['orderby'] = 'date';
	}
} else {
	$cat_content_id   = get_theme_mod( 'macro_news_todays_pick_content_category' );
	$todays_pick_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 5 ),
	);
}

$todays_pick_args = apply_filters( 'macro_news_todays_pick_section_args', $todays_pick_args );

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

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

	$most_recent_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 3 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $most_recent_content_ids ) ) ) {
		$most_recent_args['post__in'] = array_filter( $most_recent_content_ids );
		$most_recent_args['orderby']  = 'post__in';
	} else {
		$most_recent_args['orderby'] = 'date';
	}
} else {
	$cat_content_id   = get_theme_mod( 'macro_news_most_recent_content_category' );
	$most_recent_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 3 ),
	);
}

$most_recent_args = apply_filters( 'macro_news_most_recent_section_args', $most_recent_args );

macro_news_render_todays_pick_section( $todays_pick_args, $most_recent_args );

/**
 * Render Todays Pick Section.
 */
function macro_news_render_todays_pick_section( $todays_pick_args, $most_recent_args ) {
	?>
	<section id="macro_news_todays_pick_section" class="magazine-frontpage-section magazine-todays-pick-section  has-most-recent">
		<?php
		if ( is_customize_preview() ) :
			macro_news_section_link( 'macro_news_todays_pick_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<div class="magazine-two-sections-wrap">

				<?php

				require get_template_directory() . '/template-parts/todays-pick/todays-pick.php';

				require get_template_directory() . '/template-parts/todays-pick/most-recent.php';

				?>

			</div>
		</div>
	</section>
	<?php
}
