This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
forked from tgardner/wordpress-flickr-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlickrPanel.php
454 lines (354 loc) · 14.1 KB
/
FlickrPanel.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
// Load Dependencies
require_once(dirname(__FILE__) . '/lib/inc.templater.php');
// Load JSON support for PHP < 5.2
include_once(dirname(__FILE__) . '/lib/inc.json.php');
if(!class_exists('FlickrPanel')) :
class FlickrPanel
{
function __construct()
{
add_action('init', array(&$this, 'Initialize'));
}
function Initialize()
{
add_action('media_buttons', array(&$this, 'RenderMediaButton'), 20);
add_action('media_upload_flickr', array(&$this, 'CreateMediaFrame'));
add_action('media_upload_flickr_public', array(&$this, 'CreateMediaFrame'));
add_action('media_upload_flickr_upload', array(&$this, 'CreateMediaFrame'));
add_action('media_upload_flickr_sets', array(&$this, 'CreateMediaFrame'));
add_action('admin_print_styles-media-upload-popup', array(&$this, 'LoadCSS'));
add_action('admin_print_scripts-media-upload-popup', array(&$this, 'LoadJavascript'));
add_filter('wp_handle_upload', array(&$this, 'UploadPhoto'));
}
function LoadCSS()
{
global $type;
if($type == 'flickr') {
wp_admin_css('css/media');
wp_enqueue_style('wfm-media-panel-css', plugins_url("/css/media_panel.css", __FILE__));
}
}
function LoadJavascript()
{
global $flickr_manager, $type;
if($type == 'flickr') {
wp_enqueue_script('wfm-common', plugins_url('/js/wfm-common.js', __FILE__), array('jquery'), '20110429');
wp_enqueue_script('wfm-media-panel', plugins_url('/js/MediaPanel.js', __FILE__), array('jquery'), '20110429');
$params = array(
'WFMPath' => $flickr_manager->absoluteURL
,'WFMAjaxURL' => plugins_url(basename(__FILE__), __FILE__)
,'WFMAjaxNonce' => wp_create_nonce('wfm_ajax-action')
,'loadingImage' => plugins_url('/images/loading.gif', __FILE__)
,'loadingText' => __('Loading...', 'flickr-manager')
,'deleteConfirm' => __('Are you sure you want to delete this?', 'flickr-manager')
,'wrapBefore' => $flickr_manager->settings["before_wrap"]
,'wrapAfter' => $flickr_manager->settings["after_wrap"]
);
$scriptTag = "<script type='text/javascript'>\n// <![CDATA[\n";
foreach($params as $k => $v) {
$scriptTag .= sprintf("var %s = '%s';\n", $k, $v);
}
$scriptTag .= "//]]>\n</script>";
echo $scriptTag;
}
}
function CreateMediaFrame()
{
wp_iframe(array(&$this, 'RenderMediaFrame'));
}
function RenderMediaButton()
{
global $post_ID, $temp_ID, $flickr_manager;
$uploading_iframe_ID = (int) ($post_ID == 0 ? $temp_ID : $post_ID);
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
$flickr_upload_iframe_src = apply_filters('media_flickr_iframe_src', "$media_upload_iframe_src&type=flickr");
$flickr_title = __('Add Flickr Photo', 'flickr-manager');
$media_button = '<a href="%s&tab=flickr&TB_iframe=true&height=500&width=640"
class="thickbox" title="%s"><img src="%s/images/flickr-media.gif" alt="%s" /></a>';
echo sprintf($media_button, $flickr_upload_iframe_src, $flickr_title, $flickr_manager->absoluteURL, $flickr_title);
}
function RenderMediaFrame()
{
global $flickr_manager, $tab, $type;
add_filter('media_upload_tabs', array(&$this, 'GetMediaTitles'));
if(!$flickr_manager->flickr->auth_checkToken()) {
$error = new Page('templates/Error.tpl');
echo $error->render(array(
'Error' => sprintf('%s <a href="%s">Settings->Flickr</a>'
,__('Error: Please authenticate through ', 'flickr-manager')
,admin_url("/options-general.php?page=" . $flickr_manager->GetBaseName()))
));
return;
}
switch ($tab) {
case 'flickr_upload':
$this->MediaUploadPanel();
break;
default:
$this->MediaPhotoPanel();
break;
}
}
function GetMediaTitles() {
return array(
'flickr' => __('Photo stream', 'flickr-manager'),
'flickr_sets' => __('Albums', 'flickr-manager'),
'flickr_public' => __('Everyone', 'flickr-manager'),
'flickr_upload' => __('Upload', 'flickr-manager')
);
}
function MediaPhotoPanel()
{
global $tab, $flickr_manager;
$titles = $this->GetMediaTitles();
media_upload_header();
$params = array(
'Title' => $titles[$tab]
,'Search' => __('Search', 'flickr-manager')
,'AbsoluteURL' => $flickr_manager->absoluteURL
,'Tab' => $tab
);
$page = new Page('templates/MediaPanel.tpl');
echo $page->render($params);
}
function MediaUploadPanel()
{
global $tab, $flickr_manager;
$titles = $this->GetMediaTitles();
add_filter('media_upload_form_url', array(&$this, 'UploadFormFilter'));
media_upload_image();
}
function UploadFormFilter($url) {
$patterns = array('/type=[^&]+/i', '/tab=[^&]+/i');
$replacements = array('type=flickr', 'tab=flickr_upload');
return preg_replace($patterns, $replacements, $url);
}
function UploadPhoto($file) {
global $flickr_manager;
$type = $_REQUEST['type'];
if($type == "flickr") {
$pid = $flickr_manager->flickr->sync_upload($file['file'], basename($file['file']), '', '');
// Delete file
unlink($file['file']);
if(!empty($pid) && !$flickr_manager->flickr->error_code) {
$file['error'] = "flickr_$pid";
} else {
return wp_handle_upload_error($file, __('An error occurred while trying to upload your photo', 'flickr-manager'));
}
}
return $file;
}
function func_GetPhotos() {
global $flickr_manager;
$page = (empty($_POST['page'])) ? 1 : $_POST['page'];
$filter = (empty($_POST['filter'])) ? null : $_POST['filter'];
$owner = ($_POST['tab'] == 'flickr_public') ? null : $flickr_manager->settings['nsid'];
$photos = $flickr_manager->GetPhotos($page, $owner, $filter);
$images = array();
if(is_array($photos['photo']) && !empty($photos['photo'])) {
foreach($photos['photo'] as $photo) {
array_push($images, array(
'square' => $flickr_manager->flickr->buildPhotoURL($photo, 'square')
,'title' => htmlspecialchars($photo['title'])
));
}
}
$data = array(
'photos' => $images
,'pages' => $photos['pages']
);
echo json_encode($data);
}
function func_GetPhotoset($photoset) {
global $flickr_manager;
$photosets = $flickr_manager->flickr->photosets_getList($flickr_manager->settings['nsid']);
if(empty($photoset) && !empty($photosets['photoset'])) {
$photoset = $photosets['photoset'][0]['id'];
}
$priv = ($flickr_manager->settings['privacy_filter'] == 'true') ? 1 : NULL;
$page = (empty($_POST['page'])) ? 1 : $_POST['page'];
$photos = $flickr_manager->flickr->photosets_getPhotos($photoset
,'original_format,date_upload,owner_name'
,$priv
,$flickr_manager->settings['per_page']
,$page);
$images = array();
if(is_array($photos['photoset']) && !empty($photos['photoset'])) {
foreach ($photos['photoset']['photo'] as $photo) {
array_push($images, array(
'square' => $flickr_manager->flickr->buildPhotoURL($photo, 'square')
,'title' => htmlspecialchars($photo['title'])
));
}
}
$data = array(
'photos' => $images
,'pages' => $photos['photoset']['pages']
,'photosets' => $photosets['photoset']
,'insertText' => __('Insert Set','flickr-manager')
);
echo json_encode($data);
}
function func_PhotoInfo($pid)
{
global $flickr_manager;
if(!empty($pid)) {
$page = new Page('templates/InsertPhoto.tpl');
// Get photo info and bypass caching in case the image has been changed
$flickr_manager->flickr->request('flickr.photos.getInfo', array('photo_id' => $pid), true);
$photo = $flickr_manager->flickr->parsed_response ? $flickr_manager->flickr->parsed_response : false;
$sizes = $flickr_manager->flickr->photos_getSizes($pid);
$photo = $photo['photo'];
$tags = "";
foreach($photo['tags']['tag'] as $tag) {
$tags .= sprintf("%s ", $tag['raw']);
}
$sizeMarkup = FlickrPanel::GetSizeOptions('imageSize', $sizes, 'thumbnail');
$overlayOptions = FlickrPanel::GetSizeOptions('overlaySize', $sizes, $flickr_manager->settings['lightbox_default']);
$params = array(
'Thumbnail' => $flickr_manager->flickr->buildPhotoURL($photo, 'thumbnail')
,'Title' => htmlspecialchars($photo['title'])
,'File' => basename($flickr_manager->flickr->buildPhotoURL($photo, 'medium'))
,'Uploaded' => htmlspecialchars(date('Y-m-d H:i:s', intval($photo['dateuploaded'])))
,'TitleLabel' => __('Title', 'flickr-manager')
,'TagsLabel' => __('Tags', 'flickr-manager')
,'Tags' => $tags
,'SpaceSeparated' => __('Space separated list', 'flickr-manager')
,'DescriptionLabel' => __('Description', 'flickr-manager')
,'Description' => htmlspecialchars(trim($photo['description']))
,'LinkLabel' => __('Link URL', 'flickr-manager')
,'Link' => htmlspecialchars($photo['urls']['url'][0]['_content'])
,'AlignmentLabel' => __('Alignment', 'flickr-manager')
,'AlignNone' => __('None', 'flickr-manager')
,'AlignLeft' => __('Left', 'flickr-manager')
,'AlignCentre' => __('Center', 'flickr-manager')
,'AlignRight' => __('Right', 'flickr-manager')
,'SizeLabel' => __('Size', 'flickr-manager')
,'Sizes' => $sizeMarkup
,'InsertLabel' => __('Insert into Post', 'flickr-manager')
,'OwnerControls' => FlickrPanel::GetOwnerControls()
,'DefaultOverlay' => ($flickr_manager->settings['lightbox_enable'] == "true") ? "checked" : ''
,'OverlaySizes' => $overlayOptions
,'EnableOverlay' => __('Enable Viewer', 'flickr-manager')
,'OverlaySizeLabel' => __('Viewer Size', 'flickr-manager')
,'SetName' => ''
,'SetLabel' => __('Image Group', 'flickr-manager')
);
echo $page->render($params);
}
}
function func_PhotosetInfo($photoset)
{
global $flickr_manager;
if(!empty($photoset)) {
$set = $flickr_manager->flickr->photosets_getInfo($photoset);
$buildSize = create_function('$id,$desc', 'return array("id"=>$id,"label"=>$desc);');
$sizes = array( $buildSize('square', __('Square', 'flickr-manager')),
$buildSize('thumbnail', __('Thumbnail', 'flickr-manager')),
$buildSize('small', __('Small', 'flickr-manager')),
$buildSize('medium', __('Medium', 'flickr-manager')),
$buildSize('large', __('Large', 'flickr-manager')));
if($flickr_manager->settings['is_pro'] == '1')
$sizes[] = $buildSize('original', __('Original', 'flickr-manager'));
$sizeMarkup = FlickrPanel::GetSizeOptions('imageSize', $sizes, 'thumbnail');
$overlayOptions = FlickrPanel::GetSizeOptions('overlaySize', $sizes, $flickr_manager->settings['lightbox_default']);
$page = new Page('templates/InsertPhotoset.tpl');
$params = array(
'TitleLabel' => __('Title', 'flickr-manager')
,'DescriptionLabel' => __('Description', 'flickr-manager')
,'SizeLabel' => __('Size', 'flickr-manager')
,'EnableOverlay' => __('Enable Viewer', 'flickr-manager')
,'OverlaySizeLabel' => __('Viewer Size', 'flickr-manager')
,'NumPhotos' => __('* Leave blank to display the entire album.', 'flickr-manager')
,'PhotosLabel' => __('Number of Photos', 'flickr-manager')
,'Title' => htmlspecialchars($set['title'])
,'Description' => htmlspecialchars($set['description'])
,'Sizes' => $sizeMarkup
,'OverlaySizes' => $overlayOptions
,'DefaultOverlay' => ($flickr_manager->settings['lightbox_enable'] == "true") ? 'checked="checked"' : ''
,'InsertLabel' => __('Insert into Post', 'flickr-manager')
);
echo $page->render($params);
}
}
function GetOwnerControls()
{
$markup = '<input class="button savePhoto" type="button" value="%s" />';
$markup .= '<input class="button deletePhoto" type="button" value="%s" />';
return sprintf($markup
,__('Save', 'flickr-manager')
,__('Delete', 'flickr-manager'));
}
function GetSizeOptions($name, $sizes, $default) {
$html = '';
foreach($sizes as $size) {
$option = '<div class="image-size-item">
<input id="[Name]-[ID]" type="radio" value="[ID]" name="[Name]" [Checked] />
<label for="[Name]-[ID]">[Text]</label>
<label class="help" for="[Name]-[ID]">[Desc]</label>
</div>';
if(empty($size['id'])) {
$size['id'] = str_replace(" ","_",strtolower($size['label']));
}
$html .= str_replace(array('[ID]','[Text]','[Desc]','[Checked]','[Name]')
,array(
$size['id']
,$size['label']
,!empty($size['width']) ? sprintf("(%s × %s)", $size['width'], $size['height']) : ''
,(strtolower($size['id']) == $default) ? 'checked="checked"' : ''
,$name
),$option);
}
return $html;
}
function func_SaveInfo()
{
global $flickr_manager;
echo $flickr_manager->SavePhoto($_POST['pid']
,$_POST['title']
,$_POST['description']
,$_POST['tags']);
}
function func_DeletePhoto($photoid)
{
global $flickr_manager;
$flickr_manager->flickr->photos_delete($photoid);
}
}
endif;
if(strtoupper($_SERVER['REQUEST_METHOD']) == "POST" && basename($_SERVER['REQUEST_URI']) == basename(__FILE__))
{
// AJAX Handler
error_reporting(E_ERROR);
header("Cache-Control: no-cache");
header("Pragma: no-cache");
// Load Wordpress Core
require_once( dirname(__FILE__) . '/../../../wp-load.php');
$wp->init();
check_ajax_referer('wfm_ajax-action');
switch($_POST['func']) {
case 'func.getPhotos':
FlickrPanel::func_GetPhotos();
break;
case 'func.photoInfo':
FlickrPanel::func_PhotoInfo($_POST['pid']);
break;
case 'func.saveInfo':
FlickrPanel::func_SaveInfo();
break;
case 'func.deletePhoto':
FlickrPanel::func_DeletePhoto($_POST['pid']);
break;
case 'func.getPhotoset':
FlickrPanel::func_GetPhotoset($_POST['photoset']);
break;
case 'func.photosetInfo':
FlickrPanel::func_PhotosetInfo($_POST['photoset']);
break;
default:
die(__("Invalid function call or reference!", 'flickr-manager'));
break;
}
}
?>