<?php

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

$charity_elite_content_id   = array();
$charity_elite_content_type = get_theme_mod( 'charity_elite_mission_content_type', 'page' );

if ( 'post' === $charity_elite_content_type ) {
	$charity_elite_content_id[] = get_theme_mod( 'charity_elite_mission_content_post' );
} else {
	$charity_elite_content_id[] = get_theme_mod( 'charity_elite_mission_content_page' );
}
$charity_elite_args = array(
	'post_type'           => $charity_elite_content_type,
	'posts_per_page'      => absint( 1 ),
	'ignore_sticky_posts' => true,
);

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

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

charity_elite_render_mission_section( $charity_elite_args );

/**
 * Render Mission Section
 */

function charity_elite_render_mission_section( $charity_elite_args ) {
	$charity_elite_query = new WP_Query( $charity_elite_args );
	if ( $charity_elite_query->have_posts() ) :
		$charity_elite_section_subtitle = get_theme_mod( 'charity_elite_mission_subtitle', '' );
		$charity_elite_button_label     = get_theme_mod( 'charity_elite_mission_button_label', __( 'Read More', 'charity-elite' ) );
		$charity_elite_mission_points   = get_theme_mod( 'charity_elite_mission_points', '' );

		while ( $charity_elite_query->have_posts() ) :
			$charity_elite_query->the_post();
			?>
			<section id="charity_elite_mission_section" class="ascendoor-frontpage-section mission-section mission-image-right">
				<?php
				if ( is_customize_preview() ) :
					charity_elite_section_link( 'charity_elite_mission_section' );
				endif;
				?>
				<div class="ascendoor-wrapper">
					<div class="section-body">
						<div class="mission-wrapper">
							<div class="section-image">
								<?php the_post_thumbnail(); ?>
							</div>
							<div class="section-text section-header-subtitle">
								<?php if ( ! empty( $charity_elite_section_subtitle ) ) { ?>
									<p class="section-subtitle"><?php echo esc_html( $charity_elite_section_subtitle ); ?></p>
								<?php } ?>
								<h3 class="section-title"><?php the_title(); ?></h3>
								<p class="description">
									<?php echo wp_kses_post( wp_trim_words( get_the_content(), 50 ) ); ?>
								</p>
								<?php $charity_elite_mission_point_list = ! empty( $charity_elite_mission_points ) ? explode( '|', $charity_elite_mission_points ) : array(); ?>
								<div class="mission-points">
									<ul>
										<?php foreach ( $charity_elite_mission_point_list as $charity_elite_list ) : ?>
											<li><?php echo esc_html( $charity_elite_list ); ?></li>
										<?php endforeach; ?>
									</ul>
								</div>
								<?php if ( ! empty( $charity_elite_button_label ) ) { ?>
									<div class="section-button">
										<a href="<?php the_permalink(); ?>" class="ascendoor-button"><?php echo esc_html( $charity_elite_button_label ); ?></a>
									</div>
								<?php } ?>
							</div>
						</div>
					</div>
				</div>
			</section>
			<?php
		endwhile;
		wp_reset_postdata();
	endif;
}
