<?php
/**
 * Template part for displaying home featured blocks section.
 *
 * @package University_Hub
 */

?>
<?php
	$featured_blocks_number = absint( university_hub_get_option( 'featured_blocks_number' ) );
	$featured_blocks_column = university_hub_get_option( 'featured_blocks_column' );
	$featured_block_pages = array();
	for ( $i = 0; $i <= $featured_blocks_number; $i++ ) {
		$page = university_hub_get_option( 'featured_blocks_page_' . $i );
		if ( absint( $page ) > 0 ) {
			$featured_block_pages[] = absint( $page );
		}
	}
	if ( ! empty( $featured_block_pages ) ) {
		$featured_block_pages = array_unique( $featured_block_pages );
	}
?>
<div id="university-hub-featured-blocks" class="home-section-featured-blocks">
	<div class="container">
			<?php if ( ! empty( $featured_block_pages ) ) : ?>
				<?php
				$qargs = array(
					'posts_per_page' => absint( $featured_blocks_number ),
					'no_found_rows'  => true,
					'orderby'        => 'post__in',
					'post_type'      => 'page',
					'post__in'       => $featured_block_pages,
				);

				// Fetch posts.
				$the_query = new WP_Query( $qargs );
				?>
				<?php if ( $the_query->have_posts() ) : ?>
					<div class="inner-wrapper featured-blocks-column-<?php echo absint( $featured_blocks_column ) ?>">

						<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
							<article>
								<?php if ( has_post_thumbnail() ) : ?>
									<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'university-hub-thumb' ); ?></a>
								<?php endif; ?>

								<div class="entry-content">
									<header class="entry-header">
										<h2 class="block-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
									</header>
									<?php
									$excerpt = university_hub_the_excerpt( 20 );
									echo wp_kses_post( wpautop( $excerpt ) );
									?>
									<a class="more" href="<?php the_permalink(); ?>"><?php esc_html_e( 'Learn More', 'university-hub-pro' ); ?></a>
								</div>
							</article>

						<?php endwhile; ?>

					</div><!-- .inner-wrapper -->
					<?php wp_reset_postdata(); ?>
				<?php endif; ?>

			<?php else: ?>

				<?php if ( current_user_can( 'edit_theme_options' ) ) : ?>
					<p><strong><?php esc_html_e( 'No pages selected to be displayed as featured blocks. In Customizer, go to Homepage Sections -> Featured Blocks to choose pages.', 'university-hub-pro' ); ?></strong></p>
				<?php endif; ?>

			<?php endif; ?>

	</div> <!-- .container -->
</div><!-- .home-section-featured-blocks -->
