<?php
/**
 * Template part for displaying front page introduction.
 *
 * @package Social News
 */

// Banner Section.
$banner_section = get_theme_mod( 'social_news_banner_section_enable', false );

if ( false === $banner_section ) {
	return;
}

$featured_posts_content_ids  = $main_news_content_ids = array();
$featured_posts_content_type = get_theme_mod( 'social_news_banner_featured_posts_content_type', 'post' );
$main_news_content_type      = get_theme_mod( 'social_news_banner_main_news_content_type', 'post' );
$featured_posts_count        = get_theme_mod( 'social_news_banner_featured_posts_count', 3 );
$main_news_count             = get_theme_mod( 'social_news_banner_main_news_count', 3 );

if ( in_array( $main_news_content_type, array( 'post', 'page' ) ) ) {

	for ( $i = 1; $i <= $main_news_count; $i++ ) {
		$main_news_content_ids[] = get_theme_mod( 'social_news_banner_main_news_' . $main_news_content_type . '_' . $i );
	}

	$main_news_args = array(
		'post_type'           => $main_news_content_type,
		'posts_per_page'      => absint( $main_news_count ),
		'ignore_sticky_posts' => true,
	);

	if ( ! empty( array_filter( $main_news_content_ids ) ) ) {
		$main_news_args['post__in'] = array_filter( $main_news_content_ids );
		$main_news_args['orderby']  = 'post__in';
	} else {
		$main_news_args['orderby'] = 'date';
	}
} elseif ( $main_news_content_type === 'category' ) {
	$cat_content_id = get_theme_mod( 'social_news_banner_main_news_category' );
	$main_news_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( $main_news_count ),
	);
} elseif ( $main_news_content_type === 'recent' ) {
	$main_news_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( $main_news_count ),
		'ignore_sticky_posts' => true,
	);
}

if ( in_array( $featured_posts_content_type, array( 'post', 'page' ) ) ) {

	for ( $i = 1; $i <= $featured_posts_count; $i++ ) {
		$featured_posts_content_ids[] = get_theme_mod( 'social_news_banner_featured_posts_' . $featured_posts_content_type . '_' . $i );
	}

	$featured_posts_args = array(
		'post_type'           => $featured_posts_content_type,
		'posts_per_page'      => absint( $featured_posts_count ),
		'ignore_sticky_posts' => true,
	);

	if ( ! empty( array_filter( $featured_posts_content_ids ) ) ) {
		$featured_posts_args['post__in'] = array_filter( $featured_posts_content_ids );
		$featured_posts_args['orderby']  = 'post__in';
	} else {
		$featured_posts_args['orderby'] = 'date';
	}
} elseif ( $featured_posts_content_type === 'category' ) {
	$cat_content_id      = get_theme_mod( 'social_news_banner_featured_posts_category' );
	$featured_posts_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( $featured_posts_count ),
	);
} elseif ( $featured_posts_content_type === 'recent' ) {
	$featured_posts_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( $featured_posts_count ),
		'ignore_sticky_posts' => true,
	);
}

?>

<div id="social_news_banner_section" class="main-banner-section adore-navigation">
	<div class="theme-wrapper">
		<div class="main-banner-section-wrapper">
			
			<?php

			require get_template_directory() . '/inc/frontpage-sections/banner-sections/banner-main-news.php';
			require get_template_directory() . '/inc/frontpage-sections/banner-sections/banner-featured-posts.php';

			?>
				
		</div>
	</div>
</div>
