<?php

if ( ! defined( 'ABSPATH' ) ) exit;

$gridpress_terms_class = '';
$gridpress_terms_str   = '';
$gridpress_author_img_url = '';

$gridpress_comments_count = 0;

if (isset($gridpress_card_data['type']) && $gridpress_card_data['type'] === 'post') {

    $gridpress_comments_count = get_comments_number($gridpress_card_data['id']);

    $gridpress_author_img_url = get_avatar_url($gridpress_card_data['author_id'], 62);

    if (
        isset($gridpress_display_settings['top_filter_tax']) &&
        !empty($gridpress_display_settings['top_filter_tax'])
    ) {
        $gridpress_taxonomy = $gridpress_display_settings['top_filter_tax'];

        $gridpress_tax_terms = wp_get_post_terms(
            $gridpress_card_data['id'],
            $gridpress_taxonomy,
            ['fields' => 'names']
        );

        if (!is_wp_error($gridpress_tax_terms) && !empty($gridpress_tax_terms)) {
            $gridpress_terms_str = implode(', ', array_map('sanitize_text_field', $gridpress_tax_terms));
        }

        $gridpress_terms_class = apply_filters(
            'gridpress_card_item_term_classes',
            $gridpress_terms_class,
            $gridpress_taxonomy,
            $gridpress_card_data['id']
        );
    }

} else {

    $gridpress_author_imag_attach_id = isset($gridpress_card_data['author_image_attach_id'])
        ? absint($gridpress_card_data['author_image_attach_id'])
        : 0;

    $gridpress_author_img_url = $gridpress_author_imag_attach_id
        ? wp_get_attachment_image_url($gridpress_author_imag_attach_id, 'thumbnail')
        : '';

    $gridpress_terms_str = isset($gridpress_card_data['category'])
        ? sanitize_text_field($gridpress_card_data['category'])
        : '';
}

/**
 * Safe date
 */
$gridpress_post_date = !empty($gridpress_card_data['post_date'])
    ? sanitize_text_field(str_replace('/', '-', $gridpress_card_data['post_date']))
    : '';

?>

<div class="jc-cards<?php echo esc_attr($gridpress_terms_class); ?>">

    <div class="jc-cards-image">

        <?php if (!empty($gridpress_post_date) && !empty($gridpress_display_settings['show_date']) && $gridpress_display_settings['show_date'] === 'on') : ?>
            <time class="jc-cards-date"
                  datetime="<?php echo esc_attr(wp_date('Y-m-d', strtotime($gridpress_post_date))); ?>">

                <strong class="jc-cards-day">
                    <?php echo esc_html(wp_date('d', strtotime($gridpress_post_date))); ?>
                </strong>

                <?php echo esc_html(wp_date('M, y', strtotime($gridpress_post_date))); ?>
            </time>
        <?php endif; ?>
        <?php
        $gridpress_card_url   = isset( $gridpress_card_data['url'] ) ? sanitize_url( (string) $gridpress_card_data['url'] ) : '';
        $gridpress_image_url  = isset( $gridpress_card_data['imag_url'] ) ? sanitize_url( (string) $gridpress_card_data['imag_url'] ) : '';
        $gridpress_card_title = isset( $gridpress_card_data['title'] ) ? sanitize_text_field( (string) $gridpress_card_data['title'] ) : '';
        ?>
        <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
            <img
                src="<?php echo esc_url( $gridpress_image_url ); ?>"
                alt="<?php echo esc_attr( $gridpress_card_title ); ?>"
            />
        </a>
    </div>

    <div class="jc-cards-textbox">
        <div class="jc-cards-head">

            <?php if (
                !empty($gridpress_terms_str) &&
                !empty($gridpress_display_settings['show_terms']) &&
                $gridpress_display_settings['show_terms'] === 'on'
            ) : ?>
                <strong class="jc-cards-category">
                    <?php echo esc_html($gridpress_terms_str); ?>
                </strong>
            <?php endif; ?>

            <?php
            $gridpress_card_url   = isset( $gridpress_card_data['url'] ) ? sanitize_url( (string) $gridpress_card_data['url'] ) : '';
            $gridpress_card_title = isset( $gridpress_card_data['short_title'] ) ? sanitize_text_field( (string) $gridpress_card_data['short_title'] ) : '';
            ?>
            <h2>
                <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
                    <?php echo esc_html( $gridpress_card_title ); ?>
                </a>
            </h2>
        </div>

        <?php if (
            !empty($gridpress_card_data['short_excerpt']) &&
            !empty($gridpress_display_settings['show_excerpt']) &&
            $gridpress_display_settings['show_excerpt'] === 'on'
        ) : ?>
            <p class="gridpress-dynm-carditm-excerpt">
                <?php echo esc_html($gridpress_card_data['short_excerpt']); ?>
            </p>
        <?php endif; ?>

        <?php if (
            !empty($gridpress_card_data['author']) &&
            !empty($gridpress_display_settings['show_author']) &&
            $gridpress_display_settings['show_author'] === 'on'
        ) : ?>
            <div class="jc-cards-author">
                <?php
                $gridpress_author_name = isset( $gridpress_card_data['author'] )
                    ? sanitize_text_field( (string) $gridpress_card_data['author'] )
                    : '';
                ?>
                <?php if ( ! empty( $gridpress_author_img_url ) ) : ?>
                    <div class="jc-cards-avatar">
                        <img
                            src="<?php echo esc_url( $gridpress_author_img_url ); ?>"
                            alt="<?php echo esc_attr( $gridpress_author_name ); ?>"
                        />
                    </div>
                <?php endif; ?>
                <?php if ( ! empty( $gridpress_author_name ) ) : ?>
                    <strong class="jc-cards-subtext">
                        <?php echo esc_html( $gridpress_author_name ); ?>
                    </strong>
                <?php endif; ?>
            </div>
        <?php endif; ?>

    </div>

</div>