Adding (image) caching to my Tumblr widget

I’ve hacked together a simple plugin to show the latest image from a Tumblr blog.

To avoid upsetting the Tumblr API (v1), I want to cache that image within WordPress…

Read More

I think perhaps I can do this somehow using the Transients API, but have no idea where to start with this… or is there a simple way to do it with straight up PHP?

Thanks 🙂

<?php
/*
Plugin Name: Topshop Tumblr Widget
Plugin URI:  
Description: Displays images from the Topshop Tumblr using their v1 API
Author: James Greig
Version: 0.1
Author URI: http://greig.cc
*/

/*
Based on the Tumblr plugin by V.J.Catkick - http://www.vjcatkick.com/
Data source: http://topshop.tumblr.com/api/read?start=0&num=1&type=photo

*/


function widget_topshop_tumblr() {
    if ( !function_exists('register_sidebar_widget') )
        return;

    function widget_topshop_tumblr_photos( $args ) {
        extract($args);

        $options = get_option('widget_topshop_tumblr_photos');
        $tuid = $options['tumblr_recents_src_uid'];

        $output = '<div id="tumblr_recent_photos">';
        $output .= '<a href="http://topshop.tumblr.com/" class="logo-tumblr"></a>';
        // $output .= '<a href="#" class="arrow-left"></a>';
        // $output .= '<a href="#" class="arrow-right"></a>';


if ( function_exists('simplexml_load_file') ) {

    $tumblr_userid = $tuid;
    $tumblr_num = '1';
    $img_style = $tumblr_recents_img_style;
    $tumblr_size = '250';
    $pagecounter = 0;

    $_tumblrurl = 'http://' . $tumblr_userid . '.tumblr.com/api/read?start=' . $pagecounter . '&num=' . $tumblr_num . '&type=photo';
    $_tumblrurl  = urlencode( $_tumblrurl );
    $_tumblr_xml = @simplexml_load_file( $_tumblrurl );

if( $_tumblr_xml && $_tumblr_xml->posts[0] ) {

    // 0.1.2
    if( $tumblr_recents_additional_html ) {
        $output .= str_replace( "","", $tumblr_recents_additional_html );
    }

    foreach( $_tumblr_xml->posts[0]->post as $p ) {
        $photourl = $p->{"photo-url"}[3];
        $linkurl = $p[url];
        $output .= "<div class='image-tumblr'>";
        // $output .= '<a href="' . $linkurl . '" target="_blank" >';
        $output .= '<a href="http://topshop.tumblr.com/" target="_blank" >';
        $output .= '<img src="' . $photourl . '" border="0" alt="' . $linkurl . '" />';
        $output .= '</a>';
        $output .= '</div>';
    }
}

} else {
    $output .= 'Requires PHP 5.1';
}

        $output .= '</div>';

        // These lines generate the output
        echo $before_widget . $before_title . $title . $after_title;
        echo $output;
        echo $after_widget;
    }

    function widget_topshop_tumblr_photos_control() {
        $options = $newoptions = get_option('widget_topshop_tumblr_photos');
        if ( $_POST["tumblr_recents_src_submit"] ) {
            $newoptions['tumblr_recents_src_uid'] = strip_tags(stripslashes($_POST["tumblr_recents_src_uid"]));
        } /* if */
        if ( $options != $newoptions ) {
            $options = $newoptions;
            update_option('widget_topshop_tumblr_photos', $options);
        } /* if */

        // those are default value
        if ( !$options['tumblr_recents_src_uid'] ) $options['tumblr_recents_src_uid'] = 'topshop';
        $tuid = htmlspecialchars($options['tumblr_recents_src_uid'], ENT_QUOTES);
?>

        <?php _e('Tumblr ID:'); ?> <input style="width: 100px;" id="tumblr_recents_src_uid" name="tumblr_recents_src_uid" type="text" value="<?php echo $tuid; ?>" />.tumblr.com<br />

        <input type="hidden" id="tumblr_recents_src_submit" name="tumblr_recents_src_submit" value="1" />

<?php
    } /* widget_topshop_tumblr_photos_control() */

    register_sidebar_widget('Topshop Tumblr Widget', 'widget_topshop_tumblr_photos');
    register_widget_control('Topshop Tumblr Widget', 'widget_topshop_tumblr_photos_control' );
} /* widget_topshop_tumblr() */

add_action('plugins_loaded', 'widget_topshop_tumblr');

?>

Related posts

Leave a Reply