<?php
/**
 * Advertisement Image widget.
 *
 * @package Nevo
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Advertisement Image widget.
 *
 * Class Nevo_Advertisement_Image
 */
class Nevo_Advertisement_Image extends Nevo_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {

		$this->widget_cssclass    = 'widget_qc_image';
		$this->widget_description = esc_html__( 'Add your Advertisement here', 'nevo' );
		$this->widget_name        = esc_html__( '*Νevo* Advertisement Image', 'nevo' );
		$this->settings = array(
			'title'       => array(
				'type'    => 'text',
				'default' => '',
				'label'   => esc_html__( 'Title:', 'nevo' ),
			),
			'image_link'    => array(
				'type'    => 'url',
				'default' => '',
				'label'   => esc_html__( 'Advertisement Image Link', 'nevo' ),
			),
			'width'       => array(
				'type'    => 'number',
				'default' => '',
				'col'     => 'col-1',
				'label'   => esc_html__( 'Width:', 'nevo' ),
			),
			'height'       => array(
				'type'    => 'number',
				'default' => '',
				'col'     => 'col-2',
				'label'   => esc_html__( 'Height:', 'nevo' ),
			),
			'alt'         => array(
				'type'    => 'text',
				'default' => '',
				'label'   => esc_html__( 'Custom Image Alt:', 'nevo' ),
			),
			'text_align' => array(
				'type'    => 'select',
				'default' => 'text-start',
				'label'   => esc_html__( 'Text Align', 'nevo' ),
				'choices' => array(
					'text-start'   => __( 'Left', 'nevo' ),
					'text-center' => __( 'Center', 'nevo' ),
					'text-end'  => __( 'Right', 'nevo' ),
				),
			),
			'image_url'   => array(
				'type'    => 'image',
				'default' => '',
				'label'   => esc_html__( 'Advertisement Image', 'nevo' ),
			),
			'separator1'      => array(
				'type'    => 'separator',
				'default' => 1,
			),
		);

		parent::__construct();

	}

	/**
	 * Output widget.
	 *
	 * @see WP_Widget
	 *
	 * @param array $args     Arguments.
	 * @param array $instance Widget instance.
	 */
	public function widget( $args, $instance ) {
		$title      = apply_filters( 'widget_title', ! empty( $instance['title'] ) ? $instance['title'] : '' );
		$image_link = ! empty( $instance['image_link'] ) ? esc_url( $instance['image_link'] ) : '';
		$width      = ! empty( $instance['width'] ) ? esc_attr( $instance['width'] ) : 'auto';
		$height     = ! empty( $instance['height'] ) ? esc_attr( $instance['height'] ) : 'auto';
		$image_alt  = ! empty( $instance['alt'] ) ? esc_attr( $instance['alt'] ) : '';
		$image_url  = ! empty( $instance['image_url'] ) ? esc_url( $instance['image_url'] ) : '';
		$text_align = isset( $instance['text_align'] ) ? esc_attr( $instance['text_align'] ) : 'text-start';

		$this->widget_start( $args );

		if ( ! empty( $title ) ) {
			echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
			//echo wp_kses_post( $args['before_title'] ) . esc_html( $title ) . wp_kses_post( $args['after_title'] );
		}

		$output = '';

		if ( ! empty( $image_url ) ) {
			if ( empty( $image_alt ) ) {
				$image_id  = attachment_url_to_postid( $image_url );
				$image_alt = esc_attr( get_post_meta( $image_id, '_wp_attachment_image_alt', true ) );
			}

			$output .= '<div class="qc-content ' . esc_attr( $text_align ) . '">';
			if ( ! empty( $image_link ) ) {
				$output .= '<a href="' . esc_url( $image_link ) . '" class="single_ad_image" target="_blank" rel="nofollow">';
			}
			$output .= '<img src="' . esc_url( $image_url ) . '" width="' . ( $width === 'auto' ? '' : esc_attr( $width ) . 'px' ) . '" height="' . ( $height === 'auto' ? '' : esc_attr( $height ) . 'px' ) . '" alt="' . esc_attr( $image_alt ) . '" style="width:' . esc_attr( $width ) . '; height:' . esc_attr( $height ) . ';">';
			if ( ! empty( $image_link ) ) {
				$output .= '</a>';
			}
			$output .= '</div>';

			echo wp_kses_post( $output );
		}

		$this->widget_end( $args );
	}
}