<?php
/**
 * Related posts functions
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}
/**
 * Fetches posts based on categories
 *
 * @param array   $post_categories Array of term objects.
 * @param integer $limit Post count. -1 = no limit.
 * @param array   $exclude_ids Post ids to exclude.
 */
function multimagazine_get_posts_by_cats( $post_categories, $limit = 10, $exclude_ids = array() ) {
	$cids = array();
	foreach ( $post_categories as $term ) {
		$cids[] = $term->term_id;
	}
	$options = array(
		'post_type'           => 'post',
		'post_status'         => 'publish',
		'category__in'        => $cids,
		'posts_per_page'      => $limit,
		'post__not_in'        => $exclude_ids,
		'ignore_sticky_posts' => true,
	);
	$query   = new WP_Query( $options );
	return $query;
}

/**
 * Prepares query data to retrieve related posts
 *
 * @param object  $post Current post object.
 * @param integer $limit Number of related posts to get.
 */
function multimagazine_prepare_related_queries( $post, $limit ) {
	$post_cats = wp_get_post_categories( $post->ID, array( 'fields' => 'all' ) );

	$queries = array();

	while ( ! empty( $post_cats ) ) {
		$query     = multimagazine_get_posts_by_cats( $post_cats, $limit, array( $post->ID ) );
		$queries[] = $query;

		if ( $limit > 0 ) {
			$limit -= $query->found_posts;
		}

		if ( $limit > 0 ) {
			$cids = array();
			foreach ( $post_cats as $child ) {
				if ( ! empty( $child->parent ) ) {
					$cids[] = $child->parent;
				}
			}

			$post_cats = empty( $cids ) ? false : get_categories( array( 'include' => $cids ) );
		} else {
			break;
		}
	}

	return $queries;
}

/**
 * Gets related posts
 *
 * @param object  $post Current post object.
 * @param integer $limit Number of posts to retrieve.
 */
function multimagazine_related_posts( $post, $limit ) {
	$max_posts           = $limit;
	$list_layout         = get_theme_mod( 'multi_related_layout', MULTIMAGAZINE_RELATED_LAYOUT );
	$multimagazine_heading_style = multimagazine_heading_style();

	$queries = multimagazine_prepare_related_queries( $post, $limit );

	if ( 'slider' === $list_layout ) :
		multimagazine_post_slider( $queries, __( 'Related posts', 'multimagazine' ), 'related-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;

		}
		?>
		<?php if ( ! empty( $queries ) && $queries[0]->have_posts() ) : ?>
		<div class="related-posts additional-post-list">
			<h2 class="spec-heading <?php echo esc_attr( $multimagazine_heading_style ); ?>"><span><?php esc_html_e( 'Related posts', '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;
	endif;

	wp_reset_postdata();
}

/**
 * Gets links to related posts for a selected post
 *
 * @param object  $post Current post object.
 * @param integer $limit Number of posts to retrieve.
 */
function multimagazine_get_related_posts_links( $post, $limit ) {
	$queries = multimagazine_prepare_related_queries( $post, $limit );

	$related_posts = array();

	foreach ( $queries as $query ) {
		if ( $query->have_posts() ) {
			while ( $query->have_posts() ) {
				$query->the_post();
				$related_posts[] = get_the_permalink();
			}
		}
	}

	wp_reset_postdata();
	return $related_posts;
}
