<?php
/**
 * Latest Blog section
 *
 * This is the template for the content of latest_blog section
 *
 * @package Theme Palace
 * @subpackage Kompany
 * @since Kompany 1.0.0
 */
if ( ! function_exists( 'kompany_add_latest_blog_section' ) ) :
    /**
    * Add latest_blog section
    *
    *@since Kompany 1.0.0
    */
    function kompany_add_latest_blog_section() {
        $options = kompany_get_theme_options();
        // Check if latest_blog is enabled on frontpage
        $latest_blog_enable = apply_filters( 'kompany_section_status', true, 'latest_blog_section_enable' );

        if ( true !== $latest_blog_enable ) {
            return false;
        }
        // Get latest_blog section details
        $section_details = array();
        $section_details = apply_filters( 'kompany_filter_latest_blog_section_details', $section_details );

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

        // Render latest_blog section now.
        kompany_render_latest_blog_section( $section_details );
    }
endif;

if ( ! function_exists( 'kompany_get_latest_blog_section_details' ) ) :
    /**
    * latest_blog section details.
    *
    * @since Kompany 1.0.0
    * @param array $input latest_blog section details.
    */
    function kompany_get_latest_blog_section_details( $input ) {
        $options = kompany_get_theme_options();

        // Content type.
        $latest_blog_content_type  = $options['latest_blog_content_type'];
        $content = array();
        switch ( $latest_blog_content_type ) {
            
            case 'page':
                $page_ids = array();
                $author = array();

                for ( $i = 1; $i <= 3; $i++ ) {
                    if ( ! empty( $options['latest_blog_content_page_' . $i] ) ) :
                        $page_ids[] = $options['latest_blog_content_page_' . $i];
                    endif;
                }
                
                $args = array(
                    'post_type'         => 'page',
                    'post__in'          => ( array ) $page_ids,
                    'posts_per_page'    => absint( 3 ),
                    'orderby'           => 'post__in',
                    );                    
            break;

            case 'post':
                $post_ids = array();
                $author = array();

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

            case 'category':
                $cat_id = ! empty( $options['latest_blog_content_category'] ) ? $options['latest_blog_content_category'] : '';
                $args = array(
                    'post_type'         => 'post',
                    'posts_per_page'    => absint( 3 ),
                    'cat'               => absint( $cat_id ),
                    'ignore_sticky_posts'   => true,
                    );                    
            break;
            
            default:
            break;
        }

        // 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['author_id'] = get_post_field('post_author', get_the_ID());
                $page_post['title']     = get_the_title();
                $page_post['url']       = get_the_permalink();
                $page_post['excerpt']   = kompany_trim_content( 15 );
                $page_post['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'large' ) : 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;
// latest_blog section content details.
add_filter( 'kompany_filter_latest_blog_section_details', 'kompany_get_latest_blog_section_details' );


if ( ! function_exists( 'kompany_render_latest_blog_section' ) ) :
  /**
   * Start latest_blog section
   *
   * @return string latest_blog content
   * @since Kompany 1.0.0
   *
   */
   function kompany_render_latest_blog_section( $content_details = array() ) {
        $options = kompany_get_theme_options();

        $section_title = !empty( $options['latest_blog_title'] ) ? $options['latest_blog_title'] : '';

        if ( empty( $content_details ) ) {
            return;
        } ?>
        
        <div id="kompany_latest_blog_section" class="relative page-section">
            <div class="wrapper">
                <?php if( !empty( $section_title ) ): ?>
                    <div class="section-header">
                        <h2 class="section-title"><?php echo esc_html( $section_title ); ?></h2>
                    </div><!-- .section-header -->
                <?php endif; ?>

                <div class="archive-blog-wrapper  col-3 clear">

                    <?php foreach ($content_details as $content ): ?>

                        <article class="has-post-thumbnail">
                            <div class="post-wrapper clear">
                                <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">
                                        <span class="cat-links">
                                            <?php the_category( '', '', $content['id'] ); ?>
                                        </span><!-- .cat-links -->

                                        <?php kompany_posted_on( $content['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['excerpt'] ); ?></p>
                                    </div><!-- .entry-content -->

                                    <div class="admin-profile">
                                        <div class="admin-image">
                                            <a href="<?php echo esc_url( get_author_posts_url( $content['author_id']) ); ?>"><img src="<?php echo get_avatar_url($content['author_id'], ['size' => '70']); ?>" alt="admin"></a>
                                        </div><!-- .admin-image -->

                                        <div class="admin-container">
                                            <div class="admin-header">
                                                <h2 class="admin-name"><a href="<?php echo esc_url( get_author_posts_url( $content['author_id']) ); ?>"><?php echo get_the_author_meta('display_name', $content['author_id']); ?></a></h2>
                                            </div><!-- .admin-header -->
                                        </div><!-- .entry-container -->
                                    </div>
                                </div><!-- .entry-container -->
                            </div>
                        </article>

                    <?php endforeach; ?>

                </div><!-- .archive-blog-wrapper -->
            </div><!-- .wrapper -->
        </div><!-- #latest-blog-section -->

    <?php }
endif;