PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.36
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.36
3.36 3.35 3.34 3.33 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 2.28 2.29 2.30 2.31 2.32 2.33 2.34 2.40 2.41 2.42 2.43 2.44 All 141 releases
photonic / Admin / Wizard / Flickr.php

Flickr.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 3.36, at Admin/Wizard/Flickr.php

734 lines 31.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Photonic_Plugin\Admin\Wizard;
4
5 use Photonic_Plugin\Core\Photonic;
6 use Photonic_Plugin\Core\Utilities;
7
8 class Flickr extends Source {
9 private static ?Flickr $instance = null;
10
11 protected function __construct() {
12 parent::__construct();
13 $this->provider = 'flickr';
14 $this->api_base = Wizard::base_apis()['flickr'];
15
16 global $photonic_flickr_thumb_size, $photonic_flickr_tile_size, $photonic_flickr_main_size, $photonic_flickr_video_size;
17 $this->allowed_image_sizes['flickr'] = [
18 'thumb_size' => [
19 '' => $this->default_from_settings,
20 's' => esc_html__('Small square, 75x75px', 'photonic'),
21 'q' => esc_html__('Large square, 150x150px', 'photonic'),
22 't' => esc_html__('Thumbnail, 100px on longest side', 'photonic'),
23 'm' => esc_html__('Small, 240px on longest side', 'photonic'),
24 'n' => esc_html__('Small, 320px on longest side', 'photonic'),
25 ],
26 'tile_size' => [
27 '' => $this->default_from_settings,
28 'same' => esc_html__('Same as Main image size', 'photonic'),
29 'n' => esc_html__('Small, 320px on longest side', 'photonic'),
30 'none' => esc_html__('Medium, 500px on the longest side', 'photonic'),
31 'z' => esc_html__('Medium, 640px on longest side', 'photonic'),
32 'c' => esc_html__('Medium, 800px on longest side', 'photonic'),
33 'b' => esc_html__('Large, 1024px on longest side', 'photonic'),
34 'h' => esc_html__('Large, 1600px on longest side', 'photonic'),
35 'k' => esc_html__('Large, 2048px on longest side', 'photonic'),
36 'o' => esc_html__('Original', 'photonic'),
37 ],
38 'main_size' => [
39 '' => $this->default_from_settings,
40 'none' => esc_html__('Medium, 500px on the longest side', 'photonic'),
41 'z' => esc_html__('Medium, 640px on longest side', 'photonic'),
42 'c' => esc_html__('Medium, 800px on longest side', 'photonic'),
43 'b' => esc_html__('Large, 1024px on longest side', 'photonic'),
44 'h' => esc_html__('Large, 1600px on longest side', 'photonic'),
45 'k' => esc_html__('Large, 2048px on longest side', 'photonic'),
46 'o' => esc_html__('Original', 'photonic'),
47 ],
48 'video_size' => [
49 '' => $this->default_from_settings,
50 'Site MP4' => esc_html__('Site MP4', 'photonic'),
51 'Mobile MP4' => esc_html__('Mobile MP4', 'photonic'),
52 'HD MP4' => esc_html__('HD MP4', 'photonic'),
53 'Video Original' => esc_html__('Video Original', 'photonic'),
54 'Video Player' => esc_html__('Video Player', 'photonic'),
55 '1080p' => '1080p',
56 '720p' => '720p',
57 '360p' => '360p',
58 '288p' => '288p',
59 ],
60 ];
61 $this->allowed_image_sizes['flickr']['thumb_size'][''] .= ' - ' . $this->allowed_image_sizes['flickr']['thumb_size'][$photonic_flickr_thumb_size];
62 $this->allowed_image_sizes['flickr']['tile_size'][''] .= ' - ' . $this->allowed_image_sizes['flickr']['tile_size'][$photonic_flickr_tile_size];
63 $this->allowed_image_sizes['flickr']['main_size'][''] .= ' - ' . $this->allowed_image_sizes['flickr']['main_size'][$photonic_flickr_main_size];
64 $this->allowed_image_sizes['flickr']['video_size'][''] .= ' - ' . $this->allowed_image_sizes['flickr']['video_size'][$photonic_flickr_video_size];
65
66 }
67
68 public static function get_instance(): Flickr {
69 if (null === self::$instance) {
70 self::$instance = new Flickr();
71 }
72 return self::$instance;
73 }
74
75 public function get_screen_2(): array {
76 return [
77 'header' => esc_html__('Choose Type of Gallery', 'photonic'),
78 'display' => [
79 'kind' => [
80 'type' => 'field_list',
81 'list_type' => 'sequence',
82 'list' => [
83 'display_type' => [
84 'desc' => esc_html__('What do you want to show?', 'photonic'),
85 'type' => 'select',
86 'options' => [
87 '' => '',
88 'single-photo' => esc_html__('A Single Photo', 'photonic'),
89 'multi-photo' => esc_html__('Multiple Photos', 'photonic'),
90 'album-photo' => esc_html__('Photos from an Album / Photoset', 'photonic'),
91 'gallery-photo' => esc_html__('Photos from a Gallery', 'photonic'),
92 'multi-album' => esc_html__('Multiple Albums', 'photonic'),
93 'multi-gallery' => esc_html__('Multiple Galleries', 'photonic'),
94 'collection' => esc_html__('Albums from a single collection', 'photonic'),
95 'collections' => esc_html__('Multiple collections', 'photonic'),
96 ],
97 'req' => 1,
98 ],
99 'for' => [
100 'desc' => esc_html__('For whom?', 'photonic'),
101 'type' => 'radio',
102 'options' => [
103 'current' => sprintf(esc_html__('Current user (Defined under %s)', 'photonic'), '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Default user</em>'),
104 'other' => esc_html__('Another user', 'photonic'),
105 'group' => esc_html__('Group', 'photonic'),
106 'any' => esc_html__('All users', 'photonic'),
107 ],
108 'option-conditions' => [
109 'group' => ['display_type' => ['multi-photo']],
110 'any' => ['display_type' => ['multi-photo']],
111 ],
112 'req' => 1,
113 ],
114 'login' => [
115 'desc' => sprintf(esc_html__('User name, e.g. %s', 'photonic'), 'https://www.flickr.com/photos/<span style="text-decoration: underline">username</span>/'),
116 'type' => 'text',
117 'std' => '',
118 'conditions' => ['for' => ['other']],
119 'req' => 1,
120 ],
121 'group' => [
122 'desc' => sprintf(esc_html__('Group name, e.g. %s', 'photonic'), 'https://www.flickr.com/groups/<span style="text-decoration: underline">groupname</span>/'),
123 'type' => 'text',
124 'std' => '',
125 'conditions' => ['for' => ['group']],
126 'req' => 1,
127 ],
128 ],
129 ],
130 ],
131 ];
132 }
133
134 public function get_screen_3(): array {
135 return [
136 'header' => esc_html__('Build your gallery', 'photonic'),
137 'single-photo' => [
138 'header' => esc_html__('Pick a photo', 'photonic'),
139 'desc' => esc_html__('From the list below pick the single photo you wish to display.', 'photonic'),
140 'display' => [
141 'container' => [
142 'type' => 'thumbnail-selector',
143 'mode' => 'single',
144 'for' => 'selected_data',
145 ],
146 ],
147 ],
148 'multi-photo' => [
149 'header' => esc_html__('All your photos', 'photonic'),
150 'desc' => esc_html__('You can show all your photos, or apply tags to show some of them.', 'photonic'),
151 'display' => [
152 'tags' => [
153 'desc' => esc_html__('Tags', 'photonic'),
154 'type' => 'text',
155 'hint' => esc_html__('Comma-separated list of tags', 'photonic')
156 ],
157
158 'tag_mode' => [
159 'desc' => esc_html__('Tag mode', 'photonic'),
160 'type' => 'select',
161 'options' => [
162 'any' => esc_html__('Any tag', 'photonic'),
163 'all' => esc_html__('All tags', 'photonic'),
164 ],
165 ],
166
167 'text' => [
168 'desc' => esc_html__('With text', 'photonic'),
169 'type' => 'text',
170 ],
171
172 'privacy_filter' => [
173 'desc' => esc_html__('Privacy filter', 'photonic'),
174 'type' => 'select',
175 'options' => [
176 '' => esc_html__('None', 'photonic'),
177 '1' => esc_html__('Public photos', 'photonic'),
178 '2' => esc_html__('Private photos visible to friends', 'photonic'),
179 '3' => esc_html__('Private photos visible to family', 'photonic'),
180 '4' => esc_html__('Private photos visible to friends & family', 'photonic'),
181 '5' => esc_html__('Completely private photos', 'photonic'),
182 ],
183 'hint' => sprintf(esc_html__('Applicable only if Flickr private photos are turned on (%1$s) and Back-end authentication is off (%2$s)', 'photonic'), '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Allow User Login</em>', '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Access Token</em>'),
184 ],
185
186 'container' => [
187 'type' => 'thumbnail-selector',
188 'mode' => 'none',
189 'for' => 'selected_data',
190 ],
191 ],
192 ],
193 'album-photo' => [
194 'header' => esc_html__('Pick your album', 'photonic'),
195 'desc' => esc_html__('From the list below pick the album whose photos you wish to display. Photos from that album will show up as thumbnails.', 'photonic'),
196 'display' => [
197 'privacy_filter' => [
198 'desc' => esc_html__('Privacy filter', 'photonic'),
199 'type' => 'select',
200 'options' => [
201 '' => esc_html__('None', 'photonic'),
202 '1' => esc_html__('Public photos', 'photonic'),
203 '2' => esc_html__('Private photos visible to friends', 'photonic'),
204 '3' => esc_html__('Private photos visible to family', 'photonic'),
205 '4' => esc_html__('Private photos visible to friends & family', 'photonic'),
206 '5' => esc_html__('Completely private photos', 'photonic'),
207 ],
208 'hint' => sprintf(esc_html__('Applicable only if Flickr private photos are turned on (%1$s) and Back-end authentication is off (%2$s)', 'photonic'), '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Allow User Login</em>', '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Access Token</em>'),
209 ],
210
211 'container' => [
212 'type' => 'thumbnail-selector',
213 'mode' => 'single',
214 'for' => 'selected_data',
215 ],
216 ],
217 ],
218 'gallery-photo' => [
219 'header' => esc_html__('Pick your gallery', 'photonic'),
220 'desc' => esc_html__('From the list below pick the gallery whose photos you wish to display. Photos from that gallery will show up as thumbnails.', 'photonic'),
221 'display' => [
222 'container' => [
223 'type' => 'thumbnail-selector',
224 'mode' => 'single',
225 'for' => 'selected_data',
226 ],
227 ],
228 ],
229 'multi-album' => [
230 'header' => esc_html__('Pick your albums / photosets', 'photonic'),
231 'desc' => esc_html__('From the list below pick the albums / photosets you wish to display. Each album will show up as a single thumbnail.', 'photonic'),
232 'display' => [
233 'selection' => [
234 'desc' => esc_html__('What do you want to show?', 'photonic'),
235 'type' => 'select',
236 'options' => [
237 'all' => esc_html__('Automatic all (will automatically add new albums)', 'photonic'),
238 'selected' => esc_html__('Selected albums / photosets', 'photonic'),
239 'not-selected' => esc_html__('All except selected albums / photosets', 'photonic'),
240 ],
241 'hint' => esc_html__('If you pick "Automatic all" your selections below will be ignored.', 'photonic'),
242 'req' => 1,
243 ],
244 'container' => [
245 'type' => 'thumbnail-selector',
246 'mode' => 'multi',
247 'for' => 'selected_data',
248 ],
249 ],
250 ],
251 'multi-gallery' => [
252 'header' => esc_html__('Pick your galleries', 'photonic'),
253 'desc' => esc_html__('From the list below pick the galleries you wish to display. Each album will show up as a single thumbnail.', 'photonic'),
254 'display' => [
255 'selection' => [
256 'desc' => esc_html__('What do you want to show?', 'photonic'),
257 'type' => 'select',
258 'options' => [
259 'all' => esc_html__('Automatic all (will automatically add new galleries)', 'photonic'),
260 'selected' => esc_html__('Selected galleries', 'photonic'),
261 'not-selected' => esc_html__('All except selected galleries', 'photonic'),
262 ],
263 'req' => 1,
264 'hint' => esc_html__('If you pick "Automatic all" your selections below will be ignored.', 'photonic'),
265 ],
266 'container' => [
267 'type' => 'thumbnail-selector',
268 'mode' => 'multi',
269 'for' => 'selected_data',
270 ],
271 ],
272 ],
273 'collection' => [
274 'header' => esc_html__('Pick your collection', 'photonic'),
275 'desc' => esc_html__('From the list below pick the collection you wish to display. The albums within the collections will show up as single thumbnails.', 'photonic'),
276 'display' => [
277 'container' => [
278 'type' => 'thumbnail-selector',
279 'mode' => 'single',
280 'for' => 'selected_data',
281 ],
282 ],
283 ],
284 'collections' => [
285 'header' => esc_html__('Pick your collections', 'photonic'),
286 'desc' => esc_html__('From the list below pick the collections you wish to display. The albums within the collections will show up as single thumbnails.', 'photonic'),
287 'display' => [
288 'selection' => [
289 'desc' => esc_html__('What do you want to show?', 'photonic'),
290 'type' => 'select',
291 'options' => [
292 'all' => esc_html__('Automatic all (will automatically add new collections)', 'photonic'),
293 'selected' => esc_html__('Selected collections', 'photonic'),
294 'not-selected' => esc_html__('All except selected collections', 'photonic'),
295 ],
296 'req' => 1,
297 'hint' => esc_html__('If you pick "Automatic all" your selections below will be ignored.', 'photonic'),
298 ],
299 'container' => [
300 'type' => 'thumbnail-selector',
301 'mode' => 'multi',
302 'for' => 'selected_data',
303 ],
304 ],
305 ],
306 ];
307 }
308
309 public function get_screen_4(): array {
310 return [];
311 }
312
313 public function get_screen_5(): array {
314 global $photonic_flickr_media, $photonic_flickr_title_caption;
315 // $output['slideshow'] = $this->get_slideshow_options();
316 return [
317 'flickr' => [
318 'L1' => [
319 'sort' => [
320 'desc' => esc_html__('Sort by', 'photonic'),
321 'type' => 'select',
322 'options' => [
323 '' => '',
324 'date-posted-desc' => esc_html__('Date posted, descending', 'photonic'),
325 'date-posted-asc' => esc_html__('Date posted, ascending', 'photonic'),
326 'date-taken-asc' => esc_html__('Date taken, ascending', 'photonic'),
327 'date-taken-desc' => esc_html__('Date taken, descending', 'photonic'),
328 'interestingness-desc' => esc_html__('Interestingness, descending', 'photonic'),
329 'interestingness-asc' => esc_html__('Interestingness, ascending', 'photonic'),
330 'relevance' => esc_html__('Relevance', 'photonic'),
331 ],
332 'conditions' => ['display_type' => ['multi-photo']],
333 ],
334 'media' => [
335 'desc' => esc_html__('Media to Show', 'photonic'),
336 'type' => 'select',
337 'options' => Utilities::media_options(true, $photonic_flickr_media),
338 'std' => '',
339 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Media to show</em>'),
340 ],
341 'caption' => [
342 'desc' => esc_html__('Photo titles and captions', 'photonic'),
343 'type' => 'select',
344 'options' => Utilities::title_caption_options(true, $photonic_flickr_title_caption),
345 'std' => '',
346 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Photo titles and captions</em>'),
347 ],
348 'headers' => $this->get_header_options(['display_type' => ['album-photo', 'gallery-photo']]),
349 ],
350 'L2' => [],
351 'L3' => [
352 'collections_display' => [
353 'desc' => esc_html__('Expand Collections', 'photonic'),
354 'type' => 'select',
355 'options' => [
356 '' => '',
357 'lazy' => esc_html__('Lazy loading', 'photonic'),
358 'expanded' => esc_html__('Expanded upfront', 'photonic'),
359 ],
360 'hint' => sprintf(
361 esc_html__('The Collections API is slow, so, if you are displaying collections, pick %1$slazy loading%2$s if your collections have many albums / photosets.', 'photonic'),
362 '<a href="https://aquoid.com/plugins/photonic/flickr/flickr-collections/" target="_blank">',
363 '</a>'
364 ),
365 ],
366 'headers' => $this->get_header_options(),
367 ],
368 'main_size' => [
369 'desc' => esc_html__('Main image size', 'photonic'),
370 'type' => 'select',
371 'options' => $this->allowed_image_sizes['flickr']['main_size'],
372 'std' => '',
373 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Main image size</em>'),
374 ],
375 'video_size' => [
376 'desc' => esc_html__('Main video size', 'photonic'),
377 'type' => 'select',
378 'options' => $this->allowed_image_sizes['flickr']['video_size'],
379 'std' => '',
380 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Video size</em>'),
381 ],
382 ]
383 ];
384 }
385
386 public function get_square_size_options(): array {
387 return [
388 'thumb_size' => [
389 'desc' => esc_html__('Thumbnail size', 'photonic'),
390 'type' => 'select',
391 'options' => $this->allowed_image_sizes['flickr']['thumb_size'],
392 'std' => '',
393 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Thumbnail size</em>'),
394 ],
395 ];
396 }
397
398 public function get_random_size_options(): array {
399 return [
400 'tile_size' => [
401 'desc' => esc_html__('Tile size', 'photonic'),
402 'type' => 'select',
403 'options' => $this->allowed_image_sizes['flickr']['tile_size'],
404 'std' => '',
405 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Tile image size</em>'),
406 ],
407 ];
408 }
409
410 public function make_request($display_type, $for, $flattened_fields): array {
411 if ('multi-photo' !== $display_type && in_array($for, ['group', 'any'], true)) {
412 $err = esc_html__('Incompatible selections:', 'photonic') . "<br/>\n";
413 $err .= $flattened_fields['display_type']['desc'] . ": " . $flattened_fields['display_type']['options'][$display_type] . "<br/>\n";
414 $err .= $flattened_fields['for']['desc'] . ": " . $flattened_fields['for']['options'][$for] . "<br/>\n";
415 return ['error' => $err];
416 }
417
418 $group = '';
419 $login = '';
420
421 if (check_ajax_referer('photonic-wizard-next-' . get_current_user_id())) {
422 $group = sanitize_text_field(wp_unslash($_POST['group'] ?? ''));
423 $login = sanitize_text_field(wp_unslash($_POST['login'] ?? ''));
424 }
425
426 global $photonic_flickr_default_user, $photonic_flickr_api_key;
427 if ('current' === $for && empty($photonic_flickr_default_user)) {
428 return [
429 'error' => sprintf(
430 esc_html__('Default user not defined under %1$s. %2$sSelect "Another user" and put in your user id.', 'photonic'),
431 '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings &rarr; Default User</em>',
432 '<br/>'
433 )
434 ];
435 }
436
437 if (('group' === $for && empty($group)) || ('other' === $for && empty($login))) {
438 return ['error' => $this->error_mandatory];
439 }
440
441 $parameters = [];
442 $user = 'current' === $for ? $photonic_flickr_default_user : ('other' === $for ? $login : '');
443 if (('other' === $for || 'current' === $for) && !empty($user)) {
444 $url = 'https://' . $this->api_base . '/services/rest/?format=json&nojsoncallback=1&api_key=' . $photonic_flickr_api_key . '&method=flickr.urls.lookupUser&url=' . rawurlencode('https://www.flickr.com/photos/') . $user;
445 $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
446 $response = $this->process_response($response, 'flickr', 'user');
447 if (!empty($response['error'])) {
448 // Maybe the user provided the full URL instead of just the user name?
449 $url = 'https://' . $this->api_base . '/services/rest/?format=json&nojsoncallback=1&api_key=' . $photonic_flickr_api_key . '&method=flickr.urls.lookupUser&url=' . rawurlencode($user);
450 $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
451 $response = $this->process_response($response, 'flickr', 'user');
452 if (!empty($response['error'])) {
453 return $response;
454 }
455 $parameters = array_merge($response['success'], $parameters);
456 }
457 else {
458 $parameters = array_merge($response['success'], $parameters);
459 }
460 }
461
462 if ('group' === $for && !empty($group)) {
463 $url = 'https://' . $this->api_base . '/services/rest/?format=json&nojsoncallback=1&api_key=' . $photonic_flickr_api_key . '&method=flickr.urls.lookupGroup&url=' . rawurlencode('https://www.flickr.com/groups/') . $group;
464 $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
465 $response = $this->process_response($response, 'flickr', 'group');
466 if (!empty($response['error'])) {
467 // Maybe the user provided the full URL instead of just the group name?
468 $url = 'https://' . $this->api_base . '/services/rest/?format=json&nojsoncallback=1&api_key=' . $photonic_flickr_api_key . '&method=flickr.urls.lookupGroup&url=' . rawurlencode($user);
469 $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
470 $response = $this->process_response($response, 'flickr', 'group');
471 if (!empty($response['error'])) {
472 return $response;
473 }
474 $parameters = array_merge($response['success'], $parameters);
475 }
476 else {
477 $parameters = array_merge($response['success'], $parameters);
478 }
479 }
480
481 // All OK so far. Let's try to get the data for the next screen
482 $parameters['api_key'] = $photonic_flickr_api_key;
483 $parameters['format'] = 'json';
484 $parameters['nojsoncallback'] = 1;
485
486 if ('single-photo' === $display_type) {
487 $parameters['view'] = 'photo';
488 $parameters['method'] = 'flickr.photos.search';
489 $parameters['extras'] = 'url_q';
490 $parameters['per_page'] = 500;
491 }
492 elseif ('multi-photo' === $display_type) {
493 $parameters['view'] = 'photos';
494 $parameters['method'] = 'flickr.photos.search';
495 $parameters['extras'] = 'url_q';
496 }
497 elseif ('multi-album' === $display_type || 'album-photo' === $display_type) {
498 $parameters['view'] = 'photosets';
499 $parameters['method'] = 'flickr.photosets.getList';
500 $parameters['primary_photo_extras'] = 'url_q';
501 $parameters['per_page'] = 500;
502 }
503 elseif ('multi-gallery' === $display_type || 'gallery-photo' === $display_type) {
504 $parameters['view'] = 'galleries';
505 $parameters['method'] = 'flickr.galleries.getList';
506 $parameters['primary_photo_extras'] = 'url_q';
507 $parameters['per_page'] = 500;
508 }
509 elseif ('collection' === $display_type || 'collections' === $display_type) {
510 $parameters['view'] = 'collections';
511 $parameters['method'] = 'flickr.collections.getTree';
512 }
513
514 global $photonic_flickr_access_token;
515 require_once PHOTONIC_PATH . '/Platforms/Flickr.php';
516 $module = \Photonic_Plugin\Platforms\Flickr::get_instance();
517 if (!empty($photonic_flickr_access_token)) {
518 $parameters = $module->sign_call('https://' . $this->api_base . '/services/rest/', 'GET', $parameters);
519 }
520
521 $url = add_query_arg($parameters, 'https://' . $this->api_base . '/services/rest/');
522 $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
523
524 $hidden = [];
525 if (isset($parameters['user_id'])) {
526 $hidden['user_id'] = $parameters['user_id'];
527 }
528 if (isset($parameters['group_id'])) {
529 $hidden['group_id'] = $parameters['group_id'];
530 }
531
532 return [$response, $hidden, $url];
533 }
534
535 /**
536 * Processes a response from Flickr to build it out into a gallery of thumbnails. Flickr has L1, L2 and L3 displays in the flow.
537 *
538 * @param $response
539 * @param $display_type
540 * @param null $url
541 * @param array $pagination
542 * @return array
543 */
544 public function process_response($response, $display_type, $url = null, &$pagination = []): array {
545 $objects = [];
546 $body = json_decode(wp_remote_retrieve_body($response));
547
548 if (isset($body->stat) && 'fail' === $body->stat) {
549 Photonic::log($response);
550 return ['error' => $body->message];
551 }
552
553 if (isset($body->photosets) && isset($body->photosets->photoset)) {
554 $page = (int) ($body->photosets->page); // 22-ug-2022: Needed now, because photosets.getList caps per_page at 500
555 $pages = (int) ($body->photosets->pages); // 22-ug-2022: Needed now, because photosets.getList caps per_page at 500
556 $photosets = $body->photosets->photoset;
557 foreach ($photosets as $flickr_object) {
558 $object = [];
559 $object['id'] = $flickr_object->id;
560 $object['title'] = esc_attr($flickr_object->title->_content);
561 $object['counters'] = [];
562 if (!empty($flickr_object->photos)) {
563 $object['counters'][] = esc_html(sprintf(_n('%s photo', '%s photos', $flickr_object->photos, 'photonic'), $flickr_object->photos));
564 }
565 if (!empty($flickr_object->videos)) {
566 $object['counters'][] = esc_html(sprintf(_n('%s video', '%s videos', $flickr_object->videos, 'photonic'), $flickr_object->videos));
567 }
568 $object['thumbnail'] = esc_url($flickr_object->primary_photo_extras->url_q ?? "https://farm" . $flickr_object->farm . ".staticflickr.com/" . $flickr_object->server . "/" . $flickr_object->primary . "_" . $flickr_object->secret . "_q.jpg");
569 $objects[] = $object;
570 }
571 }
572 elseif (isset($body->galleries) && isset($body->galleries->gallery)) {
573 $page = (int) ($body->galleries->page);
574 $pages = (int) ($body->galleries->pages);
575 $galleries = $body->galleries->gallery;
576 foreach ($galleries as $flickr_object) {
577 $object = [];
578 $object['id'] = substr($flickr_object->id, strpos($flickr_object->id, '-') + 1); // $flickr_object->id;
579 $object['title'] = esc_attr($flickr_object->title->_content);
580 $object['counters'] = [];
581 if (!empty($flickr_object->count_photos)) {
582 $object['counters'][] = esc_html(sprintf(_n('%s photo', '%s photos', $flickr_object->count_photos, 'photonic'), $flickr_object->count_photos));
583 }
584 if (!empty($flickr_object->count_videos)) {
585 $object['counters'][] = esc_html(sprintf(_n('%s video', '%s videos', $flickr_object->count_videos, 'photonic'), $flickr_object->count_videos));
586 }
587 $object['thumbnail'] = esc_url($flickr_object->primary_photo_extras->url_q ?? "https://farm" . $flickr_object->primary_photo_farm . ".staticflickr.com/" . $flickr_object->primary_photo_server . "/" . $flickr_object->primary_photo_id . "_" . $flickr_object->primary_photo_secret . "_q.jpg");
588 $objects[] = $object;
589 }
590 }
591 elseif (isset($body->photos) && isset($body->photos->photo)) {
592 if ('single-photo' === $display_type) {
593 $page = (int) ($body->photos->page);
594 $pages = (int) ($body->photos->pages);
595 if ($page < $pages && !empty($url)) {
596 $url = remove_query_arg('page', $url);
597 $pagination['url'] = add_query_arg(['page' => $page + 1], $url);
598 }
599 }
600 $photos = $body->photos->photo;
601 foreach ($photos as $flickr_object) {
602 $object = [];
603 $object['id'] = $flickr_object->id;
604 $object['title'] = esc_attr($flickr_object->title);
605 $object['thumbnail'] = esc_url($flickr_object->url_q ?? 'https://farm' . $flickr_object->farm . '.staticflickr.com/' . $flickr_object->server . '/' . $flickr_object->id . '_' . $flickr_object->secret . '_q.jpg');
606 $objects[] = $object;
607 }
608 }
609 elseif (isset($body->collections) && isset($body->collections->collection)) {
610 $collections = $body->collections->collection;
611 foreach ($collections as $flickr_object) {
612 $object = [];
613 $object['id'] = substr($flickr_object->id, strpos($flickr_object->id, '-') + 1);
614 $object['title'] = esc_attr($flickr_object->title);
615 $object['counters'] = [];
616 if (!empty($flickr_object->set)) {
617 $object['counters'][] = esc_html(sprintf(_n('%s album', '%s albums', count($flickr_object->set), 'photonic'), count($flickr_object->set)));
618 }
619 $object['thumbnail'] = $flickr_object->iconlarge;
620 $objects[] = $object;
621 }
622 }
623 elseif (isset($body->user)) {
624 return ['success' => ['user_id' => $body->user->id]];
625 }
626 elseif (isset($body->group)) {
627 return ['success' => ['group_id' => $body->group->id]];
628 }
629
630 if (!empty($page) && !empty($pages) && $page < $pages && !empty($url)) {
631 $url = remove_query_arg('page', $url);
632 $pagination['url'] = add_query_arg(['page' => $page + 1], $url);
633 }
634 return $objects;
635 }
636
637 /**
638 * @param $display_type
639 * @return array
640 */
641 public function construct_shortcode_from_screen_selections($display_type): array {
642 $short_code = [];
643
644 if (check_ajax_referer('photonic-wizard-next-' . get_current_user_id())) {
645 if ('single-photo' === $display_type) {
646 $short_code['view'] = 'photo';
647 $short_code['photo_id'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? ''));
648 }
649 elseif ('multi-photo' === $display_type) {
650 $short_code['view'] = 'photos';
651 }
652 elseif ('album-photo' === $display_type) {
653 $short_code['view'] = 'photosets';
654 $short_code['photoset_id'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? ''));
655 }
656 elseif ('gallery-photo' === $display_type) {
657 $short_code['view'] = 'galleries';
658 $short_code['gallery_id'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? ''));
659 }
660 elseif ('multi-album' === $display_type) {
661 $short_code['view'] = 'photosets';
662 unset($short_code['photoset_id']);
663 unset($short_code['gallery_id']);
664 unset($short_code['collection_id']);
665 }
666 elseif ('multi-gallery' === $display_type) {
667 $short_code['view'] = 'galleries';
668 }
669 elseif ('collection' === $display_type) {
670 $short_code['view'] = 'collections';
671 $short_code['collection_id'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? ''));
672 }
673 elseif ('collections' === $display_type) {
674 $short_code['view'] = 'collections';
675 }
676 }
677
678 return $short_code;
679 }
680
681 /**
682 * @param $input
683 * @return array
684 */
685 public function deconstruct_shortcode_to_screen_selections($input): array {
686 $deconstructed = [];
687
688 // Potential for an existing (old) shortcode to not have a user_id. E.g. Just defining photoset_id.
689 // Rather than defaulting to the default user, we will make the user put it in this time.
690 if (!empty($input->user_id)) {
691 $deconstructed['for'] = 'other';
692 $deconstructed['login'] = $input->user_id;
693 }
694 elseif (!empty($input->group_id)) {
695 $deconstructed['for'] = 'group';
696 $deconstructed['group'] = $input->group_id;
697 }
698
699 if (!empty($input->view)) {
700 if ('collections' === $input->view && empty($input->collection_id)) {
701 $deconstructed['display_type'] = 'collections';
702 }
703 elseif ('galleries' === $input->view && empty($input->gallery_id)) {
704 $deconstructed['display_type'] = 'multi-gallery';
705 }
706 elseif ('photosets' === $input->view && empty($input->photoset_id)) {
707 $deconstructed['display_type'] = 'multi-album';
708 }
709 elseif ('photos' === $input->view) {
710 $deconstructed['display_type'] = 'multi-photo';
711 }
712 }
713
714 if (!empty($input->collection_id)) {
715 $deconstructed['display_type'] = 'collection';
716 $deconstructed['selected_data'] = $input->collection_id;
717 }
718 elseif (!empty($input->gallery_id)) {
719 $deconstructed['display_type'] = 'gallery-photo';
720 $deconstructed['selected_data'] = $input->gallery_id;
721 }
722 elseif (!empty($input->photoset_id)) {
723 $deconstructed['display_type'] = 'album-photo';
724 $deconstructed['selected_data'] = $input->photoset_id;
725 }
726 elseif (!empty($input->photo_id)) {
727 $deconstructed['display_type'] = 'single-photo';
728 $deconstructed['selected_data'] = $input->photo_id;
729 }
730
731 return $deconstructed;
732 }
733 }
734