<?php
$enable_featured_block = bloglex_get_option('enable_featured_block');
$featured_block_category = bloglex_get_option('featured_block_category');
$enable_featured_block_category_meta = bloglex_get_option('enable_featured_block_category_meta');
$select_featured_block_category_color = bloglex_get_option('select_featured_block_category_color');
$select_featured_block_number_of_category = bloglex_get_option('select_featured_block_number_of_category');
$featured_block_category_label = bloglex_get_option('featured_block_category_label');

$enable_featured_block_author_meta = bloglex_get_option('enable_featured_block_author_meta');
$select_featured_block_author_meta = bloglex_get_option('select_featured_block_author_meta');
$featured_block_author_meta_title = bloglex_get_option('featured_block_author_meta_title');

$enable_featured_block_date_meta = bloglex_get_option('enable_featured_block_date_meta');
$select_featured_block_date = bloglex_get_option('select_featured_block_date');
$select_featured_block_date_meta_title = bloglex_get_option('select_featured_block_date_meta_title');
$select_featured_block_date_format = bloglex_get_option('select_featured_block_date_format');

$post_args = array(
  'post_type' => 'post',
  'posts_per_page' => 8,
  'post_status' => 'publish',
  'orderby' => 'date',
  'order' => 'DESC',
);
if (!empty($featured_block_category)) :
    $post_args['tax_query'][] = array(
        'taxonomy' => 'category',
        'field' => 'term_id',
        'terms' => absint($featured_block_category),
    );
endif;

$featured_block_posts = new WP_Query($post_args);

?>

<?php if ($enable_featured_block) { ?>
  <section class="wpi-section theme-featured-block">
    <div class="wrapper wrapper-gutter-medium">
      <div class="row-group">
        <?php 
        if($featured_block_posts->have_posts()) :
          while ($featured_block_posts->have_posts()) : $featured_block_posts->the_post();
          $post_index = $featured_block_posts->current_post; ?>
          <div class="column column-lg-3 column-md-6">
              <article id="featured-block-<?php the_ID(); ?>" <?php post_class('wpi-post'); ?>>
                <?php if (has_post_thumbnail()) : ?>
                  <div class="entry-image entry-image-medium image-hover-effect hover-effect-shine">
                    <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                      <?php
                        the_post_thumbnail(
                          'medium',
                          array(
                            'alt' => the_title_attribute(
                              array(
                                'echo' => false,
                              )
                            )
                          )
                        )
                      ?>
                    </a>
                  </div>
                <?php endif; ?>

                <div class="entry-details">

                  <?php
                    if ($enable_featured_block_category_meta) {
                        bloglex_post_category($select_featured_block_category_color, $featured_block_category_label, $select_featured_block_number_of_category);
                    }
                  ?>

                  <h2 class="entry-title entry-title-medium limit-line-3">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                  </h2>

                  <div class="entry-meta-wrapper">

                      <?php
                      if ($enable_featured_block_date_meta) {
                          bloglex_posted_on($select_featured_block_date_format, $select_featured_block_date_meta_title, $select_featured_block_date);
                      }
                      ?>

                      <?php
                      if ($enable_featured_block_date_meta && $enable_featured_block_author_meta) { ?>
                          <div class="entry-meta-separator"></div>
                      <?php } ?>

                      <?php
                      if ($enable_featured_block_author_meta) {
                          bloglex_posted_by($select_featured_block_author_meta, $featured_block_author_meta_title);
                      }
                      ?>

                  </div>
                  
                </div>
              </article>
        </div>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); // Reset post data ?>
          <?php endif; ?>
      </div>
    </div>
  </section>
<?php } ?>