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