<?php
	$stockie_post = StockieHelper::get_storage_item_data();
	$anim_attrs = '';
	if ( in_array( $stockie_post['animation_type'], array( 'sync', 'async' ) ) ) {
		$anim_attrs .= ' data-aos-once="true"';
		$anim_attrs .= ' data-aos="' . esc_attr( $stockie_post['animation_effect'] ) . '"';
		if ( $stockie_post['animation_type'] == 'async' ) {
			$anim_attrs .= '';
		}
	}

	$blog_grid_class = '';
	if ( in_array( 'sticky', get_post_class( '', $stockie_post['post_id'] ) ) ) {
		$blog_grid_class .= ' sticky';
	}
	if ( $stockie_post['boxed'] ) {
		$blog_grid_class .= ' boxed';
	}
	if ( $stockie_post['media']['blockquote'] ) {
		$blog_grid_class .= ' quote';
	}
	if ( ! $stockie_post['preview'] ) {
		$blog_grid_class .= ' no-preview';
	}

	if ( $stockie_post['media']['gallery'] || $stockie_post['media']['image'] ) {
		$grid_class = ' grid-3';
	} else {
		$grid_class = ' grid-1';
	}

?>
<div class="blog-grid <?php echo esc_attr( $blog_grid_class ); echo esc_attr($grid_class); ?>" <?php echo esc_attr( $anim_attrs ); ?>>

	<figure class="blog-grid-image">
		<?php if ( $stockie_post['media']['video'] ) :
            // video
            $allowed_html = array(
                'iframe' => array(
                    'frameborder' => true,
                    'allowfullscreen' => true,
                    'src' => true
                )
            );
            echo wp_kses($stockie_post['media']['video'], $allowed_html);
        ?>

		<?php elseif ( $stockie_post['media']['audio'] ) : // audio ?>
		<?php echo wp_kses( $stockie_post['media']['audio'], 'post' ); ?>

		<?php elseif ( $stockie_post['media']['gallery'] ) : //gallery ?>
        <?php echo wp_kses($stockie_post['media']['gallery'], 'post'); ?>

		<?php elseif ( $stockie_post['media']['blockquote'] ) : //blockquote ?>
		<?php $stockie_post['preview'] = wp_kses( $stockie_post['media']['blockquote'], 'post' ); ?>

		<?php elseif ( $stockie_post['media']['image'] ) : // simple link image ?>
		<a href="<?php echo esc_url( $stockie_post['url'] ); ?>">
			<div class="blog-image" style="background-image: url(<?php echo esc_url( $stockie_post['media']['image'] ); ?>)"></div>
		</a>
		<?php endif; ?>
	</figure>

	<div class="blog-grid-content">
		<div class="tags">
			<?php if ( in_array( 'sticky', get_post_class( '', $stockie_post['post_id'] ) ) ) : ?>
				<span class="tag tag-sticky"><?php esc_html_e( 'Featured', 'stockie' ); ?></span>
			<?php endif; ?>
			<?php foreach ($stockie_post['categories'] as $_category) : ?>
				<a class="tag brand-bg-color" href="<?php echo esc_url( get_category_link( $_category->cat_ID ) ); ?>">
					<?php echo esc_html( $_category->name ); ?>
				</a>
			<?php endforeach; ?>
		</div>
		<div class="post-details">
			<?php if ( $stockie_post['author'] ) : ?>
			<span class="author"><?php echo esc_html( $stockie_post['author'] ); ?></span>
			<?php endif; ?>
			<?php if ( $stockie_post['date'] ) : ?>
			<span class="date"><?php echo esc_html( $stockie_post['date'] ); ?></span>
			<?php endif; ?>
		</div>
		<h3 class="blog-grid-headline">
			<?php if ( in_array( 'sticky', get_post_class( '', $stockie_post['post_id'] ) ) ) : ?>
			<img class="sticky-icon" src="<?php echo get_stylesheet_directory_uri() ?>/assets/images/sticky.svg" alt="sticky">
			<?php endif; ?>
			<a href="<?php echo esc_url( $stockie_post['url'] ); ?>">
				<?php echo esc_html( $stockie_post['title'] ); ?>
			</a>
		</h3>

        <?php
        if (preg_match('/<!--more(.*?)?-->/', $post->post_content)) {
            $allowed_html = array(
                'a' => array(
                    'href'  => true,
                    'title' => true,
                ),
                'br'     => array(),
            );
            ?>
            <p>
                <?php echo wp_kses(get_the_content(), $allowed_html); ?>
            </p>
            <a href="<?php echo esc_url($stockie_post['url']); ?>#more-<?php echo esc_attr($post->ID) ?>" class="btn btn-link brand-color-hover">
                <?php esc_html_e('Read More', 'stockie'); ?> <i class="ion-right ion ion-ios-arrow-forward"></i>
            </a>
        <?php } else { ?>
            <p>
                <?php the_excerpt(); ?>
            </p>
            <a href="<?php echo esc_url($stockie_post['url']); ?>" class="btn btn-link brand-color-hover">
                <?php esc_html_e('Read More', 'stockie'); ?> <i class="ion-right ion ion-ios-arrow-forward"></i>
            </a>
        <?php }
        ?>

	</div>
</div>