<?php
/**
* Slider
*
* @package GridRead WordPress Theme
* @copyright Copyright (C) 2025 ThemesDNA
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
* @author ThemesDNA <themesdna@gmail.com>
*/

if ( ! function_exists( 'gridread_slider' ) ) :

function gridread_slider() { ?>

<?php gridread_before_slider(); ?>

<div class="gridread-slider-wrapper">
<div class="gridread-boxview-wrapper">
    <?php
    $slider_length = 10;
    if ( gridread_get_option('slider_length') ) {
        $slider_length = gridread_get_option('slider_length');
    }

    $slider_post_type = 'recentposts';
    if ( gridread_get_option('slider_post_type') ) {
        $slider_post_type = gridread_get_option('slider_post_type');
    }

    $slider_cat = gridread_get_option('slider_cat');
    $slider_tag = gridread_get_option('slider_tag');

    $slider_post_orderby = 'date';
    if ( gridread_get_option('slider_post_orderby') ) {
        $slider_post_orderby = gridread_get_option('slider_post_orderby');
    }

    $slider_post_order = 'DESC';
    if ( gridread_get_option('slider_post_order') ) {
        $slider_post_order = gridread_get_option('slider_post_order');
    }

    $slider_dateformat = 'F j, Y';
    if ( gridread_get_option('slider_dateformat') ) {
        $slider_dateformat = gridread_get_option('slider_dateformat');
    }

    if(($slider_post_type === 'catposts') && $slider_cat) {
        $slider_query = new WP_Query( array( 'orderby' => $slider_post_orderby, 'order' => $slider_post_order, 'posts_per_page' => $slider_length, 'nopaging' => false, 'post_status' => array( 'publish' ), 'ignore_sticky_posts' => true, 'post_type' => array( 'post' ), 'cat' => $slider_cat, 'no_found_rows' => true ) );
    } elseif(($slider_post_type === 'tagposts') && $slider_tag) {
        $slider_query = new WP_Query( array( 'orderby' => $slider_post_orderby, 'order' => $slider_post_order, 'posts_per_page' => $slider_length, 'nopaging' => false, 'post_status' => array( 'publish' ), 'ignore_sticky_posts' => true, 'post_type' => array( 'post' ), 'tag_id' => $slider_tag, 'no_found_rows' => true ) );
    } else {
        $slider_query = new WP_Query( array( 'orderby' => $slider_post_orderby, 'order' => $slider_post_order, 'posts_per_page' => $slider_length, 'nopaging' => false, 'post_status' => array( 'publish' ), 'ignore_sticky_posts' => true, 'post_type' => array( 'post' ), 'no_found_rows' => true ) );
    }

    if ($slider_query->have_posts()) : ?>

    <?php if ( gridread_get_option('show_slider_heading') ) { ?>
    <?php if ( gridread_get_option('slider_heading') ) : ?>
    <div class="gridread-slider-header"><div class="gridread-slider-header-inside"><h2 class="gridread-slider-heading"><span class="gridread-slider-heading-inside"><?php echo esc_html( gridread_get_option('slider_heading') ); ?></span></h2></div></div>
    <?php else : ?>
    <div class="gridread-slider-header"><div class="gridread-slider-header-inside"><h2 class="gridread-slider-heading"><span class="gridread-slider-heading-inside"><?php esc_html_e( 'Featured Posts', 'gridread' ); ?></span></h2></div></div>
    <?php endif; ?>
    <?php } ?>

    <div class="gridread-slider-posts gridread-clearfix">
    <div class="gridread-posts-carousel">
    <div class="owl-carousel owl-theme">
    <?php while ($slider_query->have_posts()) : $slider_query->the_post();  ?>

        <div class="gridread-slide-item">
        <div id="gridread-slider-post-<?php the_ID(); ?>" class="gridread-slider-post gridread-360w-360h-slider-thumbnail gridread-square-thumbnail">
        <div class="gridread-slider-post-inside">

        <?php if(has_post_thumbnail()) { ?>
            <div class="gridread-slider-post-thumbnail gridread-slider-post-block">
                <?php if ( gridread_get_option('remove_thumbnail_slider_link') ) { ?>
                    <?php the_post_thumbnail('gridread-360w-360h-image', array('class' => 'gridread-slider-post-thumbnail-img', 'title' => the_title_attribute('echo=0'))); ?>
                <?php } else { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" class="gridread-slider-post-thumbnail-link" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_post_thumbnail('gridread-360w-360h-image', array('class' => 'gridread-slider-post-thumbnail-img', 'title' => the_title_attribute('echo=0'))); ?></a>
                <?php } ?>

                <?php if ( !(gridread_get_option('hide_thumbnail_slider_data')) ) { ?>
                <?php if ( !(gridread_get_option('hide_post_title_slider')) || (gridread_get_option('show_slide_cats') && has_category() && ('post' == get_post_type())) || gridread_get_option('show_slide_author') || !(gridread_get_option('hide_slide_date')) || (gridread_get_option('show_slide_comments') && ( ! post_password_required() && ( comments_open() || get_comments_number() ) )) ) { ?>
                <div class="gridread-slider-thumbnail-overlay gridread-clearfix">
                <div class="gridread-slider-thumbnail-overlay-inside gridread-clearfix">
                <div class="gridread-slider-post-details gridread-slider-post-block">
                <?php
                if(gridread_get_option('show_slide_cats') && has_category()) {
                if ( 'post' == get_post_type() ) {
                    /* translators: used between list items, there is a space */
                    $categories_list = get_the_category_list( ' ' );
                    if ( $categories_list ) {
                        /* translators: 1: list of categories. */
                        printf( '<div class="gridread-slider-post-categories">' . __( '<span class="gridread-sr-only">Posted in </span>%1$s', 'gridread' ) . '</div>', $categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                    }
                }
                }
                ?>

                <?php if ( !(gridread_get_option('hide_post_title_slider')) ) { ?>
                    <?php if ( gridread_get_option('remove_post_title_slider_link') ) { ?>
                        <h3 class="gridread-slider-post-title"><?php the_title(); ?></h3>
                    <?php } else { ?>
                        <h3 class="gridread-slider-post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_title(); ?></a></h3>
                    <?php } ?>
                <?php } ?>

                <?php if ( gridread_get_option('show_slide_author') || !(gridread_get_option('hide_slide_date')) || (gridread_get_option('show_slide_comments') && ( ! post_password_required() && ( comments_open() || get_comments_number() ) )) ) { ?>
                <div class="gridread-slider-post-footer gridread-slider-post-block">
                <div class="gridread-slider-post-footer-inside">
                <?php if(gridread_get_option('show_slide_author')) { ?><span class="gridread-slider-post-author gridread-slider-post-meta"><a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php echo esc_html( get_the_author() ); ?></a></span><?php } ?>
                <?php if(!(gridread_get_option('hide_slide_date'))) { ?><span class="gridread-slider-post-date gridread-slider-post-meta"><?php echo esc_html( get_the_date($slider_dateformat) ); ?></span><?php } ?>
                <?php if(gridread_get_option('show_slide_comments') && ( ! post_password_required() && ( comments_open() || get_comments_number() ) )) { ?><span class="gridread-slider-post-comments gridread-slider-post-meta"><?php comments_popup_link( sprintf( wp_kses( /* translators: %s: post title */ __( '0 Comment<span class="gridread-sr-only"> on %s</span>', 'gridread' ), array( 'span' => array( 'class' => array(), ), ) ), wp_kses_post( get_the_title() ) ) ); ?></span><?php } ?>
                </div>
                </div>
                <?php } ?>
                </div>
                </div>
                </div>
                <?php } ?>
                <?php } ?>
            </div>
        <?php } else { ?>
            <div class="gridread-slider-post-thumbnail gridread-slider-post-thumbnail-default gridread-slider-post-block">
                <?php if ( gridread_get_option('remove_thumbnail_slider_link') ) { ?>
                    <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/no-image-360-360.jpg' ); ?>" class="gridread-slider-post-thumbnail-img"/>
                <?php } else { ?>
                    <a href="<?php echo esc_url( get_permalink() ); ?>" class="gridread-slider-post-thumbnail-link" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>"><img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/no-image-360-360.jpg' ); ?>" class="gridread-slider-post-thumbnail-img"/></a>
                <?php } ?>

                <?php if ( !(gridread_get_option('hide_thumbnail_slider_data')) ) { ?>
                <?php if ( !(gridread_get_option('hide_post_title_slider')) || (gridread_get_option('show_slide_cats') && has_category() && ('post' == get_post_type())) || gridread_get_option('show_slide_author') || !(gridread_get_option('hide_slide_date')) || (gridread_get_option('show_slide_comments') && ( ! post_password_required() && ( comments_open() || get_comments_number() ) )) ) { ?>
                <div class="gridread-slider-thumbnail-overlay gridread-clearfix">
                <div class="gridread-slider-thumbnail-overlay-inside gridread-clearfix">
                <div class="gridread-slider-post-details gridread-slider-post-block">
                <?php
                if(gridread_get_option('show_slide_cats') && has_category()) {
                if ( 'post' == get_post_type() ) {
                    /* translators: used between list items, there is a space */
                    $categories_list = get_the_category_list( ' ' );
                    if ( $categories_list ) {
                        /* translators: 1: list of categories. */
                        printf( '<div class="gridread-slider-post-categories">' . __( '<span class="gridread-sr-only">Posted in </span>%1$s', 'gridread' ) . '</div>', $categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                    }
                }
                }
                ?>

                <?php if ( !(gridread_get_option('hide_post_title_slider')) ) { ?>
                    <?php if ( gridread_get_option('remove_post_title_slider_link') ) { ?>
                        <h3 class="gridread-slider-post-title"><?php the_title(); ?></h3>
                    <?php } else { ?>
                        <h3 class="gridread-slider-post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php /* translators: %s: post title. */ echo esc_attr( sprintf( __( 'Permanent Link to %s', 'gridread' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_title(); ?></a></h3>
                    <?php } ?>
                <?php } ?>

                <?php if ( gridread_get_option('show_slide_author') || !(gridread_get_option('hide_slide_date')) || (gridread_get_option('show_slide_comments') && ( ! post_password_required() && ( comments_open() || get_comments_number() ) )) ) { ?>
                <div class="gridread-slider-post-footer gridread-slider-post-block">
                <div class="gridread-slider-post-footer-inside">
                <?php if(gridread_get_option('show_slide_author')) { ?><span class="gridread-slider-post-author gridread-slider-post-meta"><a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php echo esc_html( get_the_author() ); ?></a></span><?php } ?>
                <?php if(!(gridread_get_option('hide_slide_date'))) { ?><span class="gridread-slider-post-date gridread-slider-post-meta"><?php echo esc_html( get_the_date($slider_dateformat) ); ?></span><?php } ?>
                <?php if(gridread_get_option('show_slide_comments') && ( ! post_password_required() && ( comments_open() || get_comments_number() ) )) { ?><span class="gridread-slider-post-comments gridread-slider-post-meta"><?php comments_popup_link( sprintf( wp_kses( /* translators: %s: post title */ __( '0 Comment<span class="gridread-sr-only"> on %s</span>', 'gridread' ), array( 'span' => array( 'class' => array(), ), ) ), wp_kses_post( get_the_title() ) ) ); ?></span><?php } ?>
                </div>
                </div>
                <?php } ?>
                </div>
                </div>
                </div>
                <?php } ?>
                <?php } ?>
            </div>
        <?php } ?>

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

    <?php endwhile; ?>
    </div>
    </div>
    </div>

    <?php wp_reset_postdata();  // Restore global post data stomped by the_post().
    endif; ?>
</div>
</div>

<?php gridread_after_slider(); ?>

<?php }

endif;


function gridread_slider_area() {
    if ( gridread_is_slider_active() ) {
        if ( 'front-page' === gridread_get_option('slider_display') ) {
            if ( is_front_page() && is_home() && !is_paged() ) {
                gridread_slider();
            }
        } elseif ( 'front-page-static' === gridread_get_option('slider_display') ) {
            if ( is_front_page() && is_singular() ) {
                gridread_slider();
            }
        } elseif ( 'blog-posts-index' === gridread_get_option('slider_display') ) {
            if ( is_home() && !is_paged() ) {
                gridread_slider();
            }
        } elseif ( 'everywhere' === gridread_get_option('slider_display') ) {
                gridread_slider();
        } else {
            if ( is_front_page() && is_home() && !is_paged() ) {
                gridread_slider();
            }
        }
    }
}