/**
 * Count how many images are in a folder
 *
 * @since 1.0
 * @return number
 */
function sunshine_image_folder_count($folder)
{
    return count(sunshine_get_images_in_folder($folder));
}
function sunshine_ajax_file_save()
{
    global $sunshine;
    set_time_limit(600);
    add_filter('upload_dir', 'sunshine_custom_upload_dir');
    $gallery_id = intval($_POST['gallery_id']);
    $item_number = intval($_POST['item_number']);
    $dir = $_POST['dir'];
    $existing_file_names = array();
    $existing_images = get_children(array('post_parent' => $gallery_id, 'post_type' => 'attachment', 'post_mime_type' => 'image'));
    foreach ($existing_images as $existing_image) {
        $existing_file_names[] = get_post_meta($existing_image->ID, 'sunshine_file_name', true);
    }
    $upload_dir = wp_upload_dir();
    $folder = $upload_dir['basedir'] . '/sunshine/' . $dir;
    $images = sunshine_get_images_in_folder($folder);
    $file_path = $images[$item_number - 1];
    $file_name = basename($file_path);
    if (is_array($existing_file_names) && in_array($file_name, $existing_file_names)) {
        echo json_encode(array('status' => 'error', 'file' => $file_name, 'error' => __('Already uploaded to gallery', 'sunshine')));
        exit;
    }
    $wp_filetype = wp_check_filetype($file_name, null);
    extract($wp_filetype);
    //$file_url = $upload_dir['baseurl'].'/sunshine/'.$dir.'/'.$file;
    //$file_url = apply_filters( 'sunshine_image_save_url', $file_url, $folder );
    $new_file_name = wp_unique_filename($upload_dir['path'], $file_name);
    // copy the file to the uploads dir
    $new_file_path = $upload_dir['path'] . '/' . $new_file_name;
    if (false === @copy($file_path, $new_file_path)) {
        return new WP_Error('upload_error', sprintf(__('The selected file could not be copied to %s.', 'sunshine'), $upload_dir['path']));
    }
    // Set correct file permissions
    $stat = stat(dirname($new_file_path));
    $perms = $stat['mode'] & 0666;
    @chmod($new_file, $perms);
    $url = $upload_dir['url'] . '/' . $new_file_name;
    // Apply upload filters
    $return = apply_filters('wp_handle_upload', array('file' => $new_file_path, 'url' => $url, 'type' => $type));
    $new_file = $return['file'];
    $url = $return['url'];
    $type = $return['type'];
    $title = preg_replace('!\\.[^.]+$!', '', basename($file_name));
    $content = '';
    // use image exif/iptc data for title and caption defaults if possible
    if ($image_meta = @wp_read_image_metadata($new_file)) {
        if ('' != trim($image_meta['title'])) {
            $title = trim($image_meta['title']);
        }
        if ('' != trim($image_meta['caption'])) {
            $content = trim($image_meta['caption']);
        }
    }
    $post_date = current_time('mysql');
    $post_date_gmt = current_time('mysql', 1);
    // Construct the attachment array
    $attachment = array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $gallery_id, 'post_title' => $title, 'post_name' => $title, 'post_content' => $content, 'post_date' => $post_date, 'post_date_gmt' => $post_date_gmt);
    $new_file = str_replace(wp_normalize_path($upload_dir['basedir']), $upload_dir['basedir'], $new_file);
    // Save the data
    $attachment_id = wp_insert_attachment($attachment, $new_file, $gallery_id);
    if (!is_wp_error($attachment_id)) {
        $data = wp_generate_attachment_metadata($attachment_id, $new_file);
        $attachment_meta_data = wp_update_attachment_metadata($attachment_id, $data);
        if (!empty($image_meta)) {
            add_post_meta($attachment_id, 'created_timestamp', $image_meta['created_timestamp']);
        }
        add_post_meta($attachment_id, 'sunshine_file_name', $file_name);
        if ($attachment_meta_data['image_meta']['title']) {
            wp_update_post(array('ID' => $attachment_id, 'post_title' => $attachment_meta_data['image_meta']['title']));
        }
        do_action('sunshine_after_image_process', $attachment_id);
        echo json_encode(array('status' => 'success', 'file' => $file_name));
    }
    exit;
}