<?php

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

$charity_elite_content_ids  = array();
$charity_elite_content_type = get_theme_mod( 'charity_elite_blog_content_type', 'category' );

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

	for ( $charity_elite_i = 1; $charity_elite_i <= 3; $charity_elite_i++ ) {
		$charity_elite_content_ids[] = get_theme_mod( 'charity_elite_blog_content_post_' . $charity_elite_i );
	}

	$charity_elite_args = array(
		'post_type'           => $charity_elite_content_type,
		'posts_per_page'      => absint( 3 ),
		'ignore_sticky_posts' => true,
	);

	if ( ! empty( array_filter( $charity_elite_content_ids ) ) ) {
		$charity_elite_args['post__in'] = array_filter( $charity_elite_content_ids );
		$charity_elite_args['orderby']  = 'post__in';
	} else {
		$charity_elite_args['orderby'] = 'date';
	}

} else {
	$charity_elite_cat_content_id = get_theme_mod( 'charity_elite_blog_content_category' );
	$charity_elite_args           = array(
		'cat'            => $charity_elite_cat_content_id,
		'posts_per_page' => absint( 3 ),
	);
}

$charity_elite_args = apply_filters( 'charity_elite_blog_section_args', $charity_elite_args );

charity_elite_render_blog_section( $charity_elite_args );

/**
 * Render Blog Section.
 */
function charity_elite_render_blog_section( $charity_elite_args ) {
	$charity_elite_section_title    = get_theme_mod( 'charity_elite_blog_title', __( 'News and Articles', 'charity-elite' ) );
	$charity_elite_section_subtitle = get_theme_mod( 'charity_elite_blog_subtitle', '' );
	$charity_elite_button_label     = get_theme_mod( 'charity_elite_blog_button_label', __( 'Read More', 'charity-elite' ) );

	$charity_elite_query = new WP_Query( $charity_elite_args );
	if ( $charity_elite_query->have_posts() ) :
		?>
		<section id="charity_elite_blog_section" class="ascendoor-frontpage-section blog-section blog-style-1">
			<?php
			if ( is_customize_preview() ) :
				charity_elite_section_link( 'charity_elite_blog_section' );
			endif;
			?>
			<div class="ascendoor-wrapper">
				<?php if ( ! empty( $charity_elite_section_subtitle || $charity_elite_section_title ) ) { ?>
					<div class="section-header-subtitle">
						<p class="section-subtitle"><?php echo esc_html( $charity_elite_section_subtitle ); ?></p>
						<h3 class="section-title"><?php echo esc_html( $charity_elite_section_title ); ?></h3>
					</div>
				<?php } ?>
				<div class="section-body">
					<div class="blogs-wrapper">
						<?php
						while ( $charity_elite_query->have_posts() ) :
							$charity_elite_query->the_post();
							?>
							<div class="blog-single">
								<div class="blog-img">
									<a href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail( 'post-thumbnail' ); ?>
									</a>
								</div>
								<div class="blog-detail">
									<div class="blog-category">
										<?php the_category( '', '', get_the_ID() ); ?>
									</div>
									<h3 class="blog-title">
										<a href="<?php the_permalink(); ?>">
											<?php the_title(); ?>
										</a>
									</h3>
									<div class="blog-meta"><?php echo esc_html( get_the_date() ); ?></div>
									<div class="blog-content">
										<div class="blog-excerpt">
											<?php echo wp_kses_post( wp_trim_words( get_the_content(), 25 ) ); ?>
										</div>
										<?php if ( ! empty( $charity_elite_button_label ) ) { ?>
											<div class="blog-button">
												<a href="<?php the_permalink(); ?>" class="ascendoor-button ascendoor-button-noborder-noalternate"><?php echo esc_html( $charity_elite_button_label ); ?></a>
											</div>
										<?php } ?>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			</div>
		</section>
		<?php
	endif;
}
