<?php


$post_type = get_field('post_type');

if (empty($post_type)) goto end;

$post_layout = get_field('post_layout');
if (empty($post_layout)) $post_layout = 'list';

if ($post_type === 'resource') {
	$free_resource = get_field('free_resource');
	if (!isset($free_resource))  goto end;

	$image = get_the_post_thumbnail($free_resource, array(183, 165));
	$types = get_the_terms($free_resource, 'resource_type');
	$title = get_the_title($free_resource);
	$description = wp_trim_excerpt('', $free_resource);
	$tags = get_the_tags($free_resource);
	$link = esc_url(get_permalink($free_resource));
} else if ($post_type === 'set') {
	$resource_set = get_field('resource_set');
	if (!isset($resource_set))  goto end;

	$image = wp_get_attachment_image(get_term_meta($resource_set->term_id, 'kwp-tax-image-id', true), array(183, 165));
	$types = array(get_field('type', $resource_set->taxonomy.'_'.$resource_set->term_id));
	$title = $resource_set->name;
	$description = $resource_set->description;
	// convert to excerpt
	$description = strip_shortcodes( $description );
	$description = excerpt_remove_blocks( $description );
	$description = apply_filters( 'the_content', $description );
    $description = str_replace( ']]>', ']]&gt;', $description );
    $excerpt_length = (int) _x( '55', 'excerpt_length' );
    $excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
    $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
    $description = wp_trim_words( $description, $excerpt_length, $excerpt_more );
	$description = wp_trim_excerpt($description);

	$tags = get_field('tag', $resource_set->taxonomy.'_'.$resource_set->term_id);
	$link = esc_url(get_term_link($resource_set));
}

ob_start();
?>
<?php if ( $post_layout === 'list') : ?>
	<article class="bw-resource-entry">
		<div class="entry-content-wrap">
			<header class="bw-resource-header">
				<div class="bw-resource-image">
					<a href="<?php echo $link; ?>">
						<?php echo $image; ?>
					</a>
				</div>
				<div class="bw-title-column">
					<div class="bw-resource-tax">
						<?php if (is_array($types) && count($types)) : ?>
							<ul>
								<?php foreach ($types as $type) {
									$type_link = get_term_link($type);
									if (is_wp_error($type_link)) {
										continue;
									}
									echo '<li><a href="' . esc_url($type_link) . '">' . $type->name . '</a></li>';
								} ?>
							</ul>
						<?php endif; ?>
					</div>
					<h2 class="entry-title">
						<a href="<?php echo $link; ?>" rel="bookmark"><?php echo $title; ?></a>
					</h2>
				</div>
			</header>
			<div class="entry-content">
				<div class="entry-summary">
					<?php echo $description; ?>
				</div>
			</div>
			<footer>
				<?php get_template_part( 'template-parts/resource-content/entry_tags', get_post_type(), array('tags' => $tags ) ); ?>
			</footer>
		</div>
	</article>
<?php elseif ( $post_layout === 'home') : ?>
	<article class="bw-resource-featured kt-blocks-post-grid-item">
		<div class="kt-blocks-post-grid-item-inner-wrap kt-feat-image-align-Top kt-feat-image-mobile-align-top">
			<div class="kadence-post-image">
				<div class="kadence-post-image-intrisic kt-image-ratio-75" style="padding-bottom:75%">
					<div class="kadence-post-image-inner-intrisic">
						<a href="<?php echo $link; ?>" class="kadence-post-image-inner-wrap">
							<?php echo $image; ?>	
						</a>
					</div>
				</div>
			</div>
			<div class="kt-blocks-post-grid-item-inner">
				<header>
					<h4 class="entry-title">
						<a href="<?php echo $link; ?>"><?php echo $title; ?></a>
					</h4>
				</header>
				<div class="entry-content">
					<?php echo $description; ?>
					<div class="kt-blocks-post-readmore-wrap">
						<a class="kt-blocks-post-readmore" href="<?php echo $link; ?>">
							View Resource
							<span class="screen-reader-text">
								<?php echo $title; ?>		
							</span>
						</a>
					</div>
				</div>
			</div>
		</div>
	</article>
<?php endif; ?>
<?php
echo ob_get_clean();
end:
