<?php
/*
Theme Designed By: S O R O U S H M A R K E T
Email: info@soroushmarket.ir
Demo Website: soroushmarket.ir
Author Website: soroushmarket.ir
*/
   // register widget
   
   add_action('widgets_init', 'banner_image_widget');
   
   function banner_image_widget() {
   
       register_widget( 'banner_image' );
   
   }
     
   // add admin scripts
   
   add_action('admin_enqueue_scripts', 'banner_image_script');
   
   function banner_image_script() {
   
       wp_enqueue_media();
   
       wp_enqueue_script('ads_script', get_template_directory_uri() . '/js/upload-media.js', false, '1.0', true);
   
   }
   
   
   
   // widget class
   
   class banner_image extends WP_Widget {
   
   
   
       function banner_image() {
   
           $widget_ops = array('classname' => 'banner_image');
   
           $this->__construct('banner-image', 'نمایش بنر', $widget_ops);
   
       }
   
   
   
       function widget($args, $instance) {
   
           extract($args);
   
   
   
           // widget content
   
           
   
   ?>
<a href="<?php echo esc_url($instance['image_link']); ?>" target="<?php echo $instance['link_target']; ?>">
<img alt="<?php echo $instance['text']; ?>" title="<?php echo $instance['text']; ?>" class="img-responsive side-banner" src="<?php echo esc_url($instance['image_uri']); ?>" />
</a>
<?php
   }
   
   
   
   function update($new_instance, $old_instance) {
   
       $instance = $old_instance;
   
       $instance['text'] = strip_tags( $new_instance['text'] );
   
       $instance['image_uri'] = strip_tags( $new_instance['image_uri'] );
   
       $instance['image_link'] = strip_tags( $new_instance['image_link'] );

       $instance['link_target'] = strip_tags( $new_instance['link_target'] );
       return $instance;
   
   }
   
   
   
   function form($instance) {
   
   ?>
<p>
   <label for="<?php echo $this->get_field_id('text'); ?>">عنوان</label><br />
   <input type="text" name="<?php echo $this->get_field_name('text'); ?>" id="<?php echo $this->get_field_id('text'); ?>" value="<?php echo $instance['text']; ?>" class="widefat" />
</p>
<p>
   <label for="<?php echo $this->get_field_id('image_link'); ?>">پیوند تصویر</label><br />
   <input type="text" name="<?php echo $this->get_field_name('image_link'); ?>" id="<?php echo $this->get_field_id('image_link'); ?>" value="<?php echo $instance['image_link']; ?>" class="widefat" />
</p>
<p>
   <label for="<?php echo $this->get_field_id('link_target'); ?>">هدف</label><br />
   <select class="widefat" name="<?php echo $this->get_field_name('link_target'); ?>" id="<?php echo $this->get_field_id('link_target'); ?>">
    <?php if($instance['link_target'] == '_blank') { ?>
    <option value="_blank" selected="selected">پنجره جدید</option>
    <option value="_self">همان پنجره</option>
  <?php }else{ ?>
    <option value="_blank">پنجره جدید</option>
    <option value="_self" selected="selected">همان پنجره</option>
    <?php } ?>
    </select>
</p>
<p>
   <label for="<?php echo $this->get_field_id('image_uri'); ?>">تصویر</label><br />
   <?php
      if ( $instance['image_uri'] != '' ) :
      
          echo '<img class="custom_media_image" src="' . $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 echo $instance['image_uri']; ?>" 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="بارگذاری تصویر" style="margin-top:5px;" />
</p>
<?php
   }
   
   }
   
   ?>