WordPress.org

Ready to get started?Download WordPress

Forums

NextCellent Gallery - NextGEN Legacy
Import multiple image folders (6 posts)

  1. mcfester
    Member
    Posted 4 months ago #

    A hack was made to import multiple image folders but it only works on version 1.4.3, is it possible to code this hack for nextcellent?
    http://wordpress.org/support/topic/plugin-nextgen-gallery-mass-batch-import-or-upload?replies=15

    https://wordpress.org/plugins/nextcellent-gallery-nextgen-legacy/

  2. Iltud
    Member
    Posted 4 months ago #

    Hi all,

    I coded this hack today but slightly differently : in the "Import Folder" tab, I added a checkbox in front of each folder in the folder tree. So, you can easily choose the folders instead of typing a comma separated list.

    My hack involves 3 files :

    • /wp-content/plugins/nextcellent-gallery-nextgen-legacy/admin/ajax.php --> adding of checkbox (and some dirty inline CSS to align checkbox)
    • /wp-content/plugins/nextcellent-gallery-nextgen-legacy/admin/addgallery.php --> managing of the array of checkbox (and some typos)
    • /wp-content/plugins/nextcellent-gallery-nextgen-legacy/admin/functions.php --> adding of function import_galleries($folders_list) which is mainly a copy of import_gallery($galleryfolder) with a "foreach" and hack for the thumbnails creation

    The first hack in the old post had a bug : the thumbnails are created for only one gallery of the list.

    My hack works better : I've imported almost 100 galleries and created +3250 thumbnails in only one shot. The thumbnails creation is currently running and will take 4 hours :-)

    In NextCellent, the thumbnails are created via Ajax : there is no timeout problem. The max execution time of PHP could be perhaps problematic for the creation of galleries in database, so I recommand to import folders by chunks of 10 or 20.

    Here are the diffs for NextCellent 1.9.16 :

    --- ajax_original.php
    +++ ajax.php
    @@ -270,7 +270,7 @@
                         continue;
    
                                    if ( file_exists($root . $dir . $file) && $file != '.' && $file != '..' && is_dir($root . $dir . $file) ) {
    -                                       echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . esc_html($dir . $file) . "/\">" . esc_html($file) . "</a></li>";
    +                                       echo "<li class=\"directory collapsed\" style=\"background-position:1px;display: block;padding-left:20px;text-indent:0px\"><input style=\"position:relative;top:2px;padding: 0;margin:0;float:left;vertical-align:bottom;*overflow: hidden;\" type=\"checkbox\" name=\"multiplefolder[]\" value=\"" . esc_html($dir . $file) . "\"><a href=\"#\" style=\"\" rel=\"" . esc_html($dir . $file) . "/\">" . esc_html($file) . "</a></li>";
                                    }
                            }
    --- addgallery_original.php
    +++ addgallery.php
    @@ -57,9 +57,12 @@
                    if ( !nggGallery::current_user_can( 'NextGEN Import image folder' ))
                            wp_die(__('Cheatin’ uh?'));
    
    -               $galleryfolder = $_POST['galleryfolder'];
    -               if ( ( !empty($galleryfolder) ) AND ($defaultpath != $galleryfolder) )
    -                       nggAdmin::import_gallery($galleryfolder);
    +                       $folders_list = $_POST['multiplefolder'];
    +                       if(empty($folders_list)) {
    +                               $folders_list[] = $_POST['galleryfolder'];
    +                       }
    +
    +                       nggAdmin::import_galleries($folders_list);
            }
    
            if ( isset($_POST['uploadimage']) ){
    @@ -377,7 +380,7 @@
                            $tabs['zipupload'] = __('ZIP file', 'nggallery');
    
             if ( wpmu_enable_function('wpmuImportFolder') && nggGallery::current_user_can( 'NextGEN Import image folder' ) )
    -                       $tabs['importfolder'] = __('Import folder', 'nggallery');
    +                       $tabs['importfolder'] = __('Import folder(s)', 'nggallery');
    
            $tabs = apply_filters('ngg_addgallery_tabs', $tabs);
    
    @@ -452,7 +455,7 @@
         function tab_importfolder() {
         ?>
            <!-- import folder -->
    -       <h3><?php _e('Import an image folder', 'nggallery') ;?></h3>
    +       <h3><?php _e('Import one or more image folders', 'nggallery') ;?></h3>
                    <form name="importfolder" id="importfolder_form" method="POST" action="<?php echo $this->filepath.'#importfolder'; ?>" accept-charset="utf-8" >
                    <?php wp_nonce_field('ngg_addgallery') ?>
                            <table class="form-table">
    --- functions_original.php
    +++ functions.php
    @@ -223,6 +223,117 @@
    
            }
    
    +
    +       /**
    +        * nggAdmin::import_galleries()
    +        * TODO: Check permission of existing thumb folder & images
    +        *
    +        * @class nggAdmin
    +        * @param array $folders_list contains an array of relatives paths to the galleries themselves
    +        * @return void
    +        */
    +       static function import_galleries($folders_list) {
    +
    +               global $wpdb, $ngg, $user_ID;
    +
    +               // get the current user ID
    +               get_currentuserinfo();
    +
    +               $defaultpath = $ngg->options['gallerypath'];
    +               $imageslistforthumbnails = array();
    +               foreach ($folders_list as $galleryfolder) {
    +                       if ( ( !empty($galleryfolder) ) AND ($defaultpath != $galleryfolder) ) {
    +
    +                               $created_msg = '';
    +
    +                               // remove trailing slash at the end, if somebody use it
    +                               $galleryfolder = untrailingslashit($galleryfolder);
    +                               $gallerypath = WINABSPATH . $galleryfolder;
    +
    +                               if (!is_dir($gallerypath)) {
    +                                       nggGallery::show_error(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) .'</strong> '.__('doesn`t exist!', 'nggallery'));
    +                                       return ;
    +                               }
    +
    +                               // read list of images
    +                               $new_imageslist = nggAdmin::scandir($gallerypath);
    +
    +                               if (empty($new_imageslist)) {
    +                                       nggGallery::show_message(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) . '</strong> '.__('contains no pictures', 'nggallery'));
    +                                       return;
    +                               }
    +
    +                               // check & create thumbnail folder
    +                               if ( !nggGallery::get_thumbnail_folder($gallerypath) )
    +                                       return;
    +
    +                               // take folder name as gallery name
    +                               $galleryname = basename($galleryfolder);
    +                               $galleryname = apply_filters('ngg_gallery_name', $galleryname);
    +
    +                               // check for existing gallery folder
    +                               $gallery_id = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE path = '$galleryfolder' ");
    +
    +                               if (!$gallery_id) {
    +                                       // now add the gallery to the database
    +                                       $gallery_id = nggdb::add_gallery( $galleryname, $galleryfolder, '', 0, 0, $user_ID );
    +                                       if (!$gallery_id) {
    +                                               nggGallery::show_error(__('Database error. Could not add gallery!','nggallery'));
    +                                               return;
    +                                       }
    +                                       $created_msg = _n( 'Gallery', 'Galleries', 1, 'nggallery' ) . ' <strong>' . esc_html( $galleryname ) . '</strong> ' . __('successfully created!','nggallery') . '<br />';
    +                               }
    +
    +                               // Look for existing image list
    +                               $old_imageslist = $wpdb->get_col("SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery_id' ");
    +
    +                               // if no images are there, create empty array
    +                               if ($old_imageslist == NULL)
    +                                       $old_imageslist = array();
    +
    +                               // check difference
    +                               $new_images = array_diff($new_imageslist, $old_imageslist);
    +
    +                               // all images must be valid files
    +                               foreach($new_images as $key => $picture) {
    +
    +                                       // filter function to rename/change/modify image before
    +                                       $picture = apply_filters('ngg_pre_add_new_image', $picture, $gallery_id);
    +                                       $new_images[$key] = $picture;
    +
    +                                       if (!@getimagesize($gallerypath . '/' . $picture) ) {
    +                                               unset($new_images[$key]);
    +                                               @unlink($gallerypath . '/' . $picture);
    +                                       }
    +                               }
    +
    +                               // add images to database
    +                               $image_ids = nggAdmin::add_Images($gallery_id, $new_images);
    +
    +                               //add the preview image if needed
    +                               nggAdmin::set_gallery_preview ( $gallery_id );
    +
    +                               // Add images IDs to $imageslistforthumbnails
    +                               $imageslistforthumbnails = array_merge($imageslistforthumbnails, $image_ids);
    +                               //nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails','nggallery') );
    +
    +                               //TODO:Message will not shown, because AJAX routine require more time, message should be passed to AJAX
    +                               $message  = $created_msg . count($image_ids) .__(' picture(s) successfully added','nggallery');
    +                               $message .= ' [<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $gallery_id . '" >';
    +                               $message .=  __('Edit gallery','nggallery');
    +                               $message .= '</a>]';
    +
    +                               nggGallery::show_message($message);
    +                       }
    +               }
    +
    +               // now create ALL thumbnails
    +               nggAdmin::do_ajax_operation( 'create_thumbnail' , $imageslistforthumbnails, __('Create new thumbnails','nggallery') );
    +
    +               return;
    +       }
    +
    +
            /**
             * Scan folder for new images
             *
  3. mcfester
    Member
    Posted 4 months ago #

    What a great idea with the check box!
    It'll be a few days before I can play around with this and report back, but thanks ever so much.

    Any chance you can post the full code of the edited files so I can just copy paste and upload? This + - stuff has lost me already lol.

    Another feature missing on nextcellent is bulk opitmize images on (manage) galleries page and (edit) gallery page with EWWW Image Optimizer.

    Also a bug when manually editing a thumb doesn't obey the quality setting, file size is huge. I just did two thumbs that where 12KB and 17KB, after manual edit they are 45KB and 56KB. Big problem for me as I need to edit all preview thumbs and it makes the album page load time huge.

    Should I start new threads for these?

  4. mcfester
    Member
    Posted 4 months ago #

    Ok forget the full code, I figured it out after a few errors.

    Great hack, uploading galleries now on a test site, gonna push it to max importing 60 galleries and see if max execution time of PHP causes problems.

    I forgot to mention the bug with edit thumb isn't unique to nextcellent, its the same on nextgen 1.9.13.

    Cheers.

  5. mcfester
    Member
    Posted 4 months ago #

    Will this hack be added on the next update?

  6. niknetniko
    Member
    Posted 3 months ago #

    I don't know if WPGetReady has gotten around to testing and/or adding this.

    You can always send a pull request to speed things up a little if you want: https://bitbucket.org/wpgetready/nextcellent

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.