<?php
/**
 * Breakingnews section
 *
 * This is the template for the content of breakingnews section
 *
 * @package Theme Palace
 * @subpackage Newstation
 * @since Newstation 1.0.0
 */
if ( ! function_exists( 'newstation_add_breakingnews_section' ) ) :
    /**
    * Add breakingnews section
    *
    *@since Newstation 1.0.0
    */
    function newstation_add_breakingnews_section() {
    	$options = newstation_get_theme_options();
        // Check if breakingnews is enabled on frontpage
        $breakingnews_enable = apply_filters( 'newstation_section_status', true, 'breakingnews_section_enable' );

        if ( true !== $breakingnews_enable ) {
            return false;
        }
        // Get breakingnews section details
        $section_details = array();
        $section_details = apply_filters( 'newstation_filter_breakingnews_section_details', $section_details );

        if ( empty( $section_details ) ) {
            return;
        }

        // Render breakingnews section now.
        newstation_render_breakingnews_section( $section_details );
    }
endif;

if ( ! function_exists( 'newstation_get_breakingnews_section_details' ) ) :
    /**
    * breakingnews section details.
    *
    * @since Newstation 1.0.0
    * @param array $input breakingnews section details.
    */
    function newstation_get_breakingnews_section_details( $input ) {
        $options = newstation_get_theme_options();

        $content = array();
        
         $post_ids = array();

                for ( $i = 1; $i <= 4; $i++ ) {
                    if ( ! empty( $options['breakingnews_content_post_' . $i] ) )
                        $post_ids[] = $options['breakingnews_content_post_' . $i];
                }
                $args = array(
                    'post_type'         => 'post',
                    'post__in'          => ( array ) $post_ids,
                    'posts_per_page'    => absint( 4 ),
                    'orderby'           => 'post__in',
                    'ignore_sticky_posts'   => true,
                    );

            // Run The Loop.
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) : 
                while ( $query->have_posts() ) : $query->the_post();
                    $page_post['title']     = get_the_title();
                     $page_post['id']        = get_the_id();
                    $page_post['url']       = get_the_permalink();
                    $page_post['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'post-thumbnail' ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-590x650.jpg';

                    // Push to the main array.
                    array_push( $content, $page_post );
                endwhile;
            endif;
            wp_reset_postdata();
            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// breakingnews section content details.
add_filter( 'newstation_filter_breakingnews_section_details', 'newstation_get_breakingnews_section_details' );


if ( ! function_exists( 'newstation_render_breakingnews_section' ) ) :
  /**
   * Start breakingnews section
   *
   * @return string breakingnews content
   * @since Newstation 1.0.0
   *
   */
   function newstation_render_breakingnews_section( $content_details = array() ) {
        $options = newstation_get_theme_options();
        $title = ! empty( $options['breakingnews_title'] ) ? $options['breakingnews_title'] : '';

        if ( empty( $content_details ) ) {
            return;
        } ?>

        <div id="newstation_breakingnews_section" class="relative page-section">
                <div class="wrapper">
                <?php if (!empty( $title )): ?>

                    <h2 class="news-title"><?php echo esc_html( $title ); ?></h2>
                    
                <?php endif; ?>
                    
                    <div class="breaking-news-slider" data-slick='{"slidesToShow": 3, "slidesToScroll": 1, "infinite": true, "speed": 800, "dots": false, "arrows":false, "autoplay": false, "draggable": true, "fade": false, "adaptiveHeight": true }'>
                        
                        <?php foreach ($content_details as $content ): ?>

                            <article>
                            <div class="breaking-news-wrapper">
                                <div class="featured-image" style="background-image: url('<?php echo esc_url( $content['image'] ) ?>');"></div>
                                <div class="entry-container">
                                    <div class="entry-meta">
                                        <?php newstation_posted_on( $content['id'] ); ?>
                                        
                                        <?php the_category( '', '', $content['id'] ); ?>
                                    </div>
                                    <header class="entry-header">
                                        <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ); ?>"><?php echo esc_html( $content['title'] ); ?></a></h2>
                                    </header>
                                </div><!-- .entry-container -->
                            </div>
                        </article>
                            
                        <?php endforeach; ?>

                    </div><!-- section-content -->
                </div>
            </div>
        
    <?php }
endif;