PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.37
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.37
3.37 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 All 142 releases
photonic / Admin / Wizard / Google_Photos.php

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

432 lines 14.4 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 Google_Photos extends Source {
9 private static ?Google_Photos $instance = null;
10
11 private $notice;
12
13 protected function __construct() {
14 parent::__construct();
15 $this->provider = 'google';
16 $this->api_base = Wizard::base_apis()['google'];
17 $this->notice = sprintf(
18 esc_html__('%1$sAPI Shutdown%2$s%3$sGoogle is making a change to its APIs, which make them unusable for browsing on the web. This will prevent Photonic from working after %1$s31st March 2025%2$s. Please switch to a different platform to avoid disruption.%4$s', 'photonic'),
19 '<strong>',
20 '</strong>',
21 '<p>',
22 '</p>'
23 );
24
25 $this->allowed_image_sizes['google'] = [
26 'thumb_size' => [
27 '32' => '32',
28 '48' => 48,
29 '64' => 64,
30 '72' => 72,
31 '104' => 104,
32 '144' => 144,
33 '150' => 150,
34 '160' => 160,
35 ],
36 'tile_size' => [
37 'same' => esc_html__('Same as Main image size', 'photonic'),
38 '94' => 94,
39 '110' => 110,
40 '128' => 128,
41 '200' => 200,
42 '220' => 220,
43 '288' => 288,
44 '320' => 320,
45 '400' => 400,
46 '512' => 512,
47 '576' => 576,
48 '640' => 640,
49 '720' => 720,
50 '800' => 800,
51 '912' => 912,
52 '1024' => 1024,
53 '1152' => 1152,
54 '1280' => 1280,
55 '1440' => 1440,
56 '1600' => 1600,
57 ],
58 'main_size' => [
59 '94' => 94,
60 '110' => 110,
61 '128' => 128,
62 '200' => 200,
63 '220' => 220,
64 '288' => 288,
65 '320' => 320,
66 '400' => 400,
67 '512' => 512,
68 '576' => 576,
69 '640' => 640,
70 '720' => 720,
71 '800' => 800,
72 '912' => 912,
73 '1024' => 1024,
74 '1152' => 1152,
75 '1280' => 1280,
76 '1440' => 1440,
77 '1600' => 1600,
78 ],
79 ];
80 }
81
82 public static function get_instance(): Google_Photos {
83 if (null === self::$instance) {
84 self::$instance = new Google_Photos();
85 }
86 return self::$instance;
87 }
88
89 public function get_screen_2(): array {
90 return [
91 'header' => esc_html__('Choose Type of Gallery', 'photonic'),
92 'notice' => $this->notice,
93 'display' => [
94 'display_type' => [
95 'desc' => esc_html__('What do you want to show?', 'photonic'),
96 'type' => 'select',
97 'options' => [
98 '' => '',
99 'multi-photo' => esc_html__('Multiple Photos', 'photonic'),
100 'album-photo' => esc_html__('Photos from an Album Belonging to You', 'photonic'),
101 'shared-album-photo' => esc_html__('Photos from an Album Shared by or with You', 'photonic'),
102 'multi-album' => esc_html__('Multiple Albums', 'photonic'),
103 ],
104 'req' => 1,
105 ],
106 ],
107 ];
108 }
109
110 public function get_screen_3(): array {
111 return [
112 'header' => esc_html__('Build your gallery', 'photonic'),
113 'multi-photo' => [
114 'header' => esc_html__('All your photos', 'photonic'),
115 'notice' => $this->notice,
116 'desc' => esc_html__('You can show all your photos, or apply filters to show some of them.', 'photonic'),
117 'display' => [
118 'date_filters' => [
119 'desc' => esc_html__('Date Filters', 'photonic'),
120 'type' => 'date-filter',
121 'count' => 5
122 ],
123
124 'date_range_filters' => [
125 'desc' => esc_html__('Date Range Filters', 'photonic'),
126 'type' => 'date-range-filter',
127 'count' => 5
128 ],
129
130 'content_filters' => [
131 'desc' => esc_html__('Content Filters', 'photonic'),
132 'type' => 'text',
133 'hint' => sprintf(
134 esc_html__('Comma-separated. Pick from: %s. Filters will be applied on the front-end, not on the display below. ', 'photonic'),
135 'NONE, LANDSCAPES, RECEIPTS, CITYSCAPES, LANDMARKS, SELFIES, PEOPLE, PETS, WEDDINGS, BIRTHDAYS, DOCUMENTS, TRAVEL, ANIMALS, FOOD, SPORT, NIGHT, PERFORMANCES, WHITEBOARDS, SCREENSHOTS, UTILITY'
136 ) .
137 Photonic::doc_link("https://aquoid.com/plugins/photonic/google-photos/photos/#filtering-photos"),
138 ],
139
140 'container' => [
141 'type' => 'thumbnail-selector',
142 'mode' => 'none',
143 'for' => 'selected_data',
144 ],
145 ],
146 ],
147 'album-photo' => [
148 'header' => esc_html__('Pick your album', 'photonic'),
149 'notice' => $this->notice,
150 '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'),
151 'display' => [
152 'container' => [
153 'type' => 'thumbnail-selector',
154 'mode' => 'single',
155 'for' => 'selected_data',
156 ],
157 ],
158 ],
159 'shared-album-photo' => [
160 'header' => esc_html__('Pick your album', 'photonic'),
161 'notice' => $this->notice,
162 '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'),
163 'display' => [
164 'container' => [
165 'type' => 'thumbnail-selector',
166 'mode' => 'single',
167 'for' => 'selected_data',
168 ],
169 ],
170 ],
171 'multi-album' => [
172 'header' => esc_html__('Pick your albums', 'photonic'),
173 'notice' => $this->notice,
174 'desc' => esc_html__('From the list below pick the albums you wish to display. Each album will show up as a single thumbnail.', 'photonic'),
175 'display' => [
176 'selection' => [
177 'desc' => esc_html__('What do you want to show?', 'photonic'),
178 'type' => 'select',
179 'options' => [
180 'all' => esc_html__('Automatic all (will automatically add new albums)', 'photonic'),
181 'selected' => esc_html__('Selected albums', 'photonic'),
182 'not-selected' => esc_html__('All except selected albums', 'photonic'),
183 ],
184 'hint' => esc_html__('If you pick "Automatic all" your selections below will be ignored.', 'photonic'),
185 'req' => 1,
186 ],
187 'access' => [
188 'desc' => esc_html__('What type of album?', 'photonic'),
189 'type' => 'select',
190 'options' => [
191 '' => '',
192 'all' => esc_html__('Show both shared and not shared albums', 'photonic'),
193 'shared' => esc_html__('Only show shared albums', 'photonic'),
194 'not-shared' => esc_html__('Only show albums not shared', 'photonic'),
195 ],
196 'std' => '',
197 ],
198 'container' => [
199 'type' => 'thumbnail-selector',
200 'mode' => 'multi',
201 'for' => 'selected_data',
202 ],
203 ],
204 ],
205 ];
206 }
207
208 public function get_screen_4(): array {
209 return [];
210 }
211
212 public function get_screen_5(): array {
213 global $photonic_google_media;
214 return [
215 'google' => [
216 'L1' => [
217 'media' => [
218 'desc' => esc_html__('Media to Show', 'photonic'),
219 'type' => 'select',
220 'options' => Utilities::media_options(true, $photonic_google_media),
221 'std' => '',
222 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Google Photos &rarr; Google Photos settings &rarr; Media to show</em>'),
223 ],
224
225 'caption' => [
226 'desc' => esc_html__('Photo titles and captions', 'photonic'),
227 'type' => 'select',
228 'options' => [
229 '' => $this->default_from_settings,
230 'none' => esc_html__('No title / caption / description', 'photonic'),
231 'title' => esc_html__('Always use the photo title, even if blank', 'photonic'),
232 ],
233 'std' => '',
234 'hint' => sprintf($this->default_under, '<em>Photonic &rarr; Settings &rarr; Google Photos &rarr; Google Photos Settings &rarr; Photo titles and captions</em>'),
235 ],
236 ],
237 'main_size' => [
238 'desc' => esc_html__('Main image size', 'photonic'),
239 'type' => 'text',
240 'std' => 1600,
241 'hint' => esc_html__('Numeric values between 1 and 16383, both inclusive.', 'photonic'),
242 ],
243 ]
244 ];
245 }
246
247 public function get_square_size_options(): array {
248 return [
249 'thumb_size' => [
250 'desc' => esc_html__('Thumbnail size', 'photonic'),
251 'type' => 'text',
252 'hint' => esc_html__('Numeric values between 1 and 256, both inclusive.', 'photonic'),
253 'std' => 150,
254 ],
255 'crop_thumb' => [
256 'desc' => esc_html__('Crop Thumbnail', 'photonic'),
257 'type' => 'select',
258 'options' => [
259 'crop' => esc_html__('Crop the thumbnail', 'photonic'),
260 'no-crop' => esc_html__('Do not crop the thumbnail', 'photonic'),
261 ],
262 'std' => 'crop',
263 'hint' => esc_html__('Cropping the thumbnail presents you with a square thumbnail.', 'photonic')
264 ],
265 ];
266 }
267
268 public function get_random_size_options(): array {
269 return [
270 'tile_size' => [
271 'desc' => esc_html__('Tile size', 'photonic'),
272 'type' => 'text',
273 'hint' => esc_html__('Numeric values between 1 and 16383, both inclusive. Leave blank to use the "Main image size".', 'photonic'),
274 ],
275 ];
276 }
277
278 public function make_request($display_type, $for, $flattened_fields): array {
279 global $photonic_google_refresh_token;
280
281 require_once PHOTONIC_PATH . '/Platforms/Google_Photos.php';
282 $module = \Photonic_Plugin\Platforms\Google_Photos::get_instance();
283 $module->authenticate($photonic_google_refresh_token);
284
285 $parameters = [];
286
287 // All OK so far. Let's try to get the data for the next screen
288 $parameters['access_token'] = $module->access_token;
289
290 $query_url = '';
291 if ('multi-photo' === $display_type) {
292 $query_url = 'https://' . $this->api_base . '/v1/mediaItems:search';
293 }
294 elseif ('multi-album' === $display_type || 'album-photo' === $display_type) {
295 $query_url = 'https://' . $this->api_base . '/v1/albums?pageSize=50';
296 }
297 elseif ('shared-album-photo' === $display_type) {
298 $query_url = 'https://' . $this->api_base . '/v1/sharedAlbums?pageSize=50';
299 }
300
301 $query_url = add_query_arg($parameters, $query_url);
302 $response = wp_remote_request($query_url, ['method' => ('multi-photo' === $display_type ? 'POST' : 'GET'), 'sslverify' => PHOTONIC_SSL_VERIFY]);
303 return [$response, [], $query_url];
304 }
305
306 /**
307 * Processes a response from Google to build it out into a gallery of thumbnails. Google has both, L1 and L2 displays in the flow.
308 *
309 * @param $response
310 * @param $display_type
311 * @param $url
312 * @param array $pagination
313 * @return array
314 */
315 public function process_response($response, $display_type, $url = null, &$pagination = []): array {
316 $objects = [];
317 $body = json_decode(wp_remote_retrieve_body($response));
318 if (isset($body->albums) || isset($body->sharedAlbums)) {
319 $albums = isset($body->albums) ? $body->albums : $body->sharedAlbums;
320 foreach ($albums as $album) {
321 $object = [];
322 $object['id'] = $album->id;
323 $object['title'] = !empty($album->title) ? esc_attr($album->title) : '';
324 $object['counters'] = [esc_html(sprintf(_n('%s media item', '%s media items', $album->mediaItemsCount, 'photonic'), $album->mediaItemsCount))];
325 $object['thumbnail'] = $album->coverPhotoBaseUrl . "=w150-h150-c";
326 $objects[] = $object;
327 }
328 if (!empty($body->nextPageToken)) {
329 $pagination['url'] = add_query_arg(['pageToken' => $body->nextPageToken], remove_query_arg(['pageToken'], $url));
330 }
331 }
332 elseif (isset($body->mediaItems)) {
333 $photos = $body->mediaItems;
334 foreach ($photos as $photo) {
335 $object = [];
336 $object['id'] = $photo->id;
337 $object['title'] = !empty($photo->description) ? esc_attr($photo->description) : '';
338 $object['thumbnail'] = $photo->baseUrl . "=w150-h150-c";
339 $objects[] = $object;
340 }
341 }
342 elseif (isset($body->error)) {
343 $objects['error'] = $body->error->message;
344 }
345 return $objects;
346 }
347
348 /**
349 * @param $display_type
350 * @return array
351 */
352 public function construct_shortcode_from_screen_selections($display_type): array {
353 $short_code = [];
354
355 if (check_ajax_referer('photonic-wizard-next-' . get_current_user_id())) {
356 if ('album-photo' === $display_type) {
357 $short_code['view'] = 'photos';
358 $short_code['album_id'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? ''));
359 }
360 elseif ('shared-album-photo' === $display_type) {
361 $short_code['view'] = 'shared-photos';
362 $short_code['album_id'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? ''));
363 }
364 elseif ('multi-photo' === $display_type) {
365 $short_code['view'] = 'photos';
366 $date_filters = !empty($_POST['date_filters']) ? sanitize_text_field(wp_unslash($_POST['date_filters'])) : '';
367 $date_range_filters = !empty($_POST['date_range_filters']) ? sanitize_text_field(wp_unslash($_POST['date_range_filters'])) : '';
368 $short_code['date_filters'] = trim($date_filters . ',' . $date_range_filters, ',');
369 }
370 elseif ('multi-album' === $display_type) {
371 $short_code['view'] = 'albums';
372 }
373 }
374
375 return $short_code;
376 }
377
378 /**
379 * @param $input
380 * @return array
381 */
382 public function deconstruct_shortcode_to_screen_selections($input): array {
383 $deconstructed = [];
384
385 if (!empty($input->view)) {
386 if ('photos' === $input->view || 'shared-photos' === $input->view) {
387 if (!empty($input->album_id)) {
388 $deconstructed['display_type'] = ('photos' === $input->view) ? 'album-photo' : 'shared-album-photo';
389 $deconstructed['selected_data'] = sanitize_text_field($input->album_id);
390 }
391 else {
392 $deconstructed['display_type'] = 'multi-photo';
393 if (!empty($input->date_filters)) {
394 $filters = sanitize_text_field($input->date_filters);
395 $filters = explode(',', $filters);
396 $date_filters = [];
397 $date_range_filters = [];
398 foreach ($filters as $filter) {
399 $maybe_range = explode('-', $filter);
400 if (count($maybe_range) === 2) {
401 $date_range_filters[] = $filter;
402 }
403 elseif (count($maybe_range) === 1) {
404 $date_filters[] = $filter;
405 }
406 }
407 if (count($date_range_filters) > 5) {
408 $date_range_filters = array_slice($date_range_filters, 0, 5);
409 }
410 if (count($date_filters) > 5) {
411 $date_filters = array_slice($date_filters, 0, 5);
412 }
413 $date_range_filters = implode(',', $date_range_filters);
414 $date_filters = implode(',', $date_filters);
415 if (!empty($date_filters)) {
416 $deconstructed['date_filters'] = $date_filters;
417 }
418 if (!empty($date_range_filters)) {
419 $deconstructed['date_range_filters'] = $date_range_filters;
420 }
421 }
422 }
423 }
424 elseif ('albums' === $input->view) {
425 $deconstructed['display_type'] = 'multi-album';
426 }
427 }
428
429 return $deconstructed;
430 }
431 }
432