function sunshine_image_processor()
{
    global $sunshine;
    if (!isset($_GET['gallery'])) {
        return;
    }
    $gallery_id = intval($_GET['gallery']);
    $dir = get_post_meta($gallery_id, 'sunshine_gallery_images_directory', true);
    $upload_dir = wp_upload_dir();
    $folder = $upload_dir['basedir'] . '/sunshine/' . $dir;
    $count = sunshine_image_folder_count($folder);
    ?>
	<div class="wrap sunshine">
		<h2><?php 
    _e('Image Processor', 'sunshine');
    ?>
</h2>
		<p><?php 
    _e('We are processing your images! Please be patient, especially if you have a lot.', 'sunshine');
    ?>
</p>
		<div id="progress-bar" style="background: #000; height: 30px; position: relative;">
			<div id="percentage" style="height: 30px; background-color: green; width: 0%;"></div>
			<div id="processed" style="position: absolute; top: 0; left: 0; width: 100%; color: #FFF; text-align: center; font-size: 18px; height: 30px; line-height: 30px;">
				<span id="processed-count">0</span> of <span id="processed-total"><?php 
    echo $count;
    ?>
</span>
			</div>
		</div>
		<p align="center" id="abort"><a href="post.php?post=<?php 
    echo $gallery_id;
    ?>
&action=edit"><?php 
    _e('Abort Import', 'sunshine');
    ?>
</a></p>
		<p align="center" id="return" style="display: none;"><a href="post.php?post=<?php 
    echo $gallery_id;
    ?>
&action=edit"><?php 
    _e('Return to Gallery', 'sunshine');
    ?>
</a></p>
		<ul id="errors"></ul>
		<script type="text/javascript">
		jQuery(document).ready(function($) {
			var processed = 0;
			var total = <?php 
    echo $count;
    ?>
;
			var percent = 0;
			function sunshine_file_import(item_number) {
				var data = {
					'action': 'sunshine_file_save',
					'gallery_id': <?php 
    echo $gallery_id;
    ?>
,
					'dir': '<?php 
    echo $dir;
    ?>
',
					'item_number': item_number
				};
				$.postq('sunshineimageprocessor', ajaxurl, data, function(response) {
					var obj = $.parseJSON( response );
					processed++;
					if ( processed >= total ) {
						$('#abort').hide();						
						$('#return').show();						
					}
					$('#processed-count').html(processed);
					percent = Math.round( (processed / total) * 100);
					$('#percentage').css('width', percent+'%');
					if ( obj.error ) {
						$( '#errors' ).append( '<li><strong>' + obj.file + '</strong> - ' + obj.error + '</li>' );
					}
				});
			}
			for (i = 1; i <= total; i++) { 
			    sunshine_file_import(i);
			}
		});
		</script>
	</div>
<?php 
}
function sunshine_redirect_to_gallery_image_processor($location)
{
    global $post;
    $images_to_process = false;
    // See if there are new images in selected directory
    if (!empty($_POST['sunshine_gallery_images_directory'])) {
        $upload_dir = wp_upload_dir();
        $attachments = get_posts(array('post_type' => 'attachment', 'post_parent' => $post->ID, 'posts_per_page' => -1));
        $file_count = count($attachments);
        $file_count_in_dir = sunshine_image_folder_count($upload_dir['basedir'] . '/sunshine/' . sanitize_text_field($_POST['sunshine_gallery_images_directory']));
        if ($file_count_in_dir > $file_count) {
            $images_to_process = true;
        }
    }
    if (!empty($_POST['sunshine_gallery_images_directory']) && $images_to_process) {
        $location = get_admin_url() . 'admin.php?page=sunshine_image_processor&gallery=' . $post->ID;
    }
    return $location;
}