<?php

/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;

$external_link  = isset($campoal_options['conikal_external_link_field']) ? $campoal_options['conikal_external_link_field'] : 0;
$view_counter   = isset($campoal_options['conikal_view_counter_field']) ? $campoal_options['conikal_view_counter_field'] : 0;

$id                 = get_the_ID();
$p_link             = get_permalink($id);
$p_title            = get_the_title($id);
$p_comments         = wp_count_comments($id);
$p_excerpt          = conikal_get_excerpt_by_id($id, 12);
$p_excerpt_medium   = conikal_get_excerpt_by_id($id, 20);
$p_excerpt_long     = conikal_get_excerpt_by_id($id, 30);
$p_author           = get_the_author();
$p_categories       = get_the_category();
$p_date             = get_the_date();

$author_url         = get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ));
$avatar             = conikal_get_avatar_url( get_the_author_meta('ID'), array('size' => 64) );
$avatar_retina      = conikal_get_avatar_url( get_the_author_meta('ID'), array('size' => 128) );

// get post thumbnail
$thumbnail                  = conikal_get_post_thumbnail($id, 'conikal-petition-thumbnail', false);
$thumbnail_retina           = conikal_get_post_thumbnail($id, 'conikal-petition-thumbnail-retina', false);

$lazy_placeholder = conikal_lazy_load_placeholder('thumbnail-small');
$lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
?>
	<div class="card blogs post post-card petition-card" id="post-card-<?php echo esc_attr($id) ?>">
        <?php if (has_post_thumbnail()) { ?>
        <a href="<?php echo esc_url($p_link); ?>" class="ui fluid image" target="<?php echo esc_attr($external_link != 0 ? '_blank' : '_self') ?>" <?php echo esc_attr($external_link != 0 ? '' : 'data-bjax') ?>>
            <div class="ui dimmer">
                <div class="content">
                    <div class="center">
                        <div class="ui icon inverted circular big button"><i class="external alternate icon"></i></div>
                    </div>
                    <div class="view-counter">
                        <i class="comments icon"></i>
                        <?php echo esc_html($p_comments->approved); ?>

                        <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
                            <i class="eye icon"></i>
                            <?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true); ?>
                        <?php } ?>
                    </div>
                </div>
            </div>
            <?php if ( is_sticky() ) { ?>
                <span class="ui primary ribbon label"><i class="pin icon"></i></span>
            <?php } ?>
            <img class="lazy" data-src="<?php echo esc_url($thumbnail) ?>" data-retina="<?php echo esc_url($thumbnail_retina) ?>" src="<?php echo esc_url($lazy_placeholder) ?>" alt="<?php echo esc_attr($p_title) ?>">
        </a>
        <?php } else { 
            if ( is_sticky() ) { ?>
            <div class="ui fluid image">
                <span class="ui primary ribbon label"><i class="pin icon"></i></span>
            </div>
            <?php } 
        } ?>
        <div class="content post-content<?php echo esc_attr(has_post_thumbnail() ? '' : ' title-only') ?>">
            <?php if (!has_post_thumbnail()) { ?>
                <div class="view-counter">
                    <i class="comments icon"></i>
                    <?php echo esc_html($p_comments->approved); ?>

                    <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
                        <i class="eye icon"></i>
                        <?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true); ?>
                    <?php } ?>
                </div>
            <?php } ?>

            <?php if (has_post_thumbnail()) { ?>
                <?php
                    $categories = get_the_category();
                    $separator = ' ';
                    $output = '';
                    if($categories) {
                        foreach($categories as $category) {
                            $output .= '<a class="ui tiny label" href="' . esc_url(get_term_link($category->term_id)) . '" title="' . esc_attr(sprintf(__("View all posts in %s", 'campoal'), $category->name)) . '" data-bjax>' . esc_html($category->cat_name) . '</a>' . esc_html($separator);
                        }
                        echo trim($output, $separator);
                    }
                ?>
                <div class="header card-post-title">
                    <a href="<?php echo esc_url($p_link) ?>" data-bjax><?php the_title(); ?></a>
                </div>
            <?php } else { ?>
                <p class="text grey text center">
                    <?php echo esc_html($p_date); ?>
                </p>
                <div class="header card-post-title text center">
                    <h2 class="ui center aligned header">
                        <a href="<?php echo esc_url($p_link) ?>" data-bjax><?php the_title(); ?></a>
                    </h2>
                    <div class="post-title-divider"></div>
                </div>
            <?php } ?>
            <div class="description">
                <?php if (!empty($p_excerpt)) : ?>
                    <p class="text grey<?php echo esc_attr(has_post_thumbnail() ? '' : ' text center') ?>">
                        <?php echo esc_html(has_post_thumbnail() ? $p_excerpt : $p_excerpt_medium) ?>
                        <a href="<?php echo esc_url($p_link) ?>">
                            <?php esc_html_e('more', 'campoal') ?>
                            <i class="angle right icon read-more-icon"></i>
                        </a>
                    </p>
                <?php endif; ?>
            </div>
            <?php if (!has_post_thumbnail()) { ?>
                <div class="ui hidden divider"></div>
                <p class="text center">
                    <img class="ui avatar image lazy" data-src="<?php echo esc_url($avatar) ?>" data-retina="<?php echo esc_url($avatar_retina) ?>" src="<?php echo esc_url($lazy_placeholder_avatar) ?>" alt="<?php the_author() ?>">
                    <a href="<?php echo get_author_posts_url(get_the_author_meta('ID'), get_the_author_meta('user_nicename')); ?>" data-bjax><?php the_author() ?></a>
                </p>
            <?php } ?>
        </div>
        <?php if (has_post_thumbnail()) { ?>
        <div class="extra content">
            <span><i class="calendar outline icon"></i><?php echo esc_html($p_date); ?></span>
            <span class="right floated">
                <a href="<?php echo get_author_posts_url(get_the_author_meta('ID'), get_the_author_meta('user_nicename')); ?>" data-bjax><?php the_author() ?></a>
            </span>
        </div>
        <?php } ?>
    </div>
