<?php 

class Moesia_Flickr extends WP_Widget {

	function moesia_flickr() {
		$widget_ops = array('classname' => 'moesia_flickr_widget', 'description' => __( 'Show the latest photos from your Flickr account.', 'moesia') );
        parent::__construct(false, $name = __('Moesia FP: Flickr', 'moesia'), $widget_ops);
		$this->alt_option_name = 'moesia_flickr_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') );
	}
	
	function widget($args, $instance) {
		$cache = array();
		if ( ! $this->is_preview() ) {
			$cache = wp_cache_get( 'moesia_flickr', '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'] : __( 'Photos from Flickr', 'moesia' );
		$title 			= apply_filters( 'widget_title', $title, $instance, $this->id_base );
		$flickr_id 		= isset( $instance['id'] ) ? esc_html( $instance['id'] ) : '';
		$flickr_count	= isset( $instance['number'] ) ? absint( $instance['number'] ) : '';
		$sidebar 		= isset( $instance['sidebar'] ) ? $instance['sidebar'] : false;
		
		?>
		<section id="mflickr" class="mflickr-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; ?>
				<div class="flickr-feed">
					<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=<?php echo $flickr_count; ?>&amp;display=latest&amp;size=m&amp;layout=x&amp;source=user&amp;user=<?php echo $flickr_id; ?>&amp;raw=1"></script>	
				</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_flickr', $cache, 'widget' );
			} else {
				ob_end_flush();
			}
	
	}
	
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] 		= strip_tags($new_instance['title']);
		$instance['id'] 		= strip_tags($new_instance['id']);
		$instance['number'] 	= intval($new_instance['number']);
		$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_flickr']) )
			delete_option('moesia_flickr');		  
		  
		return $instance;
	}
	
	function flush_widget_cache() {
		wp_cache_delete('moesia_flickr', 'widget');
	}	
	
	function form($instance) {
	
		$title     		= isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
		$flickr_id   	= isset( $instance['id'] ) ? esc_html( $instance['id'] ) : '';
		$flickr_count 	= isset( $instance['number'] ) ? absint( $instance['number'] ) : '';
		$sidebar = isset( $instance['sidebar'] ) ? (bool) $instance['sidebar'] : false;
	
		?>
		
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php  _e('Title', 'moesia'); ?>:
			<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" class="widefat" value="<?php echo $title; ?>" /></label>
		</p>
				
		
		<p>
			<label for="<?php echo $this->get_field_id('id'); ?>"><?php _e('Flickr ID - you can get yours from <a target="_blank" href="http://www.idgettr.com">here</a>):', 'moesia'); ?>
			<input id="<?php echo $this->get_field_id('id'); ?>" name="<?php echo $this->get_field_name('id'); ?>" type="text" class="widefat" value="<?php echo $flickr_id; ?>" /></label>
		</p>
		
		
		<p>
			<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Enter the number of images you want to display (max. 10 photos):', 'moesia'); ?>
			<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" class="widefat" value="<?php echo $flickr_count; ?>" /></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
	}
}