/plugins/flickrfeed/trunk/flickrfeed.plugin.php

https://github.com/somefool/habari-extras · PHP · 195 lines · 163 code · 20 blank · 12 comment · 16 complexity · 2ca15f3873a35fef3daeec76aff477d4 MD5 · raw file

  1. <?php
  2. /**
  3. * FlickrFeed Plugin: Show the images from Flickr feed
  4. */
  5. class FlickrFeed extends Plugin
  6. {
  7. private static function default_options( )
  8. {
  9. return array (
  10. 'feed_type' => 'user',
  11. 'user_id' => '',
  12. 'image_count' => '6',
  13. 'image_size' => 'square',
  14. 'image_tags' => '',
  15. 'cache_expiry' => '1800',
  16. );
  17. }
  18. public function validate_uint( $value )
  19. {
  20. if ( !ctype_digit( $value ) || strstr( $value, '.' ) || $value < 0 ) {
  21. return array( _t( 'This field must be positive integer.', 'flickrfeed' ) );
  22. }
  23. return array( );
  24. }
  25. public function validate_flickr_id( $value )
  26. {
  27. if ( empty( $value ) && in_array( $this->config[ 'type' ], array( 'user', 'group' ) ) ) {
  28. return array( _t( 'A value for this field is required while type is not ‘Public’.', 'flickrfeed' ) );
  29. }
  30. return array( );
  31. }
  32. public function filter_block_list( $block_list )
  33. {
  34. $block_list[ 'flickrfeed' ] = _t( 'FlickrFeed', 'flickrfeed' );
  35. return $block_list;
  36. }
  37. private static function build_api_url( $block )
  38. {
  39. switch ( $block->feed_type ) {
  40. case 'user':
  41. return 'http://api.flickr.com/services/feeds/photos_public.gne?id=' . $block->user_id . '&tags=' . $block->image_tags . '&format=php_serial';
  42. break;
  43. case 'friends':
  44. return 'http://api.flickr.com/services/feeds/photos_friends.gne?user_id=' . $block->user_id . '&format=php_serial';
  45. break;
  46. case 'faves':
  47. return 'http://api.flickr.com/services/feeds/photos_faves.gne?id=' . $block->user_id . '&format=php_serial';
  48. break;
  49. case 'group':
  50. return 'http://api.flickr.com/services/feeds/groups_pool.gne?id=' . $block->user_id. '&format=php_serial';
  51. break;
  52. default:
  53. return 'http://api.flickr.com/services/feeds/photos_public.gne?tags=' . $block->image_tags . '&format=php_serial';
  54. break;
  55. }
  56. }
  57. private static function get_external_content( $url )
  58. {
  59. // Get PHP serialized object from Flickr
  60. $call = new RemoteRequest( $url );
  61. $call->set_timeout( 5 );
  62. $result = $call->execute( );
  63. if ( Error::is_error( $result ) ) {
  64. throw Error::raise( _t( 'Unable to contact Flickr.', 'flickrfeed' ) );
  65. }
  66. return $call->get_response_body( );
  67. }
  68. private static function parse_data( $block, $data )
  69. {
  70. // Unserialize and manipulate the data
  71. $flickrfeed = unserialize( $data );
  72. $flickrfeed = array_slice( $flickrfeed[ 'items' ], 0, $block->image_count );
  73. // Photo size
  74. foreach ( $flickrfeed as &$image ) {
  75. $image[ 'image_sizes' ] = array(
  76. 'thumbnail' => str_replace( '_m.jpg', '_t.jpg', $image[ 'm_url' ] ),
  77. 'small' => $image[ 'm_url' ],
  78. 'medium' => $image[ 'l_url' ],
  79. 'medium_z' => str_replace( '_m.jpg', '_z.jpg', $image[ 'm_url' ] ),
  80. 'large' => str_replace( '_m.jpg', '_b.jpg', $image[ 'm_url' ] ),
  81. 'original' => $image[ 'photo_url' ],
  82. 'default' => $image[ 't_url' ],
  83. );
  84. if( isset( $image[ 'image_sizes' ][ $block->image_size ] ) ) {
  85. $image[ 'image_url' ] = $image[ 'image_sizes' ][ $block->image_size ];
  86. }
  87. else {
  88. $image[ 'image_url' ] = $image[ 'image_sizes' ][ 'default' ];
  89. }
  90. }
  91. return $flickrfeed;
  92. }
  93. public function action_block_content_flickrfeed( $block, $theme )
  94. {
  95. // Load defaults
  96. foreach ( self::default_options( ) as $k => $v ) {
  97. if ( !isset( $block->$k ) )
  98. $block->$k = $v;
  99. }
  100. $cache_name = 'flickrfeed_' . md5( serialize( array(
  101. $block->type,
  102. $block->user_id,
  103. $block->image_count,
  104. $block->image_size,
  105. $block->image_tags
  106. ) ) );
  107. if ( $block->user_id != '' ) {
  108. if ( Cache::has( $cache_name ) ) {
  109. $block->images = Cache::get( $cache_name );
  110. }
  111. else {
  112. try {
  113. $url = self::build_api_url( $block );
  114. $data = self::get_external_content( $url );
  115. $images = self::parse_data( $block, $data );
  116. $block->images = $images;
  117. // Do cache
  118. Cache::set( $cache_name, $block->images, $block->cache_expiry );
  119. }
  120. catch ( Exception $e ) {
  121. $block->error = $e->getMessage( );
  122. }
  123. }
  124. }
  125. else {
  126. $block->error = _t( 'FlickrFeed Plugin is not configured properly.', 'flickrfeed' );
  127. }
  128. }
  129. public function action_block_form_flickrfeed( $form, $block )
  130. {
  131. // Load defaults
  132. foreach ( self::default_options( ) as $k => $v ) {
  133. if ( !isset( $block->$k ) )
  134. $block->$k = $v;
  135. }
  136. $form->append( 'select', 'feed_type', $block, _t( 'Photostream Type', 'flickrfeed' ) );
  137. $form->feed_type->options = array(
  138. 'public' => _t( 'Public photos & video', 'flickrfeed' ),
  139. 'user' => _t( 'Public photos & video from you', 'flickrfeed' ),
  140. 'friends' => _t( 'Your friends’ photostream', 'flickrfeed' ),
  141. 'faves' => _t( 'Public favorites from you', 'flickrfeed' ),
  142. 'group' => _t( 'Group pool', 'flickrfeed' )
  143. );
  144. $form->feed_type->add_validator( 'validate_required' );
  145. $form->append( 'text', 'user_id', $block, _t( 'Flickr ID ( You can get it from <a target="_blank" href="http://idgettr.com">idGettr</a>)', 'flickrfeed' ) );
  146. $form->user_id->add_validator( 'validate_flickr_id' );
  147. $form->append( 'text', 'image_count', $block, _t( '&#8470; of Photos', 'flickrfeed' ) );
  148. $form->image_count->add_validator( 'validate_uint' );
  149. $form->image_count->add_validator( 'validate_required' );
  150. $form->append( 'select', 'image_size', $block, _t( 'Photo Size', 'flickrfeed' ) );
  151. $form->image_size->options = array(
  152. 'square' => _t( 'Square', 'flickrfeed' ),
  153. 'thumbnail' => _t( 'Thumbnail', 'flickrfeed' ),
  154. 'small' => _t( 'Small', 'flickrfeed' ),
  155. 'medium' => _t( 'Medium ( 500 )', 'flickrfeed' ),
  156. 'medium_z' => _t( 'Medium ( 640 )', 'flickrfeed' ),
  157. 'large' => _t( 'Large', 'flickrfeed' ),
  158. 'original' => _t( 'Original', 'flickrfeed' )
  159. );
  160. $form->image_size->add_validator( 'validate_required' );
  161. $form->append( 'text', 'image_tags', $block, _t( 'Tags ( comma separated, no space )', 'flickrfeed' ) );
  162. $form->append( 'text', 'cache_expiry', $block, _t( 'Cache Expiry ( in seconds )', 'flickrfeed' ) );
  163. $form->cache_expiry->add_validator( 'validate_uint' );
  164. $form->cache_expiry->add_validator( 'validate_required' );
  165. }
  166. /**
  167. * On plugin init, add the template included with this plugin to the available templates in the theme
  168. */
  169. public function action_init( )
  170. {
  171. $this->load_text_domain( 'flickrfeed' );
  172. $this->add_template( 'block.flickrfeed', dirname( __FILE__ ) . '/block.flickrfeed.php' );
  173. }
  174. }
  175. ?>