| 1 |
<?php |
| 2 |
namespace Photonic_Plugin\Modules; |
| 3 |
|
| 4 |
use Photonic_Plugin\Core\Photonic; |
| 5 |
use Requests; |
| 6 |
use Requests_Hooks; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
require_once('OAuth1.php'); |
| 10 |
require_once('Level_One_Module.php'); |
| 11 |
|
| 12 |
/** |
| 13 |
* Processor for Flickr Galleries |
| 14 |
* |
| 15 |
*/ |
| 16 |
|
| 17 |
class Flickr extends OAuth1 implements Level_One_Module { |
| 18 |
private static $instance = null; |
| 19 |
|
| 20 |
protected function __construct() { |
| 21 |
parent::__construct(); |
| 22 |
global $photonic_flickr_api_key, $photonic_flickr_api_secret, $photonic_flickr_disable_title_link, $photonic_flickr_access_token, $photonic_flickr_token_secret; |
| 23 |
$this->api_key = trim($photonic_flickr_api_key); |
| 24 |
$this->api_secret = trim($photonic_flickr_api_secret); |
| 25 |
$this->token = trim($photonic_flickr_access_token); |
| 26 |
$this->token_secret = trim($photonic_flickr_token_secret); |
| 27 |
|
| 28 |
$this->provider = 'flickr'; |
| 29 |
$this->link_lightbox_title = empty($photonic_flickr_disable_title_link); |
| 30 |
$this->base_url = 'https://api.flickr.com/services/rest/'; |
| 31 |
|
| 32 |
$this->doc_links = [ |
| 33 |
'general' => 'https://aquoid.com/plugins/photonic/flickr/', |
| 34 |
'photo' => 'https://aquoid.com/plugins/photonic/flickr/flickr-photo', |
| 35 |
'photos' => 'https://aquoid.com/plugins/photonic/flickr/flickr-photos/', |
| 36 |
'photosets' => 'https://aquoid.com/plugins/photonic/flickr/flickr-photosets/', |
| 37 |
'galleries' => 'https://aquoid.com/plugins/photonic/flickr/flickr-galleries/', |
| 38 |
'collections' => 'https://aquoid.com/plugins/photonic/flickr/flickr-collections/', |
| 39 |
'auth' => 'https://aquoid.com/plugins/photonic/flickr/flickr-authentication', |
| 40 |
]; |
| 41 |
|
| 42 |
$this->set_oauth_done(); |
| 43 |
} |
| 44 |
|
| 45 |
public static function get_instance() { |
| 46 |
if (self::$instance == null) { |
| 47 |
self::$instance = new Flickr(); |
| 48 |
} |
| 49 |
return self::$instance; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* A very flexible function to display a user's photos from Flickr. This makes use of the Flickr API, hence it requires the user's API key. |
| 54 |
* The API key is defined in the options. The function makes use of three different APIs: |
| 55 |
* 1. <a href='https://www.flickr.com/services/api/flickr.photos.search.html'>flickr.photos.search</a> - for retrieving photos based on search critiera |
| 56 |
* 2. <a href='https://www.flickr.com/services/api/flickr.photosets.getPhotos.html'>flickr.photosets.getPhotos</a> - for retrieving photo sets |
| 57 |
* 3. <a href='https://www.flickr.com/services/api/flickr.galleries.getPhotos.html'>flickr.galleries.getPhotos</a> - for retrieving galleries |
| 58 |
* |
| 59 |
* The following short-code parameters are supported: |
| 60 |
* All |
| 61 |
* - per_page: number of photos to display |
| 62 |
* - view: photos | collections | galleries | photosets, displays hierarchically if user_id is passed |
| 63 |
* Photosets |
| 64 |
* - photoset_id |
| 65 |
* Galleries |
| 66 |
* - gallery_id |
| 67 |
* Photos |
| 68 |
* - user_id: can be obtained from the Helpers page |
| 69 |
* - tags: comma-separated list of tags |
| 70 |
* - tag_mode: any | all, tells whether any tag should be used or all |
| 71 |
* - text: string for text search |
| 72 |
* - sort: date-posted-desc | date-posted-asc | date-taken-asc | date-taken-desc | interestingness-desc | interestingness-asc | relevance |
| 73 |
* - group_id: group id for which photos will be displayed |
| 74 |
* |
| 75 |
* @param array $attr |
| 76 |
* @param array $gallery_meta |
| 77 |
* @return string |
| 78 |
* @since 1.02 |
| 79 |
*/ |
| 80 |
public function get_gallery_images($attr = [], &$gallery_meta = []) { |
| 81 |
global $photonic_flickr_title_caption, $photonic_flickr_media, $photonic_flickr_default_user, |
| 82 |
$photonic_flickr_thumb_size, $photonic_flickr_main_size, $photonic_flickr_tile_size, $photonic_flickr_video_size; |
| 83 |
|
| 84 |
$this->gallery_index++; |
| 85 |
$this->push_to_stack('Get gallery images'); |
| 86 |
$attr = array_merge( |
| 87 |
$this->common_parameters, |
| 88 |
[ |
| 89 |
// Common overrides ... |
| 90 |
'caption' => $photonic_flickr_title_caption, |
| 91 |
'thumb_size' => $photonic_flickr_thumb_size, |
| 92 |
'main_size' => $photonic_flickr_main_size, |
| 93 |
'tile_size' => $photonic_flickr_tile_size, |
| 94 |
'video_size' => $photonic_flickr_video_size, |
| 95 |
|
| 96 |
// Flickr-Specific ... |
| 97 |
// 'view' => 'photos' // photos | collections | galleries | photosets: if only a user id is passed, what should be displayed? |
| 98 |
'privacy_filter' => '', |
| 99 |
'count' => 500, |
| 100 |
'page' => 1, |
| 101 |
'paginate' => false, |
| 102 |
'collections_display' => 'expanded', |
| 103 |
'user_id' => $photonic_flickr_default_user, |
| 104 |
'collection_id' => '', |
| 105 |
'photoset_id' => '', |
| 106 |
'gallery_id' => '', |
| 107 |
'photo_id' => '', |
| 108 |
'media' => $photonic_flickr_media, |
| 109 |
], |
| 110 |
$attr); |
| 111 |
$attr = array_map('trim', $attr); |
| 112 |
|
| 113 |
extract($attr); |
| 114 |
|
| 115 |
if (empty($this->api_key)) { |
| 116 |
$this->pop_from_stack(); |
| 117 |
return $this->error(esc_html__('Flickr API Key not defined.', 'photonic').Photonic::doc_link($this->doc_links['general'])); |
| 118 |
} |
| 119 |
|
| 120 |
$query_urls = []; |
| 121 |
$flickr_params = []; |
| 122 |
|
| 123 |
$flickr_params['extras'] = 'description,owner_name,url_c,c_dims,url_h,h_dims,url_k,k_dims,url_o,o_dims,url_b,b_dims,media'; |
| 124 |
$flickr_params['primary_photo_extras'] = 'url_c,c_dims,url_h,h_dims,url_k,k_dims,url_o,o_dims,url_b,b_dims,media'; |
| 125 |
|
| 126 |
$ret = ""; |
| 127 |
$attr['iterate_level_3'] = $attr['collections_display'] === 'expanded'; |
| 128 |
$attr['per_page'] = empty($attr['per_page']) ? $attr['count'] : $attr['per_page']; |
| 129 |
$attr['photo_count'] = empty($attr['photo_count']) ? $attr['per_page'] : $attr['photo_count']; |
| 130 |
|
| 131 |
$attr['overlay_size'] = empty($attr['overlay_size']) ? $attr['thumb_size'] : $attr['overlay_size']; |
| 132 |
$attr['overlay_video_size'] = empty($attr['overlay_video_size']) ? $attr['video_size'] : $attr['overlay_video_size']; |
| 133 |
|
| 134 |
if (empty($attr['group_id'])) { |
| 135 |
$user = empty($attr['user_id']) ? $photonic_flickr_default_user : $attr['user_id']; |
| 136 |
} |
| 137 |
|
| 138 |
if (isset($view) && $view == 'photos' && !empty($attr['group_id']) && empty($attr['photoset_id'])) { |
| 139 |
$query_urls[] = $this->base_url.'?method=flickr.photos.search'; |
| 140 |
} |
| 141 |
else if (isset($view) && $view == 'photo' && !empty($attr['photo_id'])) { |
| 142 |
$query_urls[] = $this->base_url.'?method=flickr.photos.getInfo'; |
| 143 |
} |
| 144 |
else if (isset($view) && (!empty($user))) { |
| 145 |
switch ($view) { |
| 146 |
case 'collections': |
| 147 |
if (empty($attr['collection_id'])) { |
| 148 |
$collections = $this->get_collection_list($user, '', $attr['filter']); |
| 149 |
foreach ($collections as $collection) { |
| 150 |
$query_urls[] = $this->base_url.'?method=flickr.collections.getTree&collection_id='.$collection['id']; |
| 151 |
} |
| 152 |
} |
| 153 |
break; |
| 154 |
|
| 155 |
case 'galleries': |
| 156 |
if (empty($attr['gallery_id'])) { |
| 157 |
$query_urls[] = $this->base_url.'?method=flickr.galleries.getList'; |
| 158 |
} |
| 159 |
break; |
| 160 |
|
| 161 |
case 'photosets': |
| 162 |
if (empty($attr['photoset_id'])) { |
| 163 |
$query_urls[] = $this->base_url.'?method=flickr.photosets.getList'; |
| 164 |
} |
| 165 |
break; |
| 166 |
|
| 167 |
case 'photo': |
| 168 |
if (!empty($attr['photo_id'])) { |
| 169 |
$query_urls[] = $this->base_url.'?method=flickr.photos.getInfo'; |
| 170 |
} |
| 171 |
break; |
| 172 |
|
| 173 |
case 'photos': |
| 174 |
default: |
| 175 |
if (empty($attr['photoset_id']) && empty($attr['gallery_id']) && empty($attr['collection_id']) && empty($attr['photo_id'])) |
| 176 |
$query_urls[] = $this->base_url.'?method=flickr.photos.search'; |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// Collection > galleries > photosets |
| 182 |
if (!empty($attr['collection_id'])) { |
| 183 |
$collections = $this->get_collection_list($user, $attr['collection_id']); |
| 184 |
$attr['iterate_level_3'] = true; |
| 185 |
foreach ($collections as $collection) { |
| 186 |
$query_urls[] = $this->base_url.'?method=flickr.collections.getTree&collection_id='.$collection['id']; |
| 187 |
} |
| 188 |
} |
| 189 |
else if (!empty($attr['gallery_id'])) { |
| 190 |
if (empty($gallery_id_computed)) { |
| 191 |
if (empty($user)) { |
| 192 |
$this->pop_from_stack(); |
| 193 |
return esc_html__('User id or default user is required for displaying a single gallery', 'photonic'); |
| 194 |
} |
| 195 |
|
| 196 |
$this->push_to_stack("Gallery list (user '$user')"); |
| 197 |
$feed = $this->make_call($this->base_url.'?method=flickr.galleries.getList', $flickr_params); |
| 198 |
|
| 199 |
if (!is_wp_error($feed)) { |
| 200 |
if ($feed['response']['code'] == 200) { |
| 201 |
$feed = $feed['body']; |
| 202 |
$feed = json_decode($feed); |
| 203 |
if (isset($feed->galleries)) { |
| 204 |
$galleries = $feed->galleries; |
| 205 |
$galleries = $galleries->gallery; |
| 206 |
if (is_array($galleries) && count($galleries) > 0) { |
| 207 |
$gallery = $galleries[0]; |
| 208 |
$global_dbid = $gallery->id; |
| 209 |
$global_dbid = substr($global_dbid, 0, stripos($global_dbid, '-')); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if (isset($global_dbid)) { |
| 216 |
$attr['gallery_id'] = $global_dbid.'-'.$attr['gallery_id']; |
| 217 |
} |
| 218 |
$this->pop_from_stack(); |
| 219 |
} |
| 220 |
$query_urls[] = $this->base_url.'?method=flickr.galleries.getInfo'; |
| 221 |
$query_urls[] = $this->base_url.'?method=flickr.galleries.getPhotos'; |
| 222 |
} |
| 223 |
else if (!empty($attr['photoset_id'])) { |
| 224 |
$query_urls[] = $this->base_url.'?method=flickr.photosets.getInfo'; |
| 225 |
$query_urls[] = $this->base_url.'?method=flickr.photosets.getPhotos'; |
| 226 |
} |
| 227 |
|
| 228 |
if (!empty($user) && empty($photoset_id) && empty($photo_id)) { |
| 229 |
$flickr_params['user_id'] = $user; |
| 230 |
} |
| 231 |
|
| 232 |
if (!empty($attr['collection_id'])) { |
| 233 |
$flickr_params['collection_id'] = $attr['collection_id']; |
| 234 |
} |
| 235 |
else if (!empty($attr['gallery_id'])) { |
| 236 |
$flickr_params['gallery_id'] = $attr['gallery_id']; |
| 237 |
} |
| 238 |
else if (!empty($attr['photoset_id'])) { |
| 239 |
$flickr_params['photoset_id'] = $attr['photoset_id']; |
| 240 |
} |
| 241 |
else if (!empty($attr['photo_id'])) { |
| 242 |
$flickr_params['photo_id'] = $attr['photo_id']; |
| 243 |
} |
| 244 |
|
| 245 |
if (!empty($attr['tags'])) { |
| 246 |
$flickr_params['tags'] = $attr['tags']; |
| 247 |
} |
| 248 |
|
| 249 |
if (!empty($attr['tag_mode'])) { |
| 250 |
$flickr_params['tag_mode'] = $attr['tag_mode']; |
| 251 |
} |
| 252 |
|
| 253 |
if (!empty($attr['text'])) { |
| 254 |
$flickr_params['text'] = $attr['text']; |
| 255 |
} |
| 256 |
|
| 257 |
if (!empty($attr['sort'])) { |
| 258 |
$flickr_params['sort'] = $attr['sort']; |
| 259 |
} |
| 260 |
|
| 261 |
if (!empty($attr['group_id'])) { |
| 262 |
$flickr_params['group_id'] = $attr['group_id']; |
| 263 |
} |
| 264 |
|
| 265 |
global $photonic_archive_thumbs; |
| 266 |
if (is_archive() || is_home()) { |
| 267 |
if (isset($photonic_archive_thumbs) && !empty($photonic_archive_thumbs)) { |
| 268 |
if (!empty($attr['per_page']) && $photonic_archive_thumbs < $attr['per_page']) { |
| 269 |
$flickr_params['per_page'] = $photonic_archive_thumbs; |
| 270 |
$this->show_more_link = true; |
| 271 |
} |
| 272 |
else if (!empty($attr['per_page'])) { |
| 273 |
$flickr_params['per_page'] = $attr['per_page']; |
| 274 |
} |
| 275 |
} |
| 276 |
else if (!empty($attr['per_page'])) { |
| 277 |
$flickr_params['per_page'] = $attr['per_page']; |
| 278 |
} |
| 279 |
} |
| 280 |
else if (!empty($attr['per_page'])) { |
| 281 |
$flickr_params['per_page'] = $attr['per_page']; |
| 282 |
} |
| 283 |
|
| 284 |
if (!empty($attr['page'])) { |
| 285 |
$flickr_params['page'] = $attr['page']; |
| 286 |
} |
| 287 |
|
| 288 |
if (!empty($attr['privacy_filter'])) { |
| 289 |
$flickr_params['privacy_filter'] = $attr['privacy_filter']; |
| 290 |
} |
| 291 |
|
| 292 |
if (!empty($attr['media'])) { |
| 293 |
$flickr_params['media'] = $attr['media']; |
| 294 |
} |
| 295 |
|
| 296 |
// Allow users to define additional query parameters |
| 297 |
$query_urls = apply_filters('photonic_flickr_query_urls', $query_urls, $attr); |
| 298 |
$header_display = $this->get_header_display($attr); |
| 299 |
$attr['header_display'] = $header_display; |
| 300 |
|
| 301 |
$call_return = ''; |
| 302 |
foreach ($query_urls as $query_url) { |
| 303 |
$method = 'flickr.photos.getInfo'; |
| 304 |
$iterator = []; |
| 305 |
if (is_array($query_url)) { |
| 306 |
$iterator = $query_url; |
| 307 |
} |
| 308 |
else { |
| 309 |
$iterator[] = $query_url; |
| 310 |
} |
| 311 |
|
| 312 |
foreach ($iterator as $nested_query_url) { |
| 313 |
$this->push_to_stack("Nested call $method"); |
| 314 |
$method = wp_parse_args(substr($nested_query_url, stripos($nested_query_url, '?') + 1)); |
| 315 |
$method = $method['method']; |
| 316 |
$response = $this->make_call($nested_query_url, $flickr_params); |
| 317 |
$flickr_params['method'] = $method; |
| 318 |
|
| 319 |
$processed_response = $this->process_query($response, $flickr_params, $attr, $gallery_meta); |
| 320 |
$call_return .= $processed_response; |
| 321 |
$this->pop_from_stack(); |
| 322 |
} |
| 323 |
|
| 324 |
if ($this->show_more_link && $method != 'flickr.photosets.getInfo' && $method != 'flickr.photos.getInfo' && $method != 'flickr.galleries.getInfo') { |
| 325 |
$call_return .= $this->more_link_button(get_permalink().'#photonic-flickr-stream-'.$this->gallery_index); |
| 326 |
} |
| 327 |
} |
| 328 |
$this->pop_from_stack(); |
| 329 |
|
| 330 |
$ret .= $this->finalize_markup($call_return, $attr); |
| 331 |
|
| 332 |
return $ret.$this->get_stack_markup(); |
| 333 |
} |
| 334 |
|
| 335 |
function make_call($query, $flickr_params) { |
| 336 |
$params = substr($query, strlen($this->base_url)); |
| 337 |
if (strlen($params) > 1) { |
| 338 |
$params = substr($params, 1); |
| 339 |
} |
| 340 |
$params = Core::parse_parameters($params); |
| 341 |
$params['format'] = 'json'; |
| 342 |
$params['nojsoncallback'] = 1; |
| 343 |
$params['api_key'] = $this->api_key; |
| 344 |
|
| 345 |
$params = array_merge($flickr_params, $params); |
| 346 |
|
| 347 |
// We only worry about signing the call if the authentication is done. Otherwise we just show what is available. |
| 348 |
if ($this->oauth_done) { |
| 349 |
$signed_args = $this->sign_call($this->base_url, 'GET', $params); |
| 350 |
$params = $signed_args; |
| 351 |
} |
| 352 |
|
| 353 |
$this->push_to_stack("Make call ({$params['method']})"); |
| 354 |
$response = Photonic::http($this->base_url, 'GET', $params); |
| 355 |
$this->pop_from_stack(); |
| 356 |
return $response; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Retrieves a list of collection objects for a given user. This first invokes the web-service, then iterates through the collections returned. |
| 361 |
* For each collection returned it recursively looks for nested collections and sets. |
| 362 |
* |
| 363 |
* @param $user_id |
| 364 |
* @param string $collection_id |
| 365 |
* @param string $filters |
| 366 |
* @return array |
| 367 |
*/ |
| 368 |
function get_collection_list($user_id, $collection_id = '', $filters = '') { |
| 369 |
$this->push_to_stack("Collection list (collection id '$collection_id')"); |
| 370 |
$query = $this->base_url.'?method=flickr.collections.getTree'; |
| 371 |
$flickr_params = []; |
| 372 |
if (!empty($collection_id)) { |
| 373 |
$flickr_params['collection_id'] = $collection_id; |
| 374 |
} |
| 375 |
if (!empty($user_id)) { |
| 376 |
$flickr_params['user_id'] = $user_id; |
| 377 |
} |
| 378 |
|
| 379 |
$collection_list = []; |
| 380 |
if (!empty($filters)) { |
| 381 |
$collection_list = explode(',', $filters); |
| 382 |
} |
| 383 |
|
| 384 |
$feed = $this->make_call($query, $flickr_params); |
| 385 |
if (!is_wp_error($feed) && 200 == $feed['response']['code']) { |
| 386 |
$feed = $feed['body']; |
| 387 |
$feed = json_decode($feed); |
| 388 |
if ($feed->stat == 'ok') { |
| 389 |
$collections = $feed->collections; |
| 390 |
$collections = $collections->collection; |
| 391 |
$ret = []; |
| 392 |
$processed = []; |
| 393 |
foreach ($collections as $collection) { |
| 394 |
if (isset($collection->id)) { |
| 395 |
if (!in_array($collection->id, $processed)) { |
| 396 |
$iterative = $this->get_nested_collections($collection, $processed); |
| 397 |
$ret = array_merge($ret, $iterative); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
$filtered_ret = []; |
| 403 |
if (!empty($collection_list)) { |
| 404 |
foreach ($ret as $collection) { |
| 405 |
if (in_array($collection['id'], $collection_list)) { |
| 406 |
$filtered_ret[] = $collection; |
| 407 |
} |
| 408 |
} |
| 409 |
$this->pop_from_stack(); |
| 410 |
return $filtered_ret; |
| 411 |
} |
| 412 |
|
| 413 |
$this->pop_from_stack(); |
| 414 |
return $ret; |
| 415 |
} |
| 416 |
} |
| 417 |
$this->pop_from_stack(); |
| 418 |
return []; |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Goes through a Flickr collection and recursively fetches all sets and other collections within it. This is returned as |
| 423 |
* a flattened array. |
| 424 |
* |
| 425 |
* @param $collection |
| 426 |
* @param $processed |
| 427 |
* @return array |
| 428 |
*/ |
| 429 |
function get_nested_collections($collection, &$processed) { |
| 430 |
$id = isset($collection->id) ? (string)$collection->id : ''; |
| 431 |
if (in_array($id, $processed)) { |
| 432 |
return []; |
| 433 |
} |
| 434 |
|
| 435 |
$processed[] = $id; |
| 436 |
$id = substr($id, strpos($id, '-') + 1); |
| 437 |
$title = isset($collection->title) ? (string)$collection->title : ''; |
| 438 |
$description = isset($collection->description) ? (string)$collection->description : ''; |
| 439 |
$thumb = isset($collection->iconsmall) ? (string)$collection->iconsmall : (isset($collection->iconlarge) ? (string)$collection->iconlarge : ''); |
| 440 |
$thumb = ($thumb == '/images/collection_default_l.gif' || $thumb = '/images/collection_default_s.gif') ? 'https://www.flickr.com'.$thumb : $thumb; |
| 441 |
|
| 442 |
$ret = []; |
| 443 |
|
| 444 |
if (isset($collection->set)) { |
| 445 |
$inner_sets = $collection->set; |
| 446 |
$sets = []; |
| 447 |
if (count($inner_sets) > 0) { |
| 448 |
foreach ($inner_sets as $inner_set) { |
| 449 |
$sets[] = [ |
| 450 |
'id' => (string)$inner_set->id, |
| 451 |
'title' => (string)$inner_set->title, |
| 452 |
'description' => (string)$inner_set->description, |
| 453 |
]; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
$ret[] = [ |
| 458 |
'id' => $id, |
| 459 |
'title' => $title, |
| 460 |
'description' => $description, |
| 461 |
'thumb' => $thumb, |
| 462 |
'sets' => $sets, |
| 463 |
]; |
| 464 |
} |
| 465 |
|
| 466 |
if (isset($collection->collection)) { |
| 467 |
$inner_collections = $collection->collection; |
| 468 |
if (count($inner_collections) > 0) { |
| 469 |
foreach ($inner_collections as $inner_collection) { |
| 470 |
$inner_ret = $this->get_nested_collections($inner_collection, $processed); |
| 471 |
$ret = array_merge($ret, $inner_ret); |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
return $ret; |
| 476 |
} |
| 477 |
|
| 478 |
function process_query($response, $flickr_params, $short_code = [], &$gallery_meta = []) { |
| 479 |
$this->push_to_stack('Process response'); |
| 480 |
$ret = ''; |
| 481 |
|
| 482 |
$filter_list = []; |
| 483 |
if (!empty($short_code['filter'])) { |
| 484 |
$filter_list = explode(',', $short_code['filter']); |
| 485 |
} |
| 486 |
|
| 487 |
if (!is_wp_error($response)) { |
| 488 |
if ($response['response']['code'] == 200) { |
| 489 |
$body = $response['body']; |
| 490 |
$body = json_decode($body); |
| 491 |
switch ($flickr_params['method']) { |
| 492 |
case 'flickr.photos.getInfo': |
| 493 |
if (isset($body->photo)) { |
| 494 |
$photo = $body->photo; |
| 495 |
$ret .= $this->process_photo($photo, $short_code); |
| 496 |
} |
| 497 |
break; |
| 498 |
|
| 499 |
case 'flickr.photos.search': |
| 500 |
if (isset($body->photos) && isset($body->photos->photo)) { |
| 501 |
$photos = $body->photos->photo; |
| 502 |
$ret .= $this->process_photos($photos, 'stream', $flickr_params, $short_code, [ |
| 503 |
'total' => $body->photos->total, |
| 504 |
'start' => ($body->photos->page - 1) * $body->photos->perpage + 1, |
| 505 |
'end' => $body->photos->page * $body->photos->perpage > $body->photos->total ? $body->photos->total : $body->photos->page * $body->photos->perpage, |
| 506 |
'per-page' => $body->photos->perpage, |
| 507 |
] |
| 508 |
); |
| 509 |
} |
| 510 |
break; |
| 511 |
|
| 512 |
case 'flickr.photosets.getInfo': |
| 513 |
if (isset($body->photoset)) { |
| 514 |
$photoset = $body->photoset; |
| 515 |
$ret .= $this->process_photoset_header($photoset, $short_code, $gallery_meta); |
| 516 |
} |
| 517 |
break; |
| 518 |
|
| 519 |
case 'flickr.photosets.getPhotos': |
| 520 |
if (isset($body->photoset)) { |
| 521 |
$photoset = $body->photoset; |
| 522 |
if (isset($photoset->photo) && isset($photoset->owner)) { |
| 523 |
$photos = $photoset->photo; |
| 524 |
$ret .= $this->process_photos($photos, 'set', $flickr_params, $short_code, [ |
| 525 |
'total' => $photoset->total, |
| 526 |
'start' => ($photoset->page - 1) * $photoset->perpage + 1, |
| 527 |
'end' => $photoset->page * $photoset->perpage > $photoset->total ? $photoset->total : $photoset->page * $photoset->perpage, |
| 528 |
'per-page' => $photoset->perpage, |
| 529 |
] |
| 530 |
); |
| 531 |
} |
| 532 |
} |
| 533 |
break; |
| 534 |
|
| 535 |
case 'flickr.photosets.getList': |
| 536 |
if (isset($body->photosets)) { |
| 537 |
$photosets = $body->photosets; |
| 538 |
$ret .= $this->process_photosets($photosets, $filter_list, $short_code); |
| 539 |
} |
| 540 |
break; |
| 541 |
|
| 542 |
case 'flickr.galleries.getInfo': |
| 543 |
if (isset($body->gallery)) { |
| 544 |
$gallery = $body->gallery; |
| 545 |
$ret .= $this->process_gallery_header($gallery, $short_code, $gallery_meta); |
| 546 |
} |
| 547 |
break; |
| 548 |
|
| 549 |
case 'flickr.galleries.getPhotos': |
| 550 |
if (isset($body->photos)) { |
| 551 |
$photos = $body->photos; |
| 552 |
if (isset($photos->photo)) { |
| 553 |
$photos = $photos->photo; |
| 554 |
$ret .= $this->process_photos($photos, 'gallery', $flickr_params, $short_code, [ |
| 555 |
'total' => $body->photos->total, |
| 556 |
'start' => ($body->photos->page - 1) * $body->photos->perpage + 1, |
| 557 |
'end' => $body->photos->page * $body->photos->perpage > $body->photos->total ? $body->photos->total : $body->photos->page * $body->photos->perpage, |
| 558 |
'per-page' => $body->photos->perpage, |
| 559 |
] |
| 560 |
); |
| 561 |
} |
| 562 |
} |
| 563 |
break; |
| 564 |
|
| 565 |
case 'flickr.galleries.getList': |
| 566 |
if (isset($body->galleries)) { |
| 567 |
$galleries = $body->galleries; |
| 568 |
$ret .= $this->process_galleries($galleries, $filter_list, $short_code); |
| 569 |
} |
| 570 |
break; |
| 571 |
|
| 572 |
case 'flickr.collections.getTree': |
| 573 |
if (isset($body->collections)) { |
| 574 |
$collections = $body->collections; |
| 575 |
$ret .= $this->process_collections($collections, $short_code); |
| 576 |
} |
| 577 |
break; |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
else { |
| 582 |
$this->pop_from_stack(); |
| 583 |
return $this->wp_error_message($response); |
| 584 |
} |
| 585 |
|
| 586 |
$this->pop_from_stack(); |
| 587 |
return $ret; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Prints a single photo with the title as an <h3> and the caption as the image caption. |
| 592 |
* |
| 593 |
* @param $photo |
| 594 |
* @param $short_code |
| 595 |
* @return string |
| 596 |
*/ |
| 597 |
function process_photo($photo, $short_code) { |
| 598 |
$main_size = $short_code['main_size'] == 'none' ? '' : $short_code['main_size']; |
| 599 |
$main_image = $this->find_largest_image($photo, $main_size); |
| 600 |
|
| 601 |
return $this->generate_single_photo_markup([ |
| 602 |
'src' => $main_image, |
| 603 |
'href' => (isset($photo->urls) && isset($photo->urls->url) && count($photo->urls->url) > 0) ? $photo->urls->url[0]->_content : '', |
| 604 |
'title' => isset($photo->title) ? $photo->title->_content : '', |
| 605 |
'caption' => isset($photo->description) ? $photo->description->_content : '', |
| 606 |
] |
| 607 |
); |
| 608 |
} |
| 609 |
|
| 610 |
/** |
| 611 |
* Prints thumbnails for all photos returned in a query. This is used for printing the results of a search, tag, photoset or gallery. |
| 612 |
* The photos are printed in-page. |
| 613 |
* |
| 614 |
* @param $photos |
| 615 |
* @param string $parent |
| 616 |
* @param $flickr_params |
| 617 |
* @param $short_code |
| 618 |
* @param array $level_2_meta |
| 619 |
* @return string |
| 620 |
*/ |
| 621 |
function process_photos($photos, $parent, $flickr_params, $short_code, $level_2_meta = []) { |
| 622 |
global $photonic_flickr_photo_title_display, $photonic_flickr_photo_pop_title_display; |
| 623 |
global $photonic_flickr_photos_per_row_constraint, $photonic_flickr_photos_constrain_by_padding, $photonic_flickr_photos_constrain_by_count; |
| 624 |
global $photonic_flickr_photos_pop_per_row_constraint, $photonic_flickr_photos_pop_constrain_by_padding, $photonic_flickr_photos_pop_constrain_by_count; |
| 625 |
|
| 626 |
if ($short_code['display'] == 'in-page') { |
| 627 |
$title_position = $photonic_flickr_photo_title_display; |
| 628 |
$row_constraints = ['constraint-type' => $photonic_flickr_photos_per_row_constraint, 'padding' => $photonic_flickr_photos_constrain_by_padding, 'count' => $photonic_flickr_photos_constrain_by_count]; |
| 629 |
} |
| 630 |
else { |
| 631 |
$title_position = $photonic_flickr_photo_pop_title_display; |
| 632 |
$row_constraints = ['constraint-type' => $photonic_flickr_photos_pop_per_row_constraint, 'padding' => $photonic_flickr_photos_pop_constrain_by_padding, 'count' => $photonic_flickr_photos_pop_constrain_by_count]; |
| 633 |
} |
| 634 |
$photo_objects = $this->build_level_1_objects($photos, $short_code, $flickr_params, ['parent' => $parent]); |
| 635 |
|
| 636 |
$ret = $this->layout_gallery($photo_objects, |
| 637 |
[ |
| 638 |
'title_position' => $title_position, |
| 639 |
'row_constraints' => $row_constraints, |
| 640 |
'parent' => $parent, |
| 641 |
'level_2_meta' => $level_2_meta, |
| 642 |
], |
| 643 |
$short_code, |
| 644 |
1 |
| 645 |
); |
| 646 |
return $ret; |
| 647 |
} |
| 648 |
|
| 649 |
function find_largest_image($photo, $size = 'o') { |
| 650 |
if (in_array($size, ['z','','n','m','q','t','s'])) { |
| 651 |
$ret_size = $size == '' ? '' : '_'.$size; |
| 652 |
$photo_id = !empty($photo->primary) ? $photo->primary : $photo->id; |
| 653 |
return 'https://farm'.$photo->farm.'.staticflickr.com/'.$photo->server.'/'.$photo_id.'_'.$photo->secret.$ret_size.'.jpg'; |
| 654 |
} |
| 655 |
|
| 656 |
$max_to_min = ['o','k','h','b','c']; |
| 657 |
$pos = array_search($size, $max_to_min); |
| 658 |
if ($pos !== FALSE) { |
| 659 |
for ($idx = $pos; $idx < count($max_to_min); $idx++) { |
| 660 |
$value = $max_to_min[$idx]; |
| 661 |
if (isset($photo->{'url_'.$value})) { |
| 662 |
return $photo->{'url_'.$value}; |
| 663 |
} |
| 664 |
else if (isset($photo->primary_photo_extras) && isset($photo->primary_photo_extras->{'url_'.$value})) { |
| 665 |
return $photo->primary_photo_extras->{'url_'.$value}; |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
return 'https://farm'.$photo->farm.'.staticflickr.com/'.$photo->server.'/'.(!empty($photo->primary) ? $photo->primary : $photo->id).'_'.$photo->secret.'_z'.'.jpg'; |
| 670 |
} |
| 671 |
|
| 672 |
function find_largest_video_thumb(&$photo_object, $current_sizes, $shortcode_sizes) { |
| 673 |
$video_sizes = [ |
| 674 |
'o' => 'Original', |
| 675 |
'k' => 'Large 2048', |
| 676 |
'h' => 'Large 1600', |
| 677 |
'b' => 'Large', |
| 678 |
'c' => 'Medium 800', |
| 679 |
'z' => 'Medium 640', |
| 680 |
'' => 'Medium', |
| 681 |
'n' => 'Small 320', |
| 682 |
'm' => 'Small', |
| 683 |
'q' => 'Large Square', |
| 684 |
't' => 'Thumbnail', |
| 685 |
's' => 'Square', |
| 686 |
]; |
| 687 |
|
| 688 |
$max_to_min = array_keys($video_sizes); |
| 689 |
foreach ($shortcode_sizes as $type => $size) { |
| 690 |
$match_found = false; |
| 691 |
$pos = array_search($size, $max_to_min); |
| 692 |
for ($idx = $pos; $idx < count($max_to_min); $idx++) { |
| 693 |
foreach ($current_sizes as $flickr_size) { |
| 694 |
if ($flickr_size->label == $video_sizes[$max_to_min[$idx]]) { |
| 695 |
$photo_object[$type] = $flickr_size->source; |
| 696 |
$match_found = true; |
| 697 |
break; |
| 698 |
} |
| 699 |
} |
| 700 |
if ($match_found) { |
| 701 |
break; |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
function build_level_1_objects($photos, $short_code, $flickr_params = [], $options = []) { |
| 708 |
$photo_objects = []; |
| 709 |
$video_size = in_array($this->library, ['colorbox', 'fancybox', 'fancybox2', 'fancybox3', 'featherlight', 'lightgallery', 'magnific', 'swipebox']) ? $short_code['video_size'] : 'Video Player'; |
| 710 |
// $video_size = $short_code['video_size']; |
| 711 |
|
| 712 |
$main_size = $short_code['main_size'] == 'none' ? '' : $short_code['main_size']; |
| 713 |
$tile_size = (empty($short_code['tile_size']) || $short_code['tile_size'] == 'same') ? $main_size : ($short_code['tile_size'] == 'none' ? '' : $short_code['tile_size']); |
| 714 |
|
| 715 |
foreach ($photos as $photo) { |
| 716 |
$photo_object = []; |
| 717 |
$photo_object['thumbnail'] = 'https://farm'.$photo->farm.'.staticflickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'_'.$short_code['thumb_size'].'.jpg'; |
| 718 |
$photo_object['main_image'] = $this->find_largest_image($photo, $main_size); |
| 719 |
$download = $this->find_largest_image($photo); |
| 720 |
$photo_object['download'] = substr($download, 0, strlen($download)-4).'_d'.substr($download, -4); |
| 721 |
$photo_object['tile_image'] = $tile_size == $main_size ? $photo_object['main_image'] : $this->find_largest_image($photo, $tile_size); |
| 722 |
$photo_object['alt_title'] = esc_attr($photo->title); |
| 723 |
$owner = ''; |
| 724 |
if (isset($photo->owner)) { |
| 725 |
$owner = $photo->owner; |
| 726 |
} |
| 727 |
else if (isset($photo->ownername)) { |
| 728 |
$owner = $photo->ownername; |
| 729 |
} |
| 730 |
|
| 731 |
$specific = ''; |
| 732 |
if ($options['parent'] === 'set' && !empty($flickr_params['photoset_id'])) { |
| 733 |
$specific = '/in/set-'.$flickr_params['photoset_id']; |
| 734 |
} |
| 735 |
$url = "https://www.flickr.com/photos/".$owner."/".$photo->id.$specific; |
| 736 |
$photo_object['main_page'] = $url; |
| 737 |
|
| 738 |
$title = $photo->title; |
| 739 |
$photo_object['title'] = $title; |
| 740 |
|
| 741 |
if (isset($photo->description)) { |
| 742 |
$photo_object['description'] = $photo->description->_content; |
| 743 |
} |
| 744 |
else { |
| 745 |
$photo_object['description'] = ''; |
| 746 |
} |
| 747 |
|
| 748 |
if (!empty($photo->media) && $photo->media == 'video') { |
| 749 |
/* if (empty($photo->farm) || $photo->farm == '0') { |
| 750 |
$photo_object['main_image'] = $this->find_largest_image($photo, 'o'); |
| 751 |
$photo_object['tile_image'] = $photo_object['thumbnail'] = $photo_object['main_image']; |
| 752 |
}*/ |
| 753 |
$video_response = $this->make_call($this->base_url.'?method=flickr.photos.getSizes&photo_id='.$photo->id, $flickr_params); |
| 754 |
if (!is_wp_error($video_response) && 200 == $video_response['response']['code']) { |
| 755 |
$video_response = $video_response['body']; |
| 756 |
$video_response = json_decode($video_response); |
| 757 |
if ($video_response->stat == 'ok') { |
| 758 |
$video_response = $video_response->sizes; |
| 759 |
$video_response = $video_response->size; |
| 760 |
if (is_array($video_response)) { |
| 761 |
$this->find_largest_video_thumb($photo_object, $video_response, [ |
| 762 |
'main_image' => $main_size, |
| 763 |
'tile_image' => $tile_size, |
| 764 |
'thumbnail' => $short_code['thumb_size'], |
| 765 |
]); |
| 766 |
|
| 767 |
foreach ($video_response as $size) { |
| 768 |
if ($size->label != $video_size) { |
| 769 |
continue; |
| 770 |
} |
| 771 |
else { |
| 772 |
$photo_object['video'] = $size->source; |
| 773 |
$photo_object['mime'] = 'video/mp4'; |
| 774 |
break; |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
$photo_object['id'] = $photo->id; |
| 783 |
$photo_object['provider'] = $this->provider; |
| 784 |
$photo_objects[] = $photo_object; |
| 785 |
} |
| 786 |
|
| 787 |
return $photo_objects; |
| 788 |
} |
| 789 |
|
| 790 |
function build_level_2_objects($flickr_objects, $short_code = [], $filter_list = [], $options = []) { |
| 791 |
global $photonic_gallery_template_page; |
| 792 |
|
| 793 |
$main_size = $short_code['main_size'] == 'none' ? '' : $short_code['main_size']; |
| 794 |
$tile_size = (empty($short_code['tile_size']) || $short_code['tile_size'] == 'same') ? $main_size : ($short_code['tile_size'] == 'none' ? '' : $short_code['tile_size']); |
| 795 |
|
| 796 |
$objects = []; |
| 797 |
|
| 798 |
$type = $options['type']; |
| 799 |
foreach ($flickr_objects as $flickr_object) { |
| 800 |
if (!empty($filter_list) && |
| 801 |
(($type == 'photoset' && ((!in_array($flickr_object->id, $filter_list) && strtolower($short_code['filter_type']) !== 'exclude') || |
| 802 |
(in_array($flickr_object->id, $filter_list) && strtolower($short_code['filter_type']) === 'exclude'))) || |
| 803 |
($type == 'gallery' && ((!in_array(substr($flickr_object->id, stripos($flickr_object->id, '-') + 1), $filter_list) && strtolower($short_code['filter_type']) !== 'exclude') || |
| 804 |
(in_array(substr($flickr_object->id, stripos($flickr_object->id, '-') + 1), $filter_list) && strtolower($short_code['filter_type']) === 'exclude'))))) { |
| 805 |
continue; |
| 806 |
} |
| 807 |
|
| 808 |
$object = []; |
| 809 |
|
| 810 |
$internal_short_code = $short_code; |
| 811 |
unset($internal_short_code['collection_id']); |
| 812 |
$internal_short_code['layout'] = empty($short_code['photo_layout']) ? $short_code['layout'] : $short_code['photo_layout']; |
| 813 |
|
| 814 |
$object['id_1'] = $flickr_object->id; |
| 815 |
$object['title'] = esc_attr($flickr_object->title->_content); |
| 816 |
$object['description'] = esc_attr($flickr_object->description->_content); |
| 817 |
|
| 818 |
if ($type == 'gallery') { |
| 819 |
$object['thumbnail'] = "https://farm".$flickr_object->primary_photo_farm.".staticflickr.com/".$flickr_object->primary_photo_server."/".$flickr_object->primary_photo_id."_".$flickr_object->primary_photo_secret."_".$short_code['thumb_size'].".jpg"; |
| 820 |
$object['tile_image'] = "https://farm".$flickr_object->primary_photo_farm.".staticflickr.com/".$flickr_object->primary_photo_server."/".$flickr_object->primary_photo_id."_".$flickr_object->primary_photo_secret.'_'.$tile_size.".jpg"; |
| 821 |
// $object['tile_image'] = $this->find_largest_image($flickr_object, $tile_size); |
| 822 |
$object['main_page'] = $flickr_object->url; |
| 823 |
$object['counter'] = $flickr_object->count_photos; |
| 824 |
$object['classes'] = ["photonic-flickr-gallery-thumb-user-{$short_code['user_id']}"]; |
| 825 |
|
| 826 |
$internal_short_code['view'] = 'gallery'; |
| 827 |
$internal_short_code['gallery_id'] = $flickr_object->id; |
| 828 |
} |
| 829 |
else if ($type == 'photoset') { |
| 830 |
$object['thumbnail'] = "https://farm".$flickr_object->farm.".staticflickr.com/".$flickr_object->server."/".$flickr_object->primary."_".$flickr_object->secret."_".$short_code['thumb_size'].".jpg"; |
| 831 |
$object['tile_image'] = $this->find_largest_image($flickr_object, $tile_size); |
| 832 |
$owner = isset($flickr_object->owner) ? $flickr_object->owner : $short_code['user_id']; |
| 833 |
$object['main_page'] = "https://www.flickr.com/photos/$owner/sets/{$flickr_object->id}"; |
| 834 |
$object['counter'] = $flickr_object->photos; |
| 835 |
|
| 836 |
$internal_short_code['view'] = 'photoset'; |
| 837 |
$internal_short_code['photoset_id'] = $flickr_object->id; |
| 838 |
} |
| 839 |
|
| 840 |
$object['data_attributes'] = [ |
| 841 |
'photo-count' => $short_code['photo_count'], |
| 842 |
'photo-more' => empty($short_code['photo_more']) ? '' : $short_code['photo_more'], |
| 843 |
'overlay-size' => $short_code['overlay_size'], |
| 844 |
'overlay-video-size' => $short_code['overlay_video_size'], |
| 845 |
]; |
| 846 |
|
| 847 |
if ($short_code['popup'] == 'page' && !empty($photonic_gallery_template_page) && is_string(get_post_status($photonic_gallery_template_page))) { |
| 848 |
$object['gallery_url'] = $this->get_gallery_url($internal_short_code, [ |
| 849 |
'title' => $object['title'], |
| 850 |
]); |
| 851 |
} |
| 852 |
|
| 853 |
$objects[] = $object; |
| 854 |
} |
| 855 |
return $objects; |
| 856 |
} |
| 857 |
|
| 858 |
/** |
| 859 |
* Prints the header for an in-page photoset. |
| 860 |
* |
| 861 |
* @param $photoset |
| 862 |
* @param array $short_code |
| 863 |
* @param array $gallery_meta |
| 864 |
* @return string |
| 865 |
*/ |
| 866 |
function process_photoset_header($photoset, $short_code = [], &$gallery_meta = []) { |
| 867 |
global $photonic_flickr_hide_set_thumbnail, $photonic_flickr_hide_set_title, $photonic_flickr_hide_set_photo_count; |
| 868 |
|
| 869 |
$owner = $photoset->owner; |
| 870 |
$header = [ |
| 871 |
'title' => $photoset->title->_content, |
| 872 |
'description' => $photoset->description->_content, |
| 873 |
'thumb_url' => "https://farm".$photoset->farm.".staticflickr.com/".$photoset->server."/".$photoset->primary."_".$photoset->secret."_".$short_code['thumb_size'].".jpg", |
| 874 |
'link_url' => 'https://www.flickr.com/photos/'.$owner.'/sets/'.$photoset->id, |
| 875 |
]; |
| 876 |
|
| 877 |
$hidden = ['thumbnail' => !empty($photonic_flickr_hide_set_thumbnail), 'title' => !empty($photonic_flickr_hide_set_title), 'counter' => !empty($photonic_flickr_hide_set_photo_count)]; |
| 878 |
$counters = ['photos' => $photoset->photos]; |
| 879 |
|
| 880 |
$ret = $this->process_object_header($header, |
| 881 |
[ |
| 882 |
'type' => 'set', |
| 883 |
'hidden' => $this->get_hidden_headers($short_code['header_display'], $hidden), |
| 884 |
'counters' => $counters, |
| 885 |
'link' => true, |
| 886 |
'display' => $short_code['display'], |
| 887 |
], |
| 888 |
$gallery_meta |
| 889 |
); |
| 890 |
|
| 891 |
return $ret; |
| 892 |
} |
| 893 |
|
| 894 |
/** |
| 895 |
* Prints thumbnails for each photoset returned in a query. |
| 896 |
* |
| 897 |
* @param $photosets |
| 898 |
* @param array $filter_list |
| 899 |
* @param array $short_code |
| 900 |
* @return string |
| 901 |
*/ |
| 902 |
function process_photosets($photosets, $filter_list = [], $short_code = []) { |
| 903 |
global $photonic_flickr_collection_set_per_row_constraint, $photonic_flickr_collection_set_constrain_by_count, $photonic_flickr_collection_set_constrain_by_padding, |
| 904 |
$photonic_flickr_collection_set_title_display, $photonic_flickr_hide_collection_set_photos_count_display; |
| 905 |
$objects = $this->build_level_2_objects($photosets->photoset, $short_code, $filter_list, ['type' => 'photoset']); |
| 906 |
|
| 907 |
$row_constraints = ['constraint-type' => $photonic_flickr_collection_set_per_row_constraint, 'padding' => $photonic_flickr_collection_set_constrain_by_padding, 'count' => $photonic_flickr_collection_set_constrain_by_count]; |
| 908 |
$ret = $this->layout_gallery($objects, |
| 909 |
[ |
| 910 |
'row_constraints' => $row_constraints, |
| 911 |
'type' => 'photosets', |
| 912 |
'singular_type' => 'set', |
| 913 |
'title_position' => $photonic_flickr_collection_set_title_display, |
| 914 |
'level_1_count_display' => $photonic_flickr_hide_collection_set_photos_count_display, |
| 915 |
'pagination' => [ |
| 916 |
'total' => $photosets->total, |
| 917 |
'start' => ($photosets->page - 1) * $photosets->perpage + 1, |
| 918 |
'end' => $photosets->page * $photosets->perpage > $photosets->total ? $photosets-> total : $photosets->page * $photosets->perpage, |
| 919 |
'per-page' => $photosets->perpage, |
| 920 |
], |
| 921 |
], |
| 922 |
$short_code, |
| 923 |
2 |
| 924 |
); |
| 925 |
return $ret; |
| 926 |
} |
| 927 |
|
| 928 |
/** |
| 929 |
* Shows the header for a gallery invoked in-page. |
| 930 |
* |
| 931 |
* @param $gallery |
| 932 |
* @param $short_code |
| 933 |
* @param array $gallery_meta |
| 934 |
* @return string |
| 935 |
*/ |
| 936 |
function process_gallery_header($gallery, $short_code, &$gallery_meta = []) { |
| 937 |
global $photonic_flickr_hide_gallery_thumbnail, $photonic_flickr_hide_gallery_title, $photonic_flickr_hide_gallery_photo_count; |
| 938 |
$header = [ |
| 939 |
'title' => $gallery->title->_content, |
| 940 |
'description' => $gallery->description->_content, |
| 941 |
'thumb_url' => "https://farm".$gallery->primary_photo_farm.".staticflickr.com/".$gallery->primary_photo_server."/".$gallery->primary_photo_id."_".$gallery->primary_photo_secret."_".$short_code['thumb_size'].".jpg", |
| 942 |
'link_url' => $gallery->url, |
| 943 |
]; |
| 944 |
|
| 945 |
$hidden = ['thumbnail' => !empty($photonic_flickr_hide_gallery_thumbnail), 'title' => !empty($photonic_flickr_hide_gallery_title), 'counter' => !empty($photonic_flickr_hide_gallery_photo_count)]; |
| 946 |
$counters = ['photos' => $gallery->count_photos]; |
| 947 |
|
| 948 |
$ret = $this->process_object_header($header, |
| 949 |
[ |
| 950 |
'type' => 'gallery', |
| 951 |
'hidden' => $this->get_hidden_headers($short_code['header_display'], $hidden), |
| 952 |
'counters' => $counters, |
| 953 |
'link' => true, |
| 954 |
'display' => $short_code['display'], |
| 955 |
], |
| 956 |
$gallery_meta |
| 957 |
); |
| 958 |
return $ret; |
| 959 |
} |
| 960 |
|
| 961 |
/** |
| 962 |
* Prints out the thumbnails for all galleries belonging to a user. |
| 963 |
* |
| 964 |
* @param $galleries |
| 965 |
* @param array $filter_list |
| 966 |
* @param array $short_code |
| 967 |
* @return string |
| 968 |
*/ |
| 969 |
function process_galleries($galleries, $filter_list = [], $short_code = []) { |
| 970 |
global $photonic_flickr_galleries_per_row_constraint, $photonic_flickr_galleries_constrain_by_padding, |
| 971 |
$photonic_flickr_galleries_constrain_by_count, $photonic_flickr_gallery_title_display, $photonic_flickr_hide_gallery_photos_count_display; |
| 972 |
|
| 973 |
$objects = $this->build_level_2_objects($galleries->gallery, $short_code, $filter_list, ['type' => 'gallery']); |
| 974 |
|
| 975 |
$row_constraints = ['constraint-type' => $photonic_flickr_galleries_per_row_constraint, 'padding' => $photonic_flickr_galleries_constrain_by_padding, 'count' => $photonic_flickr_galleries_constrain_by_count]; |
| 976 |
$ret = $this->layout_gallery($objects, |
| 977 |
[ |
| 978 |
'row_constraints' => $row_constraints, |
| 979 |
'type' => 'galleries', |
| 980 |
'singular_type' => 'gallery', |
| 981 |
'title_position' => $photonic_flickr_gallery_title_display, |
| 982 |
'level_1_count_display' => $photonic_flickr_hide_gallery_photos_count_display, |
| 983 |
'pagination' => [ |
| 984 |
'total' => $galleries->total, |
| 985 |
'start' => ($galleries->page - 1) * $galleries->per_page + 1, |
| 986 |
'end' => $galleries->page * $galleries->per_page > $galleries->total ? $galleries->total : $galleries->page * $galleries->per_page, |
| 987 |
'per-page' => $galleries->per_page, |
| 988 |
] |
| 989 |
], |
| 990 |
$short_code, |
| 991 |
2 |
| 992 |
); |
| 993 |
return $ret; |
| 994 |
} |
| 995 |
|
| 996 |
/** |
| 997 |
* Prints a collection header, followed by thumbnails of all sets in that collection. |
| 998 |
* |
| 999 |
* @param $collections |
| 1000 |
* @param array $short_code |
| 1001 |
* @return string |
| 1002 |
*/ |
| 1003 |
function process_collections($collections, $short_code = []) { |
| 1004 |
global $photonic_flickr_hide_empty_collection_details, $photonic_flickr_collection_set_per_row_constraint, $photonic_flickr_collection_set_constrain_by_padding, |
| 1005 |
$photonic_flickr_collection_set_constrain_by_count, $photonic_flickr_hide_collection_thumbnail, $photonic_flickr_hide_collection_title, $photonic_flickr_hide_collection_set_count, $photonic_flickr_collection_set_title_display, $photonic_flickr_hide_collection_set_photos_count_display; |
| 1006 |
$ret = ''; |
| 1007 |
|
| 1008 |
$row_constraints = ['constraint-type' => $photonic_flickr_collection_set_per_row_constraint, 'padding' => $photonic_flickr_collection_set_constrain_by_padding, 'count' => $photonic_flickr_collection_set_constrain_by_count]; |
| 1009 |
$collection_headers = []; |
| 1010 |
$collection_sets = []; |
| 1011 |
|
| 1012 |
foreach ($collections->collection as $collection) { |
| 1013 |
$dont_show = false; |
| 1014 |
if (empty($collection->set) && !empty($photonic_flickr_hide_empty_collection_details)) { |
| 1015 |
$dont_show = true; |
| 1016 |
} |
| 1017 |
$id = $collection->id; |
| 1018 |
if (!$dont_show) { |
| 1019 |
$url_id = substr($id, stripos($id, '-') + 1); |
| 1020 |
if ($collection->iconsmall == '/images/collection_default_s.gif') { |
| 1021 |
$thumb = 'https://www.flickr.com'.$collection->iconsmall; |
| 1022 |
} |
| 1023 |
else { |
| 1024 |
$thumb = $collection->iconsmall; |
| 1025 |
} |
| 1026 |
$header = ['id' => $id.'-'.$short_code['user_id'], 'title' => $collection->title, 'thumb_url' => $thumb, 'link_url' => "https://www.flickr.com/photos/".$short_code['user_id']."/collections/".$url_id]; |
| 1027 |
$hidden = ['thumbnail' => !empty($photonic_flickr_hide_collection_thumbnail), 'title' => !empty($photonic_flickr_hide_collection_title), 'counter' => !empty($photonic_flickr_hide_collection_set_count)]; |
| 1028 |
$counters = []; |
| 1029 |
if (isset($collection->set)) { |
| 1030 |
$photosets = $collection->set; |
| 1031 |
$counters['sets'] = count($photosets); |
| 1032 |
} |
| 1033 |
|
| 1034 |
$header = $this->process_object_header($header, |
| 1035 |
[ |
| 1036 |
'type' => 'collection', |
| 1037 |
'hidden' => $this->get_hidden_headers($short_code['header_display'], $hidden), |
| 1038 |
'counters' => $counters, |
| 1039 |
'link' => true, |
| 1040 |
'iterate_level_3' => $short_code['iterate_level_3'], |
| 1041 |
'layout' => $short_code['layout'], |
| 1042 |
] |
| 1043 |
); |
| 1044 |
|
| 1045 |
$ret .= $header; |
| 1046 |
$collection_headers[] = ['collection' => $id, 'header' => $header]; |
| 1047 |
} |
| 1048 |
|
| 1049 |
if (isset($collection->set) && !empty($collection->set) && $short_code['iterate_level_3']) { |
| 1050 |
$flickr_objects = []; |
| 1051 |
$photosets = $collection->set; |
| 1052 |
|
| 1053 |
$parallel = []; |
| 1054 |
$psets = []; |
| 1055 |
$hooks = new Requests_Hooks(); |
| 1056 |
$hooks->register('curl.before_multi_add', [$this, 'ssl_verify_peer'], 100); |
| 1057 |
foreach ($photosets as $set) { |
| 1058 |
$parallel_params = []; |
| 1059 |
$parallel_params['format'] = 'json'; |
| 1060 |
$parallel_params['nojsoncallback'] = 1; |
| 1061 |
$parallel_params['api_key'] = $this->api_key; |
| 1062 |
$parallel_params['method'] = 'flickr.photosets.getInfo'; |
| 1063 |
$parallel_params['photoset_id'] = $set->id; |
| 1064 |
// We only worry about signing the call if the authentication is done. Otherwise we just show what is available. |
| 1065 |
if ($this->oauth_done) { |
| 1066 |
$signed_args = $this->sign_call($this->base_url, 'GET', $parallel_params); |
| 1067 |
$parallel_params = $signed_args; |
| 1068 |
} |
| 1069 |
|
| 1070 |
$parallel[] = [ |
| 1071 |
'url' => $this->base_url, |
| 1072 |
'type' => 'GET', |
| 1073 |
'data' => $parallel_params, |
| 1074 |
]; |
| 1075 |
$psets[] = $set->id; |
| 1076 |
} |
| 1077 |
$collection_sets[] = ['collection' => $id, 'count' => count($parallel)]; |
| 1078 |
|
| 1079 |
if (!empty($parallel)) { |
| 1080 |
$parallel_responses = Requests::request_multiple($parallel, ['hooks' => $hooks]); |
| 1081 |
foreach ($parallel_responses as $ps_response) { |
| 1082 |
if (is_a($ps_response, 'Requests_Response')) { |
| 1083 |
$ps_response = json_decode($ps_response->body); |
| 1084 |
if (!empty($ps_response->photoset->id)) { |
| 1085 |
$flickr_objects[array_search($ps_response->photoset->id, $psets)] = $ps_response->photoset; |
| 1086 |
} |
| 1087 |
} |
| 1088 |
} |
| 1089 |
} |
| 1090 |
ksort($flickr_objects); |
| 1091 |
|
| 1092 |
$objects = $this->build_level_2_objects($flickr_objects, $short_code, [], ['type' => 'photoset']); // No filters passed for this |
| 1093 |
$ret .= $this->layout_gallery($objects, |
| 1094 |
[ |
| 1095 |
'row_constraints' => $row_constraints, |
| 1096 |
'type' => 'photosets', |
| 1097 |
'singular_type' => 'set', |
| 1098 |
'title_position' => $photonic_flickr_collection_set_title_display, |
| 1099 |
'level_1_count_display' => $photonic_flickr_hide_collection_set_photos_count_display, |
| 1100 |
], |
| 1101 |
$short_code, |
| 1102 |
2 |
| 1103 |
); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
return $ret; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/** |
| 1110 |
* Access Token URL |
| 1111 |
* |
| 1112 |
* @return string |
| 1113 |
*/ |
| 1114 |
public function access_token_URL() { |
| 1115 |
return 'https://www.flickr.com/services/oauth/access_token'; |
| 1116 |
} |
| 1117 |
|
| 1118 |
/** |
| 1119 |
* Authenticate URL |
| 1120 |
* |
| 1121 |
* @return string |
| 1122 |
*/ |
| 1123 |
public function authenticate_URL() { |
| 1124 |
return 'https://www.flickr.com/services/oauth/authorize'; |
| 1125 |
} |
| 1126 |
|
| 1127 |
/** |
| 1128 |
* Authorize URL |
| 1129 |
* |
| 1130 |
* @return string |
| 1131 |
*/ |
| 1132 |
public function authorize_URL() { |
| 1133 |
return 'https://www.flickr.com/services/oauth/authorize'; |
| 1134 |
} |
| 1135 |
|
| 1136 |
/** |
| 1137 |
* Request Token URL |
| 1138 |
* |
| 1139 |
* @return string |
| 1140 |
*/ |
| 1141 |
public function request_token_URL() { |
| 1142 |
return 'https://www.flickr.com/services/oauth/request_token'; |
| 1143 |
} |
| 1144 |
|
| 1145 |
/** |
| 1146 |
* Method to validate that the stored token is indeed authenticated. |
| 1147 |
* |
| 1148 |
* @param $token |
| 1149 |
* @return array|WP_Error |
| 1150 |
*/ |
| 1151 |
function check_access_token($token) { |
| 1152 |
$parameters = ['method' => 'flickr.test.login', 'format' => 'json', 'nojsoncallback' => 1]; |
| 1153 |
$signed_parameters = $this->sign_call($this->base_url, 'GET', $parameters); |
| 1154 |
$end_point = $this->base_url; |
| 1155 |
$end_point .= '?'.Authenticator::build_query($signed_parameters); |
| 1156 |
$parameters = null; |
| 1157 |
|
| 1158 |
$response = Photonic::http($end_point, 'GET', $parameters); |
| 1159 |
return $response; |
| 1160 |
} |
| 1161 |
|
| 1162 |
function execute_helper($args) { |
| 1163 |
if (!empty($args['user'])) { |
| 1164 |
$url = 'https://api.flickr.com/services/rest/?format=json&nojsoncallback=1&api_key='.$this->api_key.'&method=flickr.urls.lookupUser&url='.urlencode('https://www.flickr.com/photos/').$args['user']; |
| 1165 |
} |
| 1166 |
else if (!empty($args['group'])) { |
| 1167 |
$url = 'https://api.flickr.com/services/rest/?format=json&nojsoncallback=1&api_key='.$this->api_key.'&method=flickr.urls.lookupGroup&url='.urlencode('https://www.flickr.com/groups/').$args['group']; |
| 1168 |
} |
| 1169 |
else { |
| 1170 |
return '<div class="photonic-helper">'.sprintf(esc_html__('Please pass the %1$s or %2$s attribute.', 'photonic'), '<code>user</code>', '<code>group</code>').'</div>'; |
| 1171 |
} |
| 1172 |
|
| 1173 |
$response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]); |
| 1174 |
if (!is_wp_error($response)) { |
| 1175 |
if (isset($response['response']) && isset($response['response']['code'])) { |
| 1176 |
if ($response['response']['code'] == 200) { |
| 1177 |
$body = json_decode(wp_remote_retrieve_body($response)); |
| 1178 |
if (isset($body->stat) && $body->stat == 'fail') { |
| 1179 |
Photonic::log($response); |
| 1180 |
return '<div class="photonic-helper">'.$body->message.'</div>'; |
| 1181 |
} |
| 1182 |
|
| 1183 |
if (isset($body->user)) { |
| 1184 |
return '<div class="photonic-helper">'.sprintf(esc_html__('User id: %s', 'photonic'), $body->user->id).'</div>'; |
| 1185 |
} |
| 1186 |
else if (isset($body->group)) { |
| 1187 |
return '<div class="photonic-helper">'.sprintf(esc_html__('Group id: %s', 'photonic'), $body->group->id).'</div>'; |
| 1188 |
} |
| 1189 |
else { |
| 1190 |
return '<div class="photonic-helper">'.esc_html__('No data returned.', 'photonic').'</div>'; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
else { |
| 1194 |
Photonic::log($response['response']); |
| 1195 |
return '<div class="photonic-helper">'.sprintf(esc_html__('No data returned. Error code %s', 'photonic'), $response['response']['code']).'</div>'; |
| 1196 |
} |
| 1197 |
} |
| 1198 |
else { |
| 1199 |
Photonic::log($response); |
| 1200 |
return '<div class="photonic-helper">'.esc_html__('No data returned. Empty response, or empty error code.', 'photonic').'</div>'; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
else { |
| 1204 |
return '<div class="photonic-helper">'.$response->get_error_message().'</div>'; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|