Skip to content

Instantly share code, notes, and snippets.

@NateJacobs
Created September 20, 2012 15:44
Show Gist options
  • Select an option

  • Save NateJacobs/3756702 to your computer and use it in GitHub Desktop.

Select an option

Save NateJacobs/3756702 to your computer and use it in GitHub Desktop.

Revisions

  1. NateJacobs revised this gist Sep 20, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cb-flickr-widget.php
    Original file line number Diff line number Diff line change
    @@ -43,6 +43,7 @@ public function __construct()
    * Create Widget Form
    *
    * Create the necessary form to customize the widget.
    * Use http://idgettr.com to get the ID for a group or user
    *
    * @uses title @since 1.0
    *
  2. NateJacobs revised this gist Sep 20, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cb-flickr-widget.php
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ public function form( $instance )
    * Updates any options used to customize the widget.
    *
    * @author Nate Jacobs
    * @since 0.1
    * @since 1.0
    */
    public function update( $new_instance, $old_instance )
    {
    @@ -88,7 +88,7 @@ public function update( $new_instance, $old_instance )
    * Generates the widget that displays the photos from a user or group.
    *
    * @author Nate Jacobs
    * @since 0.1
    * @since 1.0
    */
    public function widget( $args, $instance )
    {
  3. NateJacobs created this gist Sep 20, 2012.
    126 changes: 126 additions & 0 deletions cb-flickr-widget.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,126 @@
    <?php
    /*
    Plugin Name: Cactus Brick Flickr Widget
    Plugin URI: http://cactusbrick.org
    Description: Grab photos from Flickr
    Version: 1.0
    Author: Nate Jacobs
    Author URI: http://natejacobs.org
    License: GPLv2 or later
    */

    add_action( 'widgets_init', 'CactusBrickFlickrWidget' );
    function CactusBrickFlickrWidget()
    {
    register_widget( 'CactusBrickFlickrWidget' );
    }

    /**
    * Flickr Photos Widget
    *
    * Creates a widget that shows the recent Flickr photos by a user or group
    *
    * @author Nate Jacobs
    * @since 1.0
    */
    class CactusBrickFlickrWidget extends WP_Widget
    {
    /**
    * Plugin Load
    *
    * Generate options for the widget display in the admin dashboard.
    *
    * @author Nate Jacobs
    * @since 1.0
    */
    public function __construct()
    {
    $options = array( 'description' => __( 'Display photos from Flickr.com by a user or group.' ), 'classname' => 'cactus_brick_flickr' );
    parent::__construct( 'cactus_brick_flickr_widget', $name = __( 'Cactus Brick Flickr' ), $options );
    }

    /**
    * Create Widget Form
    *
    * Create the necessary form to customize the widget.
    *
    * @uses title @since 1.0
    *
    * @author Nate Jacobs
    * @since 1.0
    */
    public function form( $instance )
    {
    $instance = wp_parse_args( ( array ) $instance, array( 'title' => __( 'Cactus Brick Flickr' ), 'flickr_id' => __( ' ' ) ) );
    $title = esc_attr( $instance['title'] );
    $flickr_id = esc_attr( $instance['flickr_id'] );
    ?>
    <p>
    <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</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 $title; ?>">
    </p>
    <p>
    <label for="<?php echo $this->get_field_id( 'flickr_id' ); ?>">Flickr ID:</label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'flickr_id' ); ?>" name="<?php echo $this->get_field_name( 'flickr_id' ); ?>" type="text" value="<?php echo $flickr_id; ?>">
    </p>
    <?php
    }

    /**
    * Update Widget Options
    *
    * Updates any options used to customize the widget.
    *
    * @author Nate Jacobs
    * @since 0.1
    */
    public function update( $new_instance, $old_instance )
    {
    $instance = $old_instance;
    $instance['title'] = strip_tags( $new_instance['title'] );
    $instance['flickr_id'] = strip_tags( $new_instance['flickr_id'] );
    return $instance;
    }

    /**
    * Create Cactus Brick Flickr Widget
    *
    * Generates the widget that displays the photos from a user or group.
    *
    * @author Nate Jacobs
    * @since 0.1
    */
    public function widget( $args, $instance )
    {
    extract( $args );
    $title = apply_filters('widget_title', $instance['title'] );
    $flickr_id = $instance['flickr_id'];

    $result = wp_remote_get( "http://api.flickr.com/services/feeds/groups_pool.gne?id=$flickr_id&format=php_serial", array( 'method' => 'GET' ) );
    $this->httpcode = $result['response']['code'];
    $photos = unserialize( $result['body'] );

    echo $before_widget;
    if ( $title )
    echo $before_title . '<a href="http://www.flickr.com/groups/cactusbrick/pool/">'. $title .'</a>' . $after_title;
    try {
    if ( $this->httpcode == '200' )
    {
    foreach ( $photos['items'] as $photo )
    {
    echo "<a href=$photo[url]><img class='flickr-img' src=$photo[thumb_url]></a>";
    }
    }
    else
    {
    $error = 'There are no photos to display';
    throw new Exception($error);
    }
    }
    catch ( Exception $e )
    {
    echo $e->getMessage();
    }
    echo $after_widget;
    }
    }