/**
  * Imports galleries from WP Flickr Background
  *
  * @param object $main The object of the Main class
  */
 public static function doImport(Main $main)
 {
     global $wpdb;
     $options = get_option(static::WP_OPTION_NAME);
     $galleries = new Galleries($main);
     $images = new Images($main);
     // Turn how many gallery's we process into chunks for progress bar
     $chunks = ceil(100 / count($options['galleries']) - 1);
     $chunk = 0;
     foreach ($options['galleries'] as $wpfbg_gallery) {
         $image_set = sprintf(__('%s (Imported)', $main->getName()), $wpfbg_gallery['name']);
         $gallery_id = $galleries->save(0, $image_set, $wpfbg_gallery['desc']);
         if (!$gallery_id) {
             $main->addDelayedNotice(sprintf(__('Unable to create Image Set <strong>%s</strong>', $main->getName()), $image_set), true);
             continue;
         }
         // If we have custom CSS, add this as a meta to the gallery
         if (!empty($wpfbg_gallery['customcss'])) {
             add_post_meta($gallery_id, \Myatu\WordPress\BackgroundManager\Meta\Stylesheet::MT_CSS, $wpfbg_gallery['customcss'], true);
         }
         foreach ($wpfbg_gallery['photos'] as $photo) {
             if ($photo['id'][0] != 'L') {
                 // Images that do not start with an "L" are only available via a remote URL.
                 $r = media_sideload_image($photo['background'], $gallery_id);
                 if (is_wp_error($r)) {
                     $main->addDelayedNotice(sprintf(__('Unable to import image <em>%s</em> into Image Set <strong>%s</strong>', $main->getName()), $photo['background'], $image_set), true);
                 }
             } else {
                 // Strip any -DDDxDDD from the filename within the URL
                 $background_image_url = preg_replace('#^(.*?)(-\\d{2,4}x\\d{2,4}(?=\\.))(.*)$#', '$1$3', $photo['background']);
                 // Fetch the image ID from the posts/attachments
                 $background_image_id = $wpdb->get_var($wpdb->prepare("SELECT `ID` FROM `{$wpdb->posts}` WHERE `guid` = %s", $background_image_url));
                 // Change the parent of the image attachment to that of the gallery
                 if ($background_image_id && ($image = get_post($background_image_id))) {
                     $r = wp_insert_attachment($image, false, $gallery_id);
                 }
                 if (!$background_image_id || !$image || !$r) {
                     $main->addDelayedNotice(sprintf(__('Unable to import image <em>%s</em> into Image Set <strong>%s</strong>', $main->getName()), $background_image_url, $image_set), true);
                 }
             }
         }
         $chunk++;
         static::setProgress($chunk * $chunks);
     }
     // And voila!
     $main->addDelayedNotice(__('Completed import from WP Flickr Background', $main->getName()));
     unset($galleries);
     unset($images);
 }
Example #2
0
 /**
  * Imports galleries from NextGEN Gallery
  *
  * @param object $main The object of the Main class
  */
 public static function doImport(Main $main)
 {
     global $wpdb;
     // Just in case
     if (!isset($_REQUEST['gallery']) || empty($_REQUEST['gallery'])) {
         return;
     }
     $galleries = new Galleries($main);
     // Collect initial data
     $ngg_gallery = $wpdb->get_row($wpdb->prepare("SELECT `title`, `path`, `galdesc` FROM `{$wpdb->prefix}ngg_gallery` WHERE `gid` = %d", $_REQUEST['gallery']));
     $ngg_pictures = $wpdb->get_results($wpdb->prepare("SELECT `filename`, `alttext`, `description` FROM `{$wpdb->prefix}ngg_pictures` WHERE `galleryid` = %d", $_REQUEST['gallery']));
     if (!$ngg_gallery || !$ngg_pictures) {
         $main->addDelayedNotice(__('There was a problem obtaining NextGEN Gallery database details', $main->getName()), true);
         return;
     }
     // Create the image set
     $image_set = sprintf(__('%s (Imported)', $main->getName()), $ngg_gallery->title);
     $gallery_id = $galleries->save(0, $image_set, $ngg_gallery->galdesc);
     if (!$gallery_id) {
         $main->addDelayedNotice(sprintf(__('Unable to create Image Set <strong>%s</strong>', $main->getName()), $image_set), true);
         return;
     }
     // If we don't have any pictures to import, then finish here (so at least we have the gallery)
     if (count($ngg_pictures) == 0) {
         return;
     }
     // Turn how many gallery's we process into chunks for progress bar
     $chunks = ceil(100 / count($ngg_pictures) - 1);
     $chunk = 0;
     // Import the pictures
     foreach ($ngg_pictures as $ngg_picture) {
         $image_file = ABSPATH . trailingslashit($ngg_gallery->path) . $ngg_picture->filename;
         if (@is_file($image_file)) {
             if (!Images::importImage($image_file, $gallery_id, $ngg_picture->alttext, $ngg_picture->description, $ngg_picture->alttext)) {
                 $main->addDelayedNotice(sprintf(__('Unable to import image <em>%s</em> into Image Set <strong>%s</strong>', $main->getName()), $image_file, $image_set), true);
             }
         }
         $chunk++;
         static::setProgress($chunk * $chunks);
     }
     $main->addDelayedNotice(__('Completed import from NextGEN Gallery', $main->getName()));
     unset($galleries);
 }
Example #3
0
 /**
  * Performs the import
  *
  * @param mixed $main The object of the Main class
  */
 public static function doImport(Main $main)
 {
     $sub_dirs = !empty($_REQUEST['sub_dirs']);
     $root = trailingslashit(realpath(ABSPATH));
     $directory = realpath($root . $_REQUEST['directory']);
     $galleries = new Galleries($main);
     // Create the image set
     $desc = sprintf(__('Imported directory "%s"', $main->getName()), $_REQUEST['directory']);
     if ($sub_dirs) {
         $desc .= __(' and sub-directories', $main->getName());
     }
     $gallery_id = $galleries->save(0, 'Imported Directory', $desc);
     if (!$gallery_id) {
         $main->addDelayedNotice(sprintf(__('Unable to create Image Set <strong>%s</strong>', $main->getName()), $image_set), true);
         return;
     }
     // Import specified directory
     static::importDir($directory, $gallery_id, $main);
     // Import sub-directories, if requested
     if ($sub_dirs) {
         $iterator = new \RecursiveIteratorIterator(new \Pf4wp\Storage\IgnorantRecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
         foreach ($iterator as $fileinfo) {
             if ($fileinfo->isDir()) {
                 static::importDir($fileinfo, $gallery_id, $main);
             }
         }
     }
     $main->addDelayedNotice(__('Completed import from Local Directory', $main->getName()));
     unset($galleries);
 }
Example #4
0
 /**
  * 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()));
 }
Example #5
0
 /**
  * Called when the plugin is activated
  *
  * This will import the original background into a new image set.
  */
 public function onActivation()
 {
     global $wpdb;
     // Retrieve the background image URL and ID, or return if none specified
     if (!($background_image_url = get_theme_mod('background_image')) || !($background_image_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM `{$wpdb->posts}` WHERE `guid` = %s", $background_image_url)))) {
         return;
     }
     $galleries = new Galleries($this);
     // Create a new gallery to hold the original background.
     $gallery_id = $galleries->save(0, __('Imported Background'), __('Automatically created Image Set, containing the original background image specified in WordPress.'));
     // If we created a valid gallery, activate it, add the original background image and remove the theme modification.
     if ($gallery_id && ($image = get_post($background_image_id))) {
         $image->post_content = '';
         // Clear the URL from the content, as this will display in the info tab otherwise.
         wp_insert_attachment($image, false, $gallery_id);
         // Causes an update instead, as image->ID is set
         remove_theme_mod('background_image');
         // Set the gallery to the active one
         $this->options->active_gallery = $gallery_id;
     }
     unset($galleries);
 }