<?php
if ( ! function_exists( 'moment_home_popular_photo_array' ) ) :
    /**
     * showcase Slider
     *
     * @since Moment Pro 1.0.0
     *
     * @param null
     * @return null
     *
     */
    function moment_home_popular_photo_array() {
        global $moment_customizer_all_values;
        $moment_popular_photo_title = $moment_customizer_all_values['moment-home-popular-photo-main-title'];
        $moment_popular_photo_sub_title = $moment_customizer_all_values['moment-home-popular-photo-sub-title'];
        $moment_popular_photo_number = $moment_customizer_all_values['moment-home-popular-photo-number'];
        // print_r($moment_popular_photo_number);die('hello');
        $moment_popular_photo_category = $moment_customizer_all_values['moment-home-popular-photo-category'];
        $moment_numbers_of_words = $moment_customizer_all_values['moment-number-of-words'];
        if( 1 != $moment_customizer_all_values['moment-home-popular-photo-section-enable'] ){
            return null;
        }
        ?>

        <section class="wrapper wrap-latestpost">
            <div class="container overhidden">
                <div class="row">
                    <div class="column-md-12">
                        <header class="title-section salientthemes-animate slideInDown" data-wow-delay="0s">
                            <h2><?php echo esc_html( $moment_popular_photo_title );?></h2>
                            <h3><?php echo esc_html( $moment_popular_photo_sub_title );?></h3>
                        </header>
                    </div>
                </div>
            </div>
        
            <div class="overhidden">
                <div class="row">
                    <div class="carousel-group">
                        <?php
                            $moment_home_about_args = array(
                                'cat' => absint($moment_popular_photo_category),
                                'post_type' => 'post',
                                'posts_per_page'=>3,
                                // 'meta_key' => 'wpb_post_views_count', 
                                 'order' => 'DESC',

                            );
                            // echo $moment_home_about_args->page_count;die('here');
                            
                        $moment_home_about_post_query = new WP_Query($moment_home_about_args);
                        if ($moment_home_about_post_query->have_posts()) :
                            $clearfix = 1;
                            $data_delay = 0;
                            $i=1;
                            while ($moment_home_about_post_query->have_posts()) : $moment_home_about_post_query->the_post();
                                if(has_post_thumbnail()){
                                    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'moment-costume-medium' );
                                    $url = $thumb['0'];
                                }
                                else{
                                    $url = get_template_directory_uri().'/assets/img/no-image-430-300.png';
                                }
                                $data_wow_delay = 'data-wow-delay='.$data_delay.'s';
                                ?>
                                    <div class="carousel-item singlethumb">
                                        <div class="column-sd-12 column-xsd-12 overhidden carousel-item">
                                            <div class="block-section small-post salientthemes-animate slideInDown" data-wow-delay="0.5s">
                                                <div class="block-container clearblock">
                                                    <!-- <div class="thumb-overlay"></div> -->
                                                    <div class="thumb-holder">
                                                        <img src="<?php echo esc_url($url)?>">
                                                    </div>
                                                    <div class="block-overlay-content block-overlay-hover">
                                                        <div class="vmiddle-holder">
                                                            <div class="vmiddle">
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="box text-center">
                                                        <?php 
                                                            $moment_single_post_cat = get_the_category();
                                                            $moment_post = esc_attr($moment_single_post_cat[0]->name);
                                                            $moment_category_id = get_cat_ID($moment_post);
                                                            $moment_category_link = get_category_link( $moment_category_id );
                                                            
                                                        ?>
                                                      <div class="category cat9"><span><a href="<?php echo (esc_url($moment_category_link));?>"><?php
                                                        echo $moment_post;
                                                      ?>
                                                      </a></span></div>
                                                      <h4 class="post-title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h4>
                                                      <div class="meta"><span class="date">                                                            
                                                        <?php 
                                                            $archive_day   = get_the_time('d');
                                                            ?>
                                                            <a href="<?php echo get_day_link('', '', $archive_day); ?>"><i class="fa fa-calendar"></i> <?php echo get_the_date();?></a>
                                                                </span><span class="comments">                                                            
                                                            <a href="<?php the_permalink(); ?>">
                                                                <i class="fa fa-comment"></i> 
                                                                <?php
                                                                    $commentscount = get_comments_number();
                                                                    if($commentscount == 1): $commenttext = 'comment'; endif;
                                                                    if($commentscount > 1 || $commentscount == 0): $commenttext = 'comments'; endif;
                                                                    echo $commentscount.' '.$commenttext;
                                                                ?>
                                                            </a></span></div>
                                                      <p>
                                                         <?php 
                                                         if ( has_excerpt() ) {
                                                             the_excerpt();
                                                         } else {
                                                             $content_blog = get_the_content();
                                                             echo moment_words_count( $moment_numbers_of_words, $content_blog );
                                                         }
                                                         ?>
                                                        </p>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                 <?php $i++; ?> 
                            <?php endwhile; ?>
                            <?php endif; wp_reset_postdata(); 
                        ?>
                    </div>
                </div>
            </div>
        </section>

        <?php
    }
endif;
add_action( 'moment_homepage', 'moment_home_popular_photo_array', 60 );
