<?php

	add_action( 'widgets_init', 'enjoystyle_image_widget_init' );
	function enjoystyle_image_widget_init() {
		register_widget( 'EnjoyStyle_Image_Widget' );
	}

	class EnjoyStyle_Image_Widget extends WP_Widget {

		protected $defaults = array(
			'title'       => '',
			'link_url'    => '',
			'link_target' => '',
			'image_id'    => '',
			'size'        => 'large'
		);

		public function __construct() {
			$widget_details = array(
				'classname'   => 'EnjoyStyle_Image_Widget',
				'description' => __( 'Upload and add an image in any of your widgetized sidebars.', 'enjoystyle' )
			);

			parent::__construct( 'enjoystyle_image_widget', __( '&raquo; Image Block', 'enjoystyle' ), $widget_details );

			add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts')  );
			add_action( 'in_plugin_update_message-'. plugin_basename( __FILE__ ), array( $this, 'update_message'), 10, 2 );
		}

		public function admin_scripts() {
			global $pagenow;

			if ( in_array( $pagenow, array( 'widgets.php', 'customize.php', 'post.php' ) ) ) {
				wp_enqueue_media();
				wp_enqueue_script( 'enjoystyle-image-widget-admin-scripts', get_template_directory_uri() . '/assets/js/admin-scripts.js', array( 'jquery' ) );
			}
		}

		private function sanitize_image_size( $value ) {
			$valid_values = array(
				'full',
				'thumbnail',
				'medium',
				'large',
				'post-thumbnail'
			);

			if( ! in_array( $value, $valid_values ) ) {
				$value = 'large';
			}

			return $value;
		}

		public function register_scripts() {
		}

		public function update_message( $plugin_data, $r ) {
			if ( ! empty( $r->upgrade_notice ) ) {
				printf( '<p style="margin: 3px 0 0 0; border-top: 1px solid #ddd; padding-top: 3px">%s</p>', esc_html($r)->upgrade_notice );
			}
		}

		public function widget( $args, $instance ) {

			$instance = wp_parse_args( (array) $instance, $this->defaults );

			$link_target = $instance['link_target'] == 1 ? 'target="_blank"' : '';
			$image_size  = $instance['size'];
			$image_info  = wp_prepare_attachment_for_js( $instance['image_id'] );
			$image_src   = wp_get_attachment_image_src( intval( $instance['image_id'] ), $image_size );
			$link_url    = $instance['link_url'] ? $instance['link_url'] : $image_info['url'];

			echo wp_kses_post($args['before_widget']);

			if( ! empty( $image_src ) ) :
				?>

				<a href="<?php echo esc_url( $link_url ) ?>" <?php echo esc_html($link_target); ?>>
				<div class="widget-wrap">
				<?php

				if ( $instance['title'] ) {
					echo '<h3 class="widget-title"><span>' . wp_kses_post( apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) ) . '</span></h3>';
				}	

				?>
					<img src="<?php echo esc_url( $image_src[0] ); ?>" alt="<?php echo esc_attr( $image_info['alt'] ); ?>">
				</div>
				</a>

				<?php
			endif;

			echo wp_kses_post($args['after_widget']);
		}

		public function update( $new_instance, $old_instance ) {
			$instance = $old_instance;

			$instance['title']       = sanitize_text_field( $new_instance['title'] );
			$instance['link_url']    = esc_url_raw( $new_instance['link_url'] );
			$instance['link_target'] = intval( $new_instance['link_target'] );
			$instance['image_id']    = intval( $new_instance['image_id'] );
			$instance['size']        = $this->sanitize_image_size( $new_instance['size'] );

			return $instance;
		}

		public function form( $instance ) {

			$instance = wp_parse_args( (array) $instance, $this->defaults );

			$title       = $instance['title'];
			$link_url    = $instance['link_url'];
			$link_target = $instance['link_target'];
			$image_id    = $instance['image_id'];
			$size        = $instance['size'];

			?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
					<?php esc_html_e( 'Title:', 'enjoystyle' ); ?>
				</label>

				<input
					type="text"
					class="widefat"
					id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
					name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
					value="<?php echo esc_attr( $title ); ?>"/>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'link_url' ) ); ?>">
					<?php esc_html_e( 'Link URL', 'enjoystyle' ); ?>
				</label>

				<input
					type="text"
					class="widefat"
					id="<?php echo esc_attr( $this->get_field_id( 'link_url' ) ); ?>"
					name="<?php echo esc_attr( $this->get_field_name( 'link_url' ) ); ?>"
					value="<?php echo esc_url( $link_url ); ?>"/>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'link_target' ) ); ?>">
					<input
						type="checkbox"
						id="<?php echo esc_attr( $this->get_field_id( 'link_target' ) ); ?>"
						name="<?php echo esc_attr( $this->get_field_name( 'link_target' ) ); ?>"
						value="1" <?php checked( $link_target, 1 ); ?> />
					<?php esc_html_e( 'Open link in new tab', 'enjoystyle' ); ?>
				</label>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'image' ) ); ?>">
					<?php esc_html_e( 'Image:', 'enjoystyle' ); ?>
				</label>
				<div class="enjoystyle-image-widget-thumb-container">
					<?php $thumb_src = wp_get_attachment_image_src( intval( $image_id ), 'thumbnail' ); ?>
					<?php if( ! empty( $thumb_src ) ) : ?>
						<img src="<?php echo esc_url( $thumb_src[0] ); ?>" class="enjoystyle-image-widget-thumb" />
					<?php endif; ?>
				</div>

				<input
					type="hidden"
					class="enjoystyle-image-widget-media-id"
					name="<?php echo esc_attr( $this->get_field_name( 'image_id' ) ); ?>"
					id="<?php echo esc_attr( $this->get_field_id( 'image_id' ) ); ?>"
					value="<?php echo intval( $image_id ); ?>"
					/>

				<input
					id="<?php echo esc_attr( $this->get_field_id( 'image' ) ); ?>_button"
					class="enjoystyle-image-widget-upload-button button"
					type="button"
					value="<?php echo esc_attr__( 'Select Image', 'enjoystyle' ); ?>"/>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>">
					<?php esc_html_e( 'Image size:', 'enjoystyle' ); ?>
				</label>
				<select class="enjoystyle-image-widget-size-select" name="<?php echo esc_attr( $this->get_field_name( 'size' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>">
					<option value="large" <?php selected( $size, 'large' ); ?>><?php strip_tags( esc_html_e( 'Large', 'enjoystyle' ) ); ?></option>
					<option value="thumbnail" <?php selected( $size, 'thumbnail' ); ?>><?php strip_tags( esc_html_e( 'Thumbnail', 'enjoystyle' ) ); ?></option>
					<option value="medium" <?php selected( $size, 'medium' ); ?>><?php strip_tags( esc_html_e( 'Medium', 'enjoystyle' ) ); ?></option>
					<option value="full" <?php selected( $size, 'full' ); ?>><?php strip_tags( esc_html_e( 'Full', 'enjoystyle' ) ); ?></option>
					<?php if( get_theme_support( 'post-thumbnails' ) ) : ?>
						<option value="post-thumbnail" <?php selected( $size, 'post-thumbnail' ); ?>><?php strip_tags( esc_html_e( 'Post Thumbnail', 'enjoystyle' ) ); ?></option>
					<?php endif; ?>
				</select>
			</p>
			<?php
		}
	} //EnjoyStyle_Image_Widget
