<?php
/**
 * Featured projects
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}

/**
 * Displays featured projects
 *
 * @param object  $post Current post object.
 * @param integer $limit Number of posts to show.
 */
function multimagazine_featured_projects( $post, $limit ) {
	$max_posts           = $limit;
	$list_layout         = get_theme_mod( 'multi_related_layout', MULTIMAGAZINE_RELATED_LAYOUT );
	$multimagazine_heading_style = multimagazine_heading_style();

	$options        = array(
		'post_status'         => 'publish',
		'post_type'           => 'project',
		'posts_per_page'      => $limit,
		'post__not_in'        => array( $post->ID ),
		'ignore_sticky_posts' => true,
		'meta_key'            => 'multi_featured', // phpcs:ignore
		'meta_value'          => 1, // phpcs:ignore
	);
	$featured_other = new WP_Query( $options );

	$queries = array( $featured_other );

	$have_posts = false;
	foreach ( $queries as $query ) {
		if ( ! empty( $query ) && $query->have_posts() ) {
			$have_posts = true;
			break;
		}
	}

	if ( ! $have_posts ) {
		return;
	}

	if ( 'slider' === $list_layout ) :
		multimagazine_post_slider( $queries, __( 'Featured posts', 'multimagazine' ), 'featured-posts' );
	else :
		switch ( $list_layout ) {
			case 'grid':
				$container_class = 'columns';
				$item_class      = 'col col3';
				break;
			case 'list':
				$container_class = 'scrolled-list scrollable-fade';
				$item_class      = 'scrolled-list-item';
				break;
		}
		?>
		<div class="featured-posts additional-post-list">
			<h2 class="spec-heading <?php echo esc_attr( $multimagazine_heading_style ); ?>"><span><?php esc_html_e( 'Featured projects', 'multimagazine' ); ?></span></h2>
			<div class="<?php echo esc_attr( $container_class ); ?>">
		<?php
		foreach ( $queries as $query ) :
			if ( ! empty( $query ) && $query->have_posts() ) :
				while ( $query->have_posts() ) :
					$query->the_post();
					?>
					<article class="related-post <?php echo esc_attr( $item_class ); ?>">
					<?php if ( has_post_thumbnail() ) : ?>
					<div class="img"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'multimagazine-thumbnail-slider', array() ); ?></a></div>
					<?php endif; ?>
					<h3 class="post-title"><a href="<?php the_permalink(); ?>"><?php multimagazine_the_title(); ?></a></h3>
						<?php
						if ( ! has_post_thumbnail() ) {
							echo wp_kses_post( multimagazine_custom_excerpt( 15, $post ) );
						}
						?>
				</article>
						<?php
				endwhile;
			endif;
		endforeach;
		?>
			</div>
		</div>
		<?php
	endif;

	wp_reset_postdata();
}
