Example #1
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);
 }
 /**
  * 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 #3
0
 /** Images Iframe */
 public static function showIframeImages(Main $owner)
 {
     if (!defined('IFRAME_REQUEST')) {
         define('IFRAME_REQUEST', true);
     }
     if (!isset($owner->gallery->ID)) {
         die;
     }
     // Something didn't go quite right
     // Only if Javascript is disabled will we get here, which adds a image to the gallery directly
     if (!empty($_POST) && isset($_POST['_nonce'])) {
         if (!wp_verify_nonce($_POST['_nonce'], 'image-upload')) {
             wp_die(__('You do not have permission to do that [nonce].', $owner->getName()));
         }
         // Check if there's a valid image, and if so, let the Media Library handle the upload
         if (!empty($_FILES) && $_FILES['upload_file']['error'] == 0 && file_is_valid_image($_FILES['upload_file']['tmp_name'])) {
             media_handle_upload('upload_file', $owner->gallery->ID);
         }
     }
     iframe_header();
     $items_per_page = isset($_GET['pp']) ? $_GET['pp'] : 30;
     $page_num = isset($_GET['paged']) ? $_GET['paged'] : 1;
     // Grab the total amount of items (images) and figure out how many pages that is
     $total_items = $owner->images->getCount($owner->gallery->ID);
     if (($total_pages = ceil($total_items / $items_per_page)) < 1) {
         $total_pages = 1;
     }
     // Get a valid page number
     if ($page_num > $total_pages) {
         $page_num = $total_pages;
     } else {
         if ($page_num < 1) {
             $page_num = 1;
         }
     }
     // Grab the images
     $images = $owner->images->get($owner->gallery->ID, array('orderby' => 'menu_order', 'order' => 'asc', 'numberposts' => $items_per_page, 'offset' => ($page_num - 1) * $items_per_page));
     // The page links (for non-JS browsers)
     $page_links = paginate_links(array('base' => add_query_arg('paged', '%#%'), 'format' => '', 'prev_text' => __('&laquo;'), 'next_text' => __('&raquo;'), 'total' => $total_pages, 'current' => $page_num));
     $vars = array('images' => $images, 'current_page' => $page_num, 'page_links' => $page_links, 'nonce' => wp_nonce_field('image-upload', '_nonce', false, false));
     $owner->template->display('gallery_image.html.twig', $vars);
     iframe_footer();
     die;
 }
Example #4
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 #5
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()));
 }