<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Safe defaults
 */
$gridpress_card_data       = $gridpress_card_data ?? [];
$gridpress_display_settings = $gridpress_display_settings ?? [];

/**
 * Safe base values
 */
$gridpress_id       = absint( $gridpress_card_data['id'] ?? 0 );
$gridpress_url      = $gridpress_card_data['url'] ?? '';
$gridpress_image    = $gridpress_card_data['imag_url'] ?? '';
$gridpress_title    = $gridpress_card_data['title'] ?? '';
$gridpress_short    = $gridpress_card_data['short_title'] ?? '';
$gridpress_author   = $gridpress_card_data['author'] ?? '';
$gridpress_author_id = absint( $gridpress_card_data['author_id'] ?? 0 );

/**
 * Comments (safe int)
 */
$gridpress_comments_count = 0;

if ( $gridpress_card_data['type'] === 'post' ) {
    $gridpress_comments_count = get_comments_number( $gridpress_id );
    $gridpress_author_url     = get_author_posts_url( $gridpress_author_id );
} else {
    $gridpress_comments_count = absint( $gridpress_card_data['comments'] ?? 0 );
    $gridpress_author_url     = '';
}

/**
 * Comments text
 */
$gridpress_comments_num = sprintf(
    /* translators: %s: number of comments */
    _n(
        '%s Comment',
        '%s Comments',
        $gridpress_comments_count,
        'gridpress-grid-layout-design'
    ),
    number_format_i18n( $gridpress_comments_count )
);

/**
 * Terms
 */
$gridpress_terms_class = '';
$gridpress_terms_str   = '';

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

    if ( ! empty( $gridpress_display_settings['top_filter_tax'] ) ) {

        $gridpress_taxonomy = $gridpress_display_settings['top_filter_tax'];

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

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

        $gridpress_terms_class = apply_filters(
            'gridpress_card_item_term_classes',
            $gridpress_terms_class,
            $gridpress_taxonomy,
            $gridpress_id
        );
    }
} else {
    $gridpress_terms_str = sanitize_text_field(
        $gridpress_card_data['category'] ?? ''
    );
}

$gridpress_wrapper_class = 'jc-cards';

if ( ! empty( $gridpress_terms_class ) ) {
    $gridpress_wrapper_class .= ' ' . sanitize_html_class( $gridpress_terms_class );
}

/**
 * Date
 */
$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_wrapper_class ); ?>">
    <div class="jc-cards-holder">
        <div class="jc-cards-meta">
            <?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; ?>

            <div class="jc-cards-info">
                <?php if (
                    ! empty( $gridpress_author )
                    && ! empty( $gridpress_display_settings['show_author'] )
                    && $gridpress_display_settings['show_author'] === 'on'
                ) : ?>
                    <strong class="jc-cards-subtext gridpress-dynm-carditm-author">
                        <?php esc_html_e( 'By', 'gridpress-grid-layout-design' ); ?>
                        <a href="<?php echo esc_url( $gridpress_author_url ); ?>">
                            <?php echo esc_html( $gridpress_author ); ?>
                        </a>
                    </strong>
                <?php endif; ?>

                <?php if (
                    ! empty( $gridpress_display_settings['show_comments'] )
                    && $gridpress_display_settings['show_comments'] === 'on'
                ) : ?>
                    <strong class="jc-cards-subtext">
                        <a href="<?php echo esc_url( $gridpress_url ); ?>#comments"
                           class="gridpress-dynm-carditm-comments">
                            <?php echo esc_html( $gridpress_comments_num ); ?>
                        </a>
                    </strong>
                <?php endif; ?>
            </div>
        </div>

        <div class="jc-cards-image">
            <a href="<?php echo esc_url( $gridpress_url ); ?>">
                <img
                    src="<?php echo esc_url( $gridpress_image ); ?>"
                    alt="<?php echo esc_attr( $gridpress_title ); ?>"
                >

            </a>
        </div>
    </div>

    <div class="jc-cards-textbox">
        <?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; ?>
        <h2>
            <a href="<?php echo esc_url( $gridpress_url ); ?>">
                <?php echo esc_html( $gridpress_short ); ?>
            </a>
        </h2>
    </div>
</div>