<?php


class RSS_widget extends WP_Widget
{

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

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

    $entries = array();
    $context = [
      "ssl"=>[
        "verify_peer"=>false,
        "verify_peer_name"=>false,
      ]
    ];
    libxml_set_streams_context(stream_context_create($context));

    $xml = simplexml_load_file($feed);

    if ($xml === false) {
      // echo "Erreur lors du chargement du XML\n";
    }else{
        $entries = array_merge($entries, $xml->xpath("//item"));
        usort($entries, function ($feed1, $feed2) {
          return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
        });
        ?>
        <div class="rss-widget">
          <div class="rss-notaires-title"><?php echo $title ?></div>
          <ul><?php
            //Print all the entries
            setlocale(LC_TIME, "fr_FR");
            for ($i = 0; $i < $length; $i++) {
              $entry = $entries[$i];
              ?>
              <li>
                <a href="<?= $entry->link ?>" target="_blank">
                  <?php
                  $date = strftime('%d %B %Y', strtotime($entry->pubDate));
                  $date = str_replace("january", "janvier",  $date);
                  $date = str_replace("february", "février",  $date);
                  $date = str_replace("march", "mars",  $date);
                  $date = str_replace("april", "avril",  $date);
                  $date = str_replace("may", "mai",  $date);
                  $date = str_replace("june", "juin",  $date);
                  $date = str_replace("july", "juillet",  $date);
                  $date = str_replace("august", "aout",  $date);
                  $date = str_replace("september", "spetembre",  $date);
                  $date = str_replace("october", "octobre",  $date);
                  $date = str_replace("november", "novembre",  $date);
                  $date = str_replace("december", "décembre",  $date);
                  ?>
                <div class="rss-date"><? echo utf8_encode(strip_tags($date)) ?></div>
                  <?= $entry->title ?>
                  <div class="over">
                    <?php esc_html_e('En savoir plus', 'Avada'); ?>

                    <svg xmlns="http://www.w3.org/2000/svg" width="39" height="39" viewBox="0 0 39 39">
                      <path d="M28.7,20h0a1,1,0,0,0,0-.8h0l-.2-.3h0l-5.1-4.7a1,1,0,0,0-1.4,1.5l3.2,3H10.8a1,1,0,0,0,0,2H25.3l-3.2,3A1,1,0,0,0,23.5,25l5.1-4.8h0Z" fill="#ffffff"/>
                      <path d="M19.7,39.1A19.6,19.6,0,1,1,39.1,19.5,19.6,19.6,0,0,1,19.7,39.1ZM19.5.9A18.7,18.7,0,1,0,38.2,19.5,18.7,18.7,0,0,0,19.5.9Z" fill="#ffffff"/>
                    </svg>
                  </div>
                </a>
              </li>
              <?php
            }
            ?>
          </ul>
        </div>
        <?php
      }
  }

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

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

    $feed = isset($instance['feed']) ? $instance['feed'] : '';
    $length = isset($instance['length']) ? $instance['length'] : '';
    $title = isset($instance['title']) ? $instance['title'] : '';
    ?>
    <p>
      <label for="<?php
      echo $this->get_field_id('title') ?>">Donnez un titre au flux :</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 $this->get_field_id('feed') ?>">Saisissez l’URL du flux RSS ici :</label>
      <input class="widefat" id="<?php
      echo $this->get_field_id('feed') ?>"
             name="<?php
             echo $this->get_field_name('feed') ?>" type="text" value="<?php
      echo esc_attr($feed) ?>"/>
    </p>

    <p>
      <label for="<?php
      echo $this->get_field_id('length') ?>">Combien d’entrées souhaitez-vous afficher :</label>
      <input class="widefat" id="<?php
      echo $this->get_field_id('length') ?>"
             name="<?php
             echo $this->get_field_name('length') ?>" type="number" value="<?php
      echo esc_attr($length) ?>"/>
    </p>


    <?php
  }
}

function init_rss_widget()
{
  register_widget('RSS_widget');
}

add_action('widgets_init', 'init_rss_widget');

