<?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;

$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_link        = 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-medium', false);
$thumbnail_retina   = conikal_get_post_thumbnail($id, 'conikal-petition-medium-retina', false);

$lazy_placeholder           = conikal_lazy_load_placeholder('post-thumbnail-large');
$lazy_placeholder_avatar    = conikal_lazy_load_placeholder('avatar');
?>
<div class="classic-post-card" id="post-card-<?php echo esc_attr($id) ?>">
    <div class="behind-thumbnail">
        <?php if (has_post_thumbnail()) { ?>
        <a class="ui fluid rounded image" href="<?php echo esc_url($p_link) ?>" data-bjax>
            <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 } ?>
    </div>
    <div class="ui raised padded segment overlay-header petition-card">
        <?php if ( is_sticky() ) { ?>
            <span class="ui primary right corner large label"><i class="pin icon"></i></span>
        <?php } ?>
        <?php if($p_categories) { ?>
            <a class="ui primary small label" href="<?php echo get_term_link($p_categories[0]->term_id) ?>" data-bjax>
                <?php echo esc_html($p_categories[0]->name); ?>
            </a>
        <?php } ?>
        <span class="ui label date-label">
            <?php echo esc_html($p_date); ?>
        </span>
        <h1 class="ui header">
            <a class="overlay-title" href="<?php echo esc_url($p_link) ?>" data-bjax><?php the_title(); ?></a>
        </h1>
        <?php if (!empty($p_excerpt_long)) : ?>
        <p class="text grey description">
            <?php echo esc_html($p_excerpt_long) ?> 
            <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 class="ui basic vertical segment">
            <div class="ui right floated tiny header classic-post-author">
                <a href="<?php echo esc_url($author_link); ?>" data-bjax>
                    <img class="ui avatar image supporter-avatar 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() ?>" data-title="<?php echo esc_attr($p_author) ?>" />
                </a>
            </div>
            <?php if (!empty($p_comments->approved)) : ?>
                <a class="ui label" href="<?php echo esc_url($p_link) ?>" data-bjax>
                    <i class="comments icon"></i>
                    <?php echo sprintf( _n('%s comment', '%s comments', $p_comments->approved, 'campoal'), conikal_format_number('%!,0i', $p_comments->approved, true) ); ?>
                </a>
            <?php endif; ?>
            <a class="ui label" href="<?php echo esc_url($p_link) ?>" data-bjax>
                <i class="time icon"></i>
                <?php echo conikal_reading_time($id) ?>
            </a>
            <?php if ($view_counter == 1 && !empty( (int) conikal_get_post_views($id)) && function_exists('conikal_get_post_views')) { ?>
                <a class="ui label">
                    <i class="eye icon"></i>
                    <?php echo sprintf( _n('%s view', '%s views', (int) conikal_get_post_views($id), 'campoal'), conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true) ); ?>
                </a>
            <?php } ?>
        </div>
    </div>
</div>