<?php
/**
 * Elementor Widget
 * @package eleblog
 * @since 1.0.0
 */

namespace Elementor;
class Eleblog_Ads extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * Retrieve Elementor widget name.
	 *
	 * @return string Widget name.
	 * @since 1.0.0
	 * @access public
	 *
	 */
	public function get_name() {
		return 'ele_ads';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @return string Widget title.
	 * @since 1.0.0
	 * @access public
	 *
	 */
	public function get_title() {
		return esc_html__( 'Ads', 'ele-blog' );
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve Elementor widget icon.
	 *
	 * @return string Widget icon.
	 * @since 1.0.0
	 * @access public
	 *
	 */
	public function get_icon() {
		return 'eicon-image-bold';
	}

	/**
	 * Get widget categories.
	 *
	 * Retrieve the list of categories the Elementor widget belongs to.
	 *
	 * @return array Widget categories.
	 * @since 1.0.0
	 * @access public
	 *
	 */
	public function get_categories() {
		return [ 'ele-blog' ];
	}

	/**
	 * Register Elementor widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 * @access protected
	 */
	protected function _register_controls() {

		$this->start_controls_section(
			'settings_sticky',
			[
				'label' => esc_html__( 'Generel Settings', 'ele-blog' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);


		$this->add_control(
			'image',
			[
				'label'   => esc_html__( 'Add Image', 'ele-blog' ),
				'type'    => Controls_Manager::MEDIA,
				'dynamic' => [
					'active' => true,
				],
			]
		);

		$this->add_control(
			'url',
			[
				'label'   => esc_html__( 'Url', 'ele-blog' ),
				'type'    => Controls_Manager::URL,
				'default' => array(
					'url' => '#'
				),
			]
		);

		$this->end_controls_section();


	}

	/**
	 * Render Elementor widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 * @access protected
	 */
	protected function render() {
		$settings  = $this->get_settings_for_display();
		$image_id  = $settings['image']['id'];
		$image_url = wp_get_attachment_image_src( $image_id, 'full', false );
		$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
		?>

        <div class="add-area">
            <a href="<?php echo esc_url( $settings['url']['url'] ); ?>">
            	<img class="w-100"
                 src="<?php echo esc_url( ele_blog_img_resize(  $image_url[0],1120, 200, true  )); ?>"
                 alt="<?php echo esc_attr( $image_alt ); ?>">
            </a>
        </div>

		<?php 
	}
}

Plugin::instance()->widgets_manager->register_widget_type( new Eleblog_Ads() );