<?php

if ( ! defined( 'ABSPATH' ) ) exit;
$gridpress_grid_id = !empty( $gridpress_grid_id ) ? $gridpress_grid_id : '';
$gridpress_grid_style = !empty( $gridpress_grid_style ) ? $gridpress_grid_style : '';
if(!empty($gridpress_grid_style)){
    $gridpress_grid_style = preg_replace( '/[^0-9]/', '', (string) $gridpress_grid_style );
    $gridpress_grid_style = absint( $gridpress_grid_style );
}
$gridpress_style = ( $gridpress_grid_style > 9 ) ? $gridpress_grid_style : '0' . $gridpress_grid_style;

$gridpress_adition_class = '';

if ( $gridpress_style === '16' || $gridpress_style === '17' ) {
    $gridpress_adition_class = 'addition';
} elseif ( $gridpress_style === '29' || $gridpress_style === '30' ) {
    $gridpress_adition_class = 'addition-2';
}
?>
<div class="gridpress-dynm-cards-section jc-cards-section" data-id="<?php echo esc_attr( $gridpress_grid_id ); ?>">
<div class="gridpress-dynm-cardscon jc-cards-container">
<?php if ( ! empty( $gridpress_top_terms ) ) : ?>
   <?php
    $gridpress_ul_classes = array(
        'jc-cards-filters',
        'gridpress-dynm-topterms-filtr',
    );

    if ( ! empty( $gridpress_adition_class ) ) {
        $gridpress_ul_classes[] = sanitize_html_class( $gridpress_adition_class );
    }

    if (isset( $gridpress_posts_data_type ) && sanitize_key( $gridpress_posts_data_type ) === 'selected' ) {
        $gridpress_ul_classes[] = 'gridpress-dynm-slected-postfiltrs';
    }
    ?>

    <ul class="<?php echo esc_attr( implode( ' ', $gridpress_ul_classes ) ); ?>">
        <li class="active">
            <a href="javascript:;" data-id="all">
                <?php esc_html_e( 'All', 'gridpress-grid-layout-design' ); ?>
                <sup><?php echo isset( $gridpress_total_posts ) ? absint( $gridpress_total_posts ) : 0; ?></sup>
            </a>
        </li>
        <?php
        foreach ( $gridpress_top_terms as $gridpress_term_id => $gridpress_term_itm ) :
            $gridpress_term_id   = absint( $gridpress_id ?? $gridpress_term_id );
            $gridpress_term_name = is_array( $gridpress_term_itm ) ? ( $gridpress_term_itm['name'] ?? '' ) : '';
            $gridpress_term_cnt  = is_array( $gridpress_term_itm ) ? ( $gridpress_term_itm['count'] ?? 0 ) : 0;
            ?>
            <li>
                <a href="javascript:;" data-id="<?php echo esc_attr( $gridpress_term_id ); ?>">
                    <?php
                    // Output the safely escaped translated string, then concatenate the HTML element tags
                    echo esc_html( $gridpress_term_name ) . ' <sup>' . esc_attr( $gridpress_term_cnt ) . '</sup>';
                    ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

<?php
// Build the class string without translation functions, keeping it readable
$gridpress_wrapper_class = sprintf(
    /* translators: 1: Grid ID integer, 2: Grid design style class suffix string. */
    __( 'gridpress-dynm-cards-holdrcon gridpress-dynm-grid-%1$d jc-cards-style%2$s', 'gridpress-grid-layout-design' ),
    absint( $gridpress_grid_id ),
    sanitize_html_class( $gridpress_style )
);
?>

<div class="<?php echo esc_attr( $gridpress_wrapper_class ); ?>">
<?php
if ( in_array( $gridpress_grid_style, array( '24', '25' ), true ) ) {
    ?>
    <div class="jc-cards-holder">
    <?php
}