<?php

class Guava_Author_Widget extends WP_Widget
{
     public function __construct()
     {
          parent::__construct(
               'guava-widget',
               __( 'CT Author', 'guava' ),
               array( 'description' => __( 'Best displayed in Home page.', 'guava' ) )
          );
      }
    
     public function widget( $args, $instance )
     {
          extract( $args );
        if(!empty($instance))
       { 
          ?>
              <section  class="widget author-widget">
                  <h2 class="widget-title"><span><?php echo esc_html( $instance['title'] );?></span></h2>
                  <figure class="author">
                      <img src="<?php echo esc_url( $instance['image_uri'] );?>">
                  </figure>
                  <p><?php echo wp_kses_post( $instance['description'] );?></p>
              </section>
          <?php
        }   
     }

     public function update( $new_instance, $old_instance ){
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['description'] = wp_kses_post( $new_instance['description'] );
        $instance['image_uri'] = esc_url_raw( $new_instance['image_uri'] );
        return $instance;
     }

     public function form($instance ){
          ?>
              <p>
                 <label for="<?php echo $this->get_field_id('image_uri'); ?>">
                     <?php _e( 'Image', 'guava' ); ?>
                 </label>
                  <br />
                 <?php
                     if (isset($instance['image_uri']) && $instance['image_uri'] != '' ) :
                         echo '<img class="custom_media_image" src="' . esc_url( $instance['image_uri'] ) . '" style="margin:0;padding:0;max-width:100px;float:left;display:inline-block" /><br />';
                     endif;
                 ?>

                 <input type="text" class="widefat custom_media_url" name="<?php echo $this->get_field_name('image_uri'); ?>" id="<?php echo $this->get_field_id('image_uri'); ?>" value="<?php 
                   if (isset($instance['image_uri']) && $instance['image_uri'] != '' ) :
                     echo esc_url( $instance['image_uri'] );
                    endif;
                  ?>" style="margin-top:5px;">
                 <input type="button" class="button button-primary custom_media_button" id="custom_media_button" name="<?php echo $this->get_field_name('image_uri'); ?>" value="<?php esc_attr_e('Upload Image','guava')?>" style="margin-top:5px;" />
             </p>
             <p>
                 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title', 'guava' ); ?></label><br />
                 <input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" value="<?php
                  if (isset($instance['title']) && $instance['title'] != '' ) :
                    echo esc_attr($instance['title']);
                   endif;

                   ?>" class="widefat" />
             </p>

             <p>
                 <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e( 'Description', 'guava' ); ?></label><br />
                  <textarea  rows="8" name="<?php echo $this->get_field_name('description'); ?>" id="<?php echo $this->get_field_id('description'); ?>"  class="widefat" ><?php
                  
                    if (isset($instance['description']) && $instance['description'] != '' ) :
                       echo esc_textarea( $instance['description'] ); 
                     endif;
                   ?></textarea>
              </p>
          <?php
     }
}


add_action( 'widgets_init', 'guava_author_widget' ); 
function guava_author_widget(){     
    register_widget( 'Guava_Author_Widget' );

}


/*
Function to load media on widget
*/
if( !function_exists( 'guava_author_widgets_backend_enqueue' )):
function guava_author_widgets_backend_enqueue($hook){     
    wp_enqueue_script( 'guava-custom-widgets', get_template_directory_uri().'/assets/js/widgets-admin.js', array( 'jquery' ), true );
    wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'guava_author_widgets_backend_enqueue' );
endif;