<?php

if ( ! get_theme_mod( 'majestic_blog_enable_tile_posts_section', false ) ) {
	return;
}

$tile_posts_content_ids  = array();
$tile_posts_content_type = get_theme_mod( 'majestic_blog_tile_posts_content_type', 'post' );

if ( $tile_posts_content_type === 'post' ) {
	for ( $i = 1; $i <= 4; $i++ ) {
		$tile_posts_content_ids[] = get_theme_mod( 'majestic_blog_tile_posts_content_post_' . $i );
	}
	$tile_posts_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 4 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $tile_posts_content_ids ) ) ) {
		$tile_posts_args['post__in'] = array_filter( $tile_posts_content_ids );
		$tile_posts_args['orderby']  = 'post__in';
	} else {
		$tile_posts_args['orderby'] = 'date';
	}
} else {
	$cat_content_id  = get_theme_mod( 'majestic_blog_tile_posts_content_category' );
	$tile_posts_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 4 ),
	);
}
$tile_posts_args = apply_filters( 'majestic_blog_tile_posts_section_args', $tile_posts_args );

majestic_blog_render_tile_posts_section( $tile_posts_args );

/**
 * Render Tile Posts Section.
 */
function majestic_blog_render_tile_posts_section( $tile_posts_args ) {
	$query = new WP_Query( $tile_posts_args );
	if ( $query->have_posts() ) {
		$tile_posts_title    = get_theme_mod( 'majestic_blog_tile_posts_section_title', __( 'Tile Posts Section', 'majestic-blog' ) );
		$tile_posts_subtitle = get_theme_mod( 'majestic_blog_tile_posts_subtitle', '' );
		?>
		<section id="majestic_blog_tile_posts_section" class="tile-section section-splitter style-1">
			<?php
			if ( is_customize_preview() ) :
				majestic_blog_section_link( 'majestic_blog_tile_posts_section' );
			endif;
			?>
			<div class="section-wrapper">
				<?php if ( ! empty( $tile_posts_title || $tile_posts_subtitle ) ) : ?>
					<div class="title-heading">
						<?php if ( ! empty( $tile_posts_title ) ) { ?>
							<h3 class="main-title"><?php echo esc_html( $tile_posts_title ); ?></h3>
						<?php } ?>
						<?php if ( ! empty( $tile_posts_subtitle ) ) { ?>
							<span class="sub-title"><?php echo esc_html( $tile_posts_subtitle ); ?></span>
						<?php } ?>
					</div>
				<?php endif; ?>
				<div class="blog-post-container-wrapper">
					<?php
					while ( $query->have_posts() ) :
						$query->the_post();
						?>
						<div class="blog-post-container tile-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(); ?></a>
									</div>
								<?php } ?>
								<div class="blog-post-detail">
									<ul class="post-categories">
										<?php majestic_blog_categories_list(); ?>
									</ul>
									<h3 class="post-main-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<div class="post-meta">
										<div class="post-meta-wrapper">
											<?php
											majestic_blog_posted_by();
											majestic_blog_posted_on();
											?>
										</div>
									</div>
								</div>
							</div>
						</div>
						<?php
					endwhile;
					wp_reset_postdata();
					?>
				</div>
			</div>
		</section>
		<?php
	}
}
