<?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)
  {

  }

  // 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');

