/** * Pre-import settings * * Allows the selection of a particular gallery */ public static function preImport(Main $main) { if (isset($_REQUEST['flickr_photoset']) && !empty($_REQUEST['flickr_photoset'])) { return; } // A photoset has been selected $flickr = new FlickrApi($main); $vars = array(); $tokens = false; // Domain $domain = ''; if (preg_match('#^http[s]?:\\/\\/.+?(?=\\/|$)#i', get_site_url(), $matches)) { list($domain) = $matches; } // Callback URL $importer = is_array($class = explode('\\', get_class())) ? $class[count($class) - 1] : 'Flickr'; $callback_url = $domain . add_query_arg(array('logout' => false, 'importer' => $importer, '_nonce' => wp_create_nonce('onImportMenu'))); // Logout URL $vars['logout_url'] = add_query_arg(array('logout' => true), $callback_url); // Perform logout, if requested if (isset($_REQUEST['logout'])) { $flickr->deleteAccessTokens(); } // If we do not have valid access tokens, ask the user what to do. if (!$flickr->hasValidAccessTokens()) { if (!isset($_REQUEST['do_login'])) { // We have not been authorized to access Flickr, except public side. Ask the user what to do. $vars['ask_auth'] = true; } else { if ($do_login = $_REQUEST['do_login'] == 'yes') { // User has decided to login to Flickr $url = $flickr->getAuthorizeUrl($callback_url); if ($url) { $vars['auth_redir'] = $url; } else { // Something went wrong, repeat the process (asking what the user wants to do) $vars['errors'] = __('Unable to obtain a Flickr authorization URL. Please try again later.', $main->getName()); $vars['ask_auth'] = true; } } else { // User has decided to continue anonymously, clear any invalid tokens if present $flickr->deleteAccessTokens(); } } } else { // Valid access tokens, we grab the tokens, which contains a username $tokens = $flickr->getAccessTokens(); if ($tokens) { $vars['username'] = $tokens['username']; } } // Set anonymous flag $vars['anonymous'] = $tokens === false; // Set a username from whom we obtain the photoset $flickr_username = isset($_REQUEST['flickr_username']) ? $_REQUEST['flickr_username'] : ''; $vars['flickr_username'] = $flickr_username; // If we have a username specified, or are using our authorized tokens... if ($tokens || $flickr_username) { $photoset_list = false; if ($tokens && (empty($flickr_username) || strcasecmp($flickr_username, $tokens['username']) == 0)) { // Obtain photoset list from authenticated user $photoset_list = $flickr->call('photosets.getList'); } else { // Obtain photoset list from another user $flickr_id = false; if (!strpos($flickr_username, '@')) { // @ cannot be the first match, so safe to use a "!" // Obtain NSID by Username $flickr_id_result = $flickr->call('people.findByUsername', array('username' => $flickr_username)); if ($flickr->isValid($flickr_id_result) && isset($flickr_id_result['user']['nsid'])) { $flickr_id = $flickr_id_result['user']['nsid']; } } else { // Obtain NSID by ID $flickr_id_result = $flickr->call('people.getInfo', array('user_id' => $flickr_username)); if ($flickr->isValid($flickr_id_result) && isset($flickr_id_result['person']['nsid'])) { $flickr_id = $flickr_id_result['person']['nsid']; } // This is redundant, I know } if ($flickr_id) { $photoset_list = $flickr->call('photosets.getList', array('user_id' => $flickr_id)); } else { $vars['errors'] = sprintf(__('"%s" is not a valid Flickr user.', $main->getName()), $flickr_username); } } if ($flickr->isValid($photoset_list) && isset($photoset_list['photosets'])) { // Flickr reserves the option to return paginated results. if (isset($photoset_list['photosets']['photoset']) && is_array($photoset_list['photosets']['photoset'])) { foreach ($photoset_list['photosets']['photoset'] as $photoset) { $vars['photosets'][$photoset['id']] = sprintf('%s (%d)', $photoset['title']['_content'], $photoset['photos']); } } // Sort the array if (isset($vars['photosets'])) { asort($vars['photosets']); } } } return $main->template->render('importer_flickr.html.twig', $vars); }