<?php

class Moesia_Dribbble extends WP_Widget {

// constructor
    function moesia_dribbble() {
		$widget_ops = array('classname' => 'moesia_dribbble_widget', 'description' => __( 'Display your latest shots from Dribbble.', 'moesia') );
        parent::__construct(false, $name = __('Moesia FP: Dribbble', 'moesia'), $widget_ops);
		$this->alt_option_name = 'moesia_dribbble_widget';
		
		add_action( 'save_post', array($this, 'flush_widget_cache') );
		add_action( 'deleted_post', array($this, 'flush_widget_cache') );
		add_action( 'switch_theme', array($this, 'flush_widget_cache') );		
    }
	
	// widget form creation
	function form($instance) {

	// Check values
		$title     		= isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
		$dribbble_username   	= isset( $instance['dribbble_username'] ) ? esc_html( $instance['dribbble_username'] ) : '';
		$dribbble_number  = isset( $instance['dribbble_number'] ) ? absint( $instance['dribbble_number'] ) : '';
		$lightbox = isset( $instance['lightbox'] ) ? (bool) $instance['lightbox'] : false;
		$sidebar = isset( $instance['sidebar'] ) ? (bool) $instance['sidebar'] : false;

	?>

	<p>
	<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
	</p>

	<p>
	<label for="<?php echo $this->get_field_id('dribbble_username'); ?>"><?php _e('Enter you Dribbble username', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('dribbble_username'); ?>" name="<?php echo $this->get_field_name('dribbble_username'); ?>" type="text" value="<?php echo $dribbble_username; ?>" />
	</p>

	<p>
	<label for="<?php echo $this->get_field_id('dribbble_number'); ?>"><?php _e('Enter the number of shots you want to display', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('dribbble_number'); ?>" name="<?php echo $this->get_field_name('dribbble_number'); ?>" type="text" value="<?php echo $dribbble_number; ?>" />
	</p>

	<p><input class="checkbox" type="checkbox" <?php checked( $lightbox ); ?> id="<?php echo $this->get_field_id( 'lightbox' ); ?>" name="<?php echo $this->get_field_name( 'lightbox' ); ?>" />
	<label for="<?php echo $this->get_field_id( 'lightbox' ); ?>"><?php _e( 'Display the images in a lightbox when clicked? By default they link to their page on dribbble.com.', 'moesia' ); ?></label></p>

	<p>
	<input class="checkbox" type="checkbox" <?php checked( $sidebar ); ?> id="<?php echo $this->get_field_id( 'sidebar' ); ?>" name="<?php echo $this->get_field_name( 'sidebar' ); ?>" />
	<label for="<?php echo $this->get_field_id( 'sidebar' ); ?>"><?php _e( 'Are you going to use this widget in a sidebar? (Default styling is for front page use)', 'moesia' ); ?></label>
	</p>		

	<?php
	}

	// update widget
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] 			= strip_tags($new_instance['title']);
		$instance['dribbble_username'] 		= strip_tags($new_instance['dribbble_username']);
		$instance['dribbble_number'] 	= intval($new_instance['dribbble_number']);
		$instance['lightbox'] = isset( $new_instance['lightbox'] ) ? (bool) $new_instance['lightbox'] : false;	
		$instance['sidebar'] = isset( $new_instance['sidebar'] ) ? (bool) $new_instance['sidebar'] : false;					
		$this->flush_widget_cache();

		$alloptions = wp_cache_get( 'alloptions', 'options' );
		if ( isset($alloptions['moesia_dribbble']) )
			delete_option('moesia_dribbble');		  
		  
		return $instance;
	}
	
	function flush_widget_cache() {
		wp_cache_delete('moesia_dribbble', 'widget');
	}
	
	// display widget
	function widget($args, $instance) {
		$cache = array();
		if ( ! $this->is_preview() ) {
			$cache = wp_cache_get( 'moesia_dribbble', 'widget' );
		}

		if ( ! is_array( $cache ) ) {
			$cache = array();
		}

		if ( ! isset( $args['widget_id'] ) ) {
			$args['widget_id'] = $this->id;
		}

		if ( isset( $cache[ $args['widget_id'] ] ) ) {
			echo $cache[ $args['widget_id'] ];
			return;
		}

		ob_start();
		extract($args);

		$title 				= ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Dribbble', 'moesia' );
		$title 				= apply_filters( 'widget_title', $title, $instance, $this->id_base );
		$dribbble_username  = isset( $instance['dribbble_username'] ) ? esc_html( $instance['dribbble_username'] ) : '';
		$dribbble_number  	= isset( $instance['dribbble_number'] ) ? absint( $instance['dribbble_number'] ) : '';
		$lightbox 			= isset( $instance['lightbox'] ) ? $instance['lightbox'] : false;
		$sidebar 		= isset( $instance['sidebar'] ) ? $instance['sidebar'] : false;
?>
		<section id="dribbble" class="dribbble-area">

			<?php if ($sidebar != 1) : //Remove the container in case of sidebar use?>
			<div class="container">
			<?php endif; ?>
				<?php if ( $title ) echo $before_title . '<span class="wow bounce">' . $title . '</span>' . $after_title; ?>
				<?php $rss = fetch_feed("http://dribbble.com/players/$dribbble_username/shots.rss"); ?>
				<?php $items = $rss->get_items(0, $rss->get_item_quantity($dribbble_number)); ?>
				<div class="dribbble-container clearfix">
						
					<?php foreach ( $items as $item ):
						$title = $item->get_title();
						$link = $item->get_permalink();
						$description = $item->get_description();
						
						preg_match("/src=\"(http.*(jpg|jpeg|gif|png))/", $description, $image_url);
						$image = $image_url[1]; ?>

						<?php if ($sidebar != 1) : ?>
						<div class="col-md-3 col-sm-6 col-xs-6 wow fadeInUp dribbble-shot">
						<?php else : ?>
						<div class="col-md-4 dribbble-shot">
						<?php endif; ?>		
							<?php if ($lightbox != 1) : ?>																						
							<a href="<?php echo esc_url($link); ?>" title="<?php echo $title;?>"><img src="<?php echo esc_url($image); ?>" alt="<?php echo $title;?>"/></a>
							<?php else : ?>
							<a href="<?php echo esc_url($image); ?>" rel="prettyPhoto" title="<?php echo $title;?>"><img src="<?php echo esc_url($image); ?>" alt="<?php echo $title;?>"/></a>
							<?php endif; ?>							
						</div>																													 	
					<?php endforeach; ?>
									
				</div>			
			<?php if ($sidebar != 1) : //Remove the container in case of sidebar use?>
			</div>
			<?php endif; ?>
		</section>		
	<?php

		if ( ! $this->is_preview() ) {
			$cache[ $args['widget_id'] ] = ob_get_flush();
			wp_cache_set( 'moesia_dribbble', $cache, 'widget' );
		} else {
			ob_end_flush();
		}
	}
	
}