<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;

$view_counter = isset($campoal_options['conikal_view_counter_field']) ? $campoal_options['conikal_view_counter_field'] : 0;
$related_style = isset($campoal_options['conikal_related_style_field']) ? $campoal_options['conikal_related_style_field'] : 'classic';
$single_post_style = isset($campoal_options['conikal_single_post_style_field']) ? $campoal_options['conikal_single_post_style_field'] : 'classic';
$related_post_per_page = isset($campoal_options['conikal_related_posts_per_page_field']) ? $campoal_options['conikal_related_posts_per_page_field'] : 4;
$orig_post = $post;
$tags = wp_get_post_tags($post->ID);

if ($tags) :
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args = array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'posts_per_page' => $related_post_per_page,
        'ignore_sticky_posts' => false
    );

    $related_query = new wp_query($args);

    if ($related_query->have_posts()) : ?>
    <div class="post-related-section">
        <?php if ($related_style == 'card') : ?>
            <div class="ui huge header"><?php esc_html_e('Related Articles', 'campoal'); ?></div>
            <div class="title-divider"></div>
            <div class="ui hidden divider"></div>
            <div class="related-post-section carousel-v1">
                <div class="ui <?php echo esc_attr($single_post_style != 'classic' ? 'three' : 'two') ?> stackable cards <?php echo esc_attr($related_query->post_count > 3 && $single_post_style != 'classic' ? 'related-posts' : '') ?>">
        <?php else: ?>
            <?php if ($related_style != 'card' && $single_post_style != 'classic') : ?>
            <div class="ui centered grid">
                <div class="sixteen wide mobile sixteen wide tablet ten wide computer column">
            <?php else: ?>
            <div class="related-post-section">
                <div class="related-post-content">
            <?php endif; ?>
                    <div class="ui large header "><?php esc_html_e('Related Articles', 'campoal'); ?></div>
                    <div class="title-divider"></div>
                    <div class="ui hidden divider"></div>
        <?php endif; ?>

        <?php while( $related_query->have_posts() ) : $related_query->the_post(); ?>
            
                <?php get_template_part('loop/content-post', ($related_query->post_count > 3 && $single_post_style != 'classic' ? $related_style : ($related_style == 'card' ? 'grid' : $related_style))); ?>

        <?php endwhile; ?>

            </div>
        </div>
    </div>
<?php endif;
endif;
$post = $orig_post;
wp_reset_query();
?>
