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