<?php

// FLICKR WIDGET
add_action( 'widgets_init', function() { return register_widget( 'toppic_flickr_widget' ); } );

if ( ! class_exists( 'toppic_flickr_widget' ) ) {
	class toppic_flickr_widget extends WP_Widget{
		
		function __construct() {
			$widget_ops = array(
				'classname' => 'toppic_flickr_widget',
				'description' => esc_html__('Display the Flickr photos','toppic')
			);
			parent::__construct('toppic_flickr_widget', esc_html__('Flickr widget','toppic'), $widget_ops);
		}
		
		function widget( $args, $instance ){
			extract($args);
			$title = apply_filters('widget_title', $instance['title']); if(empty($title)) $title="Flickr";
			$display = $instance['display'];
			$id = $instance['id'];
			$number = $instance['number']; if(empty($number)) $number = 10;

			echo wp_kses_post( $before_widget);
			echo wp_kses_post( $before_title.$title.$after_title ); ?>
			
			<div class="flickr-widget clearfix">
				<script type="text/javascript" src="https://www.flickr.com/badge_code_v2.gne?count=<?php echo esc_js($number); ?>&amp;display=latest&amp;size=s&amp;layout=x&amp;source=user&amp;user=<?php echo esc_js($id); ?>"></script>
			</div>
			
			<?php echo wp_kses_post( $after_widget );
		}

		function update($new_instance, $old_instance){
			$instance = $old_instance;
			$instance['title'] = strip_tags($new_instance['title']);
			$instance['display'] = $new_instance['display'];
			$instance['id'] = $new_instance['id'];
			$instance['number'] = $new_instance['number'];
			return $instance;
		}

		function form($instance){
			$instance = wp_parse_args((array)$instance, array('title'=>'', 'text'=>'', 'title_link'=>''));
			$title = strip_tags($instance['title']);
			$display = isset( $instance['display'] ) ? $instance['display'] : false;
			$id = isset( $instance['id'] ) ? $instance['id'] : false;
			$number = isset( $instance['number'] ) ? $instance['number'] : false;

			$text = format_to_edit($instance['text']);?>
			<p>
				<label><?php esc_html_e('Title:','toppic')?><input class="widefat" id="<?php echo esc_attr( $this->get_field_id(strtolower('title'))) ?>" name="<?php echo esc_attr( $this->get_field_name(strtolower('title'))) ?>" type="text" value="<?php echo esc_attr($title)?>" />
				</label>
			</p>
			<p>
				<label><?php printf( esc_html__( 'Flickr ID ( %1$sidGettr%2$s )', 'toppic' ), '<a target="_blank" href="http://www.idgettr.com">', '</a>' ) ?><input class="widefat" id="<?php echo esc_attr( $this->get_field_id(strtolower('id'))) ?>" name="<?php echo esc_attr( $this->get_field_name(strtolower('id'))) ?>" type="text" value="<?php echo esc_attr($id)?>" />
				</label>
			</p>
			<p>
				<label><?php esc_html_e('Number of photos:','toppic')?><input class="widefat" id="<?php echo esc_attr( $this->get_field_id(strtolower('number'))) ?>" name="<?php echo esc_attr( $this->get_field_name(strtolower('number'))) ?>" type="text" value="<?php echo esc_attr($number)?>" /></label>
			</p>
			<?php
		}
	}
}
