Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created October 13, 2010 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeschinkel/625088 to your computer and use it in GitHub Desktop.
Save mikeschinkel/625088 to your computer and use it in GitHub Desktop.
<?php
/*
blog-from-flickr.php
Allows the WordPress blog owner to blog a recent photo from a Flickr Photoset just by clicking a button.
Author: Mike Schinkel (http://mikeschinkel.com)
Just drop this example into the root of your website and call directly to see it work.
Use the class in your plugins or themes.
In Answer To: http://wordpress.stackexchange.com/questions/2830/
*/
include "wp-load.php";
define('FLICKR_API_KEY','{YOUR_FLICKR_API_KEY_GOES_HERE}');
define('DEFAULT_PHOTOSET_ID','{YOUR DEFAULT_PHOTOSET_GOES_HERE');
define('FLICKR_PHOTO_COUNT',25);
define('FLICKR_GET_URL','http://api.flickr.com/services/rest/');
$photoset_id = DEFAULT_PHOTOSET_ID;
if (count($_POST)==0)
echo <<<HTML
<html>
<body>
<form method="post">
Enter Flickr Photoset ID: <input type="text" name="photoset_id" value="{$photoset_id}" size="25" />
&nbsp;<input type="submit" value="Blog Recent Photo from Flickr!" />
<input type="hidden" name="go" value="go!" />
</form>
</body>
</html>
HTML;
else
BlogFlickrPhotoSetPhoto::blog_photoset_photo($_POST['photoset_id']);
class BlogFlickrPhotoSetPhoto {
static function blog_photoset_photo($photoset_id) {
if (current_user_can('publish_posts')) {
$photoset = self::get_photoset_photos($photoset_id);
if (!$photoset) {
echo 'Not a valid Photoset ID. <a href="#">Try again</a>.';
} else {
$photo = self::get_photo_to_blog($photoset);
if (!$photo) {
echo 'Unexpected Error. Photo Retrieval Failed.';
} else {
$permalink = self::blog_photo($photo);
wp_safe_redirect($permalink);
}
}
}
}
static function blog_photo($photo) {
$img = self::get_photo_html($photo);
$photo_info = self::get_photo_info($photo->id);
$description = ($photo_info ? "{$photo_info['body']->photo->description->_content}<br/>" : '');
$url = self::get_photo_url($photo);
$post_id = wp_insert_post(array(
'post_title' => $photo->title,
'post_type' => 'post',
'post_author' => 1,
'post_content' => "{$description}{$img}",
'post_status' => 'publish',
'comment_status' => 'open',
'ping_status' => 'open',
'post_parent' => 0,
));
if ($post_id) {
update_post_meta($post_id,'_flickr_photo_id',$photo->id);
$url = get_permalink($post_id);
} else {
$url = false;
}
return $url;
}
static function get_photo_to_blog($photoset) {
global $wpdb;
$photo = false;
if (isset($photoset['body']->photoset->photo)) {
//Collect the list of Flickr Photo_ids from our "N" most resent photoset photos
$photos = $photoset['body']->photoset->photo;
foreach($photos as $index => $photo) {
$photo_ids[$index] = $photo->id;
}
$photo_id_list = "'" . implode("','",$photo_ids) . "'";
//Get a list of all photos we've already blogged
$posts = $wpdb->get_results("SELECT post_id,meta_value FROM wp_postmeta WHERE meta_key='_flickr_photo_id' && meta_value IN ({$photo_id_list})");
//Remove any photos from the list we've already blogged
$photo_ids = array_flip($photo_ids);
foreach($posts as $post) {
if (isset($photo_ids[$post->meta_value])) {
unset($photo_ids[$post->meta_value]);
}
}
//Grab the earliest photo we've not blogged
if (count($photo_ids)==0) {
$photo = false;
} else {
krsort($photo_ids);
$photo = $photos[reset($photo_ids)];
}
}
return $photo;
}
static function get_photoset_photos($photoset_id) {
$photoset = self::flickr_get(array(
'method' => 'flickr.photosets.getPhotos',
'photoset_id' => $photoset_id,
'media' => 'photos',
'per_page' => FLICKR_PHOTO_COUNT,
));
return $photoset;
}
static function get_photo_info($photo_id) {
$photo_info = self::flickr_get(array(
'method' => 'flickr.photos.getInfo',
'photo_id' => $photo_id,
));
return $photo_info;
}
static function get_photo_html($photo,$size='') {
$url = self::get_photo_url($photo,$size);
list($width, $height) = getimagesize($url);
$hwstring = image_hwstring($width, $height);
return "<img src=\"{$url}\" alt=\"{$title}\" {$hwstring} />\n";
}
static function get_photo_url($photo,$size='') {
extract((array)$photo);
$size = (!empty($size) ? "_{$size}" : ''); // If empty the 500px version will be returned.
return "http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}{$size}.jpg";
}
static function flickr_get($params,$args=array()) {
$http = new WP_Http();
$params['api_key'] = FLICKR_API_KEY;
$params['format'] = 'json';
$params['nojsoncallback'] = 1;
$params = http_build_query($params);
$url = FLICKR_GET_URL . "?{$params}";
$result = $http->get($url,$args);
if (isset($result['response']['code']) && $result['response']['code'] == 200)
$result['body'] = json_decode($result['body']);
else
$result = false;
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment