Advertisement
pskli

Créer un widget WordPress, méthode officielle

Apr 9th, 2013
1,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php class YouTube_Simple_Stats extends WP_Widget {
  2.  
  3.     // Configuration globale du widget
  4.     function __construct() {
  5.  
  6.         $widget_args = array(
  7.             'classname' => 'widget_yt_simple_stats',
  8.             'description' => 'Affiche un rapide résumé d\'un profil d\'utilisateur YouTube'
  9.         );
  10.  
  11.         $control_args = array(
  12.             'width' => 450
  13.         );
  14.  
  15.         parent::__construct(
  16.             'yt_simple_stats',
  17.             __('Statistiques simples YouTube'),
  18.             $widget_args,
  19.             $control_args
  20.         );
  21.     }
  22.  
  23.     // Affichage en front-end
  24.     function widget($args, $instance) {
  25.         extract($args);
  26.         $title = strip_tags($instance['title']);
  27.         $description = $instance['description'];
  28.         $username = strip_tags($instance['username']);
  29.  
  30.         echo $before_widget;
  31.  
  32.         echo $before_title . $title . $after_title;
  33.  
  34.         echo $description . '<hr>';
  35.  
  36.         set_include_path(get_template_directory() . '/inc/ZendGData');
  37.         require_once 'Zend/Loader.php';
  38.         Zend_Loader::loadClass('Zend_Gdata_YouTube');
  39.  
  40.         $youtube = new Zend_Gdata_YouTube();
  41.  
  42.         $user_videos = $youtube->retrieveAllEntriesForFeed($youtube->getUserUploads($username));
  43.         $total_videos = count($user_videos);
  44.         $total_duration = 0;
  45.  
  46.         foreach ($user_videos as $video) {
  47.             $total_duration = $total_duration + (int)$video->getVideoDuration();
  48.         }
  49.  
  50.         $duration_h = floor($total_duration / 3600);
  51.         $duration_m = floor(($total_duration - ($duration_h * 3600)) / 60);
  52.  
  53.         echo '<strong>J\'ai publié ' . $total_videos . ' vidéos </strong> totalisant ' . $duration_h . ' heures et ' . $duration_m . ' minutes de tutoriels gratuits.';
  54.  
  55.         echo $after_widget;
  56.     }
  57.  
  58.     // Traitement des données avant sauvegarde
  59.     function update( $new_instance, $old_instance ) {
  60.         $new_instance['title'] = strip_tags($new_instance['title']);
  61.         $new_instance['username'] = strip_tags($new_instance['username']);
  62.         return $new_instance;
  63.     }
  64.  
  65.     // Affichage du formulaire de réglages du widget en back-end
  66.     function form($instance) {
  67.         $instance = wp_parse_args(
  68.             $instance,
  69.             array(
  70.                 'title' => 'Statistiques YouTube',
  71.                 'description' => '',
  72.                 'username' => 'pierresaikali'
  73.             )
  74.         );
  75.  
  76.         $title = strip_tags($instance['title']);
  77.         $description = $instance['description'];
  78.         $username = strip_tags($instance['username']);
  79.  
  80.         ?>
  81.  
  82.         <p>
  83.             <label for="<?php echo $this->get_field_id('title'); ?>">Titre :</label>
  84.             <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); ?>" />
  85.         </p>
  86.  
  87.         <p>
  88.             <label for="<?php echo $this->get_field_id('description'); ?>">Description :</label>
  89.             <textarea class="widefat" rows="16" col="20" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>"><?php echo esc_textarea($description); ?></textarea>
  90.         </p>
  91.  
  92.         <p>
  93.             <label for="<?php echo $this->get_field_id('username'); ?>">Nom d'utilisateur :</label>
  94.             <input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" type="text" value="<?php echo esc_attr($username); ?>" />
  95.         </p>
  96.  
  97.  
  98.  
  99.     <?php }
  100. }
  101.  
  102. function init_youtube_widget() {
  103.     register_widget('YouTube_Simple_Stats');
  104. }
  105. add_action('widgets_init', 'init_youtube_widget');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement