<?php


class Person_widget extends WP_Widget
{

  // Configuration globale du widget
  function __construct()
  {
    $widget_args = array(
      'classname' => 'widget_person_widget',
      'description' => 'Box Membre Deux Torrents'
    );
    $control_args = array();
    parent::__construct(
      'widget_person',
      __('Box Membre Deux Torrents'),
      $widget_args,
      $control_args
    );
  }

  // Affichage en front-end
  function widget($args, $instance)
  {
    $before_widget = $args['before_widget'] ?? '';
    $after_widget = $args['after_widget'] ?? '';
    $before_title = $args['before_title'] ?? '';
    $after_title = $args['after_title'] ?? '';
    $title = strip_tags($instance['title']);
    $texte = strip_tags($instance['text']);
    $fonction = strip_tags($instance['fonction']);
    $email = $instance['email'] ?? '';
    $img = $instance['img'];
    if (!empty($img)) {
      ?>
      <h3><?php echo $title ?></h3>
        <div>
          <div class="wrapper">
            <img src="<?php echo $img ?>" alt="" >
            <div ><?php echo $texte ?></div>
            <div ><?php echo $email ?></div>
          </div>
        </div>
      <?php
    }
  }

  // Traitement des données avant sauvegarde
  function update($new_instance, $old_instance)
  {
    $new_instance['title'] = strip_tags($new_instance['title']);
    $new_instance['texte'] =  strip_tags($new_instance['texte']);
    $new_instance['email'] = $new_instance['email'];
    $new_instance['fonction'] =  strip_tags($new_instance['fonction']);
    $new_instance['img'] =  $new_instance['img'];
    return $new_instance;
  }

  // Affichage du formulaire de réglages du widget en back-end
  public function form($instance)
  {

    $title = isset($instance['title']) ? $instance['title'] : '';
    $img = isset($instance['img']) ? $instance['img'] : '';
    $texte = isset($instance['texte']) ? $instance['texte'] : '';
    $fonction = isset($instance['fonction']) ? $instance['fonction'] : '';
    $email = isset($instance['email']) ? $instance['email'] : '';

    $img_field_id = $this->get_field_id('img');

    ?>
    <p>
      <label for="<?php echo $this->get_field_id('title') ?>">Titre :</label>
      <input class="widefat" id="<?php echo $this->get_field_id('title') ?>"
             name="<?php echo $this->get_field_name('title') ?>" type="text" value="<?php echo esc_attr($title) ?>"/>
    </p>

    <p>
      <label for="<?php echo $img_field_id ?>">Image :</label><br/>
      <input class="upload_image" id="<?php echo $img_field_id ?>" type="hidden"
             name="<?php echo $this->get_field_name('img') ?>" value="<?php echo $img ?>"/>
      <input class="upload_image_button" id="<?php echo $img_field_id ?>_button" type="button" value="Charger une image"
             data-field-id="<?php echo $img_field_id ?>"/>
    <div id="<?php echo $img_field_id ?>_img" class="upload_image_wrapper">
      <?php if (!empty($img)): ?>
        <img src="<?php echo $img ?>"/>
        <a href="#" class="upload_image_delete" data-field-id="<?php echo $img_field_id ?>">Supprimer l'image</a>
      <?php endif ?>
    </div>
    </p>

    <p>
      <label for="<?php echo $this->get_field_id('fonction') ?>">Fonction :</label>
      <input class="widefat" id="<?php echo $this->get_field_id('fonction') ?>"
             name="<?php echo $this->get_field_name('fonction') ?>" type="text" value="<?php echo esc_attr($fonction) ?>"/>
    </p>

    <p>
      <label for="<?php echo $this->get_field_id('texte') ?>">Description :</label>
      <input class="widefat" id="<?php echo $this->get_field_id('texte') ?>"
             name="<?php echo $this->get_field_name('texte') ?>" type="text" value="<?php echo esc_attr($texte) ?>"/>
    </p>
    <p>
      <label for="<?php echo $this->get_field_id('email') ?>">email :</label>
      <input class="widefat" id="<?php echo $this->get_field_id('email') ?>"
             name="<?php echo $this->get_field_name('email') ?>" type="text" value="<?php echo esc_attr($email) ?>"/>
    </p>

    <style>
      .upload_image_wrapper img {
        width: 100%
      }
    </style>


    <?php
  }
}

function init_person_widget()
{
  register_widget('Person_widget');
}

add_action('widgets_init', 'init_person_widget');
