<?php
/**
 * @package Reach
 */

use Reach\App\Posts\BlogPosts;
use Reach\App\Attachments\Media;

$layout  = ! is_single() ? BlogPosts::getBlogLayout() : 'default';
$excerpt = 150;
switch ( $layout ) {
    case 'masonry' :
        $thumb_size = 'featured-small';
        break;
    case 'rows' :
        $thumb_size = 'cropped-small';
        $excerpt    = 450;
        break;
    default:
        $thumb_size = 'featured-post';
        break;
}
$post_class = 'blog__post post__item post__item--' . $layout;
require_once AD_THEME_PATH . 'vendor/wp-pagenavi/wp-pagenavi.php';


if ( is_search() ){
?>
    <article id="post-<?php the_ID(); ?>" <?php post_class( $post_class ); ?> style="background: transparent !important;border: none; margin-bottom: 80px;">

        <div class="post__content" style="padding: 0;">
    <?php
} else {
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class($post_class); ?>>
        <?php
        if (has_post_thumbnail()) {
            $thumbnail_id = get_post_thumbnail_id(get_the_ID());
            $attachment = Media::getAttachmentByID($thumbnail_id);
            $thumbnail = !empty($attachment->sizes[$thumb_size]) ? $attachment->sizes[$thumb_size] : $attachment->url;
            $default = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQYV2M4c+bMfwAIMANkq3cY2wAAAABJRU5ErkJggg==';
            // output thumbnail markup
            printf(
                '<div class="post__thumb post__thumb--%1$s">
        <a href="%2$s" class="post__thumb-link" rel="bookmark" %4$s>
            <img src="%3$s" alt="%5$s" style="margin-bottom: 10px;">
        </a>
    </div>',
                $layout,
                esc_url(get_permalink()),
                ('rows' === $layout ? $default : esc_url($thumbnail)),
                ('rows' === $layout ? 'style="background-image: url(' . esc_url($thumbnail) . ');"' : ''),
                esc_attr($attachment->alt)
            );
        }
        ?>
        <div class="post__content">
        <?php
    }
    ?>
        <div class="post__meta">
            <?php
            // get post date
            echo get_the_date( 'M j, Y' );

            // get post categories
            $post_categories = wp_get_post_categories( get_the_ID() );
            $categories      = [];

            foreach ( $post_categories as $category ) {
                $cat          = get_category( $category );
                $cat_link     = get_category_link( $category );
                $categories[] = '<a class="meta__category" href="' . esc_url( $cat_link ) . '">' . $cat->name . '</a>';
            }
            if ( ! empty( $categories ) ) {
                // output our category links
                echo '<span class="meta--sep">&#124;</span>' . implode( ', ', $categories );
            }
            ?>
        </div>

        <header class="post__header">
            <?php the_title( sprintf( '<a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a>' ); ?>
        </header><!-- .post__header -->

        <div class="post__excerpt">
            <?php echo BlogPosts::getPostExcerpt( $excerpt ); ?>
        </div>

        <a class="post__read-more" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <?php _e( 'Read More', 'reach' ); ?>
        </a>
    </div><!-- .post__content -->

</article><!-- #post-## -->
