<?php
/**
 * Testimonials Widget
 * @package Ultra WordPress Theme
 * @since 1.0
 * @author WPExplorer : http://www.wpexplorer.com
 * @copyright Copyright (c) 2012, WPExplorer
 * @link http://www.wpexplorer.com
 * @author WPExplorer : http://www.wpexplorer.com
 */
 
class wpex_testimonials_widget extends WP_Widget {
	
    /** constructor */
    function wpex_testimonials_widget() {
        parent::WP_Widget(false, $name = __('Symple - Testimonials','wpex'), array( 'description' => __( 'Displays your testimonials members in normal or random order.', 'wpex' ) ) );
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        $number = apply_filters('widget_number', $instance['number']);
		$order = apply_filters('widget_order', $instance['order']);  
              echo $before_widget;
                  if ( $title )
                        echo $before_title . $title . $after_title; ?>
							<div class="wpex-widget-testimonials clearfix flex-loading flexslider">
                            	<ul class="slides">
									<?php
									// Load necessary slider scripts
									wp_enqueue_script('flexslider', WPEX_JS_DIR .'/flexslider.js', array('jquery'), '2', true);
									wp_enqueue_script('wpex-testimonials-widget', get_template_directory_uri() .'/js/testimonials-widget.js', array('jquery','flexslider'), true);
									// Loop through posts
                                    global $post;
                                    $args = array(
                                        'post_type' => 'testimonials',
                                        'numberposts' => $number,
                                        'orderby' => $order,
										'no_found_rows' => true
                                    );
                                    $myposts = get_posts( $args );
                                    foreach( $myposts as $post ) : setup_postdata($post); ?>
                                    <li>
                                    	<div class="wpex-widget-testimonials-slide">
											<?php if( has_post_thumbnail() ) { ?>
                                                <a href="<?php the_permalink(); ?>" title="<?php echo get_post_meta( get_the_ID(), 'wpex_testimonial_by', true ); ?>"><img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ), 80, 80, true ); ?>" alt="<?php the_title(); ?>" /></a>
                                            <?php } ?>
                                                <div class="widget-testimonials-content clearfix">
                                                    <div class="widget-testimonials-content-title"><?php the_title(); ?></div>
                                                    <p>
														<?php
                                                        // Display excerpt 
                                                        if ( !empty( $post->post_excerpt ) ) {
                                                            echo get_the_excerpt();
                                                        } else {
                                                            echo wp_trim_words( strip_shortcodes( get_the_content() ), 18);
                                                                echo '<a href="'. get_permalink() .'" title="'. get_the_title() .'">'. __('read more','wpex') .'&rarr;</a>';
                                                        } ?>
                                                    </p>
                                                    <?php if ( get_post_meta( get_the_ID(), 'wpex_testimonial_by', true )  !== '') { ?>
                                                        <div class="widget-testimonials-content-author">
                                                            <span class="icon-user"></span><?php echo get_post_meta( get_the_ID(), 'wpex_testimonial_by', true ); ?>
                                                        </div>
                                                    <?php } ?>
                                                </div><!-- .widget-testimonials-content -->
                                            </div><!-- .wpex-widget-testimonials-slide -->
                                        </li>
                                    <?php endforeach; wp_reset_postdata(); ?>
                                </ul>
							</div>
              <?php echo $after_widget;
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
	$instance = $old_instance;
	$instance['title'] = strip_tags($new_instance['title']);
	$instance['number'] = strip_tags($new_instance['number']);
	$instance['order'] = strip_tags($new_instance['order']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {	
	    $instance = wp_parse_args( (array) $instance, array(
			'title' => __('Testimonials','wpex'),
			'number' => '-1',
			'order'=> 'ASC'
		));					
        $title = esc_attr($instance['title']);
        $number = esc_attr($instance['number']);
        $order = esc_attr( $instance['order'] ); ?>
        
        
         <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wpex'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title','wpex'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        
        <p>
        <label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Random or Recent?', 'wpex'); ?></label>
        <br />
        <select class='wpex-select' name="<?php echo $this->get_field_name('order'); ?>" id="<?php echo $this->get_field_id('order'); ?>">
        <option value="ASC" <?php if($order == 'ASC') { ?>selected="selected"<?php } ?>><?php _e('Recent', 'wpex'); ?></option>
        <option value="rand" <?php if($order == 'rand') { ?>selected="selected"<?php } ?>><?php _e('Random', 'wpex'); ?></option>
        </select>
        </p>
        
		<p>
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to Show:', 'wpex'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
        </p>
                
        <?php
    }


} // class wpex_testimonials_widget
// register Recent Posts widget
add_action('widgets_init', create_function('', 'return register_widget("wpex_testimonials_widget");')); ?>