<?php
/**
 * Widget Name: AIO Testimonials
 *
 * @package All In One Custom Post
 * @since All In One Custom Post 1.0.0 
 */

if ( ! class_exists( 'vr_widget_testimonial' ) ) {
	class vr_widget_testimonial extends WP_Widget {

		// Widget Construct
		public function __construct() {
			parent::__construct(
				false,
				_x( 'AIO Testimonials' , 'backend', PLUGIN_NAME),
				array(
					'description' => _x( 'Display testimonials custom post.', 'backend', PLUGIN_NAME),
					'classname'   => 'aio-widget-testimonials',
				)
			);
		}

		// Widget Init
		public function widget( $args, $instance ) {
						
			$title			= empty( $instance['title'] ) ? '' : $instance['title'];
			$id 			= empty( $instance['id'] ) ? '' : $instance['id'];
			$category		= empty( $instance['category'] ) ? '' : $instance['category'];
			$posts_per_page	= empty( $instance['posts_per_page'] ) ? '' : $instance['posts_per_page'];
			$module			= empty( $instance['module'] ) ? '' : $instance['module'];
			$column			= empty( $instance['column'] ) ? '' : $instance['column'];
			$animation		= empty( $instance['animation'] ) ? '' : $instance['animation'];
			$ani			= '';

			if ( $animation != '' ) { $animation = 'data-animation="' . $animation . '"'; $ani = 'fx-start'; }

			echo $args['before_widget'];
			if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; }
			?>
		
			<?php
			global $wp_query;
			$wp_query = new WP_Query( array( 
				'p'					    		=> $id,
				'posts_per_page' 	    		=> $posts_per_page,
				'vr_testimonials_categories'	=> $category,
				'post_type' 		    		=> 'vr_testimonials',
				'post_status'					=> 'publish',
				'orderby'						=> 'date',
				'order'							=> 'DESC'
			) );
			?>

			<?php if ( $module == 'slider' ) : ?>
					
				<div class="testimonial-slider clearfix <?php echo sanitize_html_class( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
					<div class="slider-body">
						<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
							<div class="slider-item">
								
								<article class="testimonial-grid">
									<div class="testimonial-body">
										<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
									</div>
									<div class="testimonial-foot clearfix">
										<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
											<div class="thumbnail">
												<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
											</div>
										<?php endif; ?>
										<cite>
											<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> / 
											<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
												<a href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
											<?php else : ?>
												<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
											<?php endif; ?>
										</cite>
									</div>
								</article>

							</div>
						<?php endwhile; ?>
					</div>
				</div>
					
			<?php elseif ( $module == 'list' ) : ?>
					
				<div class="row row-equal fx">
				<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
					
					<article class="testimonial-list column-equal clearfix <?php echo wp_strip_all_tags( $column ); ?> <?php echo sanitize_html_class( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
						<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
							<div class="thumbnail">
								<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
							</div>
						<?php endif; ?>
						<div class="testimonial-body">
							<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
							<cite>
								<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> / 
								<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
									<a class="label-main" href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
								<?php else : ?>
									<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
								<?php endif; ?>
							</cite>
						</div>
					</article>
				
				<?php endwhile; ?>
				</div>
					
			<?php elseif ( $module == 'widget' ) : ?>
				
				<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			
					<article class="testimonial-grid column-equal">
						<div class="testimonial-body">
							<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
						</div>
						<div class="testimonial-foot clearfix">
							<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
								<div class="thumbnail">
									<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
								</div>
							<?php endif; ?>
							<cite>
								<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> / 
								<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
									<a class="label-main" href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
								<?php else : ?>
									<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
								<?php endif; ?>
							</cite>
						</div>
					</article>
				
				<?php endwhile; ?>
					
			<?php else : ?>
				
				<div class="row row-equal fx">
				<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
			
					<article class="testimonial-grid <?php echo wp_strip_all_tags( $column ); ?> <?php echo sanitize_html_class( $ani ); ?>" <?php echo wp_strip_all_tags( $animation ); ?>>
						<div class="testimonial-body">
							<blockquote><i class="fa fa-quote-left"></i><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.quote' ) ); ?><i class="fa fa-quote-right"></i></blockquote>
						</div>
						<div class="testimonial-foot clearfix">
							<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : ?>
								<div class="thumbnail">
									<img width="100" height="100" src="<?php echo esc_url( aio_resize( aio_src( get_the_ID() ), 100, 100, true, true, true ) ); ?>" alt="<?php echo wp_strip_all_tags( get_the_title() ); ?>" />
								</div>
							<?php endif; ?>
							<cite>
								<?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.name' ) ); ?> / 
								<?php if ( vp_metabox( 'vr_testimonials.url' ) != '' ) : ?>
									<a class="label-main" href="<?php echo esc_url( vp_metabox( 'vr_testimonials.url' ) ); ?>"><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></a>
								<?php else : ?>
									<small><?php echo wp_strip_all_tags( vp_metabox( 'vr_testimonials.company' ) ); ?></small>
								<?php endif; ?>
							</cite>
						</div>
					</article>
				
				<?php endwhile; ?>
				</div>

			<?php endif; ?>
				
			<?php wp_reset_query(); ?>
		
			<?php
			echo $args['after_widget'];
		}

		// Form Update
		public function update( $new_instance, $old_instance ) {
			$instance = array();

			$instance['title'] 				= wp_strip_all_tags( $new_instance['title'] );
			$instance['id'] 				= esc_attr( $new_instance['id'] );	
			$instance['category'] 			= esc_attr( $new_instance['category'] );
			$instance['posts_per_page'] 	= esc_attr( $new_instance['posts_per_page'] );
			$instance['module'] 			= esc_attr( $new_instance['module'] );
			$instance['column'] 			= esc_attr( $new_instance['column'] );
			$instance['animation'] 			= esc_attr( $new_instance['animation'] );

			return $instance;
		}

		// Widget Form
		public function form( $instance ) {

			$title			= empty( $instance['title'] ) ? '' : $instance['title'];
			$id 			= empty( $instance['id'] ) ? '' : $instance['id'];
			$category		= empty( $instance['category'] ) ? '' : $instance['category'];
			$posts_per_page	= empty( $instance['posts_per_page'] ) ? '-1' : $instance['posts_per_page'];
			$module			= empty( $instance['module'] ) ? 'grid' : $instance['module'];
			$column			= empty( $instance['column'] ) ? 'col-md-3' : $instance['column'];
			$animation		= empty( $instance['animation'] ) ? '' : $instance['animation'];
			
			?>

			<p>
				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Widget Title :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo 
wp_strip_all_tags( $title ); ?>" style="width:100%;" type="text" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'id' ); ?>"><?php _e( 'Testimonial Id :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" value="<?php echo esc_attr( $id ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Testimonial Category :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" value="<?php echo esc_attr( $category ); ?>" style="width:100%;" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'posts_per_page' ); ?>"><?php _e( 'Posts Per Page :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'posts_per_page' ); ?>" name="<?php echo $this->get_field_name( 'posts_per_page' ); ?>" value="<?php echo absint( $posts_per_page ); ?>" style="width:100%;" type="number" step="1" min="-1" max="" value="-1"/>
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'module' ); ?>"><?php _e( 'Module :', PLUGIN_NAME ); ?></label>
				<select id="<?php echo $this->get_field_id( 'module' ); ?>" name="<?php echo $this->get_field_name( 'module' ); ?>" style="width:100%;">
					<option value="widget" <?php if ( $module == 'widget' ) { echo 'selected="selected"'; } ?>>Widget</option>
					<option value="grid" <?php if ( $module == 'grid' ) { echo 'selected="selected"'; } ?>>Grid</option>
					<option value="list" <?php if ( $module == 'list' ) { echo 'selected="selected"'; } ?>>List</option>
					<option value="slider" <?php if ( $module == 'slider' ) { echo 'selected="selected"'; } ?>>Slider</option>
				</select>
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'column' ); ?>"><?php _e( 'Column :', PLUGIN_NAME ); ?></label>
				<select id="<?php echo $this->get_field_id( 'column' ); ?>" name="<?php echo $this->get_field_name( 'column' ); ?>" style="width:100%;">
					<option value="col-md-1" <?php if ( $column == 'col-md-1' ) { echo 'selected="selected"'; } ?>>1</option>
					<option value="col-md-2" <?php if ( $column == 'col-md-2' ) { echo 'selected="selected"'; } ?>>2</option>
					<option value="col-sm-6 col-md-2" <?php if ( $column == 'col-sm-6 col-md-2' ) { echo 'selected="selected"'; } ?>>2-6</option>
					<option value="col-md-3" <?php if ( $column == 'col-md-3' ) { echo 'selected="selected"'; } ?>>3</option>
					<option value="col-sm-6 col-md-3" <?php if ( $column == 'col-sm-6 col-md-3' ) { echo 'selected="selected"'; } ?>>3-6</option>
					<option value="col-md-4" <?php if ( $column == 'col-md-4' ) { echo 'selected="selected"'; } ?>>4</option>
					<option value="col-sm-6 col-md-4" <?php if ( $column == 'col-sm-6 col-md-4' ) { echo 'selected="selected"'; } ?>>4-6</option>
					<option value="col-md-5" <?php if ( $column == 'col-md-5' ) { echo 'selected="selected"'; } ?>>5</option>
					<option value="col-md-6" <?php if ( $column == 'col-md-6' ) { echo 'selected="selected"'; } ?>>6</option>
					<option value="col-sm-6 col-md-6" <?php if ( $column == 'col-sm-6 col-md-6' ) { echo 'selected="selected"'; } ?>>6-6</option>
					<option value="col-md-7" <?php if ( $column == 'col-md-7' ) { echo 'selected="selected"'; } ?>>7</option>
					<option value="col-md-8" <?php if ( $column == 'col-md-8' ) { echo 'selected="selected"'; } ?>>8</option>
					<option value="col-md-9" <?php if ( $column == 'col-md-9' ) { echo 'selected="selected"'; } ?>>9</option>
					<option value="col-md-10" <?php if ( $column == 'col-md-10' ) { echo 'selected="selected"'; } ?>>10</option>
					<option value="col-md-11" <?php if ( $column == 'col-md-11' ) { echo 'selected="selected"'; } ?>>11</option>	
					<option value="col-md-12" <?php if ( $column == 'col-md-12' ) { echo 'selected="selected"'; } ?>>12</option>					
				</select>
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'animation' ); ?>"><?php _e( 'Animation :', PLUGIN_NAME ); ?></label>
				<select id="<?php echo $this->get_field_id( 'animation' ); ?>" name="<?php echo $this->get_field_name( 'animation' ); ?>" style="width:100%;">
					<option value="" <?php if ( $animation == '' ) { echo 'selected="selected"'; } ?>>None</option>
					<option value="fadeIn" <?php if ( $animation == 'fadeIn' ) { echo 'selected="selected"'; } ?>>fadeIn</option>
					<option value="fadeInUp" <?php if ( $animation == 'fadeInUp' ) { echo 'selected="selected"'; } ?>>fadeInUp</option>
					<option value="fadeInDown" <?php if ( $animation == 'fadeInDown' ) { echo 'selected="selected"'; } ?>>fadeInDown</option>
					<option value="fadeInLeft" <?php if ( $animation == 'fadeInLeft' ) { echo 'selected="selected"'; } ?>>fadeInLeft</option>
					<option value="fadeInRight" <?php if ( $animation == 'fadeInRight' ) { echo 'selected="selected"'; } ?>>fadeInRight</option>
					<option value="rotateFromLeft" <?php if ( $animation == 'rotateFromLeft' ) { echo 'selected="selected"'; } ?>>rotateFromLeft</option>
					<option value="rotateFromRight" <?php if ( $animation == 'rotateFromRight' ) { echo 'selected="selected"'; } ?>>rotateFromRight</option>
					<option value="flipInX" <?php if ( $animation == 'flipInX' ) { echo 'selected="selected"'; } ?>>flipInX</option>
					<option value="flipOutX" <?php if ( $animation == 'flipOutX' ) { echo 'selected="selected"'; } ?>>flipOutX</option>
					<option value="flipInY" <?php if ( $animation == 'flipInY' ) { echo 'selected="selected"'; } ?>>flipInY</option>
					<option value="flipOutY" <?php if ( $animation == 'flipOutY' ) { echo 'selected="selected"'; } ?>>flipOutY</option>
					<option value="bounceIn" <?php if ( $animation == 'bounceIn' ) { echo 'selected="selected"'; } ?>>zoomIn</option>
					<option value="bounceInUp" <?php if ( $animation == 'bounceInUp' ) { echo 'selected="selected"'; } ?>>bounceInUp</option>
					<option value="bounceInDown" <?php if ( $animation == 'bounceInDown' ) { echo 'selected="selected"'; } ?>>bounceInDown</option>
					<option value="bounceInLeft" <?php if ( $animation == 'bounceInLeft' ) { echo 'selected="selected"'; } ?>>bounceInLeft</option>
					<option value="bounceInRight" <?php if ( $animation == 'bounceInRight' ) { echo 'selected="selected"'; } ?>>bounceInRight</option>
				</select>
			</p>

			<?php
		}

	}
	add_action( 'widgets_init', create_function( '', 'register_widget( "vr_widget_testimonial" );' ) );
}