| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: flickrRSS |
|---|
| 4 | Plugin URI: http://eightface.com/wordpress/flickrrss/ |
|---|
| 5 | Description: Allows you to integrate the photos from a flickr rss feed into your site. |
|---|
| 6 | Version: 3.0.3 |
|---|
| 7 | License: GPL |
|---|
| 8 | Author: Dave Kellam |
|---|
| 9 | Author URI: http://eightface.com |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | function get_flickrRSS() { |
|---|
| 13 | |
|---|
| 14 | // the function can accept up to seven parameters, otherwise it uses option panel defaults |
|---|
| 15 | for($i = 0 ; $i < func_num_args(); $i++) { |
|---|
| 16 | $args[] = func_get_arg($i); |
|---|
| 17 | } |
|---|
| 18 | if (!isset($args[0])) $num_items = get_option('flickrRSS_display_numitems'); else $num_items = $args[0]; |
|---|
| 19 | if (!isset($args[1])) $type = get_option('flickrRSS_display_type'); else $type = $args[1]; |
|---|
| 20 | if (!isset($args[2])) $tags = trim(get_option('flickrRSS_tags')); else $tags = trim($args[2]); |
|---|
| 21 | if (!isset($args[3])) $imagesize = get_option('flickrRSS_display_imagesize'); else $imagesize = $args[3]; |
|---|
| 22 | if (!isset($args[4])) $before_image = stripslashes(get_option('flickrRSS_before')); else $before_image = $args[4]; |
|---|
| 23 | if (!isset($args[5])) $after_image = stripslashes(get_option('flickrRSS_after')); else $after_image = $args[5]; |
|---|
| 24 | if (!isset($args[6])) $userid = stripslashes(get_option('flickrRSS_flickrid')); else $userid = $args[6]; |
|---|
| 25 | |
|---|
| 26 | # use image cache & set location |
|---|
| 27 | $useImageCache = get_option('flickrRSS_use_image_cache'); |
|---|
| 28 | $cachePath = get_option('flickrRSS_image_cache_uri'); |
|---|
| 29 | $fullPath = get_option('flickrRSS_image_cache_dest'); |
|---|
| 30 | |
|---|
| 31 | if (!function_exists('MagpieRSS')) { // Check if another plugin is using RSS, may not work |
|---|
| 32 | include_once (ABSPATH . WPINC . '/rss-functions.php'); |
|---|
| 33 | error_reporting(E_ERROR); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | // get the feeds |
|---|
| 38 | if ($type == "public") { $rss_url = 'http://www.flickr.com/services/feeds/photos_public.gne?tags=' . $tags . '&format=rss_200'; } |
|---|
| 39 | elseif ($type == "user") { $rss_url = 'http://www.flickr.com/services/feeds/photos_public.gne?id=' . $userid . '&tags=' . $tags . '&format=rss_200'; } |
|---|
| 40 | elseif ($type == "group") { $rss_url = 'http://www.flickr.com/groups/' . $userid . '/pool/feed/?format=rss_200'; } |
|---|
| 41 | else { print "flickrRSS is not configured properly"; } |
|---|
| 42 | |
|---|
| 43 | # get rss file |
|---|
| 44 | $rss = @ fetch_rss($rss_url); |
|---|
| 45 | |
|---|
| 46 | if ($rss) { |
|---|
| 47 | $imgurl = ""; |
|---|
| 48 | # specifies number of pictures |
|---|
| 49 | $items = array_slice($rss->items, 0, $num_items); |
|---|
| 50 | |
|---|
| 51 | # builds html from array |
|---|
| 52 | foreach ( $items as $item ) { |
|---|
| 53 | if(preg_match('<img src="([^"]*)" [^/]*/>', $item['description'],$imgUrlMatches)) { |
|---|
| 54 | $imgurl = $imgUrlMatches[1]; |
|---|
| 55 | |
|---|
| 56 | #change image size |
|---|
| 57 | if ($imagesize == "square") { |
|---|
| 58 | $imgurl = str_replace("m.jpg", "s.jpg", $imgurl); |
|---|
| 59 | } elseif ($imagesize == "thumbnail") { |
|---|
| 60 | $imgurl = str_replace("m.jpg", "t.jpg", $imgurl); |
|---|
| 61 | } elseif ($imagesize == "medium") { |
|---|
| 62 | $imgurl = str_replace("_m.jpg", ".jpg", $imgurl); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | $title = htmlspecialchars(stripslashes($item['title'])); |
|---|
| 66 | $url = $item['link']; |
|---|
| 67 | |
|---|
| 68 | preg_match('<http://static.flickr\.com/\d\d?\/([^.]*)\.jpg>', $imgurl, $flickrSlugMatches); |
|---|
| 69 | $flickrSlug = $flickrSlugMatches[1]; |
|---|
| 70 | |
|---|
| 71 | # cache images |
|---|
| 72 | if ($useImageCache) { |
|---|
| 73 | |
|---|
| 74 | # check if file already exists in cache |
|---|
| 75 | # if not, grab a copy of it |
|---|
| 76 | if (!file_exists("$fullPath$flickrSlug.jpg")) { |
|---|
| 77 | if ( function_exists('curl_init') ) { // check for CURL, if not use fopen |
|---|
| 78 | $curl = curl_init(); |
|---|
| 79 | $localimage = fopen("$fullPath$flickrSlug.jpg", "wb"); |
|---|
| 80 | curl_setopt($curl, CURLOPT_URL, $imgurl); |
|---|
| 81 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1); |
|---|
| 82 | curl_setopt($curl, CURLOPT_FILE, $localimage); |
|---|
| 83 | curl_exec($curl); |
|---|
| 84 | curl_close($curl); |
|---|
| 85 | } else { |
|---|
| 86 | $filedata = ""; |
|---|
| 87 | $remoteimage = fopen($imgurl, 'rb'); |
|---|
| 88 | if ($remoteimage) { |
|---|
| 89 | while(!feof($remoteimage)) { |
|---|
| 90 | $filedata.= fread($remoteimage,1024*8); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | fclose($remoteimage); |
|---|
| 94 | $localimage = fopen("$fullPath$flickrSlug.jpg", 'wb'); |
|---|
| 95 | fwrite($localimage,$filedata); |
|---|
| 96 | fclose($localimage); |
|---|
| 97 | } // end CURL check |
|---|
| 98 | } // end file check |
|---|
| 99 | # use cached image |
|---|
| 100 | print $before_image . "<a href=\"$url\" title=\"$title\"><img src=\"$cachePath$flickrSlug.jpg\" alt=\"$title\" /></a>" . $after_image; |
|---|
| 101 | } else { |
|---|
| 102 | # grab image direct from flickr |
|---|
| 103 | print $before_image . "<a href=\"$url\" title=\"$title\"><img src=\"$imgurl\" alt=\"$title\" /></a>" . $after_image; |
|---|
| 104 | } // end use imageCache |
|---|
| 105 | } // end pregmatch |
|---|
| 106 | } // end foreach |
|---|
| 107 | } else { |
|---|
| 108 | #print "Flickr is having a massage (Flickr Blog)"; |
|---|
| 109 | } |
|---|
| 110 | } # end get_flickrRSS() function |
|---|
| 111 | |
|---|
| 112 | function widget_flickrrss_init() { |
|---|
| 113 | if (!function_exists('register_sidebar_widget')) return; |
|---|
| 114 | |
|---|
| 115 | function widget_flickrrss($args) { |
|---|
| 116 | |
|---|
| 117 | extract($args); |
|---|
| 118 | |
|---|
| 119 | $options = get_option('widget_flickrrss'); |
|---|
| 120 | $title = $options['title']; |
|---|
| 121 | |
|---|
| 122 | echo $before_widget . $before_title . $title . $after_title; |
|---|
| 123 | get_flickrRSS(); |
|---|
| 124 | echo $after_widget; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | function widget_flickrrss_control() { |
|---|
| 128 | $options = get_option('widget_flickrrss'); |
|---|
| 129 | if ( !is_array($options) ) |
|---|
| 130 | $options = array('title'=>''); |
|---|
| 131 | if ( $_POST['flickrrss-submit'] ) { |
|---|
| 132 | $options['title'] = strip_tags(stripslashes($_POST['flickrrss-title'])); |
|---|
| 133 | update_option('widget_flickrrss', $options); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | $title = htmlspecialchars($options['title'], ENT_QUOTES); |
|---|
| 137 | |
|---|
| 138 | echo '<p style="text-align:right;"><label for="flickrrss-title">Title: <input style="width: 200px;" id="gsearch-title" name="flickrrss-title" type="text" value="'.$title.'" /></label></p>'; |
|---|
| 139 | echo '<input type="hidden" id="flickrrss-submit" name="flickrrss-submit" value="1" />'; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | register_sidebar_widget('flickrRSS', 'widget_flickrrss'); |
|---|
| 143 | register_widget_control('flickrRSS', 'widget_flickrrss_control', 300, 100); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | function flickrRSS_subpanel() { |
|---|
| 147 | if (isset($_POST['update_flickrrss'])) { |
|---|
| 148 | $option_flickrid = $_POST['flickr_nsid']; |
|---|
| 149 | $option_tags = $_POST['tags']; |
|---|
| 150 | $option_display_type = $_POST['display_type']; |
|---|
| 151 | $option_display_numitems = $_POST['display_numitems']; |
|---|
| 152 | $option_display_imagesize = $_POST['display_imagesize']; |
|---|
| 153 | $option_before = $_POST['before_image']; |
|---|
| 154 | $option_after = $_POST['after_image']; |
|---|
| 155 | update_option('flickrRSS_flickrid', $option_flickrid); |
|---|
| 156 | update_option('flickrRSS_tags', $option_tags); |
|---|
| 157 | update_option('flickrRSS_display_type', $option_display_type); |
|---|
| 158 | update_option('flickrRSS_display_numitems', $option_display_numitems); |
|---|
| 159 | update_option('flickrRSS_display_imagesize', $option_display_imagesize); |
|---|
| 160 | update_option('flickrRSS_before', $option_before); |
|---|
| 161 | update_option('flickrRSS_after', $option_after); |
|---|
| 162 | ?> <div class="updated"><p>Options changes saved.</p></div> <?php |
|---|
| 163 | } |
|---|
| 164 | if (isset($_POST['save_cache_settings'])) { |
|---|
| 165 | $option_useimagecache = $_POST['use_image_cache']; |
|---|
| 166 | $option_imagecacheuri = $_POST['image_cache_uri']; |
|---|
| 167 | $option_imagecachedest = $_POST['image_cache_dest']; |
|---|
| 168 | update_option('flickrRSS_use_image_cache', $option_useimagecache); |
|---|
| 169 | update_option('flickrRSS_image_cache_uri', $option_imagecacheuri); |
|---|
| 170 | update_option('flickrRSS_image_cache_dest', $option_imagecachedest); |
|---|
| 171 | ?> <div class="updated"><p>Cache settings saved.</p></div> <?php |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | ?> |
|---|
| 175 | |
|---|
| 176 | <div class="wrap"> |
|---|
| 177 | <h2>flickrRSS Options</h2> |
|---|
| 178 | <form method="post"> |
|---|
| 179 | |
|---|
| 180 | <fieldset class="options"> |
|---|
| 181 | <table> |
|---|
| 182 | <tr> |
|---|
| 183 | <td><p><strong><label for="flickr_nsid">User ID</label>:</strong></p></td> |
|---|
| 184 | <td><input name="flickr_nsid" type="text" id="flickr_nsid" value="<?php echo get_option('flickrRSS_flickrid'); ?>" size="20" /> |
|---|
| 185 | Use the <a href="http://idgettr.com">idGettr</a> to find your id.</p></td> |
|---|
| 186 | </tr> |
|---|
| 187 | <tr> |
|---|
| 188 | <td><p><strong>Display:</strong></p></td> |
|---|
| 189 | <td> |
|---|
| 190 | <select name="display_type" id="display_type"> |
|---|
| 191 | <option <?php if(get_option('flickrRSS_display_type') == 'user') { echo 'selected'; } ?> value="user">user</option> |
|---|
| 192 | <option <?php if(get_option('flickrRSS_display_type') == 'public') { echo 'selected'; } ?> value="public">public</option> |
|---|
| 193 | <option <?php if(get_option('flickrRSS_display_type') == 'group') { echo 'selected'; } ?> value="group">group</option> |
|---|
| 194 | </select> |
|---|
| 195 | photos using |
|---|
| 196 | <select name="display_numitems" id="display_numitems"> |
|---|
| 197 | <option <?php if(get_option('flickrRSS_display_numitems') == '1') { echo 'selected'; } ?> value="1">1</option> |
|---|
| 198 | <option <?php if(get_option('flickrRSS_display_numitems') == '2') { echo 'selected'; } ?> value="2">2</option> |
|---|
| 199 | <option <?php if(get_option('flickrRSS_display_numitems') == '3') { echo 'selected'; } ?> value="3">3</option> |
|---|
| 200 | <option <?php if(get_option('flickrRSS_display_numitems') == '4') { echo 'selected'; } ?> value="4">4</option> |
|---|
| 201 | <option <?php if(get_option('flickrRSS_display_numitems') == '5') { echo 'selected'; } ?> value="5">5</option> |
|---|
| 202 | <option <?php if(get_option('flickrRSS_display_numitems') == '6') { echo 'selected'; } ?> value="6">6</option> |
|---|
| 203 | <option <?php if(get_option('flickrRSS_display_numitems') == '7') { echo 'selected'; } ?> value="7">7</option> |
|---|
| 204 | <option <?php if(get_option('flickrRSS_display_numitems') == '8') { echo 'selected'; } ?> value="8">8</option> |
|---|
| 205 | <option <?php if(get_option('flickrRSS_display_numitems') == '9') { echo 'selected'; } ?> value="9">9</option> |
|---|
| 206 | <option <?php if(get_option('flickrRSS_display_numitems') == '10') { echo 'selected'; } ?> value="10">10</option> |
|---|
| 207 | </select> |
|---|
| 208 | <select name="display_imagesize" id="display_imagesize"> |
|---|
| 209 | <option <?php if(get_option('flickrRSS_display_imagesize') == 'square') { echo 'selected'; } ?> value="square">square</option> |
|---|
| 210 | <option <?php if(get_option('flickrRSS_display_imagesize') == 'thumbnail') { echo 'selected'; } ?> value="thumbnail">thumbnail</option> |
|---|
| 211 | <option <?php if(get_option('flickrRSS_display_imagesize') == 'small') { echo 'selected'; } ?> value="small">small</option> |
|---|
| 212 | <option <?php if(get_option('flickrRSS_display_imagesize') == 'medium') { echo 'selected'; } ?> value="medium">medium</option> |
|---|
| 213 | </select> |
|---|
| 214 | <label for="mediumImages">images</label></p> |
|---|
| 215 | </td> |
|---|
| 216 | </tr> |
|---|
| 217 | <tr> |
|---|
| 218 | <td><p><strong><label for="tag">Tags:</strong></label></p></td> |
|---|
| 219 | <td><input name="tags" type="text" id="tags" value="<?php echo get_option('flickrRSS_tags'); ?>" size="40" /> Comma separated, no spaces</p> |
|---|
| 220 | </tr> |
|---|
| 221 | <tr> |
|---|
| 222 | <td><p><strong><label for="before_image">Before</label>/<label for="after_image">After</label>:</strong></p></td> |
|---|
| 223 | <td><input name="before_image" type="text" id="before_image" value="<?php echo htmlspecialchars(stripslashes(get_option('flickrRSS_before'))); ?>" size="10" /> / |
|---|
| 224 | <input name="after_image" type="text" id="after_image" value="<?php echo htmlspecialchars(stripslashes(get_option('flickrRSS_after'))); ?>" size="10" /> <em> e.g. <li></li>, <p></p></em></p> |
|---|
| 225 | </td> |
|---|
| 226 | </tr> |
|---|
| 227 | </table> |
|---|
| 228 | </fieldset> |
|---|
| 229 | |
|---|
| 230 | <p><div class="submit"><input type="submit" name="update_flickrrss" value="<?php _e('Update flickrRSS', 'update_flickrrss') ?>" style="font-weight:bold;" /></div></p> |
|---|
| 231 | </form> |
|---|
| 232 | </div> |
|---|
| 233 | |
|---|
| 234 | <div class="wrap"> |
|---|
| 235 | <h2>Cache Settings</h2> |
|---|
| 236 | |
|---|
| 237 | <form method="post"> |
|---|
| 238 | <fieldset class="options"> |
|---|
| 239 | <table> |
|---|
| 240 | <tr> |
|---|
| 241 | <td><p><strong><label for="image_cache_uri">URL:</label></strong></td> |
|---|
| 242 | <td><input name="image_cache_uri" type="text" id="image_cache_uri" value="<?php echo get_option('flickrRSS_image_cache_uri'); ?>" size="50" /> <em>e.g. http://url.com/cache/</em></p> |
|---|
| 243 | </tr> |
|---|
| 244 | <tr> |
|---|
| 245 | <td><p><strong><label for="image_cache_dest">Full Path:</label></strong></td> |
|---|
| 246 | <td><input name="image_cache_dest" type="text" id="image_cache_dest" value="<?php echo get_option('flickrRSS_image_cache_dest'); ?>" size="50" /> <em>e.g. /home/path/to/wp-content/flickrrss/cache/</em></p></td> |
|---|
| 247 | </tr> |
|---|
| 248 | <tr> |
|---|
| 249 | <td></td> |
|---|
| 250 | <td><p><input name="use_image_cache" type="checkbox" id="use_image_cache" value="true" <?php if(get_option('flickrRSS_use_image_cache') == 'true') { echo 'checked="checked"'; } ?> /> <label for="use_image_cache"><strong>Use image cache</strong> (stores thumbnails on your server)</label></p></td> |
|---|
| 251 | </tr> |
|---|
| 252 | </table> |
|---|
| 253 | </fieldset> |
|---|
| 254 | <p><div class="submit"> |
|---|
| 255 | <input type="submit" name="save_cache_settings" value="<?php _e('Save Settings', 'save_cache_settings') ?>" style="font-weight:bold;" /></div> |
|---|
| 256 | </p> |
|---|
| 257 | </form> |
|---|
| 258 | </div> |
|---|
| 259 | |
|---|
| 260 | <?php } // end flickrRSS_subpanel() |
|---|
| 261 | |
|---|
| 262 | function fR_admin_menu() { |
|---|
| 263 | if (function_exists('add_options_page')) { |
|---|
| 264 | add_options_page('flickrRSS Options Page', 'flickrRSS', 8, basename(__FILE__), 'flickrRSS_subpanel'); |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | add_action('admin_menu', 'fR_admin_menu'); |
|---|
| 269 | add_action('plugins_loaded', 'widget_flickrrss_init'); |
|---|
| 270 | ?> |
|---|