PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 2.40
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v2.40
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 / Helper.php

Helper.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 2.40, at Admin/Helper.php

457 lines 16.5 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;
3
4 use Photonic_Plugin\Core\Photonic;
5 use Photonic_Plugin\Modules\Google_Photos;
6 use Photonic_Plugin\Modules\SmugMug;
7 use WP_Error;
8
9 require_once('Admin_Page.php');
10
11 class Helper extends Admin_Page {
12 function render_content() {
13 ?>
14 <form method="post" id="photonic-helper-form">
15 <table class="photonic-helper">
16 <thead>
17 <tr>
18 <th class="photonic-helper-header" colspan="2">Flickr</th>
19 </tr>
20 </thead>
21 <tbody>
22 <tr>
23 <th class="photonic-helper-row-header" scope="row"><?php esc_html_e('Find your User ID', 'photonic'); ?></th>
24 <td><?php $this->display_flickr_id_helper(); ?></td>
25 </tr>
26 <tr>
27 <th class="photonic-helper-row-header" scope="row"><?php esc_html_e('Find your Group ID', 'photonic'); ?></th>
28 <td><?php $this->display_flickr_group_helper(); ?></td>
29 </tr>
30 </tbody>
31 </table>
32
33 <table class="photonic-helper">
34 <thead>
35 <tr>
36 <th class="photonic-helper-header" colspan="2">Google Photos</th>
37 </tr>
38 </thead>
39 <tbody>
40 <tr>
41 <th class="photonic-helper-row-header" scope="row"><?php esc_html_e('Find your Album IDs', 'photonic'); ?></th>
42 <td><?php $this->display_google_photos_album_helper(); ?></td>
43 </tr>
44 </tbody>
45 </table>
46
47 <table class="photonic-helper">
48 <thead>
49 <tr>
50 <th class="photonic-helper-header" colspan="2">SmugMug</th>
51 </tr>
52 </thead>
53 <tbody>
54 <tr>
55 <th class="photonic-helper-row-header" scope="row"><?php esc_html_e('Find your Album and Folder IDs', 'photonic'); ?></th>
56 <td><?php $this->display_smugmug_album_id_helper(); ?></td>
57 </tr>
58 </tbody>
59 </table>
60
61 <table class="photonic-helper">
62 <thead>
63 <tr>
64 <th class="photonic-helper-header" colspan="2">Zenfolio</th>
65 </tr>
66 </thead>
67 <tbody>
68 <tr>
69 <th class="photonic-helper-row-header" scope="row"><?php esc_html_e('Categories', 'photonic'); ?></th>
70 <td><?php $this->display_zenfolio_category_helper(); ?></td>
71 </tr>
72 </tbody>
73 </table>
74 </form>
75 <?php
76 }
77
78 function display_flickr_id_helper() {
79 global $photonic_flickr_api_key;
80 if (empty($photonic_flickr_api_key)) {
81 echo sprintf(esc_html__('Please set up your Flickr API Key under %s', 'photonic'), '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings</em>');
82 }
83 else {
84 echo '<label>'.esc_html__('Enter your Flickr photostream URL and click "Find"', 'photonic');
85 echo '<input type="text" value="https://www.flickr.com/photos/username/" id="photonic-flickr-user" name="photonic-flickr-user"/>';
86 echo '</label>';
87 echo '<input type="button" value="'.esc_attr__('Find', 'photonic').'" id="photonic-flickr-user-find" class="button button-primary"/>';
88 echo '<div class="result">&nbsp;</div>';
89 }
90 }
91
92 function display_flickr_group_helper() {
93 global $photonic_flickr_api_key;
94 if (empty($photonic_flickr_api_key)) {
95 echo sprintf(esc_html__('Please set up your Flickr API Key under %s', 'photonic'), '<em>Photonic &rarr; Settings &rarr; Flickr &rarr; Flickr Settings</em>');
96 }
97 else {
98 echo '<label>'.esc_html__('Enter your Flickr group URL and click "Find"', 'photonic');
99 echo '<input type="text" value="https://www.flickr.com/groups/groupname/" id="photonic-flickr-group" name="photonic-flickr-group"/>';
100 echo '</label>';
101 echo '<input type="button" value="'.esc_attr__('Find', 'photonic').'" id="photonic-flickr-group-find" class="button button-primary"/>';
102 echo '<div class="result">&nbsp;</div>';
103 }
104 }
105
106 function display_google_photos_album_helper() {
107 global $photonic_google_client_id, $photonic_google_client_secret, $photonic_google_refresh_token;
108 //global $photonic_google_use_own_keys;
109 // if (!empty($photonic_google_use_own_keys) && (empty($photonic_google_client_id) || empty($photonic_google_client_secret))) {
110 if (empty($photonic_google_client_id) || empty($photonic_google_client_secret)) {
111 echo sprintf(esc_html__('Please set up your Google Client ID and Client Secret under %s', 'photonic'),
112 '<em>Photonic &rarr; Settings &rarr; Google Photos &rarr; Google Photos Settings</em>');
113 }
114 else if (empty($photonic_google_refresh_token)) {
115 echo sprintf(esc_html__('Please obtain your Refresh Token and save it under %s', 'photonic'),
116 '<em>Photonic &rarr; Settings &rarr; Google Photos &rarr; Google Photos Settings</em>');
117 }
118 else {
119 echo esc_html__("Clicking on the button below will show all albums for the authenticated user.", 'photonic').'<br/>';
120 echo '<input type="button" value="'.esc_attr__('Find my albums', 'photonic').'" id="photonic-google-album-find" class="button button-primary"/>';
121 echo '<div class="result">&nbsp;</div>';
122 }
123 }
124
125 function display_smugmug_album_id_helper() {
126 global $photonic_smug_api_key;
127 if (empty($photonic_smug_api_key)) {
128 echo sprintf(esc_html__('Please set up your SmugMug API Key under %s', 'photonic'), '<em>Photonic &rarr; Settings &rarr; SmugMug &rarr; SmugMug Settings</em>');
129 }
130 else {
131 echo '<label>'.esc_html__('Enter your SmugMug username and click "Find"', 'photonic');
132 echo '<input type="text" value="username" id="photonic-smugmug-user" name="photonic-smugmug-user"/>';
133 echo '</label>';
134 echo '<input type="button" value="'.esc_attr__('Find', 'photonic').'" id="photonic-smugmug-user-tree" class="button button-primary"/>';
135 echo '<div class="result">&nbsp;</div>';
136 }
137 }
138
139 function display_zenfolio_category_helper() {
140 echo esc_html__('Click "List" to find all available Zenfolio categories.', 'photonic').'<br/>';
141 echo '<input type="button" value="'.esc_attr__('List', 'photonic').'" id="photonic-zenfolio-categories-find" class="button button-primary"/>';
142 echo '<div class="result">&nbsp;</div>';
143 }
144
145 function invoke_helper() {
146 if (isset($_POST['helper']) && !empty($_POST['helper'])) {
147 $helper = sanitize_text_field($_POST['helper']);
148 $photonic_options = get_option('photonic_options');
149 switch ($helper) {
150 case 'photonic-flickr-user-find':
151 $flickr_api_key = $photonic_options['flickr_api_key'];
152 $user = isset($_POST['photonic-flickr-user']) ? sanitize_text_field($_POST['photonic-flickr-user']) : '';
153 $url = 'https://api.flickr.com/services/rest/?format=json&nojsoncallback=1&api_key='.$flickr_api_key.'&method=flickr.urls.lookupUser&url='.$user;
154 $this->execute_query('flickr', $url, 'flickr.urls.lookupUser');
155 break;
156
157 case 'photonic-flickr-group-find':
158 $flickr_api_key = $photonic_options['flickr_api_key'];
159 $group = isset($_POST['photonic-flickr-group']) ? sanitize_text_field($_POST['photonic-flickr-group']) : '';
160 $url = 'https://api.flickr.com/services/rest/?format=json&nojsoncallback=1&api_key='.$flickr_api_key.'&method=flickr.urls.lookupGroup&url='.$group;
161 $this->execute_query('flickr', $url, 'flickr.urls.lookupGroup');
162 break;
163
164 case 'photonic-smugmug-user-tree':
165 $smugmug_api_key = $photonic_options['smug_api_key'];
166 $user = isset($_POST['photonic-smugmug-user']) ? sanitize_text_field($_POST['photonic-smugmug-user']) : '';
167 if (!empty($user)) {
168 require_once(PHOTONIC_PATH.'/Modules/SmugMug.php');
169 $module = SmugMug::get_instance();
170
171 $config = $module->get_config_object(100);
172 $config['expand']['Node'] = [];
173
174 //$api_call = 'https://api.smugmug.com/api/v2/user/' . $user . '?_expand=Node.ChildNodes.ChildNodes.ChildNodes.ChildNodes.ChildNodes';
175 $api_call = 'https://api.smugmug.com/api/v2/user/' . $user;
176 $args = [
177 'APIKey' => $smugmug_api_key,
178 '_accept' => 'application/json',
179 '_verbosity' => 1,
180 '_expandmethod' => 'inline',
181 'count' => 20
182 ];
183
184 $response = Photonic::http($api_call, 'GET', $args);
185 if (!is_wp_error($response)) {
186 $body = $response['body'];
187 $body = json_decode($body);
188 if ($body->Code === 200) {
189 $body = $body->Response;
190 if (isset($body->User) && isset($body->User->Uris) && isset($body->User->Uris->Node)) {
191 $node = $body->User->Uris->Node->Uri;
192 $node = explode('/', $node);
193 $node = array_pop($node);
194 $api_call = 'https://api.smugmug.com/api/v2/node/' . $node . '?_config='.json_encode($config);
195
196 if ($module->oauth_done) {
197 $args = $module->sign_call($api_call, 'GET', $args);
198 }
199 else {
200 echo sprintf(esc_html__("If you have protected albums, you will have to %1sauthenticate%2s to see the protected albums.", 'photonic'), "<a href='".admin_url('admin.php?page=photonic-auth')."'>", "</a>");
201 }
202 $response = Photonic::http($api_call, 'GET', $args);
203 if (!is_wp_error($response)) {
204 $this->process_smugmug_response($response);
205 }
206 else {
207 echo $this->get_wp_errors($response);
208 }
209 }
210 }
211 }
212 else {
213 echo $this->get_wp_errors($response);
214 }
215
216 }
217 break;
218
219 case 'photonic-google-album-find':
220 case 'photonic-google-album-more':
221 $url = 'https://photoslibrary.googleapis.com/v1/albums';
222
223 global $photonic_google_refresh_token;
224 require_once(PHOTONIC_PATH.'/Modules/Google_Photos.php');
225 $module = Google_Photos::get_instance();
226 $module->authenticate($photonic_google_refresh_token);
227 if (!empty($module->access_token)) {
228 $query_args = [
229 'access_token' => $module->access_token,
230 'pageSize' => 50,
231 ];
232 if (!empty($_POST['nextPageToken'])) {
233 $query_args['pageToken'] = $_POST['nextPageToken'];
234 }
235
236 $url = add_query_arg(
237 $query_args,
238 $url);
239 }
240
241 $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
242 if (!is_wp_error($response) && $response['response']['code'] == 200) {
243 $response = $response['body'];
244 $this->process_google_response($response, !empty($_POST['nextPageToken']));
245 }
246 else {
247 echo esc_html__("Encountered error: ")."<br/>";
248 echo $this->get_wp_errors($response);
249 }
250
251 break;
252
253 case 'photonic-zenfolio-categories-find':
254 $url = 'https://api.zenfolio.com/api/1.8/zfapi.asmx/GetCategories';
255 $this->execute_query('zenfolio', $url, 'GetCategories');
256 break;
257 }
258 }
259 die();
260 }
261
262 function execute_query($where, $url, $method) {
263 $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
264 if (!is_wp_error($response)) {
265 if (isset($response['response']) && isset($response['response']['code'])) {
266 if ($response['response']['code'] == 200) {
267 if (isset($response['body'])) {
268 if ($where == 'flickr') {
269 $this->execute_flickr_query($response['body'], $method);
270 }
271 else if ($where == 'zenfolio') {
272 $this->execute_zenfolio_query($response['body'], $method);
273 }
274 }
275 else {
276 echo '<span class="found-id-text">'.esc_html__('No response from server!', 'photonic').'</span>';
277 }
278 }
279 else {
280 echo '<span class="found-id-text">'.$response['response']['message'].'</span>';
281 }
282 }
283 else {
284 echo '<span class="found-id-text">'.esc_html__('No response from server!', 'photonic').'</span>';
285 }
286 }
287 else {
288 echo $this->get_wp_errors($response);
289 }
290 }
291
292 function execute_flickr_query($body, $method) {
293 $body = json_decode($body);
294 if (isset($body->stat) && $body->stat == 'fail') {
295 echo '<span class="found-id-text">'.$body->message.'</span>';
296 }
297 else {
298 if ($method == 'flickr.urls.lookupUser') {
299 if (isset($body->user)) {
300 echo '<span class="found-id-text">'.esc_html__('User ID:', 'photonic').'</span> <span class="found-id"><code>'.$body->user->id.'</code></span>';
301 }
302 }
303 else if ($method == 'flickr.urls.lookupGroup') {
304 if (isset($body->group)) {
305 echo '<span class="found-id-text">'.esc_html__('Group ID:', 'photonic').'</span> <span class="found-id"><code>'.$body->group->id.'</code></span>';
306 }
307 }
308 }
309 }
310
311 function process_smugmug_response($response) {
312 $body = $response['body'];
313 $body = json_decode($body);
314
315 if ($body->Code === 200) {
316 $body = $body->Response;
317 if (isset($body->Node)) {
318 $node = $body->Node;
319 if ($node->Type == 'Folder') {
320 $ret = $this->process_smugmug_node($node);
321 if (!empty($ret)) {
322 $ret = "<table class='photonic-helper-table'>\n".
323 "\t<tr>\n".
324 "\t<th>Name</th>\n".
325 "\t<th>Type</th>\n".
326 "\t<th>Thumbnail</th>\n".
327 "\t<th>Album Key</th>\n".
328 "\t<th>Security Level</th>\n".
329 "\t</tr>\n".
330 $ret.
331 "</table>\n";
332 echo $ret;
333 }
334 }
335 }
336 }
337 }
338
339 function process_smugmug_node($node) {
340 $ret = '';
341 if ($node->Type == 'Folder') {
342 $albums = [];
343 $folders = [];
344 if (isset($node->Uris->ChildNodes->Node)) {
345 $child_nodes = $node->Uris->ChildNodes->Node;
346 foreach ($child_nodes as $child) {
347 if ($child->Type == 'Album') {
348 $albums[] = $child;
349 }
350 else if ($child->Type == 'Folder') {
351 $folders[] = $child;
352 }
353 }
354
355 foreach ($albums as $album) {
356 $ret .= "\t<tr>\n";
357 $ret .= "\t\t<td>{$album->Name}</td>\n";
358 $ret .= "\t\t<td>Album</td>\n";
359 $thumb = isset($album->Uris->NodeCoverImage->Image->ThumbnailUrl) ? $album->Uris->NodeCoverImage->Image->ThumbnailUrl : '';
360 $ret .= "\t\t<td>".(empty($thumb) ? '' : "<img src='$thumb' alt='Album thumbnail'/>")."</td>\n";
361 $album_key = isset($album->Uris->Album) ? $album->Uris->Album->Uri : '';
362 $album_key = explode('/', $album_key);
363 $album_key = $album_key[count($album_key) - 1];
364 $ret .= "\t\t<td>$album_key</td>\n";
365 $ret .= "\t\t<td>{$album->SecurityType}</td>\n";
366 $ret .= "\t</tr>\n";
367 }
368
369 foreach ($folders as $folder) {
370 $ret .= "\t<tr>\n";
371 $ret .= "\t\t<td>{$folder->Name}</td>\n";
372 $ret .= "\t\t<td>Folder</td>\n";
373 $thumb = isset($folder->Uris->NodeCoverImage->Image->ThumbnailUrl) ? $folder->Uris->NodeCoverImage->Image->ThumbnailUrl : '';
374 $ret .= "\t\t<td>".(empty($thumb) ? '' : "<img src='$thumb' alt='Folder thumbnail'/>")."</td>\n";
375 $ret .= "\t\t<td>{$folder->NodeID}</td>\n";
376 $ret .= "\t\t<td>{$folder->SecurityType}</td>\n";
377 $ret .= "\t</tr>\n";
378
379 $ret .= $this->process_smugmug_node($folder);
380 }
381 }
382 }
383 return $ret;
384 }
385
386 function process_google_response($response, $more = false) {
387 $response = json_decode($response);
388 if (!empty($response->albums) && is_array($response->albums)) {
389 $albums = $response->albums;
390 if (!$more) {
391 echo "<table class='photonic-helper-table'>\n";
392 echo "\t<tr>\n";
393 echo "\t\t<th>Album Title</th>\n";
394 echo "\t\t<th>Thumbnail</th>\n";
395 echo "\t\t<th>Album ID</th>\n";
396 echo "\t\t<th>Media Count</th>\n";
397 echo "\t</tr>\n";
398 }
399
400 foreach ($albums as $album) {
401 echo "\t<tr>\n";
402 echo "\t\t<td>".(empty($album->title)? '' : esc_attr($album->title))."</td>\n";
403 echo "\t\t<td><img src='{$album->coverPhotoBaseUrl}=w75-h75-c' alt='Album thumbnail'/></td>\n";
404 echo "\t\t<td>{$album->id}</td>\n";
405 echo "\t\t<td>{$album->mediaItemsCount}</td>\n";
406 echo "\t</tr>\n";
407 }
408
409 if (!empty($response->nextPageToken)) {
410 echo "\t<tr>\n";
411 echo "\t\t<td colspan='4'>\n";
412 echo '<input type="button" value="'.esc_attr__('Load More', 'photonic').'" id="photonic-google-album-more" class="button button-primary" data-photonic-token="'.$response->nextPageToken.'"/>';
413 echo "\t\t</td>\n";
414 echo "\t</tr>\n";
415 }
416
417 if (!$more) {
418 echo "</table>\n";
419 }
420 }
421 else {
422 esc_html_e("No albums found", 'photonic');
423 }
424 }
425
426 function execute_zenfolio_query($body, $method) {
427 if ($method == 'GetCategories') {
428 $response = simplexml_load_string($body);
429 if (!empty($response->Category)) {
430 $categories = $response->Category;
431 echo "<ul class='photonic-scroll-panel'>\n";
432 foreach ($categories as $category) {
433 echo "<li>{$category->DisplayName} &ndash; {$category->Code}</li>\n";
434 }
435 echo "</ul>\n";
436 }
437 }
438 }
439
440 /**
441 * @param $response WP_Error
442 * @return string
443 */
444 private function get_wp_errors($response) {
445 $err = '';
446 if (is_wp_error($response)) {
447 $messages = $response->get_error_messages();
448 $err = '<br/><strong>'.esc_html(sprintf(_n('%s Message:', '%s Messages:', count($messages), 'photonic'),
449 count($messages)))."</strong><br/>\n";
450 foreach ($messages as $message) {
451 $err .= $message . "<br>\n";
452 }
453 }
454 return $err;
455 }
456 }
457