dribbble-widget.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // Dribbble widget for hemingway WordPress theme
  3. include_once(ABSPATH . WPINC . '/feed.php');
  4. class hemingway_dribbble_widget extends WP_Widget {
  5. function __construct() {
  6. $widget_ops = array( 'classname' => 'hemingway_dribbble_widget', 'description' => __('Displays your latest Dribbble photos.', 'hemingway') );
  7. parent::__construct( 'hemingway_dribbble_widget', __('Dribbble Widget','hemingway'), $widget_ops );
  8. }
  9. function widget($args, $instance) {
  10. extract($args);
  11. $widget_title = apply_filters('widget_title', $instance['widget_title']);
  12. $dribbble_username = $instance['dribbble_username'];
  13. $dribbble_number = $instance['dribbble_number'];
  14. $unique_id = $args['widget_id'];
  15. echo $before_widget;
  16. if (!empty($widget_title)) {
  17. echo $before_title . $widget_title . $after_title;
  18. }
  19. $rss = fetch_feed("http://dribbble.com/players/$dribbble_username/shots.rss");
  20. add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', 'return 1800;' ) );
  21. if (!is_wp_error( $rss ) ) :
  22. $items = $rss->get_items(0, $rss->get_item_quantity($dribbble_number));
  23. endif;
  24. if (!empty($items)): ?>
  25. <div class="dribbble-container">
  26. <?php foreach ( $items as $item ):
  27. $title = $item->get_title();
  28. $link = $item->get_permalink();
  29. $description = $item->get_description();
  30. preg_match("/src=\"(http.*(jpg|jpeg|gif|png))/", $description, $image_url);
  31. $image = $image_url[1]; ?>
  32. <a href="<?php echo $link; ?>" title="<?php echo $title;?>" class="dribbble-shot"><img src="<?php echo $image; ?>" alt="<?php echo $title;?>"/></a>
  33. <?php endforeach; ?>
  34. <p class="widgetmore"><a href="http://www.dribbble.com/<?php echo $dribbble_user; ?>"><?php printf( __('Follow %s on Dribbble &raquo;','hemingway'), $dribbble_username); ?></a></p>
  35. </div>
  36. <?php endif;
  37. echo $after_widget;
  38. }
  39. function update($new_instance, $old_instance) {
  40. //update and save the widget
  41. return $new_instance;
  42. }
  43. function form($instance) {
  44. // Get the options into variables, escaping html characters on the way
  45. $widget_title = $instance['widget_title'];
  46. $dribbble_username = $instance['dribbble_username'];
  47. $dribbble_number = $instance['dribbble_number'];
  48. ?>
  49. <p>
  50. <label for="<?php echo $this->get_field_id('widget_title'); ?>"><?php _e('Title', 'hemingway'); ?>:
  51. <input id="<?php echo $this->get_field_id('widget_title'); ?>" name="<?php echo $this->get_field_name('widget_title'); ?>" type="text" class="widefat" value="<?php echo $widget_title; ?>" /></label>
  52. </p>
  53. <p>
  54. <label for="<?php echo $this->get_field_id('dribbble_username'); ?>"><?php _e('Dribbble username', 'hemingway'); ?>:
  55. <input id="<?php echo $this->get_field_id('dribbble_username'); ?>" name="<?php echo $this->get_field_name('dribbble_username'); ?>" type="text" class="widefat" value="<?php echo $dribbble_username; ?>" /></label>
  56. </p>
  57. <p>
  58. <label for="<?php echo $this->get_field_id('dribbble_number'); ?>"><?php _e('Number of images to display:', 'hemingway'); ?>
  59. <input id="<?php echo $this->get_field_id('dribbble_number'); ?>" name="<?php echo $this->get_field_name('dribbble_number'); ?>" type="text" class="widefat" value="<?php echo $dribbble_number; ?>" /></label>
  60. </p>
  61. <?php
  62. }
  63. }
  64. register_widget('hemingway_dribbble_widget'); ?>