<?php

class Moesia_Insta extends WP_Widget {

// constructor
    function moesia_insta() {
		$widget_ops = array('classname' => 'moesia_insta_widget', 'description' => __( 'Display your latest photos from Instagram.', 'moesia') );
        parent::__construct(false, $name = __('Moesia FP: Instagram', 'moesia'), $widget_ops);
		$this->alt_option_name = 'moesia_insta_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'] ) : '';
		$insta_username   	= isset( $instance['insta_username'] ) ? esc_html( $instance['insta_username'] ) : '';
		$insta_token   	= isset( $instance['insta_token'] ) ? esc_html( $instance['insta_token'] ) : '';
		$insta_number  = isset( $instance['insta_number'] ) ? absint( $instance['insta_number'] ) : '';
		$sidebar = isset( $instance['sidebar'] ) ? (bool) $instance['sidebar'] : false;

	?>

	<p>
		<?php _e('To authorize the app for Instagram and get your User ID and Acces Token, ', 'moesia'); ?><a href="https://instagram.com/oauth/authorize/?client_id=467ede5a6b9b48ae8e03f4e2582aeeb3&amp;redirect_uri=http://instafeedjs.com&amp;response_type=token" target="_blank">go here</a>

	<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('insta_username'); ?>"><?php _e('Enter you Instagram user id', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('insta_username'); ?>" name="<?php echo $this->get_field_name('insta_username'); ?>" type="text" value="<?php echo $insta_username; ?>" />
	</p>

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

	<p>
	<label for="<?php echo $this->get_field_id('insta_number'); ?>"><?php _e('Enter the number of photos you want to display', 'moesia'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('insta_number'); ?>" name="<?php echo $this->get_field_name('insta_number'); ?>" type="text" value="<?php echo $insta_number; ?>" />
	</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['insta_username'] = strip_tags($new_instance['insta_username']);
		$instance['insta_token'] = strip_tags($new_instance['insta_token']);
		$instance['insta_number'] 	= intval($new_instance['insta_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_insta']) )
			delete_option('moesia_insta');		  
		  
		return $instance;
	}
	
	function flush_widget_cache() {
		wp_cache_delete('moesia_insta', 'widget');
	}
	
	// display widget
	function widget($args, $instance) {
		$cache = array();
		if ( ! $this->is_preview() ) {
			$cache = wp_cache_get( 'moesia_insta', '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'] : __( 'Instagram', 'moesia' );
		$title 			= apply_filters( 'widget_title', $title, $instance, $this->id_base );
		$insta_username = isset( $instance['insta_username'] ) ? esc_html( $instance['insta_username'] ) : '';
		$insta_token  	= isset( $instance['insta_token'] ) ? esc_html( $instance['insta_token'] ) : '';
		$insta_number  	= isset( $instance['insta_number'] ) ? absint( $instance['insta_number'] ) : '';
		$sidebar 		= isset( $instance['sidebar'] ) ? $instance['sidebar'] : false;
?>
		<section id="instagram" class="instagram-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 if ($insta_username && $insta_token) : ?>
					<div id="instafeed"></div>
				<?php endif; ?>
			<?php if ($sidebar != 1) : //Remove the container in case of sidebar use?>
			</div>
			<?php endif; ?>
			<script type="text/javascript">
				var userFeed = new Instafeed({
					get: 'user',
					userId: <?php echo $insta_username; ?>,
					accessToken: '<?php echo $insta_token; ?>',
					limit: <?php echo $insta_number; ?>,
					<?php 
						if ($sidebar != 1) :
							$cols = 'col-md-3 col-sm-6 col-xs-6';
						else :
							$cols = 'col-md-4';
						endif;
					?>
					template: '<div class="<?php echo $cols; ?> insta-photo"><a href="{{link}}" target="_blank"><img src="{{image}}" /></a></div>',
					resolution: 'low_resolution'

				});
				userFeed.run();
			</script>			
		</section>		
	<?php

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