<?php


class download_widget extends WP_Widget
{

// Configuration globale du widget
  function __construct()
  {
    $widget_args = array(
      'classname' => 'widget_download_widget',
      'description' => 'Custom download Widget notaires'
    );
    $control_args = array();
    parent::__construct(
      'widget_download',
      __('Custom download Widget notaires'),
      $widget_args,
      $control_args
    );
  }

// Affichage en front-end
  function widget($args, $instance)
  {
    extract($args);
    $title = strip_tags($instance['title']);


    if (get_field("document", 'option')): ?>
     <div class="download-widget">
       <div class="download-widget__title"><?php echo $title ?></div>
      <ul class="download-widget__list">
        <?php while (the_repeater_field('document', 'option')):
          if (get_sub_field('afficher_en_pied_de_page') == true):
            ?>
            <li class="download-widget__item">
              <a href="<?php echo get_sub_field('file_document')["url"]; ?>"
                 target="_blank">
                <span><?php the_sub_field('nom-document'); ?></span>
                <svg viewBox="0 0 14 17"><path d="M0,17H14V15H0ZM4,0V6H0l7,7,7-7H10V0Z" fill="#595959"/></svg>
              </a></li>
          <?php endif; ?>
        <?php endwhile; ?>
      </ul>
     </div>
    <?php endif;

  }

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

  // Affichage du formulaire de réglages du widget en back-end
  public function form($instance)
  {
    $title = isset($instance['title']) ? $instance['title'] : '';
    ?>
    <p>
      <label for="<?php
      echo $this->get_field_id('title') ?>">Donnez un 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>
    <?php
  }
}

function init_download_widget()
{
  register_widget('download_widget');
}

add_action('widgets_init', 'init_download_widget');

