Advertisement
AeroMaxx

Flickr Photostream

Dec 15th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.70 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Flickr Photostream
  4. Plugin URI: http://miromannino.it/projects/flickr-photostream/
  5. Description: Shows the flickr photostream
  6. Version: 1.5
  7. Author: Miro Mannino
  8. Author URI: http://miromannino.it/about-me/
  9.  
  10. Copyright 2012 Miro Mannino (miro.mannino@gmail.com)
  11. thanks to Dan Coulter for phpFlickr Class (dan@dancoulter.com)
  12.  
  13. This file is part of Flickr Photostream.
  14.  
  15. Flickr Photostream is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by the Free Software
  17. Foundation, either version 3 of the License, or (at your option) any later version.
  18.  
  19. Flickr Photostream is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  21. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License along with Flickr
  24. Photostream Wordpress Plugin.  If not, see <http://www.gnu.org/licenses/>.
  25. */
  26.  
  27. //Add the link to the plugin page
  28. add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'flickrps_plugin_settings_link' );
  29. function flickrps_plugin_settings_link($links) {
  30.   $settings_link = '<a href="options-general.php?page=flickrps.php">Settings</a>';
  31.   array_unshift($links, $settings_link);
  32.   return $links;
  33. }
  34.  
  35. //Activation hook, we check that the upload dir is writable
  36. register_activation_hook( __FILE__ , 'flickrps_plugin_activate');
  37. if(!function_exists( 'flickrps_plugin_uninstall')){
  38.     function flickrps_plugin_activate(){
  39.         $upload_dir = wp_upload_dir();
  40.         @mkdir($upload_dir['basedir'].'/phpFlickrCache');
  41.         if(!is_writable($upload_dir['basedir'].'/phpFlickrCache')){
  42.             deactivate_plugins(basename(__FILE__)); // Deactivate ourself
  43.             wp_die(__('Flickr Photostream can\'t be activated: the cache Folder is not writable', 'flickr-photostream')
  44.                 . ' (' . $upload_dir['basedir'] .'/phpFlickrCache' . ')'
  45.             );
  46.         }
  47.     }
  48. }
  49.  
  50. //Add the language and the permalink
  51. add_action('init', 'flickrps_init');
  52. function flickrps_init() {
  53.     /* languages */
  54.     load_plugin_textdomain('flickr-photostream', false, dirname(plugin_basename( __FILE__ )) . '/languages/');
  55. }
  56.  
  57. //Add the vars so that WP recognizes it
  58. add_filter('query_vars','flickrps_insert_query_vars');
  59. function flickrps_insert_query_vars( $vars ) {
  60.     array_push($vars, 'flickrpsp');
  61.     return $vars;
  62. }
  63.  
  64. //Register with hook 'wp_enqueue_scripts' which can be used for front end CSS and JavaScript
  65. add_action('wp_enqueue_scripts', 'addFlickrPhotostreamCSSandJS');
  66. function addFlickrPhotostreamCSSandJS() {
  67.     wp_register_style('flickrPsCSS', plugins_url('css/flickr-photostream.css', __FILE__));
  68.     wp_register_style('justifiedGalleryCSS', plugins_url('justifiedgallery/css/jquery.justifiedgallery.css', __FILE__));
  69.     wp_register_script('justifiedGalleryJS', plugins_url('justifiedgallery/js/jquery.justifiedgallery-min.js', __FILE__));
  70.     wp_enqueue_style('flickrPsCSS');
  71.     wp_enqueue_style('justifiedGalleryCSS');
  72.     wp_enqueue_script('jquery');
  73.     wp_enqueue_script('justifiedGalleryJS');
  74. }
  75.  
  76. add_action('parse_request', 'addFlickrPhotostreamURL');
  77. function addFlickrPhotostreamURL() {
  78.     global $flickrpsp;
  79.  
  80.     // Manually parse the URL request
  81.     if (!empty($_SERVER['REQUEST_URI'])) {
  82.         $urlvars = explode('/', $_SERVER['REQUEST_URI']);
  83.     }
  84.  
  85.     var_dump($_SERVER['REQUEST_URI']);
  86.  
  87.     // Check for querystring variables
  88.     if ((!empty($urlvars[1])) && (!empty($urlvars[2]))) {
  89.         $_REQUEST['page'] = $urlvars[1];
  90.         $_REQUEST['flickrpsp'] = $urlvars[2];
  91.  
  92.         $flickrpsp = $urlvars[2];
  93.         }
  94.  
  95.         // ... handle multiple types of URL by checking $urlvars[1] here
  96. }
  97.  
  98. //[flickrps] shortcode
  99. function flickr_photostream( $atts, $content = null ) {
  100.     static $shortcode_unique_id = 0;
  101.     $ris = "";
  102.  
  103.     require_once("phpFlickr/phpFlickr.php");
  104.  
  105.     //Page---------
  106.     $permalink = get_permalink();
  107.  
  108.     //if (strpos($permalink,'?') === false) $permalink .= '?'; else $permalink .= '&';
  109.     global $flickrpsp;
  110.     if(empty($flickrpsp)){ //we use local instance of flickrpsp for the multiple instances
  111.         $l_flickrpsp = 1;
  112.     }else{
  113.         $l_flickrpsp = $flickrpsp;
  114.     }
  115.  
  116.     //Options-----------------------
  117.     extract( shortcode_atts( array(
  118.         'user_id' => get_option('$flickr_photostream_userID'), // Flickr User ID
  119.         'images_height' => get_option('$flickr_photostream_imagesHeight'), // Flickr images size
  120.         'max_num_photos' => get_option('$flickr_photostream_maxPhotosPP'), // Max number of Photos 
  121.         'justify_last_row' => (get_option('$flickr_photostream_justifyLastRow') == 1 ? 'true' : 'false'),
  122.         'fixed_height' => (get_option('$flickr_photostream_fixedHeight') == 1 ? 'true' : 'false'),
  123.         'lightbox' => (get_option('$flickr_photostream_lightbox') == 1 ? 'true' : 'false'),
  124.         'captions' => (get_option('$flickr_photostream_captions') == 1 ? 'true' : 'false'),
  125.         'no_pages' => (get_option('$flickr_photostream_noPages') == 1 ? 'true' : 'false'),
  126.         'margins' => get_option('$flickr_photostream_margins'),
  127.     ), $atts ) );
  128.  
  129.     $images_height = (int)$images_height;
  130.     if($images_height < 30) $images_height = 30;
  131.  
  132.     $max_num_photos = (int)$max_num_photos;
  133.     if ($max_num_photos < 1) $max_num_photos = 1;
  134.  
  135.     $justify_last_row = (strcmp($justify_last_row, 'true') == 0)? TRUE : FALSE;
  136.  
  137.     $fixed_height = (strcmp($fixed_height, 'true') == 0)? TRUE : FALSE;
  138.  
  139.     $lightbox = (strcmp($lightbox, 'true') == 0)? TRUE : FALSE;
  140.  
  141.     $captions = (strcmp($captions, 'true') == 0)? TRUE : FALSE;
  142.  
  143.     $margins = (int)$margins;
  144.     if ($margins < 0) $margins = 1;
  145.     if ($margins > 30) $margins = 30;
  146.  
  147.     $no_pages = (strcmp($no_pages, 'true') == 0) ? TRUE : FALSE;
  148.     if($no_pages) $l_flickrpsp = 1;
  149.     //-----------------------------
  150.  
  151.     //Inizialization---------------
  152.     $flickrAPIKey = get_option('$flickr_photostream_APIKey'); //Flickr API Key
  153.    
  154.     $f = new phpFlickr($flickrAPIKey);
  155.     $upload_dir = wp_upload_dir();
  156.     $f->enableCache("fs", $upload_dir['basedir']."/phpFlickrCache");
  157.  
  158.     //Errors-----------------------
  159.     if($f->test_echo() == false){
  160.         return('<div style="color:red">' . __('Invalid Flickr API Key', 'flickr-photostream') . '</div>'); 
  161.     }
  162.  
  163.     if($f->urls_getUserProfile($user_id) == false){
  164.         return('<div style="color:red">' . __('The user not exists', 'flickr-photostream') . '</div>');
  165.     }
  166.    
  167.     //Photo loading----------------
  168.     $photos_url = $f->urls_getUserPhotos($user_id);
  169.     $photos = $f->people_getPublicPhotos($user_id, NULL, "description", $max_num_photos, $l_flickrpsp);
  170.     if(count((array)$photos['photos']['photo']) == 0) return(__('No photos', 'flickr-photostream'));
  171.  
  172.     //we calculate that the aspect ratio has an average of 4:3
  173.     if($images_height <= 75){
  174.         $imgSize = "thumbnail"; //thumbnail (longest side:100)
  175.         $usedSuffix = "lt100";
  176.     }else if($images_height <= 180){
  177.         $imgSize = "small"; //small (longest side:240)
  178.         $usedSuffix = "lt240";
  179.     }else{ //if <= 240
  180.         $imgSize = "small_320"; //small (longest side:320)
  181.         $usedSuffix = "lt320";
  182.     }
  183.  
  184.     /*$ris .= '<!-- Flickr Photostream by Miro Mannino -->' . "\n"
  185.          .  '<div class="justifiedGallery"'
  186.          .  ' data-row-height="' . $images_height . '"'
  187.          .  ' data-justify-last-row="' . ($justify_last_row ? 'true' : 'false') . '"'
  188.          .  ' data-fixed-height="' . ($fixed_height ? 'true' : 'false') . '"'
  189.          .  ' data-lightbox="' . ($lightbox ? 'true' : 'false') . '"'
  190.          .  ' data-captions="' . ($captions ? 'true' : 'false') . '"'
  191.          .  ' data-margins="' . $margins . '"'
  192.          .  ' data-used-suffix="' . $usedSuffix . '" >';
  193.     */
  194.  
  195.     $ris .= '<!-- Flickr Photostream by Miro Mannino -->' . "\n"
  196.          .  '<div id="flickrPhotostream-' . $shortcode_unique_id . '" >';
  197.  
  198.     $r = 0;
  199.    
  200.     foreach ((array)$photos['photos']['photo'] as $photo) {
  201.         if($lightbox){
  202.             $ris .=
  203.                 '<a href="' . $f->buildPhotoURL($photo, "large") . '" title="' . $photo[title] . '">';
  204.         }else{
  205.             $ris .=
  206.                 '<a href="' . $photos_url . $photo[id] . '/in/photostream/lightbox/" target="_blank" title="' . $photo[title] . '">';
  207.         }
  208.        
  209.         $ris .= '<img alt="' . $photo[title] . '" src="' . $f->buildPhotoURL($photo, $imgSize) . '" />'
  210.              .  '</a>';
  211.        
  212.     }
  213.  
  214.     $ris .= '</div>'
  215.          .  '<script type="text/javascript">'
  216.          .  'jQuery("#flickrPhotostream-' . $shortcode_unique_id . '").justifiedGallery({'
  217.          .  '\'usedSuffix\':\'' . $usedSuffix . '\', '
  218.          .  '\'justifyLastRow\':' . ($justify_last_row ? 'true' : 'false') . ', '
  219.          .  '\'rowHeight\':' . $images_height . ', '
  220.          .  '\'fixedHeight\':' . ($fixed_height ? 'true' : 'false') . ', '
  221.          .  '\'lightbox\':' . ($lightbox ? 'true' : 'false') . ', '
  222.          .  '\'captions\':' . ($captions ? 'true' : 'false') . ', '
  223.          .  '\'margins\':' . $margins
  224.          .  '});'
  225.          .  '</script>';
  226.  
  227.  
  228.     //Navigation---------------------
  229.     if(!$no_pages){
  230.        
  231.         $nextPhotos = $f->people_getPublicPhotos($user_id, NULL, "description", $max_num_photos, $l_flickrpsp + 1);
  232.         if(sizeof($nextPhotos['photos']['photo']) > 0 || $l_flickrpsp > 1){
  233.             $ris .= '<div class="page-link">';
  234.         }
  235.  
  236.         if(sizeof($nextPhotos['photos']['photo']) > 0){
  237.             $ris .=
  238.               '<div class="nav-flickrps-next">'
  239.             . '<a href="' . $permalink . 'page/' . ((int)$l_flickrpsp + 1) . '">' . __('<span class="meta-nav">&larr;</span> Older photos', 'flickr-photostream') . '</a>'
  240.             . '</div>';
  241.         }
  242.  
  243.         if($l_flickrpsp > 1){
  244.             $ris .=
  245.               '<div class="nav-flickrps-prev">'
  246.             . '<a href="' . $permalink. 'page/' . ((int)$l_flickrpsp - 1) . '">' . __('Newer photos <span class="meta-nav">&rarr;</span>', 'flickr-photostream') . '</a>'
  247.             . '</div>';
  248.         }
  249.  
  250.         if(sizeof($nextPhotos['photos']['photo']) > 0 || $l_flickrpsp > 1){
  251.             $ris .= '</div>';
  252.         }
  253.     }
  254.  
  255.     $shortcode_unique_id++;
  256.     return($ris);
  257. }
  258. add_shortcode('flickrps', 'flickr_photostream');
  259.  
  260. //Options
  261. include("flickr-photostream-setting.php");
  262.  
  263. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement