<?php
/**
 * Buzz_Agency Testimonial Section Functions And Definations
 *
 * @package Buzz_Agency
 */
function buzz_agency_testimonial_callback(){
	$testimonial_option = get_theme_mod('buzz_agency_homepage_setting_testimonial_option',0);
	$testimonial_category = get_theme_mod( 'buzz_agency_testimonial_section_cat',0 );
	$number = get_theme_mod('buzz_agency_testimonial_num',5);
	$image_bg = get_theme_mod('buzz_agency_testimonial_image','');
	if($testimonial_option == '1'): 
?>	
	<section class="testimonial-section">
	    <div class="testimonial-content-wrapper">
	        <div class="testimonial-item-wrapper os-animation" data-os-animation="slideInLeft">


	        	<?php $testimonial_page  = get_theme_mod('buzz_agency_testimonial_page',0); ?>

	        		<?php   if( !empty( $testimonial_page ) ): 

	        			$args = array (                                 
	        			'page_id'           => absint( $testimonial_page ),
	        			'post_status'       => 'publish',
	        			'post_type'         => 'page',
	        			);

	        			$loop_testimonial = new WP_Query($args);

	        			if ( $loop_testimonial->have_posts() ) : ?>

				            <header class="entry-header heading">

				            		<?php while ($loop_testimonial->have_posts()) : $loop_testimonial->the_post();?>

				               			 <h2 class="entry-title"> <?php the_title(); ?> </h2>

									<?php endwhile; 
							 		   wp_reset_postdata();?>

				            </header>

				<?php endif;
				endif;
				?> 

	            <div class="owl-carousel owl-theme testimonial-slider">
					<?php
					if( !empty( $testimonial_category) ) {
						$loop = new WP_Query(
							array(
								'post_type' => 'post',    
								'category_name' => esc_html($testimonial_category),
								'posts_per_page' => absint( $number ),  
							)
						);
					}else{
						$loop = new WP_Query( array( 'post_type'=>'post','posts_per_page'=>absint( $number ), ) );
					}   
					if($loop->have_posts() ) {

						while($loop->have_posts() ) {
							$loop->the_post();
							$image = wp_get_attachment_url( get_post_thumbnail_id());
							?> 
	                <div class="testimonial-item">
	                    <div class="entry-content">
	                        <p><?php echo esc_html(wp_trim_words(get_the_content(),35,'...')); ?></p>
	                    </div>
	                    <div class="testimonial-author-wrapper">
	                        <figure class="author-img">
	                           <img src="<?php echo esc_url($image);?>" />
	                        </figure>
	                        <div class="testimonial-author-info">
	                            <h4 class="author-name">
	                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
	                            </h4>
	                            <div class="testimonial-author-rating">
	                                <img src="<?php echo esc_url( get_template_directory_uri() )?>/assets/img/rating.png" alt="">
	                            </div>
	                        </div>
	                    </div>

	                </div>
	               		<?php 
	               	}
	               	wp_reset_postdata();
	               }
	               ?>  
	            </div>
	        </div>

			<?php if( !empty( $image_bg) ) { ?>         
		        <figure class="featured-image os-animation" data-os-animation="slideInRight"
		            data-os-animation-delay="0.4s">
		             <img src="<?php echo esc_url($image_bg);?>" />
		        </figure>
		    <?php } ?>    
	    </div>

	</section>
	
	<?php endif;   ?>	
<?php
}
add_action('buzz_agency_testimonial_callback_action','buzz_agency_testimonial_callback');		