<?php 


class WalkerPress_Author_Box_Widget extends WP_Widget {
  function __construct() {
    add_action('admin_enqueue_scripts', array($this, 'walkerpress_authorbox_widget_scripts'));
    parent::__construct(
      'walkerpress_author_box_widget', // Base ID
      __('WalkerPress: Author Box', 'walkerpress'), // Widget Name
      array( 'description' => __( 'Widget for displaying author info in sidebar', 'walkerpress' ), )
    );
    
  }
   /**
    * Outputs the content of the widget
    *
    * @param array $args
    * @param array $instance
    */
public function walkerpress_authorbox_widget_scripts(){
    wp_enqueue_script( 'media-upload' );
   wp_enqueue_media();
    wp_enqueue_script('widgets-media-upload', get_template_directory_uri() .'/js/walkerpress-media-upload.js', array('jquery'));
}
 

  // array $instance Saved values from database.
  public function widget( $args, $instance ) {
    $title    = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : '';
    $author_name    = isset( $instance['author_name'] ) ? apply_filters( 'author_name', $instance['author_name'] ) : '';
    ?>
  <div class="walkerpress-author-box-widget">
    <?php if ( $title  ) {
        echo '<h5 class="walkerpress-custom-header"> <span>'.  apply_filters( 'widget_title', $instance['title'] ) .'</span></h5>';
      }?>
      <div class="author-box-content">
          <?php if(!empty($instance['image'])){?>
              <div class="author-image">
                  <img src='<?php echo esc_url($instance['image']) ?>' title="<?php echo  $author_name;?>" />
              </div>
          <?php } ?>
            <?php if(!empty($author_name)){?>
              <div class="author-name">
                <?php echo esc_html($author_name); ?>
              </div>
            <?php } ?>

            <?php if(!empty($instance['author_description'])){?>
             <div class="author-description"><?php echo esc_html($instance['author_description']);?></div>
            <?php } ?>
            <div class="author-social-profiles">

                <?php if(!empty($instance['author_facebook_profile'])){?>
                    <a class="author-facebook" href="<?php echo esc_url($instance['author_facebook_profile']);?>" target="blank"> <i class="fab fa-facebook-f"></i> </a>
                <?php } ?>
                <?php if(!empty($instance['author_twitter_profile'])){?>
                    <a class="author-twitter" href="<?php echo esc_url($instance['author_twitter_profile']);?>" target="blank"> <i class="fab fa-twitter"></i> </a>
                 <?php } ?>
                 <?php if(!empty($instance['author_linkedin_profile'])){?>
                    <a class="author-linkedin" href="<?php echo esc_url($instance['author_linkedin_profile']);?>" target="blank"> <i class="fab fa-linkedin-in"></i> </a>
                 <?php } ?>
                 <?php if(!empty($instance['author_instagram_profile'])){?>
                    <a class="author-instagram" href="<?php echo esc_url($instance['author_instagram_profile']);?>" target="blank"> <i class="fab fa-instagram"></i> </a>
                 <?php } ?>
                 <?php if(!empty($instance['author_youtube_profile'])){?>
                    <a class="author-youtube" href="<?php echo esc_url($instance['author_youtube_profile']);?>" target="blank"> <i class="fab fa-youtube"></i> </a>
                 <?php } ?>
                 <?php if(!empty($instance['author_github_profile'])){?>
                    <a class="author-youtube" href="<?php echo esc_url($instance['author_github_profile']);?>" target="blank"> <i class="fab fa-github-alt"></i> </a>
                 <?php } ?>
            </div>
        </div>
  </div>
      
     
 <?php    
 
  }

  /**
   * Back-end widget form.
   *
   * @see WP_Widget::form()
   *
   * @param array $instance Previously saved values from database.
   */
  public function form( $instance ) {
    
    // if text contain a value, save it to $text
   
    // if title contain a value
    if ( isset( $instance[ 'title' ] ) ) {
      $title = $instance[ 'title' ];
    }
    else {
      $title = __('Author Info','walkerpress');
    }
    if ( isset( $instance[ 'author_name' ] ) ) {
      $author_name = $instance[ 'author_name' ];
    }
    else {
      $author_name = '';
    }
    
    if ( isset( $instance[ 'author_description' ] ) ) {
      $author_description = $instance[ 'author_description' ];
    }
    else {
      $author_description = '';
    }
    if(isset($instance['author_facebook_profile'])){
      $author_facebook_profile = $instance['author_facebook_profile'];
    }else {
      $author_facebook_profile = '';
    }
    if(isset($instance['author_twitter_profile'])){
      $author_twitter_profile = $instance['author_twitter_profile'];
    }else {
      $author_twitter_profile = '';
    }
    if(isset($instance['author_linkedin_profile'])){
      $author_linkedin_profile = $instance['author_linkedin_profile'];
    }else {
      $author_linkedin_profile = '';
    }
    if(isset($instance['author_instagram_profile'])){
      $author_instagram_profile = $instance['author_instagram_profile'];
    }else {
      $author_instagram_profile = '';
    }
    if(isset($instance['author_youtube_profile'])){
      $author_youtube_profile = $instance['author_youtube_profile'];
    }else {
      $author_youtube_profile = '';
    }
    if(isset($instance['author_github_profile'])){
      $author_github_profile = $instance['author_github_profile'];
    }else {
      $author_github_profile = '';
    }

    
    
    if(isset($instance['image']))
        {
            $image = $instance['image'];
        } else{
          $image = '';
        }
    
    ?>
    <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Widget Title', 'walkerpress' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
      </p>
    <p>
        <label for="<?php echo $this->get_field_name( 'image' ); ?>"><?php _e( 'Photo:', 'walkerpress' ); ?></label>
       
          <div class="author-image">
              <img width="" height="100" id="wpw-author-image" src='<?php echo esc_url($image); ?>' title="<?php echo  $author_name;?>" />
          </div>
     
        <input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat walkerpress_media_url" type="hidden" size="36"  value="<?php echo esc_url( $image ); ?>" />
        
         <?php if(!empty($instance['image'])){?>
           <input class="upload_image_button" type="button" value="Replace Photo" />
         <?php }else{?>
           <input class="upload_image_button" type="button" value="Upload Photo" />
         <?php } ?>
       
    </p>
    <p>
      <label for="<?php echo $this->get_field_id( 'author_name' ); ?>"><?php _e( 'Name:', 'walkerpress' ); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id( 'author_name' ); ?>" name="<?php echo $this->get_field_name( 'author_name' ); ?>" type="text" value="<?php echo esc_attr( $author_name ); ?>">
    </p>
    <p>
       <label for="<?php echo $this->get_field_id( 'author_description' ); ?>"><?php _e( 'Description:', 'walkerpress' ); ?></label> 
      <textarea  row ="20" class="widefat" id="<?php echo $this->get_field_id( 'author_description' ); ?>" name="<?php echo $this->get_field_name( 'author_description' ); ?>"><?php echo esc_html( $author_description ); ?> </textarea>
    </p>
    <p>
      <label for="<?php echo $this->get_field_id( 'author_facebook_profile' ); ?>"><?php _e( 'Facebook', 'walkerpress' ); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id( 'author_facebook_profile' ); ?>" name="<?php echo $this->get_field_name( 'author_facebook_profile' ); ?>" type="text" value="<?php echo esc_url( $author_facebook_profile ); ?>">
    </p>
    <p>
      <label for="<?php echo $this->get_field_id( 'author_twitter_profile' ); ?>"><?php _e( 'Twitter', 'walkerpress' ); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id( 'author_twitter_profile' ); ?>" name="<?php echo $this->get_field_name( 'author_twitter_profile' ); ?>" type="text" value="<?php echo esc_url( $author_twitter_profile ); ?>">
    </p>
    <p>
      <label for="<?php echo $this->get_field_id( 'author_linkedin_profile' ); ?>"><?php _e( 'Linkedin', 'walkerpress' ); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id( 'author_linkedin_profile' ); ?>" name="<?php echo $this->get_field_name( 'author_linkedin_profile' ); ?>" type="text" value="<?php echo esc_url( $author_linkedin_profile ); ?>">
    </p>
    <p>
      <label for="<?php echo $this->get_field_id( 'author_instagram_profile' ); ?>"><?php _e( 'Instagram', 'walkerpress' ); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id( 'author_instagram_profile' ); ?>" name="<?php echo $this->get_field_name( 'author_instagram_profile' ); ?>" type="text" value="<?php echo esc_url( $author_instagram_profile ); ?>">
    </p>
    <p>
      <label for="<?php echo $this->get_field_id( 'author_youtube_profile' ); ?>"><?php _e( 'Youtube', 'walkerpress' ); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id( 'author_youtube_profile' ); ?>" name="<?php echo $this->get_field_name( 'author_youtube_profile' ); ?>" type="text" value="<?php echo esc_url( $author_youtube_profile ); ?>">
    </p>
    <p>
      <label for="<?php echo $this->get_field_id( 'author_github_profile' ); ?>"><?php _e( 'Github', 'walkerpress' ); ?></label> 
      <input class="widefat" id="<?php echo $this->get_field_id( 'author_github_profile' ); ?>" name="<?php echo $this->get_field_name( 'author_github_profile' ); ?>" type="text" value="<?php echo esc_url( $author_github_profile ); ?>">
    </p>
    
    
    <?php 
  }

  /**
   * Sanitize widget form values as they are saved.
   *
   * @see WP_Widget::update()
   *
   * @param array $new_instance Values just sent to be saved.
   * @param array $old_instance Previously saved values from database.
   *
   * @return array Updated safe values to be saved.
   */
  public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['image'] = ( ! empty( $new_instance['image'] ) ) ? strip_tags( $new_instance['image'] ) : '';
    $instance['author_name'] = ( ! empty( $new_instance['author_name'] ) ) ? strip_tags( $new_instance['author_name'] ) : '';
    $instance['author_description'] = ( ! empty( $new_instance['author_description'] ) ) ? strip_tags( $new_instance['author_description'] ) : '';
    $instance['author_facebook_profile'] = ( ! empty( $new_instance['author_facebook_profile'] ) ) ? strip_tags( $new_instance['author_facebook_profile'] ) : '';
    $instance['author_twitter_profile'] = ( ! empty( $new_instance['author_twitter_profile'] ) ) ? strip_tags( $new_instance['author_twitter_profile'] ) : '';
    $instance['author_linkedin_profile'] = ( ! empty( $new_instance['author_linkedin_profile'] ) ) ? strip_tags( $new_instance['author_linkedin_profile'] ) : '';
    $instance['author_instagram_profile'] = ( ! empty( $new_instance['author_instagram_profile'] ) ) ? strip_tags( $new_instance['author_instagram_profile'] ) : '';
    $instance['author_youtube_profile'] = ( ! empty( $new_instance['author_youtube_profile'] ) ) ? strip_tags( $new_instance['author_youtube_profile'] ) : '';
    $instance['author_github_profile'] = ( ! empty( $new_instance['author_github_profile'] ) ) ? strip_tags( $new_instance['author_github_profile'] ) : '';
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
    return $instance;
  }

} // class Foo_Widget

// register Foo_Widget widget
function register_walkerpress_author_box_widget() {
    register_widget( 'WalkerPress_Author_Box_Widget' );
}
add_action( 'widgets_init', 'register_walkerpress_author_box_widget' );