<?php
// Ensure the necessary functions exist before proceeding
if (function_exists('united_blog_get_option')) {

    $enable_ticker_posts = united_blog_get_option('enable_ticker_posts');

    if ($enable_ticker_posts) {
        // Fetch ticker options
        $ticker_posts_cat = united_blog_get_option('ticker_posts_cat');
        $no_of_ticker_posts = united_blog_get_option('no_of_ticker_posts', 4);
        $ticker_posts_orderby = united_blog_get_option('ticker_posts_orderby', 'date');
        $ticker_posts_order = united_blog_get_option('ticker_posts_order', 'desc');
        $enable_ticker_posts_author_meta = united_blog_get_option('enable_ticker_posts_author_meta');
        $select_ticker_posts_author_meta = united_blog_get_option('select_ticker_posts_author_meta');
        $ticker_posts_author_meta_title = united_blog_get_option('ticker_posts_author_meta_title');
        $enable_ticker_posts_date_meta = united_blog_get_option('enable_ticker_posts_date_meta');
        $select_ticker_posts_date = united_blog_get_option('select_ticker_posts_date');
        $single_ticker_post_date_meta_title = united_blog_get_option('single_ticker_post_date_meta_title');
        $select_ticker_posts_date_format = united_blog_get_option('select_ticker_posts_date_format') ?: get_option('date_format');
        $show_ticker_category = united_blog_get_option('show_ticker_category');
        $ticker_category_color_display = united_blog_get_option('ticker_category_color_display');
        $ticker_category_limit = united_blog_get_option('ticker_category_limit');
        $show_ticker_post_meta_icon = united_blog_get_option('show_ticker_post_meta_icon');
        $ticker_post_meta = united_blog_get_option('ticker_post_meta');

        // Ticker meta settings
        $ticker_post_meta_settings = array(
            'date_format' => esc_attr($select_ticker_posts_date_format),
            'show_icons' => esc_attr($show_ticker_post_meta_icon),
        );

        // Convert id to ID if needed
        if ('id' === $ticker_posts_orderby) {
            $ticker_posts_orderby = 'ID';
        }

        // Query arguments for ticker posts
        $post_args = array(
            'post_type' => 'post',
            'posts_per_page' => absint($no_of_ticker_posts),
            'post_status' => 'publish',
            'no_found_rows' => true,
            'ignore_sticky_posts' => true,
            'orderby' => esc_attr($ticker_posts_orderby),
            'order' => esc_attr($ticker_posts_order),
        );

        // Validate and sanitize category IDs
        if (!empty($ticker_posts_cat)) {
            $ticker_posts_cat = array_map('absint', (array) $ticker_posts_cat);
            $post_args['tax_query'] = array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'term_id',
                    'terms' => $ticker_posts_cat,
                    'operator' => 'IN',
                ),
            );
        }

        // Slider settings
        $data_slider = array();
        $data_slider['spaceBetween'] = 0;
        $data_slider['speed'] = 9000;

        // Query for ticker posts
        $ticker_posts = new WP_Query($post_args);
        if ($ticker_posts->have_posts()) :
            $ticker_label_text = united_blog_get_option('ticker_label_text', __('Latest Blog', 'united-blog'));
            ?>
            <div class="united-ticker-bar">
                <div class="wrapper">
                    <div class="united-ticker-area">
                        <?php if (united_blog_get_option('enable_ticker_label', true)) : ?>
                            <div class="united-ticker-title">
                                <span class="ticker-loader"></span>
                                <?php
                                if (!empty($ticker_label_text)) :
                                    echo esc_html($ticker_label_text);
                                else :
                                    esc_html_e('Latest Blog', 'united-blog');
                                endif;
                                ?>
                            </div>
                        <?php endif; ?>
                        <div class="united-ticker-content">
                            <div class="united-ticker-init swiper"
                                 data-ticker='<?php echo esc_attr(json_encode($data_slider)); ?>'>
                                <div class="swiper-wrapper united-ticker-init-wrapper">
                                    <?php
                                    while ($ticker_posts->have_posts()) :
                                        $ticker_posts->the_post();
                                        ?>
                                        <div class="swiper-slide united-ticker-init-item">
                                            <article id="ticker-post-<?php the_ID(); ?>" <?php post_class('united-article united-article-ticker'); ?>>
                                                <?php if (has_post_thumbnail()) : ?>
                                                    <div class="entry-image">
                                                        <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                                                            <?php
                                                            $alt_text = the_title_attribute(array('echo' => false));
                                                            the_post_thumbnail(
                                                                'thumbnail',
                                                                array('alt' => esc_attr($alt_text))
                                                            );
                                                            ?>
                                                        </a>
                                                    </div>
                                                <?php endif; ?>
                                                <div class="entry-details">
                                                    <?php
                                                    if ($show_ticker_category) {
                                                        united_blog_post_categories(
                                                            esc_attr($ticker_category_color_display),
                                                            absint($ticker_category_limit)
                                                        );
                                                    }
                                                    ?>
                                                    <h3 class="entry-title entry-title-xsmall">
                                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                    </h3>
                                                    <div class="entry-meta-wrapper hide-on-tablet hide-on-mobile">
                                                        <?php united_blog_post_meta_info($ticker_post_meta, $ticker_post_meta_settings); ?>
                                                    </div>
                                                </div>
                                            </article>
                                        </div>
                                    <?php
                                    endwhile;
                                    wp_reset_postdata();
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php
        else :
            echo '<p>' . esc_html__('No recent posts available.', 'united-blog') . '</p>';
        endif;
    }
}
?>
