<?php

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

$gridpress_terms_class = '';
$gridpress_terms_str   = '';
$gridpress_likes_str   = '';

/**
 * Post type handling
 */
if ( isset($gridpress_card_data['type']) && $gridpress_card_data['type'] === 'post' ) {

    $gridpress_taxonomy = $gridpress_display_settings['top_filter_tax'] ?? '';

    if ( ! empty($gridpress_taxonomy) && ! empty($gridpress_card_data['id']) ) {

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

        if ( ! is_wp_error($gridpress_tax_terms) && ! empty($gridpress_tax_terms) ) {

            foreach ( $gridpress_tax_terms as $gridpress_tax_termitm ) {

                $gridpress_term_link = get_term_link($gridpress_tax_termitm->term_id);

                if ( ! is_wp_error( $gridpress_term_link ) ) {
                    // Decouple tracking paths completely via explicit string type casting
                    $gridpress_safe_link = isset( $gridpress_term_link ) ? (string) $gridpress_term_link : '';
                    $gridpress_safe_name = isset( $gridpress_tax_termitm->name ) ? (string) $gridpress_tax_termitm->name : '';

                    // Direct inline rendering to eliminate loop allocation trace traps
                    $gridpress_terms_str .= '<a href="' . esc_url( $gridpress_safe_link ) . '">#' . esc_html( $gridpress_safe_name ) . '</a> ';
                }
            }
        }

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

} else {

    $gridpress_terms_str = $gridpress_card_data['category'] ?? '';

    if ( ! empty($gridpress_card_data['item_likes']) ) {
        $gridpress_likes_str = $gridpress_card_data['item_likes'];
    }
}

$gridpress_post_date = ! empty($gridpress_card_data['post_date'])
    ? str_replace('/', '-', $gridpress_card_data['post_date'])
    : '';
?>

<div class="jc-cards<?php echo esc_attr($gridpress_terms_class); ?>">
    <?php
    $gridpress_card_url   = sanitize_url( (string) ( $gridpress_card_data['url'] ?? '' ) );
    $gridpress_card_title = sanitize_text_field( (string) ( $gridpress_card_data['short_title'] ?? '' ) );
    ?>
    <strong class="jc-cards-title">
        <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
            <?php echo esc_html( $gridpress_card_title ); ?>
        </a>
    </strong>
    <?php
    $gridpress_card_url   = isset( $gridpress_card_data['url'] ) ? (string) $gridpress_card_data['url'] : '';
    $gridpress_image_url  = isset( $gridpress_card_data['imag_url'] ) ? (string) $gridpress_card_data['imag_url'] : '';
    $gridpress_card_title = isset( $gridpress_card_data['title'] ) ? (string) $gridpress_card_data['title'] : '';
    ?>
    <div class="jc-cards-image">
        <a class="jc-cards-image-wrap" 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">

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

        <?php if ( ! empty($gridpress_post_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))); ?>">
                <?php echo esc_html(wp_date('M d, Y', strtotime($gridpress_post_date))); ?>
            </time>
        <?php endif; ?>

        <?php if ( ! empty($gridpress_likes_str) ) : ?>
            <a href="<?php echo esc_url($gridpress_card_data['url'] ?? ''); ?>" class="jc-cards-like-button">
                <img src="<?php echo esc_url(GRIDPRESS_URL); ?>images/heart.svg" alt="<?php echo esc_attr__('Likes', 'gridpress-grid-layout-design'); ?>">
                <strong class="jc-cards-like-text">
                    <?php echo wp_kses_post($gridpress_likes_str); ?>
                </strong>
            </a>
        <?php endif; ?>

    </div>

</div>