<?php
if ( ! get_theme_mod( 'mindful_blog_enable_categories_section', false ) ) {
	return;
}
$mindful_blog_section_content = $mindful_blog_content_ids = array();
for ( $mindful_blog_i = 1; $mindful_blog_i <= 4; $mindful_blog_i++ ) {
	$mindful_blog_content_post_id = get_theme_mod( 'mindful_blog_categories_content_category_' . $mindful_blog_i );
	if ( ! empty( $mindful_blog_content_post_id ) ) {
		$mindful_blog_content_ids[] = $mindful_blog_content_post_id;
	}
}
$mindful_blog_args = array(
	'taxonomy'   => 'category',
	'number'     => 4,
	'include'    => array_filter( $mindful_blog_content_ids ),
	'orderby'    => 'include',
	'hide_empty' => false,
);

$mindful_blog_terms = get_terms( $mindful_blog_args );
$mindful_blog_i     = 1;
foreach ( $mindful_blog_terms as $mindful_blog_value ) {
	$mindful_blog_data['title']         = $mindful_blog_value->name;
	$mindful_blog_data['permalink']     = get_term_link( $mindful_blog_value->term_id );
	$mindful_blog_data['thumbnail_url'] = get_theme_mod( 'mindful_blog_category_category_image_' . $mindful_blog_i, '' );
	array_push( $mindful_blog_section_content, $mindful_blog_data );
	$mindful_blog_i++;
}

$mindful_blog_section_content = apply_filters( 'mindful_blog_categories_section_content', $mindful_blog_section_content );

mindful_blog_render_categories_section( $mindful_blog_section_content );

/**
 * Render Categories Section
 */
function mindful_blog_render_categories_section( $mindful_blog_section_content ) {
	$mindful_blog_categories_title = get_theme_mod( 'mindful_blog_categories_title', __( 'Categories', 'mindful-blog' ) );
	?>

	<section id="mindful_blog_categories_section" class="mindful-blog-frontpage-section mindful-blog-categories-section categories-style-2 column-4">
		<?php
		if ( is_customize_preview() ) :
			mindful_blog_section_link( 'mindful_blog_categories_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<div class="ascen-body-wrap">
				<?php if ( ! empty( $mindful_blog_categories_title ) ) : ?>
					<div class="section-header">
						<h3 class="section-title"><?php echo esc_html( $mindful_blog_categories_title ); ?></h3>
					</div>
				<?php endif; ?>
				<div class="categories-wrapper">
					<?php foreach ( $mindful_blog_section_content as $mindful_blog_content ) : ?>
						<div class="category-single">
							<div class="category-img">
								<?php if ( ! empty( $mindful_blog_content['thumbnail_url'] ) ) : ?>
									<img src="<?php echo esc_url( $mindful_blog_content['thumbnail_url'] ); ?>" alt="<?php echo esc_attr( $mindful_blog_content['title'] ); ?>">
								<?php endif; ?>
							</div>
							<a href="<?php echo esc_url( $mindful_blog_content['permalink'] ); ?>">
								<span class="title"><?php echo esc_html( $mindful_blog_content['title'] ); ?></span>
							</a>
						</div>
					<?php endforeach; ?>
				</div>
			</div>
		</div>
	</section>	

	<?php
}
