First pass at async upload, multi-upload, and gallery feature. Modified names from patch. Hat tip: tellyworth, skeltoac.

git-svn-id: https://develop.svn.wordpress.org/trunk@6659 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Matt Mullenweg
2008-01-25 19:21:11 +00:00
parent 0db02d1e0d
commit dd37de2de7
15 changed files with 2323 additions and 24 deletions

View File

@@ -14,7 +14,8 @@ function image_upload_form( $action_url, $values = array(), $error = null ) {
$image_url = attribute_escape( @$values['image-url'] );
$image_title = attribute_escape( @$values['image-title'] );
$image_align = @$values['image-url'];
$post_id = $_GET['post_id'];
?>
<div id="media-upload-header">
<h3>Add Image</h3>
@@ -24,13 +25,55 @@ function image_upload_form( $action_url, $values = array(), $error = null ) {
<li class="last"><?php _e('Flickr'); ?></li>
</ul>
</div>
<?php if ($error) { ?>
<div id="media-upload-error">
<?php echo $error->get_error_message(); ?>
</div>
<?php } ?>
<div id="media-upload-error">
<?php if ($error) {
echo $error->get_error_message();
} ?>
</div>
<script type="text/javascript">
<!--
jQuery(document).ready(function(){
var swfu = new SWFUpload({
upload_url : "<?php echo get_option('siteurl').'/wp-admin/async-upload.php'; ?>",
flash_url : "<?php echo get_option('siteurl').'/wp-includes/js/swfupload/swfupload_f9.swf'; ?>",
file_post_name: "async-upload",
swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
degraded_element_id : "html-upload-ui", // when swfupload is unavailable
//file_types : "*.jpg;*.gif;*.png",
file_size_limit : "<?php echo wp_max_upload_size(); ?> B",
post_params : {
"post_id" : "<?php echo $post_id; ?>",
"auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>",
"type" : "image",
},
swfupload_loaded_handler : uploadLoadedImage,
upload_progress_handler : uploadProgressImage,
upload_success_handler : uploadSuccessImage,
upload_error_handler: uploadError,
file_queued_handler : fileQueuedImage,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
custom_settings : {
progressTarget : "flash-upload-ui",
cancelButtonId : "btnCancel2"
},
debug: false,
});
document.getElementById("flash-browse-button").onclick = function () { swfu.selectFile(); };
});
//-->
</script>
<form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($action_url); ?>" id="image-upload" class="media-upload-form">
<p><label for="image-file"><?php _e('Choose image'); ?></label>
<p id="flash-upload-ui">
<label for="flash-browse-button"><?php _e('Choose image'); ?></label>
<input id="flash-browse-button" type="button" value="<?php _e('Browse'); ?>" />
<label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label></p>
<p id="html-upload-ui"><label for="image-file"><?php _e('Choose image'); ?></label>
<input type="file" name="image-file" id="image-file" />
<label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label></p>
<p><label for="image-alt" class="required"><?php _e('&lt;alt&gt; (required)'); ?></label>
@@ -57,7 +100,7 @@ function image_upload_form( $action_url, $values = array(), $error = null ) {
<button name="image-add" id="image-add" class="button-ok" value="1"><?php _e('Add Image'); ?></button>
<a href="#" onclick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a>
</p>
<input type="hidden" name="parent_post_id" value="<?php echo attribute_escape('parent_post_id'); ?>" />
<input type="hidden" name="post_id" value="<?php echo attribute_escape($post_id); ?>" />
<?php wp_nonce_field( 'inlineuploading' ); ?>
</form>
<?php
@@ -69,15 +112,28 @@ function image_upload_handler() {
return new wp_error( 'upload_not_allowed', __('You are not allowed to upload files.') );
}
check_admin_referer('inlineuploading');
if ( empty($_POST['image-add']) ) {
// no button click, we're just displaying the form
wp_iframe( 'image_upload_form', get_option('siteurl') . '/wp-admin/media-upload.php?type=image' );
}
else {
// Add Image button was clicked
$id = image_upload_post();
check_admin_referer('inlineuploading');
// if the async flash uploader was used, the attachment has already been inserted and its ID is passed in post.
// otherwise this is a regular form post and we still have to handle the upload and create the attachment.
if ( !empty($_POST['attachment_id']) ) {
$id = intval($_POST['attachment_id']);
// store the title and alt into the attachment post
wp_update_post(array(
'ID' => $id,
'post_title' => $_POST['image-title'],
'post_content' => $_POST['image-alt'],
));
}
else {
$id = image_upload_post();
}
// if the input was invalid, redisplay the form with its current values
if ( is_wp_error($id) )
@@ -88,6 +144,45 @@ function image_upload_handler() {
}
}
// this returns html to include in the single image upload form when the async flash upload has finished
// i.e. show a thumb of the image, and include the attachment id as a hidden input
function async_image_callback($id) {
$thumb_url = wp_get_attachment_thumb_url($id);
if ( empty($thumb_url) )
$thumb_url = wp_mime_type_icon($id);
if ($thumb_url) {
$out = '<p><input type="hidden" name="attachment_id" id="attachment_id" value="'.intval($id).'" />'
. '<img src="'.wp_get_attachment_thumb_url($id).'" class="pinkynail" /> '
. basename(wp_get_attachment_url($id)).'</p>';
}
else {
$out = '<p><input type="hidden" name="attachment_id" id="attachment_id" value="'.intval($id).'" />'
. basename(wp_get_attachment_url($id)).'</p>';
}
$post = get_post($id);
$title = addslashes($post->post_title);
$alt = addslashes($post->post_content);
// populate the input fields with post data (which in turn comes from exif/iptc)
$out .= <<<EOF
<script type="text/javascript">
<!--
jQuery('#image-alt').val('{$alt}').attr('disabled', false);
jQuery('#image-title').val('{$title}').attr('disabled', false);
jQuery('#image-url').attr('disabled', false);
jQuery('#image-add').attr('disabled', false);
-->
</script>
EOF;
return $out;
}
add_filter('async_upload_image', 'async_image_callback');
function image_send_to_editor($id, $alt, $title, $align, $url='') {
$img_src = wp_get_attachment_url($id);
@@ -97,7 +192,7 @@ function image_send_to_editor($id, $alt, $title, $align, $url='') {
if ( isset($meta['width'], $meta['height']) )
$hwstring = ' width="'.intval($meta['width']).'" height="'.intval($meta['height']).'"';
$html = '<img src="'.attribute_escape($img_src).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'"'.$hwstring.' class="align-'.attribute_escape($align).'" />';
$html = '<img src="'.attribute_escape($img_src).'" rel="attachment wp-att-'.attribute_escape($id).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'"'.$hwstring.' class="align-'.attribute_escape($align).'" />';
if ( $url )
$html = '<a href="'.attribute_escape($url).'">'.$html.'</a>';
@@ -152,12 +247,51 @@ function image_upload_post() {
if ( !is_wp_error($id) )
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
return $id;
}
// this handles the file upload POST itself, creating the attachment post
function media_handle_upload($file_id, $post_id, $post_data = array()) {
$overrides = array('test_form'=>false);
$file = wp_handle_upload($_FILES[$file_id], $overrides);
if ( isset($file['error']) )
return new wp_error( 'upload_error', $file['error'] );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = preg_replace('/\.[^.]+$/', '', basename($file));
$content = '';
// use image exif/iptc data for title and caption defaults if possible
if ( $image_meta = @wp_read_image_metadata($file) ) {
if ( trim($image_meta['title']) )
$title = $image_meta['title'];
if ( trim($image_meta['caption']) )
$content = $image_meta['caption'];
}
// Construct the attachment array
$attachment = array_merge( array(
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => $content,
), $post_data );
// Save the data
$id = wp_insert_attachment($attachment, $file, $post_parent);
if ( !is_wp_error($id) ) {
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
}
return $id;
wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");
}
// wrap iframe content (produced by $content_func) in a doctype, html head/body etc
// any additional function args will be passed to content_func
function wp_iframe($content_func /* ... */) {
@@ -194,10 +328,13 @@ if ( is_string($content_func) )
function media_buttons() { // just a placeholder for now
global $post_ID, $temp_ID;
$uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
$uploading_iframe_src = wp_nonce_url("media-upload.php?type=image&amp;&amp;post_id=$uploading_iframe_ID", 'inlineuploading');
$uploading_iframe_src = apply_filters('uploading_iframe_src', $uploading_iframe_src);
$image_upload_iframe_src = wp_nonce_url("media-upload.php?type=image&amp;post_id=$uploading_iframe_ID", 'inlineuploading');
$image_upload_iframe_src = apply_filters('image_upload_iframe_src', $image_upload_iframe_src);
$multimedia_upload_iframe_src = wp_nonce_url("media-upload.php?type=multimedia&amp;post_id=$uploading_iframe_ID", 'inlineuploading');
$multimedia_upload_iframe_src = apply_filters('multimedia_upload_iframe_src', $multimedia_upload_iframe_src);
$out = <<<EOF
<a href="{$uploading_iframe_src}&TB_iframe=true&height=500&width=460" class="thickbox">
<a href="{$multimedia_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox">Multimedia</a>
<a href="{$image_upload_iframe_src}&TB_iframe=true&height=500&width=460" class="thickbox">
<img src="./images/media-buttons.gif" alt="" />
</a>
EOF;
@@ -225,7 +362,214 @@ function media_admin_css() {
wp_admin_css('css/media');
}
add_action('media_upload_multimedia', 'multimedia_upload_handler');
add_action('media_upload_image', 'image_upload_handler');
add_action('admin_head_image_upload_form', 'media_admin_css');
?>
function multimedia_upload_handler() {
if ( !current_user_can('upload_files') ) {
return new wp_error( 'upload_not_allowed', __('You are not allowed to upload files.') );
}
if ( empty($_POST) ) {
// no button click, we're just displaying the form
wp_iframe( 'multimedia_upload_form' );
} elseif ( empty($_POST['upload-button']) ) {
// Insert multimedia button was clicked
check_admin_referer('multimedia-form');
if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
$post = $_post = get_post($attachment_id, ARRAY_A);
$post['post_content'] = $attachment['post_content'];
$post['post_title'] = $attachment['post_title'];
if ( $post != $_post )
wp_update_post($post);
if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t )
if ( isset($attachment[$t]) )
wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
}
media_send_to_editor('[gallery]');
} else {
// Upload File button was clicked
$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
wp_iframe( 'multimedia_upload_form' );
}
}
function get_multimedia_items( $post_id ) {
$attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=\"menu_order ASC, ID ASC\"");
if ( empty($attachments) )
return '';
foreach ( $attachments as $id => $attachment ) {
$output .= "\n<div id='multimedia-item-$id' class='multimedia-item preloaded'><div id='media-upload-error-$id'></div><span class='filename'></span><div class='progress'><div class='bar'></div></div>";
$output .= get_multimedia_item($id);
$output .= " <div class='progress clickmask'></div>\n</div>";
}
return $output;
}
function get_multimedia_item( $attachment_id ) {
$thumb_url = wp_get_attachment_thumb_url( $attachment_id );
if ( empty($thumb_url) )
$thumb_url = wp_mime_type_icon( $attachment_id );
$title_label = __('Title');
$description_label = __('Description');
$tags_label = __('Tags');
$toggle_on = __('Describe &raquo;');
$toggle_off = __('Describe &laquo;');
$post = get_post($attachment_id);
$filename = basename($post->guid);
$title = attribute_escape($post->post_title);
$description = attribute_escape($post->post_content);
if ( $_tags = get_the_tags($attachment_id) ) {
foreach ( $_tags as $tag )
$tags[] = $tag->name;
$tags = attribute_escape(join(', ', $tags));
}
$delete_href = wp_nonce_url("post.php?action=delete-post&amp;post=$attachment_id", 'delete-post_' . $attachment_id);
$delete = __('Delete');
$item = "
<a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
<a class='toggle describe-toggle-off' href='#'>$toggle_off</a>
<span class='filename new'>$filename</span>
<div class='slidetoggle describe'>
<img class='thumbnail' src='$thumb_url' alt='' />
<fieldset>
<p><label for='attachments[$attachment_id][post_title]'>$title_label</label><input type='text' id='attachments[$attachment_id][post_title]' name='attachments[$attachment_id][post_title]' value='$title' /></p>
<p><label for='attachments[$attachment_id][post_content]'>$description_label</label><input type='text' id='attachments[$attachment_id][post_content]' name='attachments[$attachment_id][post_content]' value='$description' /></p>
";
if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t ) {
$tax = get_taxonomy($t);
$t_title = !empty($tax->title) ? $tax->title : $t;
if ( false === $terms = get_object_term_cache( $attachment_id, $t ) )
$_terms = wp_get_object_terms($attachment_id, $t);
if ( $_terms ) {
foreach ( $_terms as $term )
$terms[] = $term->name;
$terms = join(', ', $terms);
} else
$terms = '';
$item .= "<p><label for='attachments[$attachment_id][$t]'>$t_title</label><input type='text' id='attachments[$attachment_id][$t]' name='attachments[$attachment_id][$t]' value='$terms' /></p>\n";
}
$item .= "
</fieldset>
<p><a id='del$attachment_id' class='delete' href='$delete_href'>$delete</a></p>
</div>
";
return $item;
}
function multimedia_upload_form( $error = null ) {
$flash_action_url = get_option('siteurl') . '/wp-admin/async-upload.php?type=multimedia';
$form_action_url = get_option('siteurl') . '/wp-admin/media-upload.php?type=multimedia';
$post_id = intval($_REQUEST['post_id']);
?>
<div id="media-upload-header">
<h3>Add Images</h3>
</div>
<div id="media-upload-error">
<?php if ($error) { ?>
<?php echo $error->get_error_message(); ?>
<?php } ?>
</div>
<script type="text/javascript">
<!--
jQuery(function($){
swfu = new SWFUpload({
upload_url : "<?php echo attribute_escape( $flash_action_url ); ?>",
flash_url : "<?php echo get_option('siteurl').'/wp-includes/js/swfupload/swfupload_f9.swf'; ?>",
file_post_name: "async-upload",
file_types: "*.*",
post_params : {
"post_id" : "<?php echo $post_id; ?>",
"auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>",
"type" : "multimedia"
},
swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
degraded_element_id : "html-upload-ui", // when swfupload is unavailable
//upload_start_handler : uploadStart,
upload_progress_handler : uploadProgressMultimedia,
//upload_error_handler : uploadError,
upload_success_handler : uploadSuccessMultimedia,
upload_complete_handler : uploadCompleteMultimedia,
file_dialog_start_handler : fileDialogStart,
file_queued_handler : fileQueuedMultimedia,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
debug: false,
});
$("#flash-browse-button").bind( "click", function(){swfu.selectFiles();});
$("#insert-multimedia").bind( "click", function(){jQuery(this).parents('form').get(0).submit();});
$(".multimedia-item.preloaded").each(function(){uploadSuccessMultimedia({id:this.id.replace(/[^0-9]/g, '')},'');jQuery('#insert-multimedia').attr('disabled', '');});
$("a.delete").bind('click',function(){$.ajax({url:'admin-ajax.php',type:'post',data:{id:this.id.replace(/del/,''),action:'delete-post',_ajax_nonce:this.href.replace(/^.*wpnonce=/,'')}});$(this).parents(".multimedia-item").eq(0).slideToggle(300, function(){$(this).remove();});return false;});
});
//-->
</script>
<p id="flash-upload-ui" style="display:none">
<input id="flash-browse-button" type="button" value="<?php _e('Choose Files'); ?>" />
<label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label>
</p>
<form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form">
<div id="html-upload-ui">
<p><label for="async-upload"><?php _e('Choose image'); ?></label>
<input type="file" name="async-upload" id="async-upload" />
<label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label>
</p>
<p>
<button id="upload-button" name="upload-button" value="1" class="button-ok"><?php _e('Add Image'); ?></button>
<a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a>
</p>
<input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>" />
<br style="clear:both" />
</div>
<div id="multimedia-items">
<?php echo get_multimedia_items($post_id); ?>
</div>
<p class="submit">
<a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a>
<input type="button" class="submit" id="insert-multimedia" value="<?php _e('Insert gallery into post'); ?>" disabled="disabled" />
</p>
<?php wp_nonce_field('multimedia-form'); ?>
</form>
<?php
}
add_action('admin_head_multimedia_upload_form', 'media_admin_css');
add_filter('async_upload_multimedia', 'get_multimedia_item');
add_filter('media_upload_multimedia', 'multimedia_upload_handler');
// Any 'attachment' taxonomy will be included in the description input form for the multi uploader
// Example:
//register_taxonomy('attachment_people', 'attachment', array('title' => 'People'));
?>

View File

@@ -745,10 +745,15 @@ function wp_convert_bytes_to_hr( $bytes ) {
return $size . $units[$power];
}
function wp_import_upload_form( $action ) {
function wp_max_upload_size() {
$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
$bytes = apply_filters( 'import_upload_size_limit', min($u_bytes, $p_bytes), $u_bytes, $p_bytes );
$bytes = apply_filters( 'upload_size_limit', min($u_bytes, $p_bytes), $u_bytes, $p_bytes );
return $bytes;
}
function wp_import_upload_form( $action ) {
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
$size = wp_convert_bytes_to_hr( $bytes );
?>
<form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo attribute_escape($action) ?>">