<?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_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);
$thumbnail_post             = conikal_get_post_thumbnail($id, 'conikal-post-list', false);
$thumbnail_post_retina      = conikal_get_post_thumbnail($id, 'conikal-post-list-retina', false);

$lazy_placeholder           = conikal_lazy_load_placeholder('post-thumbnail-list');
$lazy_placeholder_medium    = conikal_lazy_load_placeholder('thumbnail-medium');
$lazy_placeholder_avatar    = conikal_lazy_load_placeholder('avatar');
?>
<div class="ui segment list-post-card" id="post-card-<?php echo esc_attr($id) ?>">
    <?php if ( is_sticky() ) { ?>
        <span class="ui primary right corner large label"><i class="pin icon"></i></span>
    <?php } ?>
    <div class="ui grid">
        <?php if (has_post_thumbnail()) { ?>
        <div class="sixteen wide mobile six wide tablet five wide computer column list-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 large button"><i class="external alternate icon"></i></div>
                        </div>
                        <div class="view-counter">
                        <?php if (function_exists('conikal_get_post_views')) {
                        if ($view_counter == 1 && !empty(conikal_get_post_views($id))) { ?>
                            <i class="eye icon"></i>
                            <?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true); ?>
                        <?php }
                        } ?>
                        </div>
                    </div>
                </div>
                <img class="display-tablet only display-computer only lazy" data-src="<?php echo esc_url($thumbnail_post) ?>" data-retina="<?php echo esc_url($thumbnail_post_retina) ?>" src="<?php echo esc_url($lazy_placeholder) ?>" alt="<?php the_title_attribute(); ?>">
                <img class="display-mobile only lazy" data-src="<?php echo esc_url($thumbnail) ?>" data-retina="<?php echo esc_url($thumbnail_retina) ?>" src="<?php echo esc_url($lazy_placeholder_medium) ?>" alt="<?php the_title_attribute(); ?>">
            </a>
        </div>
        <?php } ?>
        <div class="sixteen wide mobile <?php echo esc_attr(has_post_thumbnail() ? 'ten wide tablet eleven wide computer' : 'sixteen wide tablet sixteen wide computer') ?> column list-post-content">
            <?php if($p_categories) { ?>
                <a class="ui primary small label category-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>
            <h2 class="ui header list-post-title">
                <a href="<?php echo esc_url($p_link) ?>" data-bjax><?php the_title(); ?></a>
            </h2>
            <?php if (!empty($p_excerpt)) : ?>
            <p class="text grey<?php echo esc_attr(!empty($p_title) ? ' list-post-excerpt' : '') ?>">
                <?php echo esc_html($p_excerpt) ?> 
                <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="<?php echo esc_attr(has_post_thumbnail() ? 'list-post-footer' : 'list-post-footer-block') ?>">
                <div class="ui left floated tiny header meta-labels">
                    <?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>
                </div>
                <a class="ui right floated tiny header list-post-author" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' )); ?>" data-bjax>
                    <img class="ui avatar small 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 class="clearfix"></div>
            </div>
        </div>
    </div>
</div> 