<?php
/**
* Media Posts Section
*
* This is the template for the content of media posts section
*
* @package Theme Palace
* @subpackage Power News
* @since Power News 1.0.0
*/

$options = power_news_get_theme_options();

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

$content_details = array();
$content_details['video'] = array();

    $post_ids = array();

    if ( ! empty( $options['media_video_post_content_post'] ) ) :
        $post_ids[] = $options['media_video_post_content_post'];
    endif;

    $args = array(
        'post_type'            => 'post',
        'post__in'             => ( array ) $post_ids,
        'posts_per_page'       => 1,
        '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['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['video'], $page_post );
    endwhile;
    endif;
    wp_reset_postdata();


    $content_details['media_posts'] = array();
    $cat_ids = ! empty( $options['media_post_content_category'] ) ? $options['media_post_content_category'] : '';
    $args1 = array(
        'post_type'         => 'post',
        'posts_per_page'    => 4,
        'cat'               => absint( $cat_ids ),
        'ignore_sticky_posts'   => true,
    );                    

// Run The Loop.
$query = new WP_Query( $args1 );
if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post();
$media_posts_page_post['id']        = get_the_id();
$media_posts_page_post['auth_id']   = get_the_author_meta('ID');
$media_posts_page_post['title']     = get_the_title();
$media_posts_page_post['content']   = get_the_content();
$media_posts_page_post['url']       = get_the_permalink();
$media_posts_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['media_posts'], $media_posts_page_post );
endwhile;
endif;
wp_reset_postdata();
?>


<div id="power_news_media_posts_section" class="media-posts-section relative grid-layout">
    <?php if ( is_customize_preview()):
        power_news_section_tooltip( 'media-posts-section-class' );
    endif; ?>
    <?php if ( ! empty( $options['media_post_title'] ) ) : ?>
        <div class="section-header">
            <h2 class="section-title"><?php echo esc_html( $options['media_post_title'] ); ?></h2>
        </div><!-- .section-header -->
    <?php endif; ?>

    <div class="media-posts-wrapper col-2 clear">
        <div class="half-width grid-layout">

            <?php foreach ( $content_details['video'] as $content ) :
             ?>
             <article class="has-post-thumbnail">
                <div class="featured-image" style="background-image: url('<?php echo esc_url( $content['image'] ); ?>');">
                    <div class="overlay"></div> 
                    <?php if ( !empty( $options['media_video_post_link' ] )): ?>

                        <div class="video-button">
                            <a href="<?php echo esc_url( $options['media_video_post_link'] ); ?>" class="popup-youtube">
                                <?php echo power_news_get_svg( array( 'icon' => 'play' ) ); ?>
                            </a>
                        </div><!-- .video-button -->

                    <?php endif ?>

                </div><!-- .featured-image -->

                <div class="entry-container">
                    <div class="entry-meta">
                        <?php power_news_posted_on( $content['id'] ); ?>
                            <?php 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 class="entry-content">
                        <p><?php echo esc_html( $content['content'] ); ?></p>
                    </div><!-- .entry-content -->

                </div><!-- .entry-container -->
            </article>

            <?php endforeach; ?>

        </div><!-- .half-width -->

        <div class="half-width widget_recent_news">
            <ul>
                <?php foreach( $content_details['media_posts'] as $content ) : ?>
                    <li>
                        <a href="<?php echo esc_url( $content['url'] ); ?>"><img src="<?php echo esc_url( $content['image'] ); ?>" alt="<?php echo esc_attr( $content['title'] ); ?>"></a>
                        <div class="entry-container">
                            <div class="entry-meta">
                                <span class="cat-links">
                                    <?php the_category( '', '', $content['id'] ); ?>
                                </span><!-- .cat-links -->
                            </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 -->
                    </li>
                <?php endforeach; ?>
            </ul>
        </div><!-- .half-width -->
    </div><!-- .media-posts-wrapper -->
</div><!-- #media-posts -->

<?php endif; ?>