<?php
/**
* Multi Post section
*
* This is the template for the content of multi post section
*
* @package Theme Palace
* @subpackage Power News
* @since Power News 1.0.0
*/

$options = power_news_get_theme_options();

if( $options['multi_post_section_enable'] !== false ):

    $content_details = array();

    $cat_ids = ! empty( $options['multi_post_col1_content_category'] ) ? $options['multi_post_col1_content_category'] : '';
    $col1_args = array(
        'post_type'         => 'post',
        'posts_per_page'    => 3,
        'cat'               => absint( $cat_ids ),
        'ignore_sticky_posts'   => true,
    );


// Run The Loop.
$query = new WP_Query( $col1_args );
if ( $query->have_posts() ) :
    $content_details['col1']  = array();
    while ( $query->have_posts() ) : $query->the_post();
        $page_post['id']        = get_the_id();
        $page_post['auth_id']   = get_the_author_meta('ID');
        $page_post['title']     = get_the_title();
        $page_post['content']   = get_the_content();
        $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_details['col1'], $page_post );
    endwhile;
endif;
wp_reset_postdata();


$cat_ids = ! empty( $options['multi_post_col2_content_category'] ) ? $options['multi_post_col2_content_category'] : '';
$col2_args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 3,
    'cat'               =>  $cat_ids,
    'ignore_sticky_posts'   => true,
);

// Run The Loop.
$query = new WP_Query( $col2_args );
if ( $query->have_posts() ) :
    $content_details['col2'] = array();
    while ( $query->have_posts() ) : $query->the_post();
        $page_post['id']        = get_the_id();
        $page_post['auth_id']   = get_the_author_meta('ID');
        $page_post['title']     = get_the_title();
        $page_post['content']   = get_the_content();
        $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_details['col2'], $page_post );
    endwhile;
endif;
wp_reset_postdata();

if(!empty($content_details)) :?>

    <div id="power_news_multiple_posts_section" class="multiple-posts-section relative list-layout">
       <?php if ( is_customize_preview()):
        power_news_section_tooltip( 'multiple-posts-section-class' );
    endif; ?>
        <div class="multiple-posts-wrapper clear">
            <div class="section-content">
                <div class="section-header col1">
                    <h2 class="section-title"><?php echo esc_html($options['multi_post_col1_title']); ?></h2>
                </div><!-- .section-header -->

                <?php if(!empty($content_details['col1'])) : ?>
                <div class="world-news">
                    <?php foreach( $content_details['col1'] as $key => $content ) : ?>
                        <article class="has-post-thumbnail">
                            <div class="featured-image" style="background-image: url('<?php echo esc_url($content['image']); ?>');">
                                <a href="<?php echo esc_url($content['url'] ); ?>" class="post-thumbnail-link"></a>
                            </div><!-- .featured-image -->

                            <div class="entry-container">
                                <div class="entry-meta">
                                    <?php power_news_posted_on( $content['id'] ); ?>

                                    <?php if( $key == 0 )  echo power_news_author( $content['auth_id'] );?>
                                </div><!-- .entry-meta -->

                                <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 -->
                        </article>
                    <?php endforeach; ?>
                </div><!-- #world-news -->
            </div><!-- .section-content -->
            <?php endif; ?>
            
            <?php if(!empty($content_details['col2'])) : ?>
            <div class="section-content">
                <div class="section-header col2">
                    <h2 class="section-title"><?php echo esc_html($options['multi_post_col2_title']); ?></h2>
                </div><!-- .section-header -->

                <div class="world-news">
                    <?php foreach( $content_details['col2'] as $key => $content ) : ?>
                        <article class="has-post-thumbnail">
                            <div class="featured-image" style="background-image: url('<?php echo esc_url($content['image']); ?>');">
                                <a href="<?php echo esc_url($content['url'] ); ?>" class="post-thumbnail-link"></a>
                            </div><!-- .featured-image -->

                            <div class="entry-container">
                                <div class="entry-meta">
                                    <?php $tt = power_news_posted_on( $content['id'] ); ?>

                                    <?php if( $key == 0 )  echo power_news_author( $content['auth_id'] );?>
                                </div><!-- .entry-meta -->

                                <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 -->
                        </article>
                    <?php endforeach; ?>
                </div><!-- #world-news -->
            </div><!-- .section-content -->
            <?php endif; ?>
        </div><!-- .col-2 -->
    </div><!-- #multiple-posts -->
    <?php endif; ?>
<?php endif; ?>

