PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 2.41
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v2.41
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 / Google_Photos.php

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

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