<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
global $post;
$categories = get_the_category($post->ID);
if($categories) {
    foreach( $categories as $individual_category ) {
        $category_ids[] = $individual_category->term_id;
    }
    $theme_options = pmthemes_theme_options();
    if ( $theme_options['related_post_order'] == 'random-posts' ) {
        $post_order = 'RAND';
    } else if ( $theme_options['related_post_order'] == 'old-posts' ) {
        $post_order = 'ASC';
    } else {
        $post_order = 'DESC';
    }
    $args = array(
        'category__in' => $category_ids,
        'numberposts' => $theme_options['related_post_count'],
        'orderby' => 'date',
        'order'   => $post_order,
        'post__not_in' => array($post->ID)
    );
    $related_posts = get_posts( $args );
    echo '<div class="related-posts-grid">';
    echo '<div class="reltitlebg">';
    echo '<h3 class="reltitle">'. $theme_options['related_header_title'] . '</h3>';
    echo '</div>';
    if ( isset($theme_options['related_post_style']) and $theme_options['related_post_style'] == 'four-cols' ) :
        echo '<div class="related-posts-columns related-posts-four-columns clearfix">';
    elseif ( isset($theme_options['related_post_style']) and $theme_options['related_post_style'] == 'three-cols' ) :
        echo '<div class="related-posts-columns related-posts-three-columns clearfix">';
    elseif ( isset($theme_options['related_post_style']) and $theme_options['related_post_style'] == 'two-cols' ) :
        echo '<div class="related-posts-columns related-posts-two-columns clearfix">';
    elseif ( isset($theme_options['related_post_style']) and $theme_options['related_post_style'] == 'simple-list' ) :
        echo '<div class="related-posts-columns posts-one-column clearfix">';
    endif;
    if($related_posts) {
        foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>
            <div class="related-post-column clearfix">
                <article id="post-<?php the_ID(); ?>">
                    <?php if ( isset($theme_options['related_posts_thumb']) and $theme_options['related_posts_thumb'] == true ) : ?>
                        <a href="<?php the_permalink() ?>" rel="bookmark">
                            <?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured_image'); } else { ?>
                                <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/thumbnail.png" width="100%" height="auto"/>
                            <?php } ?>
                        </a>
                    <?php endif; ?>
                    <header class="entry-header">
                        <?php
                        the_title( sprintf( '<h4 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h4>' );
                        ?>
                    </header>
                    <?php if ( isset($theme_options['show_post_dates']) and $theme_options['show_post_dates'] == true ) : ?>
                        <span class="meta-date">
                        <?php printf(__('<time datetime="%1$s">%2$s</time>', 'pmthemes'),
                            esc_attr( get_the_date( 'c' ) ),
                            esc_html( get_the_date() ) );
                        ?>
                        </span>
                    <?php endif; ?>
                    <?php if ( isset($theme_options['related_posts_excerpt']) and $theme_options['related_posts_excerpt'] == true ) : ?>
                        <div class="entry">
                            <?php add_filter('excerpt_length', 'pmthemes_related_posts_excerpt'); the_excerpt(); ?>
                        </div>
                        <?php remove_filter('excerpt_length', 'pmthemes_related_posts_excerpt'); endif; ?>
                </article>
            </div>
        <?php endforeach; }
    else { ?>
        <div class="related-post-column clearfix">
            <article id="post-<?php the_ID(); ?>">
                <?php echo '<p>' . __( 'No Related Posts Yet!', 'pmthemes' ) . '</p>'; ?>
            </article>
        </div>
    <?php }
}
wp_reset_postdata();
echo '</div>';
echo '</div>';
?>