/** * Performs the import from Flickr * * @param object $main The object of the Main class */ public static function doImport(Main $main) { // Just in case if (!isset($_REQUEST['flickr_photoset']) || empty($_REQUEST['flickr_photoset'])) { return; } $galleries = new Galleries($main); $images = new Images($main); $flickr = new FlickrApi($main); // Create local Image Set if ($flickr->isValid($photoset_info = $flickr->call('photosets.getInfo', array('photoset_id' => $_REQUEST['flickr_photoset'])))) { $image_set = sprintf(__('%s (Imported)', $main->getName()), $photoset_info['photoset']['title']['_content']); $gallery_id = $galleries->save(0, $image_set, $photoset_info['photoset']['description']['_content']); if (!$gallery_id) { $main->addDelayedNotice(sprintf(__('Unable to create Image Set <strong>%s</strong>', $main->getName()), $image_set), true); return; } } else { $main->addDelayedNotice(__('Invalid or inaccessible Flickr Photo Set selected', $main->getName()), true); return; } $page = 1; $pb_chunk = 0; $failed = 0; // Iterate photos on Flickr while ($flickr->isValid($photoset = $flickr->call('photosets.getPhotos', array('photoset_id' => $_REQUEST['flickr_photoset'], 'media' => 'photos', 'page' => $page))) && isset($photoset['photoset'])) { $photoset = $photoset['photoset']; $pages = $photoset['pages']; $total = $photoset['total']; $pb_chunks = ceil(100 / $total - 1); // For progress bar // Iterate each photo in current 'page' foreach ($photoset['photo'] as $photo) { $image_url = ''; $description = ''; $title = $photo['title']; $can_download = true; // Attempt to obtain additional information about the photo, including the license if ($flickr->isValid($info = $flickr->call('photos.getInfo', array('photo_id' => $photo['id'], 'secret' => $photo['secret']))) && isset($info['photo'])) { $info = $info['photo']; $license_info = $flickr->getLicenseById($info['license']); // Obtain license details $can_download = $info['usage']['candownload'] == 1; $description = sprintf(__('<p>%s</p><p>By: <a href="http://www.flickr.com/photos/%s/%s/">%s</a> (%s)</p>', $main->getName()), $info['description']['_content'], $info['owner']['nsid'], $info['id'], $info['owner']['username'], !empty($license_info['url']) ? sprintf('<a href="%s">%s</a>', $license_info['url'], $license_info['name']) : $license_info['name']); } // Select the largest size available to us if ($can_download && $flickr->isValid($sizes = $flickr->call('photos.getSizes', array('photo_id' => $photo['id'])))) { $current_w = 0; $current_h = 0; foreach ($sizes['sizes']['size'] as $size) { if ($size['width'] > $current_w || $size['height'] > $current_h) { $image_url = $size['source']; $current_w = $size['width']; $current_h = $size['height']; } } } // If we have an URL, download it and insert the photo into the local Image Set if (!empty($image_url)) { if (!Images::importImage($image_url, $gallery_id, $title, $description)) { $failed++; } } // Update progress bar $pb_chunk++; static::setProgress($pb_chunk * $pb_chunks); } // Go to next page of photos on Flickr if ($page < $pages) { $page++; } else { break; } } if ($failed > 0) { $main->addDelayedNotice(sprintf(__('%d photos could not be added.', $main->getName()), $failed), true); } $main->addDelayedNotice(__('Completed import from Flickr', $main->getName())); }
/** * Handles the 'Download From URL' tab * * This will close the TB Frame if the photo is 'inserted' (saved), or download * the image from an external source if 'submitted'. If the image comes from Flickr, * it will also attempt to obtain more details about it through the Flickr API. * * @since 1.0.8 * This works different from the WordPress 'From URL' tab, is it will * download the image locally and place it directly in the Media Library */ public function onBgmUrl() { $errors = false; $attachment_id = isset($_POST['attachment_id']) ? $_POST['attachment_id'] : 0; // Save any changes made, then close the TB Frame if successful if (isset($_POST['insert'])) { $errors = media_upload_form_handler(); if (!$errors) { echo '<html><head><script type="text/javascript">/* <![CDATA[ */ var win=window.dialogArguments||opener||parent||top; win.tb_remove(); /* ]]> */</script></head><body></body></html>'; die; } } // Attempt to download and save the image if (isset($_POST['submit']) && current_user_can('upload_files')) { check_admin_referer('media-form'); // die() if invalid NONCE $gallery_id = (int) $_POST['post_id']; $image_url = trim($_POST['url']); if (!empty($image_url)) { $title = ''; $description = ''; // Check if it's a Flickr URL if (preg_match('#^http[s]?://farm\\d{1,3}\\.(?:staticflickr|static\\.flickr).com\\/\\d+\\/(\\d+)_.+\\.(?:jpg|png|gif)$#i', $image_url, $matches)) { $flickr_photo_id = $matches[1]; // Obtain some more details about the image from Flickr $flickr = new FlickrApi($this->owner); if ($flickr->isValid($info = $flickr->call('photos.getInfo', array('photo_id' => $flickr_photo_id))) && isset($info['photo'])) { $info = $info['photo']; $title = $info['title']['_content']; $license_info = $flickr->getLicenseById($info['license']); $description = sprintf(__('<p>%s</p><p>By: <a href="http://www.flickr.com/photos/%s/%s/">%s</a> (%s)</p>', $this->owner->getName()), $info['description']['_content'], $info['owner']['nsid'], $info['id'], $info['owner']['username'], !empty($license_info['url']) ? sprintf('<a href="%s">%s</a>', $license_info['url'], $license_info['name']) : $license_info['name']); } unset($flickr); } $attachment_id = Images::importImage($image_url, $gallery_id, $title, $description); if (!$attachment_id) { $errors = __('Unable to import image at specified URL', $this->owner->getName()); } } } // Display the form wp_enqueue_style('media'); // Either this, or give callback function a funky 'media_' prefix >_< return wp_iframe(array($this, 'onBgmUrlForm'), $errors, $attachment_id); }