Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php class YouTube_Simple_Stats extends WP_Widget {
- // Configuration globale du widget
- function __construct() {
- $widget_args = array(
- 'classname' => 'widget_yt_simple_stats',
- 'description' => 'Affiche un rapide résumé d\'un profil d\'utilisateur YouTube'
- );
- $control_args = array(
- 'width' => 450
- );
- parent::__construct(
- 'yt_simple_stats',
- __('Statistiques simples YouTube'),
- $widget_args,
- $control_args
- );
- }
- // Affichage en front-end
- function widget($args, $instance) {
- extract($args);
- $title = strip_tags($instance['title']);
- $description = $instance['description'];
- $username = strip_tags($instance['username']);
- echo $before_widget;
- echo $before_title . $title . $after_title;
- echo $description . '<hr>';
- set_include_path(get_template_directory() . '/inc/ZendGData');
- require_once 'Zend/Loader.php';
- Zend_Loader::loadClass('Zend_Gdata_YouTube');
- $youtube = new Zend_Gdata_YouTube();
- $user_videos = $youtube->retrieveAllEntriesForFeed($youtube->getUserUploads($username));
- $total_videos = count($user_videos);
- $total_duration = 0;
- foreach ($user_videos as $video) {
- $total_duration = $total_duration + (int)$video->getVideoDuration();
- }
- $duration_h = floor($total_duration / 3600);
- $duration_m = floor(($total_duration - ($duration_h * 3600)) / 60);
- echo '<strong>J\'ai publié ' . $total_videos . ' vidéos </strong> totalisant ' . $duration_h . ' heures et ' . $duration_m . ' minutes de tutoriels gratuits.';
- echo $after_widget;
- }
- // Traitement des données avant sauvegarde
- function update( $new_instance, $old_instance ) {
- $new_instance['title'] = strip_tags($new_instance['title']);
- $new_instance['username'] = strip_tags($new_instance['username']);
- return $new_instance;
- }
- // Affichage du formulaire de réglages du widget en back-end
- function form($instance) {
- $instance = wp_parse_args(
- $instance,
- array(
- 'title' => 'Statistiques YouTube',
- 'description' => '',
- 'username' => 'pierresaikali'
- )
- );
- $title = strip_tags($instance['title']);
- $description = $instance['description'];
- $username = strip_tags($instance['username']);
- ?>
- <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 $this->get_field_id('description'); ?>">Description :</label>
- <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>
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('username'); ?>">Nom d'utilisateur :</label>
- <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); ?>" />
- </p>
- <?php }
- }
- function init_youtube_widget() {
- register_widget('YouTube_Simple_Stats');
- }
- add_action('widgets_init', 'init_youtube_widget');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement