<?php 

/**
* Front Three Box Section Hook 
*/
if ( ! function_exists( 'front_three_box_section_hook' ) ) :
    function front_three_box_section_hook() {  ?>
        <div class="newshit-three-box-wrapper pt-4">
            <div class="container">
                <div class="row">
                    <div class="col-12 col-sm-12 col-md-6 col-lg-4 col-xl-4">
                        <div class="newshit-box-items">

                            <?php if( !empty( get_theme_mod( 'box1_title', 'Sport' ) ) ) : ?>
                                <div class="newshit-section-title">
                                    <span><?php echo esc_html( get_theme_mod( 'box1_title', 'Sport' ) ); ?></span>
                                </div>
                            <?php endif; ?>

                            <div class="post-small-items">
                                <?php 
                                if( is_plugin_active( 'web-stories/web-stories.php' ) && 'web-story' === get_theme_mod('box1_post_type', 'post' ) ){
                                    $post_typebox1 = 'web-story';
                                }else{
                                    $post_typebox1 = 'post';
                                }
                                if( 'web-story' == $post_typebox1 && true === get_theme_mod('enable_box1_story_filter' ) && !empty( get_theme_mod( 'box1_story_filter_cat', '' ) )){
                                    $p = new \WP_Query( array(
                                        'post_type' => 'web-story',
                                        'posts_per_page' => get_theme_mod( 'box1_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box1_post_orderby' ),
                                        'order'          => get_theme_mod( 'box1_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'web_story_category',
                                                'terms'    => get_theme_mod( 'box1_story_filter_cat' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }elseif( 'post' == $post_typebox1 && true === get_theme_mod( 'enable_box1_post_filter', true ) && 'one' === get_theme_mod( 'box1_post_filter','one' ) && !empty( get_theme_mod( 'box1_post_cat', '' ) )){
                                    $p = new \WP_Query( array( 
                                        'post_type' => 'post',
                                        'posts_per_page' => get_theme_mod( 'box1_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box1_post_orderby' ),
                                        'order'          => get_theme_mod( 'box1_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'category',
                                                'terms'    => get_theme_mod( 'box1_post_cat' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }elseif( 'post' == $post_typebox1 && true === get_theme_mod( 'enable_box1_post_filter', true ) && 'two' === get_theme_mod('box1_post_filter' ) && !empty( get_theme_mod( 'box1_post_tag', '' ) )){
                                    $p = new \WP_Query( array( 
                                        'post_type' => 'post',
                                        'posts_per_page' => get_theme_mod( 'box1_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box1_post_orderby' ),
                                        'order'          => get_theme_mod( 'box1_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'post_tag',
                                                'terms'    => get_theme_mod( 'box1_post_tag' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }else{
                                    $p = new \WP_Query( array( 
                                        'post_type'      => $post_type,
                                        'posts_per_page' => get_theme_mod( 'box1_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box1_post_orderby' ),
                                        'order'          => get_theme_mod( 'box1_post_order' ),
                                    ));
                                }
                                ?>
                                <?php while ( $p->have_posts() ): $p->the_post(); ?>
                                <div class="post-small-item">
                                    <div class="post-small-image">
                                        <?php if( has_post_thumbnail() ) : ?>
                                            <a href="<?php echo esc_url(the_permalink()); ?>">
                                                <img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' ) ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>">
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                    <div class="post-small-content">
                                        <?php if( true === get_theme_mod( 'enable_box1_meta_items', true ) ) : ?>
                                        <div class="post-small-meta">
                                            <ul>
                                                <?php 

                                                $box1_meta_items = get_theme_mod('box1_meta_items');
                                                $default_meta = array('author', 'date', 'cats');
                                                
                                                // If no values are set, use the default values
                                                if (empty( $box1_meta_items ) || !is_array( $box1_meta_items )) {
                                                    $box1_meta_items = $default_meta;
                                                }
                                                
                                                foreach( $box1_meta_items as $newshit_meta  ) : ?>
                                                    <?php if( $newshit_meta == 'author'  ) : ?>
                                                        <li><?php newshit_posted_by(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'date' ) : ?>
                                                        <li><?php newshit_posted_on(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'comment' && get_comments_number() != 0 ) : ?>
                                                        <li class="comment-number"><?php newshit_comment_count(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'cats' ) : ?>
                                                        <li>
                                                            <?php
                                                                if( 'post' === get_theme_mod( 'box1_post_type', 'post' ) ){
                                                                    newshit_post_first_category();
                                                                }else{
                                                                    newshit_story_post_cat();
                                                                } 
                                                            ?> 
                                                        </li>
                                                    <?php endif; ?>
                                                
                                                <?php  endforeach; ?>
                                            </ul>
                                        </div>
                                        <?php endif; ?>
                                        <h4 class="post-small-title">
                                            <a href="<?php echo esc_url(the_permalink()); ?>"><?php echo newshit_words_limit( get_the_title(), get_theme_mod( 'box1_title_lanth', 20 ) ); ?></a>
                                        </h4>
                                    </div>
                                </div>
                                <?php endwhile; wp_reset_query(); ?>
                            </div>
                        </div>
                    </div>

                    <div class="col-12 col-sm-12 col-md-6 col-lg-4 col-xl-4">
                        <div class="newshit-box-items">
                            <?php if( !empty( get_theme_mod( 'box2_title', 'Health' ) ) ) : ?>
                                <div class="newshit-section-title">
                                    <span><?php echo esc_html( get_theme_mod( 'box2_title', 'Health' ) ); ?></span>
                                </div>
                            <?php endif; ?>

                            <div class="post-small-items">
                                <?php 
                                if( is_plugin_active( 'web-stories/web-stories.php' ) && 'web-story' === get_theme_mod('box2_post_type', 'post' ) ){
                                    $post_typebox2 = 'web-story';
                                }else{
                                    $post_typebox2 = 'post';
                                }
                                if( 'web-story' == $post_typebox2 && true === get_theme_mod('enable_box2_story_filter' ) && !empty( get_theme_mod( 'box2_story_filter_cat', '' ) )){
                                    $p = new \WP_Query( array(
                                        'post_type' => 'web-story',
                                        'posts_per_page' => get_theme_mod( 'box2_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box2_post_orderby' ),
                                        'order'          => get_theme_mod( 'box2_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'web_story_category',
                                                'terms'    => get_theme_mod( 'box2_story_filter_cat' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }elseif( 'post' == $post_typebox2 && true === get_theme_mod( 'enable_box2_post_filter', true ) && 'one' === get_theme_mod( 'box2_post_filter','one' ) && !empty( get_theme_mod( 'box2_post_cat', '' ) )){
                                    $p = new \WP_Query( array( 
                                        'post_type' => 'post',
                                        'posts_per_page' => get_theme_mod( 'box2_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box2_post_orderby' ),
                                        'order'          => get_theme_mod( 'box2_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'category',
                                                'terms'    => get_theme_mod( 'box2_post_cat' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }elseif( 'post' == $post_typebox2 && true === get_theme_mod( 'enable_box2_post_filter', true ) && 'two' === get_theme_mod('box2_post_filter' ) && !empty( get_theme_mod( 'box2_post_tag', '' ) )){
                                    $p = new \WP_Query( array( 
                                        'post_type' => 'post',
                                        'posts_per_page' => get_theme_mod( 'box2_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box2_post_orderby' ),
                                        'order'          => get_theme_mod( 'box2_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'post_tag',
                                                'terms'    => get_theme_mod( 'box2_post_tag' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }else{
                                    $p = new \WP_Query( array( 
                                        'post_type'      => $post_type,
                                        'posts_per_page' => get_theme_mod( 'box2_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box2_post_orderby' ),
                                        'order'          => get_theme_mod( 'box2_post_order' ),
                                    ));
                                }
                                ?>
                                <?php while ( $p->have_posts() ): $p->the_post(); ?>
                                <div class="post-small-item">
                                    <div class="post-small-image">
                                        <?php if( has_post_thumbnail() ) : ?>
                                            <a href="<?php echo esc_url(the_permalink()); ?>">
                                                <img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' ) ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>">
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                    <div class="post-small-content">
                                        <?php if( true === get_theme_mod( 'enable_box2_meta_items', true ) ) : ?>
                                        <div class="post-small-meta">
                                            <ul>
                                                <?php 
                                                $box2_meta_items = get_theme_mod('box2_meta_items');
                                                $default_meta = array('author', 'date', 'cats');
                                                
                                                // If no values are set, use the default values
                                                if (empty( $box2_meta_items ) || !is_array( $box2_meta_items )) {
                                                    $box2_meta_items = $default_meta;
                                                }
                                                foreach( $box2_meta_items as $newshit_meta  ) : ?>
                                                    <?php if( $newshit_meta == 'author'  ) : ?>
                                                        <li><?php newshit_posted_by(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'date' ) : ?>
                                                        <li><?php newshit_posted_on(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'comment' && get_comments_number() != 0 ) : ?>
                                                        <li class="comment-number"><?php newshit_comment_count(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'cats' ) : ?>
                                                        <li>
                                                            <?php
                                                                if( 'post' === get_theme_mod( 'box2_post_type', 'post' ) ){
                                                                    newshit_post_first_category();
                                                                }else{
                                                                    newshit_story_post_cat();
                                                                } 
                                                            ?>
                                                        </li>
                                                    <?php endif; ?>
                                                
                                                <?php  endforeach; ?>
                                            </ul>
                                        </div>
                                        <?php endif; ?>
                                        <h4 class="post-small-title">
                                            <a href="<?php echo esc_url(the_permalink()); ?>"><?php echo newshit_words_limit( get_the_title(), get_theme_mod( 'box2_title_lanth', 20 ) ); ?></a>
                                        </h4>
                                    </div>
                                </div>
                                <?php endwhile; wp_reset_query(); ?>
                            </div>
                        </div>
                    </div>

                    <div class="col-12 col-sm-12 col-md-6 col-lg-4 col-xl-4">
                        <div class="newshit-box-items">

                            <?php if( !empty( get_theme_mod( 'box3_title', 'Technology' ) ) ) : ?>
                                <div class="newshit-section-title">
                                    <span><?php echo esc_html( get_theme_mod( 'box3_title', 'Technology' ) ); ?></span>
                                </div>
                            <?php endif; ?>

                            <div class="post-small-items">
                                <?php 
                                if( is_plugin_active( 'web-stories/web-stories.php' ) && 'web-story' === get_theme_mod('box3_post_type', 'post' ) ){
                                    $post_typebox3 = 'web-story';
                                }else{
                                    $post_typebox3 = 'post';
                                }
                                if( 'web-story' == $post_typebox3 && true === get_theme_mod('enable_box3_story_filter' ) && !empty( get_theme_mod( 'box3_story_filter_cat', '' ) )){
                                    $p = new \WP_Query( array(
                                        'post_type' => 'web-story',
                                        'posts_per_page' => get_theme_mod( 'box3_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box3_post_orderby' ),
                                        'order'          => get_theme_mod( 'box3_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'web_story_category',
                                                'terms'    => get_theme_mod( 'box3_story_filter_cat' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }elseif( 'post' == $post_typebox3 && true === get_theme_mod( 'enable_box3_post_filter', true ) && 'one' === get_theme_mod( 'box3_post_filter','one' ) && !empty( get_theme_mod( 'box3_post_cat', '' ) )){
                                    $p = new \WP_Query( array( 
                                        'post_type' => 'post',
                                        'posts_per_page' => get_theme_mod( 'box3_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box3_post_orderby' ),
                                        'order'          => get_theme_mod( 'box3_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'category',
                                                'terms'    => get_theme_mod( 'box3_post_cat' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }elseif( 'post' == $post_typebox3 && true === get_theme_mod( 'enable_box3_post_filter', true ) && 'two' === get_theme_mod('box3_post_filter' ) && !empty( get_theme_mod( 'box3_post_tag', '' ) )){
                                    $p = new \WP_Query( array( 
                                        'post_type' => 'post',
                                        'posts_per_page' => get_theme_mod( 'box3_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box3_post_orderby' ),
                                        'order'          => get_theme_mod( 'box3_post_order' ),
                                        'tax_query'      => array(
                                            array(
                                                'taxonomy' => 'post_tag',
                                                'terms'    => get_theme_mod( 'box3_post_tag' ),
                                                'field' => 'term_id',
                                            )
                                        )
                                    ));
                                }else{
                                    $p = new \WP_Query( array( 
                                        'post_type'      => $post_type,
                                        'posts_per_page' => get_theme_mod( 'box3_dispaly_item', 4 ),
                                        'orderby'        => get_theme_mod( 'box3_post_orderby' ),
                                        'order'          => get_theme_mod( 'box3_post_order' ),
                                    ));
                                }
                                ?>
                                <?php while ( $p->have_posts() ): $p->the_post(); ?>
                                <div class="post-small-item">
                                    <div class="post-small-image">
                                        <?php if( has_post_thumbnail() ) : ?>
                                            <a href="<?php echo esc_url(the_permalink()); ?>">
                                                <img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' ) ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>">
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                    <div class="post-small-content">
                                        <?php if( true === get_theme_mod( 'enable_box3_meta_items', true ) ) : ?>
                                        <div class="post-small-meta">
                                            <ul>
                                                <?php 
                                                $box3_meta_items = get_theme_mod('box3_meta_items');
                                                $default_meta = array('author', 'date', 'cats');
                                                
                                                // If no values are set, use the default values
                                                if (empty( $box3_meta_items ) || !is_array( $box3_meta_items )) {
                                                    $box3_meta_items = $default_meta;
                                                }
                                                foreach( $box3_meta_items as $newshit_meta  ) : ?>
                                                    <?php if( $newshit_meta == 'author'  ) : ?>
                                                        <li><?php newshit_posted_by(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'date' ) : ?>
                                                        <li><?php newshit_posted_on(); ?></li>
                                                    <?php endif; ?>

                                                    <?php if( $newshit_meta == 'comment' && get_comments_number() != 0 ) : ?>
                                                        <li class="comment-number"><?php newshit_comment_count(); ?></li>
                                                    <?php endif; ?>
                                                    
                                                    <?php if( $newshit_meta == 'cats' ) : ?>
                                                        <li>
                                                            <?php
                                                                if( 'post' === get_theme_mod( 'box3_post_type', 'post' ) ){
                                                                    newshit_post_first_category();
                                                                }else{
                                                                    newshit_story_post_cat();
                                                                } 
                                                            ?>
                                                        </li>
                                                    <?php endif; ?>
                                                
                                                <?php  endforeach; ?>
                                            </ul>
                                        </div>
                                        <?php endif; ?>
                                        <h4 class="post-small-title">
                                            <a href="<?php echo esc_url(the_permalink()); ?>"><?php echo newshit_words_limit( get_the_title(), get_theme_mod( 'box3_title_lanth', 20 ) ); ?></a>
                                        </h4>
                                    </div>
                                </div>
                                <?php endwhile; wp_reset_query(); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php  }
endif;
add_action( 'front_three_box_section_action', 'front_three_box_section_hook' );