Wordpress 2.6.2
[autoinstalls/wordpress.git] / wp-admin / includes / media.php
index 1000d15bf1faa2cf21097e01187b94fde94a419f..d91ffc2398827107eb75a45a6fec907e31724e89 100644 (file)
@@ -26,11 +26,14 @@ function update_gallery_tab($tabs) {
 add_filter('media_upload_tabs', 'update_gallery_tab');
 
 function the_media_upload_tabs() {
+       global $redir_tab;
        $tabs = media_upload_tabs();
 
        if ( !empty($tabs) ) {
                echo "<ul id='sidemenu'>\n";
-               if ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
+               if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
+                       $current = $redir_tab;
+               elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
                        $current = $_GET['tab'];
                else {
                        $keys = array_keys($tabs);
@@ -41,8 +44,8 @@ function the_media_upload_tabs() {
                        if ( $current == $callback )
                                $class = " class='current'";
                        $href = add_query_arg(array('tab'=>$callback, 's'=>false, 'paged'=>false, 'post_mime_type'=>false, 'm'=>false));
-                       $link = "<a href='$href'$class>$text</a>";
-                       echo "\t<li id='tab-$callback'>$link</li>\n";
+                       $link = "<a href='" . clean_url($href) . "'$class>$text</a>";
+                       echo "\t<li id='" . attribute_escape("tab-$callback") . "'>$link</li>\n";
                }
                echo "</ul>\n";
        }
@@ -55,20 +58,39 @@ function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = fal
        $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
 
        if ( $url )
-               $html = "<a href='".attribute_escape($url)."'$rel>$html</a>";
+               $html = '<a href="' . clean_url($url) . "\"$rel>$html</a>";
 
        $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size );
 
        return $html;
 }
 
+function image_add_caption( $html, $id, $alt, $title, $align, $url, $size ) {
+
+       if ( empty($alt) || apply_filters( 'disable_captions', '' ) ) return $html;
+       $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
+
+       preg_match( '/width="([0-9]+)/', $html, $matches );
+       if ( ! isset($matches[1]) ) return $html;
+       $width = $matches[1];
+
+       $html = preg_replace( '/align[^\s\'"]+\s?/', '', $html );
+       if ( empty($align) ) $align = 'none';
+
+       $shcode = '[caption id="' . $id . '" align="align' . $align
+       . '" width="' . $width . '" caption="' . $alt . '"]' . $html . '[/caption]';
+
+       return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
+}
+add_filter( 'image_send_to_editor', 'image_add_caption', 20, 7 );
+
 function media_send_to_editor($html) {
        ?>
 <script type="text/javascript">
-<!--
-top.send_to_editor('<?php echo addslashes($html); ?>');
-top.tb_remove();
--->
+/* <![CDATA[ */
+var win = window.dialogArguments || opener || parent || top;
+win.send_to_editor('<?php echo addslashes($html); ?>');
+/* ]]> */
 </script>
        <?php
        exit;
@@ -80,7 +102,7 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) {
        $file = wp_handle_upload($_FILES[$file_id], $overrides);
 
        if ( isset($file['error']) )
-               return new wp_error( 'upload_error', $file['error'] );
+               return new WP_Error( 'upload_error', $file['error'] );
 
        $url = $file['url'];
        $type = $file['type'];
@@ -115,6 +137,47 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) {
 
 }
 
+function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
+       $overrides = array('test_form'=>false);
+       $file = wp_handle_sideload($file_array, $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'];
+       }
+
+       $title = @$desc;
+
+       // 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 $url;
+       }
+       return $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
@@ -126,9 +189,12 @@ function wp_iframe($content_func /* ... */) {
 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
 <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
 <?php
-wp_admin_css( 'css/global' );
-wp_admin_css();
-wp_admin_css( 'css/colors' );
+wp_enqueue_style( 'global' );
+wp_enqueue_style( 'wp-admin' );
+wp_enqueue_style( 'colors' );
+if ( 0 === strpos( $content_func, 'media' ) )
+       wp_enqueue_style( 'media' );
+
 ?>
 <script type="text/javascript">
 //<![CDATA[
@@ -136,6 +202,7 @@ function addLoadEvent(func) {if ( typeof wpOnload!='function'){wpOnload=func;}el
 //]]>
 </script>
 <?php
+do_action('admin_print_styles');
 do_action('admin_print_scripts');
 do_action('admin_head');
 if ( is_string($content_func) )
@@ -167,36 +234,15 @@ function media_buttons() {
        $audio_title = __('Add Audio');
        $out = <<<EOF
 
-       <a href="{$image_upload_iframe_src}&amp;TB_iframe=true&amp;height=500&amp;width=640" class="thickbox" title='$image_title'><img src='images/media-button-image.gif' alt='$image_title' /></a>
-       <a href="{$video_upload_iframe_src}&amp;TB_iframe=true&amp;height=500&amp;width=640" class="thickbox" title='$video_title'><img src='images/media-button-video.gif' alt='$video_title' /></a>
-       <a href="{$audio_upload_iframe_src}&amp;TB_iframe=true&amp;height=500&amp;width=640" class="thickbox" title='$audio_title'><img src='images/media-button-music.gif' alt='$audio_title' /></a>
-       <a href="{$media_upload_iframe_src}&amp;TB_iframe=true&amp;height=500&amp;width=640" class="thickbox" title='$media_title'><img src='images/media-button-other.gif' alt='$media_title' /></a>
+       <a href="{$image_upload_iframe_src}&amp;TB_iframe=true" id="add_image" class="thickbox" title='$image_title'><img src='images/media-button-image.gif' alt='$image_title' /></a>
+       <a href="{$video_upload_iframe_src}&amp;TB_iframe=true" id="add_video" class="thickbox" title='$video_title'><img src='images/media-button-video.gif' alt='$video_title' /></a>
+       <a href="{$audio_upload_iframe_src}&amp;TB_iframe=true" id="add_audio" class="thickbox" title='$audio_title'><img src='images/media-button-music.gif' alt='$audio_title' /></a>
+       <a href="{$media_upload_iframe_src}&amp;TB_iframe=true" id="add_media" class="thickbox" title='$media_title'><img src='images/media-button-other.gif' alt='$media_title' /></a>
 
 EOF;
        printf($context, $out);
 }
 add_action( 'media_buttons', 'media_buttons' );
-
-function media_buttons_head() {
-$siteurl = get_option('siteurl');
-echo "<style type='text/css' media='all'>
-       @import '{$siteurl}/wp-includes/js/thickbox/thickbox.css?1';
-       div#TB_title {
-               background-color: #222222;
-               color: #cfcfcf;
-       }
-       div#TB_title a, div#TB_title a:visited {
-               color: #cfcfcf;
-       }
-</style>\n";
-}
-
-add_action( 'admin_print_scripts', 'media_buttons_head' );
-
-function media_admin_css() {
-       wp_admin_css('css/media');
-}
-
 add_action('media_upload_media', 'media_upload_handler');
 
 function media_upload_form_handler() {
@@ -210,6 +256,8 @@ function media_upload_form_handler() {
                        $post['post_title'] = $attachment['post_title'];
                if ( isset($attachment['post_excerpt']) )
                        $post['post_excerpt'] = $attachment['post_excerpt'];
+               if ( isset($attachment['menu_order']) )
+                       $post['menu_order'] = $attachment['menu_order'];
 
                $post = apply_filters('attachment_fields_to_save', $post, $attachment);
 
@@ -232,7 +280,7 @@ function media_upload_form_handler() {
        if ( isset($_POST['send']) ) {
                $keys = array_keys($_POST['send']);
                $send_id = (int) array_shift($keys);
-               $attachment = $_POST['attachments'][$send_id];
+               $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
                $html = $attachment['post_title'];
                if ( !empty($attachment['url']) ) {
                        if ( strpos($attachment['url'], 'attachment_id') || false !== strpos($attachment['url'], get_permalink($_POST['post_id'])) )
@@ -280,12 +328,36 @@ function media_upload_image() {
                        $errors = $return;
        }
 
-       if ( isset($_POST['save']) )
+       if ( isset($_POST['save']) ) {
                $errors['upload_notice'] = __('Saved.');
+               return media_upload_gallery();
+       }
 
        return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
 }
 
+function media_sideload_image($file, $post_id, $desc = null) {
+       if (!empty($file) ) {
+               $file_array['name'] = basename($file);
+               $file_array['tmp_name'] = download_url($file);
+               $desc = @$desc;
+
+               $id = media_handle_sideload($file_array, $post_id, $desc);
+               $src = $id;
+
+               if ( is_wp_error($id) ) {
+                       @unlink($file_array['tmp_name']);
+                       return $id;
+               }
+       }
+
+       if ( !empty($src) ) {
+               $alt = @$desc;
+               $html = "<img src='$src' alt='$alt' />";
+               return $html;
+       }
+}
+
 function media_upload_audio() {
        if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
                // Upload File button was clicked
@@ -318,8 +390,10 @@ function media_upload_audio() {
                        $errors = $return;
        }
 
-       if ( isset($_POST['save']) )
+       if ( isset($_POST['save']) ) {
                $errors['upload_notice'] = __('Saved.');
+               return media_upload_gallery();
+       }
 
        return wp_iframe( 'media_upload_type_form', 'audio', $errors, $id );
 }
@@ -356,8 +430,10 @@ function media_upload_video() {
                        $errors = $return;
        }
 
-       if ( isset($_POST['save']) )
+       if ( isset($_POST['save']) ) {
                $errors['upload_notice'] = __('Saved.');
+               return media_upload_gallery();
+       }
 
        return wp_iframe( 'media_upload_type_form', 'video', $errors, $id );
 }
@@ -394,8 +470,10 @@ function media_upload_file() {
                        $errors = $return;
        }
 
-       if ( isset($_POST['save']) )
+       if ( isset($_POST['save']) ) {
                $errors['upload_notice'] = __('Saved.');
+               return media_upload_gallery();
+       }
 
        return wp_iframe( 'media_upload_type_form', 'file', $errors, $id );
 }
@@ -410,6 +488,7 @@ function media_upload_gallery() {
                        $errors = $return;
        }
 
+       wp_enqueue_script('admin-gallery');
        return wp_iframe( 'media_upload_gallery_form', $errors );
 }
 
@@ -429,8 +508,9 @@ function media_upload_library() {
 function image_attachment_fields_to_edit($form_fields, $post) {
        if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
                $form_fields['post_title']['required'] = true;
+
                $form_fields['post_excerpt']['label'] = __('Caption');
-               $form_fields['post_excerpt']['helps'][] = __('Alternate text, e.g. "The Mona Lisa"');
+               $form_fields['post_excerpt']['helps'][] = __('Also used as alternate text for the image');
 
                $form_fields['post_content']['label'] = __('Description');
 
@@ -546,6 +626,10 @@ function get_attachment_fields_to_edit($post, $errors = null) {
                                </script>\n",
                        'helps'      => __('Enter a link URL or click above for presets.'),
                ),
+       'menu_order'   => array(
+                       'label'      => __('Order'),
+                       'value'      => $edit_post->menu_order
+               ),
        );
 
        foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
@@ -583,7 +667,7 @@ function get_media_items( $post_id, $errors ) {
                if ( $post && $post->post_type == 'attachment' )
                        $attachments = array($post->ID => $post);
                else
-                       $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=menu_order ASC, ID&order=DESC");
+                       $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
        } else {
                if ( is_array($GLOBALS['wp_the_query']->posts) )
                        foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
@@ -601,6 +685,8 @@ function get_media_items( $post_id, $errors ) {
 }
 
 function get_media_item( $attachment_id, $args = null ) {
+       global $redir_tab;
+
        $default_args = array( 'errors' => null, 'send' => true, 'delete' => true, 'toggle' => true );
        $args = wp_parse_args( $args, $default_args );
        extract( $args, EXTR_SKIP );
@@ -648,10 +734,27 @@ function get_media_item( $attachment_id, $args = null ) {
        }
 
        $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
+       $display_title = wp_html_excerpt($display_title, 60);
+
+       $gallery = ( (isset($_REQUEST['tab']) && 'gallery' == $_REQUEST['tab']) || (isset($redir_tab) && 'gallery' == $redir_tab) ) ? true : false;
+       $order = '';
+
+       foreach ( $form_fields as $key => $val ) {
+               if ( 'menu_order' == $key ) {
+                       if ( $gallery )
+                               $order = '<div class="menu_order"> <input class="menu_order_input" type="text" id="attachments['.$attachment_id.'][menu_order]" name="attachments['.$attachment_id.'][menu_order]" value="'.$val['value'].'" /></div>';
+                       else
+                               $order = '<input type="hidden" name="attachments['.$attachment_id.'][menu_order]" value="'.$val['value'].'" />';
+
+                       unset($form_fields['menu_order']);
+                       break;
+               }
+       }
 
        $item = "
        $type
        $toggle_links
+       $order
        <div class='filename new'>$display_title</div>
        <table class='slidetoggle describe $class'>
                <thead class='media-item-info'>
@@ -676,9 +779,11 @@ function get_media_item( $attachment_id, $args = null ) {
        if ( $send )
                $send = "<input type='submit' class='button' name='send[$attachment_id]' value='" . attribute_escape( __( 'Insert into Post' ) ) . "' />";
        if ( $delete )
-               $delete = "<a href='$delete_href' id='del[$attachment_id]' disabled='disabled' class='delete'>" . __('Delete') . "</button>";
+               $delete = "<a href=\"#\" class=\"del-link\" onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __('Delete') . "</a>";
        if ( ( $send || $delete ) && !isset($form_fields['buttons']) )
-               $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $delete</td></tr>\n");
+               $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $delete
+               <div id=\"del_attachment_$attachment_id\" class=\"del-attachment\" style=\"display:none;\">" . sprintf(__("You are about to delete <strong>%s</strong>."), $filename) . " <a href=\"$delete_href\" id=\"del[$attachment_id]\" class=\"delete\">" . __('Continue') . "</a>
+               <a href=\"#\" class=\"del-link\" onclick=\"this.parentNode.style.display='none';return false;\">" . __('Cancel') . "</a></div></td></tr>\n");
 
        $hidden_fields = array();
 
@@ -700,6 +805,7 @@ function get_media_item( $attachment_id, $args = null ) {
                }
 
                $required = $field['required'] ? '<abbr title="required" class="required">*</abbr>' : '';
+               $aria_required = $field['required'] ? " aria-required='true' " : '';
                $class  = $id;
                $class .= $field['required'] ? ' form-required' : '';
 
@@ -707,9 +813,9 @@ function get_media_item( $attachment_id, $args = null ) {
                if ( !empty($field[$field['input']]) )
                        $item .= $field[$field['input']];
                elseif ( $field['input'] == 'textarea' ) {
-                       $item .= "<textarea type='text' id='$name' name='$name'>" . attribute_escape( $field['value'] ) . "</textarea>";
+                       $item .= "<textarea type='text' id='$name' name='$name'>" . attribute_escape( $field['value'] ) . $aria_required . "</textarea>";
                } else {
-                       $item .= "<input type='text' id='$name' name='$name' value='" . attribute_escape( $field['value'] ) . "' />";
+                       $item .= "<input type='text' id='$name' name='$name' value='" . attribute_escape( $field['value'] ) . "'" . $aria_required . "/>";
                }
                if ( !empty($field['helps']) )
                        $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique((array) $field['helps']) ) . '</p>';
@@ -754,7 +860,7 @@ function media_upload_header() {
 function media_upload_form( $errors = null ) {
        global $type, $tab;
 
-       $flash_action_url = get_option('siteurl') . "/wp-admin/async-upload.php";
+       $flash_action_url = admin_url('async-upload.php');
 
        // If Mac and mod_security, no Flash. :(
        $flash = true;
@@ -765,7 +871,6 @@ function media_upload_form( $errors = null ) {
        $post_id = intval($_REQUEST['post_id']);
 
 ?>
-<input type='hidden' name='post_id' value='<?php echo (int) $post_id; ?>' />
 <div id="media-upload-notice">
 <?php if (isset($errors['upload_notice']) ) { ?>
        <?php echo $errors['upload_notice']; ?>
@@ -776,18 +881,22 @@ function media_upload_form( $errors = null ) {
        <?php echo $errors['upload_error']->get_error_message(); ?>
 <?php } ?>
 </div>
+
+<?php do_action('pre-upload-ui'); ?>
+
 <?php if ( $flash ) : ?>
 <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'; ?>",
+                       flash_url : "<?php echo includes_url('js/swfupload/swfupload_f9.swf'); ?>",
                        file_post_name: "async-upload",
                        file_types: "<?php echo apply_filters('upload_file_glob', '*.*'); ?>",
                        post_params : {
                                "post_id" : "<?php echo $post_id; ?>",
-                               "auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>",
+                               "auth_cookie" : "<?php if ( is_ssl() ) echo $_COOKIE[SECURE_AUTH_COOKIE]; else echo $_COOKIE[AUTH_COOKIE]; ?>",
+                               "_wpnonce" : "<?php echo wp_create_nonce('media-form'); ?>",
                                "type" : "<?php echo $type; ?>",
                                "tab" : "<?php echo $tab; ?>",
                                "short" : "1"
@@ -812,24 +921,27 @@ jQuery(function($){
 //-->
 </script>
 
-
 <div id="flash-upload-ui">
+<?php do_action('pre-flash-upload-ui'); ?>
        <p><input id="flash-browse-button" type="button" value="<?php echo attribute_escape( __( 'Choose files to upload' ) ); ?>" class="button" /></p>
-       <p><?php _e('After a file has been uploaded, you can add titles and descriptions.'); ?></p>
+<?php do_action('post-flash-upload-ui'); ?>
+       <p class="howto"><?php _e('After a file has been uploaded, you can add titles and descriptions.'); ?></p>
 </div>
 
 <?php endif; // $flash ?>
 
 <div id="html-upload-ui">
+<?php do_action('pre-html-upload-ui'); ?>
        <p>
-       <input type="file" name="async-upload" id="async-upload" /> <input type="submit" class="button" name="html-upload" value="<?php echo attribute_escape(__('Upload')); ?>" /> <a href="#" onClick="return top.tb_remove();"><?php _e('Cancel'); ?></a>
+       <input type="file" name="async-upload" id="async-upload" /> <input type="submit" class="button" name="html-upload" value="<?php echo attribute_escape(__('Upload')); ?>" /> <a href="#" onclick="return top.tb_remove();"><?php _e('Cancel'); ?></a>
        </p>
-       <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
        <br class="clear" />
        <?php if ( is_lighttpd_before_150() ): ?>
        <p><?php _e('If you want to use all capabilities of the uploader, like uploading multiple files at once, please upgrade to lighttpd 1.5.'); ?></p>
        <?php endif;?>
+<?php do_action('post-html-upload-ui'); ?>
 </div>
+<?php do_action('post-upload-ui'); ?>
 <?php
 }
 
@@ -838,7 +950,8 @@ function media_upload_type_form($type = 'file', $errors = null, $id = null) {
 
        $post_id = intval($_REQUEST['post_id']);
 
-       $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type=$type&tab=type&post_id=$post_id";
+       $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
+       $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
 
        $callback = "type_form_$type";
 ?>
@@ -873,23 +986,100 @@ jQuery(function($){
 <h3><?php _e('From URL'); ?></h3>
 </div>
 
+<script type="text/javascript">
+//<![CDATA[
+var addExtImage = {
+
+       width : '',
+       height : '',
+       align : 'alignnone',
+
+       insert : function() {
+               var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = null;
+
+               if ( '' == f.src.value || '' == t.width ) return false;
+
+               if ( f.title.value ) {
+                       title = f.title.value.replace(/['"<>]+/g, '');
+                       title = ' title="'+title+'"';
+               }
+
+               if ( f.alt.value ) {
+                       alt = f.alt.value.replace(/['"<>]+/g, '');
+<?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
+                       caption = f.alt.value.replace(/'/g, '&#39;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+<?php } ?>
+               }
+
+               cls = caption ? '' : ' class="'+t.align+'"';
+
+               html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
+
+               if ( f.url.value )
+                       html = '<a href="'+f.url.value+'">'+html+'</a>';
+
+               if ( caption )
+                       html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
+
+               var win = window.dialogArguments || opener || parent || top;
+               win.send_to_editor(html);
+       },
+
+       resetImageData : function() {
+               var t = addExtImage;
+
+               t.width = t.height = '';
+               document.getElementById('go_button').style.color = '#bbb';
+               if ( ! document.forms[0].src.value )
+                       document.getElementById('status_img').src = 'images/required.gif';
+               else document.getElementById('status_img').src = 'images/no.png';
+       },
+
+       updateImageData : function() {
+               var t = addExtImage;
+
+               t.width = t.preloadImg.width;
+               t.height = t.preloadImg.height;
+               document.getElementById('go_button').style.color = '#333';
+               document.getElementById('status_img').src = 'images/yes.png';
+       },
+
+       getImageData : function() {
+               var t = addExtImage, src = document.forms[0].src.value;
+
+               if ( ! src ) {
+                       t.resetImageData();
+                       return false;
+               }
+               document.getElementById('status_img').src = 'images/loading.gif';
+               t.preloadImg = new Image();
+               t.preloadImg.onload = t.updateImageData;
+               t.preloadImg.onerror = t.resetImageData;
+               t.preloadImg.src = src;
+       }
+}
+//]]>
+</script>
+
 <div id="media-items">
 <div class="media-item media-blank">
 <?php echo call_user_func($callback); ?>
 </div>
 </div>
 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
+</form>
 <?php
        endif;
 }
 
 function media_upload_gallery_form($errors) {
+       global $redir_tab;
+
+       $redir_tab = 'gallery';
        media_upload_header();
 
        $post_id = intval($_REQUEST['post_id']);
-
-       $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type={$GLOBALS['type']}&tab=gallery&post_id=$post_id";
-
+       $form_action_url = admin_url("media-upload.php?type={$GLOBALS['type']}&tab=gallery&post_id=$post_id");
 ?>
 
 <script type="text/javascript">
@@ -907,15 +1097,22 @@ jQuery(function($){
 <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form validate" id="gallery-form">
 <?php wp_nonce_field('media-form'); ?>
 <?php //media_upload_form( $errors ); ?>
-
+<table class="widefat">
+<thead><tr>
+<th><?php _e('Media'); ?></th>
+<th class="order-head"><?php _e('Order'); ?></th>
+</tr></thead>
+</table>
 <div id="media-items">
 <?php echo get_media_items($post_id, $errors); ?>
 </div>
+<p class="ml-submit">
 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
 <input type="submit" class="button insert-gallery" name="insert-gallery" value="<?php echo attribute_escape( __( 'Insert gallery into post' ) ); ?>" />
 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
 <input type="hidden" name="type" value="<?php echo attribute_escape( $GLOBALS['type'] ); ?>" />
 <input type="hidden" name="tab" value="<?php echo attribute_escape( $GLOBALS['tab'] ); ?>" />
+</p>
 </form>
 <?php
 }
@@ -927,7 +1124,7 @@ function media_upload_library_form($errors) {
 
        $post_id = intval($_REQUEST['post_id']);
 
-       $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type={$GLOBALS['type']}&tab=library&post_id=$post_id";
+       $form_action_url = admin_url("media-upload.php?type={$GLOBALS['type']}&tab=library&post_id=$post_id");
 
        $_GET['paged'] = intval($_GET['paged']);
        if ( $_GET['paged'] < 1 )
@@ -948,11 +1145,11 @@ function media_upload_library_form($errors) {
 <input type="hidden" name="post_mime_type" value="<?php echo attribute_escape( $_GET['post_mime_type'] ); ?>" />
 
 <div id="search-filter">
+       <label class="hidden" for="post-search-input"><?php _e('Search Media');?>:</label>
        <input type="text" id="post-search-input" name="s" value="<?php the_search_query(); ?>" />
        <input type="submit" value="<?php echo attribute_escape( __( 'Search Media' ) ); ?>" class="button" />
 </div>
 
-<p>
 <ul class="subsubsub">
 <?php
 $type_links = array();
@@ -968,7 +1165,7 @@ if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
 }
 if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
        $class = ' class="current"';
-$type_links[] = "<li><a href='" . add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false)) . "'$class>".__('All Types')."</a>";
+$type_links[] = "<li><a href='" . clean_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
 foreach ( $post_mime_types as $mime_type => $label ) {
        $class = '';
 
@@ -978,13 +1175,12 @@ foreach ( $post_mime_types as $mime_type => $label ) {
        if ( wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
                $class = ' class="current"';
 
-       $type_links[] = "<li><a href='" . add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false)) . "'$class>" . sprintf(__ngettext($label[2][0], $label[2][1], $num_posts[$mime_type]), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
+       $type_links[] = "<li><a href='" . clean_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf(__ngettext($label[2][0], $label[2][1], $num_posts[$mime_type]), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
 }
 echo implode(' | </li>', $type_links) . '</li>';
 unset($type_links);
 ?>
 </ul>
-</p>
 
 <div class="tablenav">
 
@@ -1059,51 +1255,84 @@ jQuery(function($){
 <div id="media-items">
 <?php echo get_media_items(null, $errors); ?>
 </div>
+<p class="ml-submit">
 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
+</p>
 </form>
 <?php
 }
 
 function type_form_image() {
+
+       if ( apply_filters( 'disable_captions', '' ) ) {
+               $alt = __('Alternate Text');
+               $alt_help = __('Alt text for the image, e.g. "The Mona Lisa"');
+       } else {
+               $alt = __('Image Caption');
+               $alt_help = __('Also used as alternate text for the image');
+       }
+
        return '
        <table class="describe"><tbody>
+               <tr>
+                       <th valign="top" scope="row" class="label" style="width:120px;">
+                               <span class="alignleft"><label for="src">' . __('Source') . '</label></span>
+                               <span class="alignright"><img id="status_img" src="images/required.gif" title="required" alt="required" /></span>
+                       </th>
+                       <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
+               </tr>
+
                <tr>
                        <th valign="top" scope="row" class="label">
-                               <span class="alignleft"><label for="insertonly[src]">' . __('Image URL') . '</label></span>
+                               <span class="alignleft"><label for="title">' . __('Image Title') . '</label></span>
                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
                        </th>
-                       <td class="field"><input id="insertonly[src]" name="insertonly[src]" value="" type="text"></td>
+                       <td class="field"><p><input id="title" name="title" value="" type="text" aria-required="true" /></p></td>
                </tr>
+
                <tr>
                        <th valign="top" scope="row" class="label">
-                               <span class="alignleft"><label for="insertonly[alt]">' . __('Description') . '</label></span>
-                               <span class="alignright"><abbr title="required" class="required">*</abbr></span>
+                               <span class="alignleft"><label for="alt">' . $alt . '</label></span>
                        </th>
-                       <td class="field"><input id="insertonly[alt]" name="insertonly[alt]" value="" type="text"></td>
+                       <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
+                       <p class="help">' . $alt_help . '</p></td>
                </tr>
-               <tr><td></td><td class="help">' . __('Alternate text, e.g. "The Mona Lisa"') . '</td></tr>
+
                <tr class="align">
-                       <th valign="top" scope="row" class="label"><label for="insertonly[align]">' . __('Alignment') . '</label></th>
+                       <th valign="top" scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
                        <td class="field">
-                               <input name="insertonly[align]" id="image-align-none-0" value="none" type="radio" checked="checked" />
-                               <label for="image-align-none-0" class="align image-align-none-label">' . __('None') . '</label>
-                               <input name="insertonly[align]" id="image-align-left-0" value="left" type="radio" />
-                               <label for="image-align-left-0" class="align image-align-left-label">' . __('Left') . '</label>
-                               <input name="insertonly[align]" id="image-align-center-0" value="center" type="radio" />
-                               <label for="image-align-center-0" class="align image-align-center-label">' . __('Center') . '</label>
-                               <input name="insertonly[align]" id="image-align-right-0" value="right" type="radio" />
-                               <label for="image-align-right-0" class="align image-align-right-label">' . __('Right') . '</label>
+                               <input name="align" id="align-none" value="alignnone" onclick="addExtImage.align=this.value" type="radio" checked="checked" />
+                               <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
+                               <input name="align" id="align-left" value="alignleft" onclick="addExtImage.align=this.value" type="radio" />
+                               <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
+                               <input name="align" id="align-center" value="aligncenter" onclick="addExtImage.align=this.value" type="radio" />
+                               <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
+                               <input name="align" id="align-right" value="alignright" onclick="addExtImage.align=this.value" type="radio" />
+                               <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
                        </td>
                </tr>
+
+               <tr>
+                       <th valign="top" scope="row" class="label">
+                               <span class="alignleft"><label for="url">' . __('Link URL') . '</label></span>
+                       </th>
+                       <td class="field"><input id="url" name="url" value="" type="text" /><br />
+
+                       <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
+                       <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
+                       <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
+               </tr>
+
                <tr>
                        <td></td>
                        <td>
-                               <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
+                               <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . attribute_escape(__('Insert into Post')) . '" />
                        </td>
                </tr>
        </tbody></table>
 ';
+
 }
 
 function type_form_audio() {
@@ -1114,14 +1343,14 @@ function type_form_audio() {
                                <span class="alignleft"><label for="insertonly[href]">' . __('Audio File URL') . '</label></span>
                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
                        </th>
-                       <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text"></td>
+                       <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
                </tr>
                <tr>
                        <th valign="top" scope="row" class="label">
                                <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
                        </th>
-                       <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text"></td>
+                       <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
                </tr>
                <tr><td></td><td class="help">' . __('Link text, e.g. "Still Alive by Jonathan Coulton"') . '</td></tr>
                <tr>
@@ -1142,14 +1371,14 @@ function type_form_video() {
                                <span class="alignleft"><label for="insertonly[href]">' . __('Video URL') . '</label></span>
                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
                        </th>
-                       <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text"></td>
+                       <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
                </tr>
                <tr>
                        <th valign="top" scope="row" class="label">
                                <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
                        </th>
-                       <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text"></td>
+                       <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
                </tr>
                <tr><td></td><td class="help">' . __('Link text, e.g. "Lucy on YouTube"') . '</td></tr>
                <tr>
@@ -1170,14 +1399,14 @@ function type_form_file() {
                                <span class="alignleft"><label for="insertonly[href]">' . __('URL') . '</label></span>
                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
                        </th>
-                       <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text"></td>
+                       <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
                </tr>
                <tr>
                        <th valign="top" scope="row" class="label">
                                <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
                        </th>
-                       <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text"></td>
+                       <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
                </tr>
                <tr><td></td><td class="help">' . __('Link text, e.g. "Ransom Demands (PDF)"') . '</td></tr>
                <tr>
@@ -1190,6 +1419,49 @@ function type_form_file() {
 ';
 }
 
+// support a GET parameter for disabling the flash uploader
+function media_upload_use_flash($flash) {
+        if ( array_key_exists('flash', $_REQUEST) )
+                $flash = !empty($_REQUEST['flash']);
+        return $flash;
+}
+
+add_filter('flash_uploader', 'media_upload_use_flash');
+
+function media_upload_flash_bypass() {
+        echo '<p class="upload-flash-bypass">';
+        printf( __('You are using the Flash uploader.  Problems?  Try the <a href="%s">Browser uploader</a> instead.'), clean_url(add_query_arg('flash', 0)) );
+        echo '</p>';
+}
+
+add_action('post-flash-upload-ui', 'media_upload_flash_bypass');
+
+function media_upload_html_bypass() {
+        echo '<p class="upload-html-bypass">';
+        if ( array_key_exists('flash', $_REQUEST) )
+                // the user manually selected the browser uploader, so let them switch back to Flash
+                printf( __('You are using the Browser uploader.  Try the <a href="%s">Flash uploader</a> instead.'), clean_url(add_query_arg('flash', 1)) );
+        else
+                // the user probably doesn't have Flash
+                printf( __('You are using the Browser uploader.') );
+
+        echo '</p>';
+}
+
+add_action('post-flash-upload-ui', 'media_upload_flash_bypass');
+add_action('post-html-upload-ui', 'media_upload_html_bypass');
+
+// make sure the GET parameter sticks when we submit a form
+function media_upload_bypass_url($url) {
+        if ( array_key_exists('flash', $_REQUEST) )
+                $url = add_query_arg('flash', intval($_REQUEST['flash']));
+        return $url;
+}
+
+add_filter('media_upload_form_url', 'media_upload_bypass_url');
+
+
+
 add_filter('async_upload_image', 'get_media_item', 10, 2);
 add_filter('async_upload_audio', 'get_media_item', 10, 2);
 add_filter('async_upload_video', 'get_media_item', 10, 2);
@@ -1199,12 +1471,9 @@ add_action('media_upload_image', 'media_upload_image');
 add_action('media_upload_audio', 'media_upload_audio');
 add_action('media_upload_video', 'media_upload_video');
 add_action('media_upload_file', 'media_upload_file');
-add_action('admin_head_media_upload_type_form', 'media_admin_css');
 
 add_filter('media_upload_gallery', 'media_upload_gallery');
-add_action('admin_head_media_upload_gallery_form', 'media_admin_css');
 
 add_filter('media_upload_library', 'media_upload_library');
-add_action('admin_head_media_upload_library_form', 'media_admin_css');
 
 ?>