]> scripts.mit.edu Git - autoinstallsdev/wordpress.git/blob - wp-admin/includes/media.php
Wordpress 3.1
[autoinstallsdev/wordpress.git] / wp-admin / includes / media.php
1 <?php
2 /**
3  * WordPress Administration Media API.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * {@internal Missing Short Description}}
11  *
12  * @since 2.5.0
13  *
14  * @return unknown
15  */
16 function media_upload_tabs() {
17         $_default_tabs = array(
18                 'type' => __('From Computer'), // handler action suffix => tab text
19                 'type_url' => __('From URL'),
20                 'gallery' => __('Gallery'),
21                 'library' => __('Media Library')
22         );
23
24         return apply_filters('media_upload_tabs', $_default_tabs);
25 }
26
27 /**
28  * {@internal Missing Short Description}}
29  *
30  * @since 2.5.0
31  *
32  * @param unknown_type $tabs
33  * @return unknown
34  */
35 function update_gallery_tab($tabs) {
36         global $wpdb;
37
38         if ( !isset($_REQUEST['post_id']) ) {
39                 unset($tabs['gallery']);
40                 return $tabs;
41         }
42
43         $post_id = intval($_REQUEST['post_id']);
44
45         if ( $post_id )
46                 $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
47
48         if ( empty($attachments) ) {
49                 unset($tabs['gallery']);
50                 return $tabs;
51         }
52
53         $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
54
55         return $tabs;
56 }
57 add_filter('media_upload_tabs', 'update_gallery_tab');
58
59 /**
60  * {@internal Missing Short Description}}
61  *
62  * @since 2.5.0
63  */
64 function the_media_upload_tabs() {
65         global $redir_tab;
66         $tabs = media_upload_tabs();
67
68         if ( !empty($tabs) ) {
69                 echo "<ul id='sidemenu'>\n";
70                 if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
71                         $current = $redir_tab;
72                 elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
73                         $current = $_GET['tab'];
74                 else
75                         $current = apply_filters('media_upload_default_tab', 'type');
76
77                 foreach ( $tabs as $callback => $text ) {
78                         $class = '';
79                         if ( $current == $callback )
80                                 $class = " class='current'";
81                         $href = add_query_arg(array('tab'=>$callback, 's'=>false, 'paged'=>false, 'post_mime_type'=>false, 'm'=>false));
82                         $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
83                         echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
84                 }
85                 echo "</ul>\n";
86         }
87 }
88
89 /**
90  * {@internal Missing Short Description}}
91  *
92  * @since 2.5.0
93  *
94  * @param unknown_type $id
95  * @param unknown_type $alt
96  * @param unknown_type $title
97  * @param unknown_type $align
98  * @param unknown_type $url
99  * @param unknown_type $rel
100  * @param unknown_type $size
101  * @return unknown
102  */
103 function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
104
105         $html = get_image_tag($id, $alt, $title, $align, $size);
106
107         $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
108
109         if ( $url )
110                 $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
111
112         $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
113
114         return $html;
115 }
116
117 /**
118  * {@internal Missing Short Description}}
119  *
120  * @since 2.6.0
121  *
122  * @param unknown_type $html
123  * @param unknown_type $id
124  * @param unknown_type $alt
125  * @param unknown_type $title
126  * @param unknown_type $align
127  * @param unknown_type $url
128  * @param unknown_type $size
129  * @return unknown
130  */
131 function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
132
133         if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
134                 return $html;
135
136         $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
137
138         if ( ! preg_match( '/width="([0-9]+)/', $html, $matches ) )
139                 return $html;
140
141         $width = $matches[1];
142
143         $caption = str_replace( array( '>',    '<',    '"',      "'" ),
144                                                         array( '&gt;', '&lt;', '&quot;', '&#039;' ),
145                                                         $caption
146                                                   );
147
148         $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
149         if ( empty($align) )
150                 $align = 'none';
151
152         $shcode = '[caption id="' . $id . '" align="align' . $align
153         . '" width="' . $width . '" caption="' . addslashes($caption) . '"]' . $html . '[/caption]';
154
155         return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
156 }
157 add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
158
159 /**
160  * {@internal Missing Short Description}}
161  *
162  * @since 2.5.0
163  *
164  * @param unknown_type $html
165  */
166 function media_send_to_editor($html) {
167 ?>
168 <script type="text/javascript">
169 /* <![CDATA[ */
170 var win = window.dialogArguments || opener || parent || top;
171 win.send_to_editor('<?php echo addslashes($html); ?>');
172 /* ]]> */
173 </script>
174 <?php
175         exit;
176 }
177
178 /**
179  * {@internal Missing Short Description}}
180  *
181  * This handles the file upload POST itself, creating the attachment post.
182  *
183  * @since 2.5.0
184  *
185  * @param string $file_id Index into the {@link $_FILES} array of the upload
186  * @param int $post_id The post ID the media is associated with
187  * @param array $post_data allows you to overwrite some of the attachment
188  * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
189  * @return int the ID of the attachment
190  */
191 function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
192
193         $time = current_time('mysql');
194         if ( $post = get_post($post_id) ) {
195                 if ( substr( $post->post_date, 0, 4 ) > 0 )
196                         $time = $post->post_date;
197         }
198
199         $name = $_FILES[$file_id]['name'];
200         $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
201
202         if ( isset($file['error']) )
203                 return new WP_Error( 'upload_error', $file['error'] );
204
205         $name_parts = pathinfo($name);
206         $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
207
208         $url = $file['url'];
209         $type = $file['type'];
210         $file = $file['file'];
211         $title = $name;
212         $content = '';
213
214         // use image exif/iptc data for title and caption defaults if possible
215         if ( $image_meta = @wp_read_image_metadata($file) ) {
216                 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
217                         $title = $image_meta['title'];
218                 if ( trim( $image_meta['caption'] ) )
219                         $content = $image_meta['caption'];
220         }
221
222         // Construct the attachment array
223         $attachment = array_merge( array(
224                 'post_mime_type' => $type,
225                 'guid' => $url,
226                 'post_parent' => $post_id,
227                 'post_title' => $title,
228                 'post_content' => $content,
229         ), $post_data );
230
231         // Save the data
232         $id = wp_insert_attachment($attachment, $file, $post_id);
233         if ( !is_wp_error($id) ) {
234                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
235         }
236
237         return $id;
238
239 }
240
241 /**
242  * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
243  *
244  * @since 2.6.0
245  *
246  * @param array $file_array Array similar to a {@link $_FILES} upload array
247  * @param int $post_id The post ID the media is associated with
248  * @param string $desc Description of the sideloaded file
249  * @param array $post_data allows you to overwrite some of the attachment
250  * @return int|object The ID of the attachment or a WP_Error on failure
251  */
252 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
253         $overrides = array('test_form'=>false);
254
255         $file = wp_handle_sideload($file_array, $overrides);
256         if ( isset($file['error']) )
257                 return new WP_Error( 'upload_error', $file['error'] );
258
259         $url = $file['url'];
260         $type = $file['type'];
261         $file = $file['file'];
262         $title = preg_replace('/\.[^.]+$/', '', basename($file));
263         $content = '';
264
265         // use image exif/iptc data for title and caption defaults if possible
266         if ( $image_meta = @wp_read_image_metadata($file) ) {
267                 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
268                         $title = $image_meta['title'];
269                 if ( trim( $image_meta['caption'] ) )
270                         $content = $image_meta['caption'];
271         }
272
273         $title = isset($desc) ? $desc : '';
274
275         // Construct the attachment array
276         $attachment = array_merge( array(
277                 'post_mime_type' => $type,
278                 'guid' => $url,
279                 'post_parent' => $post_id,
280                 'post_title' => $title,
281                 'post_content' => $content,
282         ), $post_data );
283
284         // Save the attachment metadata
285         $id = wp_insert_attachment($attachment, $file, $post_id);
286         if ( !is_wp_error($id) )
287                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
288
289         return $id;
290 }
291
292 /**
293  * {@internal Missing Short Description}}
294  *
295  * Wrap iframe content (produced by $content_func) in a doctype, html head/body
296  * etc any additional function args will be passed to content_func.
297  *
298  * @since 2.5.0
299  *
300  * @param unknown_type $content_func
301  */
302 function wp_iframe($content_func /* ... */) {
303 ?>
304 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
305 <html xmlns="http://www.w3.org/1999/xhtml" <?php do_action('admin_xml_ns'); ?> <?php language_attributes(); ?>>
306 <head>
307 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
308 <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
309 <?php
310 wp_enqueue_style( 'global' );
311 wp_enqueue_style( 'wp-admin' );
312 wp_enqueue_style( 'colors' );
313 // Check callback name for 'media'
314 if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
315         || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
316         wp_enqueue_style( 'media' );
317 wp_enqueue_style( 'ie' );
318 ?>
319 <script type="text/javascript">
320 //<![CDATA[
321 addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
322 var userSettings = {'url':'<?php echo SITECOOKIEPATH; ?>','uid':'<?php if ( ! isset($current_user) ) $current_user = wp_get_current_user(); echo $current_user->ID; ?>','time':'<?php echo time(); ?>'};
323 var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
324 isRtl = <?php echo (int) is_rtl(); ?>;
325 //]]>
326 </script>
327 <?php
328 do_action('admin_enqueue_scripts', 'media-upload-popup');
329 do_action('admin_print_styles-media-upload-popup');
330 do_action('admin_print_styles');
331 do_action('admin_print_scripts-media-upload-popup');
332 do_action('admin_print_scripts');
333 do_action('admin_head-media-upload-popup');
334 do_action('admin_head');
335
336 if ( is_string($content_func) )
337         do_action( "admin_head_{$content_func}" );
338 ?>
339 </head>
340 <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?>>
341 <?php
342         $args = func_get_args();
343         $args = array_slice($args, 1);
344         call_user_func_array($content_func, $args);
345
346         do_action('admin_print_footer_scripts');
347 ?>
348 <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
349 </body>
350 </html>
351 <?php
352 }
353
354 /**
355  * {@internal Missing Short Description}}
356  *
357  * @since 2.5.0
358  */
359 function media_buttons() {
360         $do_image = $do_audio = $do_video = true;
361         if ( is_multisite() ) {
362                 $media_buttons = get_site_option( 'mu_media_buttons' );
363                 if ( empty($media_buttons['image']) )
364                         $do_image = false;
365                 if ( empty($media_buttons['audio']) )
366                         $do_audio = false;
367                 if ( empty($media_buttons['video']) )
368                         $do_video = false;
369         }
370         $out = '';
371
372         if ( $do_image )
373                 $out .= _media_button(__('Add an Image'), 'images/media-button-image.gif?ver=20100531', 'image');
374         if ( $do_video )
375                 $out .= _media_button(__('Add Video'), 'images/media-button-video.gif?ver=20100531', 'video');
376         if ( $do_audio )
377                 $out .= _media_button(__('Add Audio'), 'images/media-button-music.gif?ver=20100531', 'audio');
378
379         $out .= _media_button(__('Add Media'), 'images/media-button-other.gif?ver=20100531', 'media');
380
381         $context = apply_filters('media_buttons_context', __('Upload/Insert %s'));
382
383         printf($context, $out);
384 }
385 add_action( 'media_buttons', 'media_buttons' );
386
387 function _media_button($title, $icon, $type) {
388         return "<a href='" . esc_url( get_upload_iframe_src($type) ) . "' id='add_$type' class='thickbox' title='$title'><img src='" . esc_url( admin_url( $icon ) ) . "' alt='$title' onclick='return false;' /></a>";
389 }
390
391 function get_upload_iframe_src($type) {
392         global $post_ID, $temp_ID;
393         $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
394         $upload_iframe_src = add_query_arg('post_id', $uploading_iframe_ID, 'media-upload.php');
395
396         if ( 'media' != $type )
397                 $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
398         $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
399
400         return add_query_arg('TB_iframe', true, $upload_iframe_src);
401 }
402
403 /**
404  * {@internal Missing Short Description}}
405  *
406  * @since 2.5.0
407  *
408  * @return unknown
409  */
410 function media_upload_form_handler() {
411         check_admin_referer('media-form');
412
413         $errors = null;
414
415         if ( isset($_POST['send']) ) {
416                 $keys = array_keys($_POST['send']);
417                 $send_id = (int) array_shift($keys);
418         }
419
420         if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
421                 $post = $_post = get_post($attachment_id, ARRAY_A);
422                 if ( isset($attachment['post_content']) )
423                         $post['post_content'] = $attachment['post_content'];
424                 if ( isset($attachment['post_title']) )
425                         $post['post_title'] = $attachment['post_title'];
426                 if ( isset($attachment['post_excerpt']) )
427                         $post['post_excerpt'] = $attachment['post_excerpt'];
428                 if ( isset($attachment['menu_order']) )
429                         $post['menu_order'] = $attachment['menu_order'];
430
431                 if ( isset($send_id) && $attachment_id == $send_id ) {
432                         if ( isset($attachment['post_parent']) )
433                                 $post['post_parent'] = $attachment['post_parent'];
434                 }
435
436                 $post = apply_filters('attachment_fields_to_save', $post, $attachment);
437
438                 if ( isset($attachment['image_alt']) ) {
439                         $image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
440                         if ( $image_alt != stripslashes($attachment['image_alt']) ) {
441                                 $image_alt = wp_strip_all_tags( stripslashes($attachment['image_alt']), true );
442                                 // update_meta expects slashed
443                                 update_post_meta( $attachment_id, '_wp_attachment_image_alt', addslashes($image_alt) );
444                         }
445                 }
446
447                 if ( isset($post['errors']) ) {
448                         $errors[$attachment_id] = $post['errors'];
449                         unset($post['errors']);
450                 }
451
452                 if ( $post != $_post )
453                         wp_update_post($post);
454
455                 foreach ( get_attachment_taxonomies($post) as $t ) {
456                         if ( isset($attachment[$t]) )
457                                 wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
458                 }
459         }
460
461         if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
462                 <script type="text/javascript">
463                 /* <![CDATA[ */
464                 var win = window.dialogArguments || opener || parent || top;
465                 win.tb_remove();
466                 /* ]]> */
467                 </script>
468                 <?php
469                 exit;
470         }
471
472         if ( isset($send_id) ) {
473                 $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
474
475                 $html = $attachment['post_title'];
476                 if ( !empty($attachment['url']) ) {
477                         $rel = '';
478                         if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
479                                 $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
480                         $html = "<a href='{$attachment['url']}'$rel>$html</a>";
481                 }
482
483                 $html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
484                 return media_send_to_editor($html);
485         }
486
487         return $errors;
488 }
489
490 /**
491  * {@internal Missing Short Description}}
492  *
493  * @since 2.5.0
494  *
495  * @return unknown
496  */
497 function media_upload_image() {
498         $errors = array();
499         $id = 0;
500
501         if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
502                 // Upload File button was clicked
503                 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
504                 unset($_FILES);
505                 if ( is_wp_error($id) ) {
506                         $errors['upload_error'] = $id;
507                         $id = false;
508                 }
509         }
510
511         if ( !empty($_POST['insertonlybutton']) ) {
512                 $alt = $align = '';
513
514                 $src = $_POST['insertonly']['src'];
515                 if ( !empty($src) && !strpos($src, '://') )
516                         $src = "http://$src";
517                 $alt = esc_attr($_POST['insertonly']['alt']);
518                 if ( isset($_POST['insertonly']['align']) ) {
519                         $align = esc_attr($_POST['insertonly']['align']);
520                         $class = " class='align$align'";
521                 }
522                 if ( !empty($src) )
523                         $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
524
525                 $html = apply_filters('image_send_to_editor_url', $html, esc_url_raw($src), $alt, $align);
526                 return media_send_to_editor($html);
527         }
528
529         if ( !empty($_POST) ) {
530                 $return = media_upload_form_handler();
531
532                 if ( is_string($return) )
533                         return $return;
534                 if ( is_array($return) )
535                         $errors = $return;
536         }
537
538         if ( isset($_POST['save']) ) {
539                 $errors['upload_notice'] = __('Saved.');
540                 return media_upload_gallery();
541         }
542
543         if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
544                 return wp_iframe( 'media_upload_type_url_form', 'image', $errors, $id );
545
546         return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
547 }
548
549 /**
550  * Download an image from the specified URL and attach it to a post.
551  *
552  * @since 2.6.0
553  *
554  * @param string $file The URL of the image to download
555  * @param int $post_id The post ID the media is to be associated with
556  * @param string $desc Optional. Description of the image
557  * @return string|WP_Error Populated HTML img tag on success
558  */
559 function media_sideload_image($file, $post_id, $desc = null) {
560         if ( ! empty($file) ) {
561                 // Download file to temp location
562                 $tmp = download_url( $file );
563
564                 // Set variables for storage
565                 // fix file filename for query strings
566                 preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
567                 $file_array['name'] = basename($matches[0]);
568                 $file_array['tmp_name'] = $tmp;
569
570                 // If error storing temporarily, unlink
571                 if ( is_wp_error( $tmp ) ) {
572                         @unlink($file_array['tmp_name']);
573                         $file_array['tmp_name'] = '';
574                 }
575
576                 // do the validation and storage stuff
577                 $id = media_handle_sideload( $file_array, $post_id, $desc );
578                 // If error storing permanently, unlink
579                 if ( is_wp_error($id) ) {
580                         @unlink($file_array['tmp_name']);
581                         return $id;
582                 }
583
584                 $src = wp_get_attachment_url( $id );
585         }
586
587         // Finally check to make sure the file has been saved, then return the html
588         if ( ! empty($src) ) {
589                 $alt = isset($desc) ? esc_attr($desc) : '';
590                 $html = "<img src='$src' alt='$alt' />";
591                 return $html;
592         }
593 }
594
595 /**
596  * {@internal Missing Short Description}}
597  *
598  * @since 2.5.0
599  *
600  * @return unknown
601  */
602 function media_upload_audio() {
603         $errors = array();
604         $id = 0;
605
606         if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
607                 // Upload File button was clicked
608                 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
609                 unset($_FILES);
610                 if ( is_wp_error($id) ) {
611                         $errors['upload_error'] = $id;
612                         $id = false;
613                 }
614         }
615
616         if ( !empty($_POST['insertonlybutton']) ) {
617                 $href = $_POST['insertonly']['href'];
618                 if ( !empty($href) && !strpos($href, '://') )
619                         $href = "http://$href";
620
621                 $title = esc_attr($_POST['insertonly']['title']);
622                 if ( empty($title) )
623             $title = esc_attr( basename($href) );
624
625                 if ( !empty($title) && !empty($href) )
626             $html = "<a href='" . esc_url($href) . "' >$title</a>";
627
628                 $html = apply_filters('audio_send_to_editor_url', $html, $href, $title);
629
630                 return media_send_to_editor($html);
631         }
632
633         if ( !empty($_POST) ) {
634                 $return = media_upload_form_handler();
635
636                 if ( is_string($return) )
637                         return $return;
638                 if ( is_array($return) )
639                         $errors = $return;
640         }
641
642         if ( isset($_POST['save']) ) {
643                 $errors['upload_notice'] = __('Saved.');
644                 return media_upload_gallery();
645         }
646
647         if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
648                 return wp_iframe( 'media_upload_type_url_form', 'audio', $errors, $id );
649
650         return wp_iframe( 'media_upload_type_form', 'audio', $errors, $id );
651 }
652
653 /**
654  * {@internal Missing Short Description}}
655  *
656  * @since 2.5.0
657  *
658  * @return unknown
659  */
660 function media_upload_video() {
661         $errors = array();
662         $id = 0;
663
664         if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
665                 // Upload File button was clicked
666                 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
667                 unset($_FILES);
668                 if ( is_wp_error($id) ) {
669                         $errors['upload_error'] = $id;
670                         $id = false;
671                 }
672         }
673
674         if ( !empty($_POST['insertonlybutton']) ) {
675                 $href = $_POST['insertonly']['href'];
676                 if ( !empty($href) && !strpos($href, '://') )
677                         $href = "http://$href";
678
679                 $title = esc_attr($_POST['insertonly']['title']);
680         if ( empty($title) )
681             $title = esc_attr( basename($href) );
682
683                 if ( !empty($title) && !empty($href) )
684             $html = "<a href='" . esc_url($href) . "' >$title</a>";
685
686                 $html = apply_filters('video_send_to_editor_url', $html, $href, $title);
687
688                 return media_send_to_editor($html);
689         }
690
691         if ( !empty($_POST) ) {
692                 $return = media_upload_form_handler();
693
694                 if ( is_string($return) )
695                         return $return;
696                 if ( is_array($return) )
697                         $errors = $return;
698         }
699
700         if ( isset($_POST['save']) ) {
701                 $errors['upload_notice'] = __('Saved.');
702                 return media_upload_gallery();
703         }
704
705         if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
706                 return wp_iframe( 'media_upload_type_url_form', 'video', $errors, $id );
707
708         return wp_iframe( 'media_upload_type_form', 'video', $errors, $id );
709 }
710
711 /**
712  * {@internal Missing Short Description}}
713  *
714  * @since 2.5.0
715  *
716  * @return unknown
717  */
718 function media_upload_file() {
719         $errors = array();
720         $id = 0;
721
722         if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
723                 // Upload File button was clicked
724                 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
725                 unset($_FILES);
726                 if ( is_wp_error($id) ) {
727                         $errors['upload_error'] = $id;
728                         $id = false;
729                 }
730         }
731
732         if ( !empty($_POST['insertonlybutton']) ) {
733                 $href = $_POST['insertonly']['href'];
734                 if ( !empty($href) && !strpos($href, '://') )
735                         $href = "http://$href";
736
737                 $title = esc_attr($_POST['insertonly']['title']);
738                 if ( empty($title) )
739                         $title = basename($href);
740                 if ( !empty($title) && !empty($href) )
741                         $html = "<a href='" . esc_url($href) . "' >$title</a>";
742                 $html = apply_filters('file_send_to_editor_url', $html, esc_url_raw($href), $title);
743                 return media_send_to_editor($html);
744         }
745
746         if ( !empty($_POST) ) {
747                 $return = media_upload_form_handler();
748
749                 if ( is_string($return) )
750                         return $return;
751                 if ( is_array($return) )
752                         $errors = $return;
753         }
754
755         if ( isset($_POST['save']) ) {
756                 $errors['upload_notice'] = __('Saved.');
757                 return media_upload_gallery();
758         }
759
760         if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
761                 return wp_iframe( 'media_upload_type_url_form', 'file', $errors, $id );
762
763         return wp_iframe( 'media_upload_type_form', 'file', $errors, $id );
764 }
765
766 /**
767  * {@internal Missing Short Description}}
768  *
769  * @since 2.5.0
770  *
771  * @return unknown
772  */
773 function media_upload_gallery() {
774         $errors = array();
775
776         if ( !empty($_POST) ) {
777                 $return = media_upload_form_handler();
778
779                 if ( is_string($return) )
780                         return $return;
781                 if ( is_array($return) )
782                         $errors = $return;
783         }
784
785         wp_enqueue_script('admin-gallery');
786         return wp_iframe( 'media_upload_gallery_form', $errors );
787 }
788
789 /**
790  * {@internal Missing Short Description}}
791  *
792  * @since 2.5.0
793  *
794  * @return unknown
795  */
796 function media_upload_library() {
797         $errors = array();
798         if ( !empty($_POST) ) {
799                 $return = media_upload_form_handler();
800
801                 if ( is_string($return) )
802                         return $return;
803                 if ( is_array($return) )
804                         $errors = $return;
805         }
806
807         return wp_iframe( 'media_upload_library_form', $errors );
808 }
809
810 /**
811  * Retrieve HTML for the image alignment radio buttons with the specified one checked.
812  *
813  * @since 2.7.0
814  *
815  * @param unknown_type $post
816  * @param unknown_type $checked
817  * @return unknown
818  */
819 function image_align_input_fields( $post, $checked = '' ) {
820
821         if ( empty($checked) )
822                 $checked = get_user_setting('align', 'none');
823
824         $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
825         if ( !array_key_exists( (string) $checked, $alignments ) )
826                 $checked = 'none';
827
828         $out = array();
829         foreach ( $alignments as $name => $label ) {
830                 $name = esc_attr($name);
831                 $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
832                         ( $checked == $name ? " checked='checked'" : "" ) .
833                         " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
834         }
835         return join("\n", $out);
836 }
837
838 /**
839  * Retrieve HTML for the size radio buttons with the specified one checked.
840  *
841  * @since 2.7.0
842  *
843  * @param unknown_type $post
844  * @param unknown_type $check
845  * @return unknown
846  */
847 function image_size_input_fields( $post, $check = '' ) {
848
849                 // get a list of the actual pixel dimensions of each possible intermediate version of this image
850                 $size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size'));
851
852                 if ( empty($check) )
853                         $check = get_user_setting('imgsize', 'medium');
854
855                 foreach ( $size_names as $size => $label ) {
856                         $downsize = image_downsize($post->ID, $size);
857                         $checked = '';
858
859                         // is this size selectable?
860                         $enabled = ( $downsize[3] || 'full' == $size );
861                         $css_id = "image-size-{$size}-{$post->ID}";
862                         // if this size is the default but that's not available, don't select it
863                         if ( $size == $check ) {
864                                 if ( $enabled )
865                                         $checked = " checked='checked'";
866                                 else
867                                         $check = '';
868                         } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
869                                 // if $check is not enabled, default to the first available size that's bigger than a thumbnail
870                                 $check = $size;
871                                 $checked = " checked='checked'";
872                         }
873
874                         $html = "<div class='image-size-item'><input type='radio' " . disabled( $enabled, false, false ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />";
875
876                         $html .= "<label for='{$css_id}'>$label</label>";
877                         // only show the dimensions if that choice is available
878                         if ( $enabled )
879                                 $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
880
881                         $html .= '</div>';
882
883                         $out[] = $html;
884                 }
885
886                 return array(
887                         'label' => __('Size'),
888                         'input' => 'html',
889                         'html'  => join("\n", $out),
890                 );
891 }
892
893 /**
894  * Retrieve HTML for the Link URL buttons with the default link type as specified.
895  *
896  * @since 2.7.0
897  *
898  * @param unknown_type $post
899  * @param unknown_type $url_type
900  * @return unknown
901  */
902 function image_link_input_fields($post, $url_type = '') {
903
904         $file = wp_get_attachment_url($post->ID);
905         $link = get_attachment_link($post->ID);
906
907         if ( empty($url_type) )
908                 $url_type = get_user_setting('urlbutton', 'post');
909
910         $url = '';
911         if ( $url_type == 'file' )
912                 $url = $file;
913         elseif ( $url_type == 'post' )
914                 $url = $link;
915
916         return "
917         <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
918         <button type='button' class='button urlnone' title=''>" . __('None') . "</button>
919         <button type='button' class='button urlfile' title='" . esc_attr($file) . "'>" . __('File URL') . "</button>
920         <button type='button' class='button urlpost' title='" . esc_attr($link) . "'>" . __('Post URL') . "</button>
921 ";
922 }
923
924 /**
925  * {@internal Missing Short Description}}
926  *
927  * @since 2.5.0
928  *
929  * @param unknown_type $form_fields
930  * @param unknown_type $post
931  * @return unknown
932  */
933 function image_attachment_fields_to_edit($form_fields, $post) {
934         if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
935                 $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
936                 if ( empty($alt) )
937                         $alt = '';
938
939                 $form_fields['post_title']['required'] = true;
940
941                 $form_fields['image_alt'] = array(
942                         'value' => $alt,
943                         'label' => __('Alternate Text'),
944                         'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
945                 );
946
947                 $form_fields['align'] = array(
948                         'label' => __('Alignment'),
949                         'input' => 'html',
950                         'html'  => image_align_input_fields($post, get_option('image_default_align')),
951                 );
952
953                 $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
954
955         } else {
956                 unset( $form_fields['image_alt'] );
957         }
958         return $form_fields;
959 }
960
961 add_filter('attachment_fields_to_edit', 'image_attachment_fields_to_edit', 10, 2);
962
963 /**
964  * {@internal Missing Short Description}}
965  *
966  * @since 2.5.0
967  *
968  * @param unknown_type $form_fields
969  * @param unknown_type $post
970  * @return unknown
971  */
972 function media_single_attachment_fields_to_edit( $form_fields, $post ) {
973         unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
974         return $form_fields;
975 }
976
977 /**
978  * {@internal Missing Short Description}}
979  *
980  * @since 2.8.0
981  *
982  * @param unknown_type $form_fields
983  * @param unknown_type $post
984  * @return unknown
985  */
986 function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
987         unset($form_fields['image_url']);
988         return $form_fields;
989 }
990
991 /**
992  * {@internal Missing Short Description}}
993  *
994  * @since 2.5.0
995  *
996  * @param unknown_type $post
997  * @param unknown_type $attachment
998  * @return unknown
999  */
1000 function image_attachment_fields_to_save($post, $attachment) {
1001         if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
1002                 if ( strlen(trim($post['post_title'])) == 0 ) {
1003                         $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
1004                         $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
1005                 }
1006         }
1007
1008         return $post;
1009 }
1010
1011 add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
1012
1013 /**
1014  * {@internal Missing Short Description}}
1015  *
1016  * @since 2.5.0
1017  *
1018  * @param unknown_type $html
1019  * @param unknown_type $attachment_id
1020  * @param unknown_type $attachment
1021  * @return unknown
1022  */
1023 function image_media_send_to_editor($html, $attachment_id, $attachment) {
1024         $post =& get_post($attachment_id);
1025         if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1026                 $url = $attachment['url'];
1027                 $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
1028                 $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
1029                 $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
1030                 $rel = ( $url == get_attachment_link($attachment_id) );
1031
1032                 return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
1033         }
1034
1035         return $html;
1036 }
1037
1038 add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
1039
1040 /**
1041  * {@internal Missing Short Description}}
1042  *
1043  * @since 2.5.0
1044  *
1045  * @param unknown_type $post
1046  * @param unknown_type $errors
1047  * @return unknown
1048  */
1049 function get_attachment_fields_to_edit($post, $errors = null) {
1050         if ( is_int($post) )
1051                 $post =& get_post($post);
1052         if ( is_array($post) )
1053                 $post = (object) $post;
1054
1055         $image_url = wp_get_attachment_url($post->ID);
1056
1057         $edit_post = sanitize_post($post, 'edit');
1058
1059
1060
1061         $form_fields = array(
1062                 'post_title'   => array(
1063                         'label'      => __('Title'),
1064                         'value'      => $edit_post->post_title
1065                 ),
1066                 'image_alt'   => array(),
1067                 'post_excerpt' => array(
1068                         'label'      => __('Caption'),
1069                         'value'      => $edit_post->post_excerpt
1070                 ),
1071                 'post_content' => array(
1072                         'label'      => __('Description'),
1073                         'value'      => $edit_post->post_content,
1074                         'input'      => 'textarea'
1075                 ),
1076                 'url'          => array(
1077                         'label'      => __('Link URL'),
1078                         'input'      => 'html',
1079                         'html'       => image_link_input_fields($post, get_option('image_default_link_type')),
1080                         'helps'      => __('Enter a link URL or click above for presets.')
1081                 ),
1082                 'menu_order'   => array(
1083                         'label'      => __('Order'),
1084                         'value'      => $edit_post->menu_order
1085                 ),
1086                 'image_url'     => array(
1087                         'label'      => __('File URL'),
1088                         'input'      => 'html',
1089                         'html'       => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
1090                         'value'      => wp_get_attachment_url($post->ID),
1091                         'helps'      => __('Location of the uploaded file.')
1092                 )
1093         );
1094
1095         foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1096                 $t = (array) get_taxonomy($taxonomy);
1097                 if ( ! $t['public'] )
1098                         continue;
1099                 if ( empty($t['label']) )
1100                         $t['label'] = $taxonomy;
1101                 if ( empty($t['args']) )
1102                         $t['args'] = array();
1103
1104                 $terms = get_object_term_cache($post->ID, $taxonomy);
1105                 if ( empty($terms) )
1106                         $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1107
1108                 $values = array();
1109
1110                 foreach ( $terms as $term )
1111                         $values[] = $term->name;
1112                 $t['value'] = join(', ', $values);
1113
1114                 $form_fields[$taxonomy] = $t;
1115         }
1116
1117         // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1118         // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1119         $form_fields = array_merge_recursive($form_fields, (array) $errors);
1120
1121         $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
1122
1123         return $form_fields;
1124 }
1125
1126 /**
1127  * Retrieve HTML for media items of post gallery.
1128  *
1129  * The HTML markup retrieved will be created for the progress of SWF Upload
1130  * component. Will also create link for showing and hiding the form to modify
1131  * the image attachment.
1132  *
1133  * @since 2.5.0
1134  *
1135  * @param int $post_id Optional. Post ID.
1136  * @param array $errors Errors for attachment, if any.
1137  * @return string
1138  */
1139 function get_media_items( $post_id, $errors ) {
1140         $attachments = array();
1141         if ( $post_id ) {
1142                 $post = get_post($post_id);
1143                 if ( $post && $post->post_type == 'attachment' )
1144                         $attachments = array($post->ID => $post);
1145                 else
1146                         $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
1147         } else {
1148                 if ( is_array($GLOBALS['wp_the_query']->posts) )
1149                         foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
1150                                 $attachments[$attachment->ID] = $attachment;
1151         }
1152
1153         $output = '';
1154         foreach ( (array) $attachments as $id => $attachment ) {
1155                 if ( $attachment->post_status == 'trash' )
1156                         continue;
1157                 if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
1158                         $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div>$item\n</div>";
1159         }
1160
1161         return $output;
1162 }
1163
1164 /**
1165  * Retrieve HTML form for modifying the image attachment.
1166  *
1167  * @since 2.5.0
1168  *
1169  * @param int $attachment_id Attachment ID for modification.
1170  * @param string|array $args Optional. Override defaults.
1171  * @return string HTML form for attachment.
1172  */
1173 function get_media_item( $attachment_id, $args = null ) {
1174         global $redir_tab;
1175
1176         if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
1177                 $thumb_url = $thumb_url[0];
1178         else
1179                 $thumb_url = false;
1180
1181         $post = get_post( $attachment_id );
1182
1183         $default_args = array( 'errors' => null, 'send' => $post->post_parent ? post_type_supports( get_post_type( $post->post_parent ), 'editor' ) : true, 'delete' => true, 'toggle' => true, 'show_title' => true );
1184         $args = wp_parse_args( $args, $default_args );
1185         $args = apply_filters( 'get_media_item_args', $args );
1186         extract( $args, EXTR_SKIP );
1187
1188         $toggle_on  = __( 'Show' );
1189         $toggle_off = __( 'Hide' );
1190
1191         $filename = basename( $post->guid );
1192         $title = esc_attr( $post->post_title );
1193
1194         if ( $_tags = get_the_tags( $attachment_id ) ) {
1195                 foreach ( $_tags as $tag )
1196                         $tags[] = $tag->name;
1197                 $tags = esc_attr( join( ', ', $tags ) );
1198         }
1199
1200         $post_mime_types = get_post_mime_types();
1201         $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
1202         $type = array_shift( $keys );
1203         $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
1204
1205         $form_fields = get_attachment_fields_to_edit( $post, $errors );
1206
1207         if ( $toggle ) {
1208                 $class = empty( $errors ) ? 'startclosed' : 'startopen';
1209                 $toggle_links = "
1210         <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
1211         <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
1212         } else {
1213                 $class = 'form-table';
1214                 $toggle_links = '';
1215         }
1216
1217         $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
1218         $display_title = $show_title ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60 ) . "</span></div>" : '';
1219
1220         $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
1221         $order = '';
1222
1223         foreach ( $form_fields as $key => $val ) {
1224                 if ( 'menu_order' == $key ) {
1225                         if ( $gallery )
1226                                 $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='" . esc_attr( $val['value'] ). "' /></div>";
1227                         else
1228                                 $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
1229
1230                         unset( $form_fields['menu_order'] );
1231                         break;
1232                 }
1233         }
1234
1235         $media_dims = '';
1236         $meta = wp_get_attachment_metadata( $post->ID );
1237         if ( is_array( $meta ) && array_key_exists( 'width', $meta ) && array_key_exists( 'height', $meta ) )
1238                 $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
1239         $media_dims = apply_filters( 'media_meta', $media_dims, $post );
1240
1241         $image_edit_button = '';
1242         if ( gd_edit_image_support( $post->post_mime_type ) ) {
1243                 $nonce = wp_create_nonce( "image_editor-$post->ID" );
1244                 $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <img src='" . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . "' class='imgedit-wait-spin' alt='' />";
1245         }
1246
1247         $attachment_url = get_permalink( $attachment_id );
1248
1249         $item = "
1250         $type_html
1251         $toggle_links
1252         $order
1253         $display_title
1254         <table class='slidetoggle describe $class'>
1255                 <thead class='media-item-info' id='media-head-$post->ID'>
1256                 <tr valign='top'>
1257                         <td class='A1B1' id='thumbnail-head-$post->ID'>
1258                         <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' style='margin-top: 3px' /></a></p>
1259                         <p>$image_edit_button</p>
1260                         </td>
1261                         <td>
1262                         <p><strong>" . __('File name:') . "</strong> $filename</p>
1263                         <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
1264                         <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
1265                         if ( !empty( $media_dims ) )
1266                                 $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
1267
1268                         $item .= "</td></tr>\n";
1269
1270
1271
1272         $item .= "
1273                 </thead>
1274                 <tbody>
1275                 <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
1276                 <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
1277
1278         $defaults = array(
1279                 'input'      => 'text',
1280                 'required'   => false,
1281                 'value'      => '',
1282                 'extra_rows' => array(),
1283         );
1284
1285         if ( $send )
1286                 $send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
1287         if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
1288                 if ( !EMPTY_TRASH_DAYS ) {
1289                         $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Delete Permanently' ) . '</a>';
1290                 } elseif ( !MEDIA_TRASH ) {
1291                         $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
1292                          <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "
1293                          <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
1294                          <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
1295                          </div>";
1296                 } else {
1297                         $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
1298                         <a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-attachment_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
1299                 }
1300         } else {
1301                 $delete = '';
1302         }
1303
1304         $thumbnail = '';
1305         $calling_post_id = 0;
1306         if ( isset( $_GET['post_id'] ) )
1307                 $calling_post_id = absint( $_GET['post_id'] );
1308         elseif ( isset( $_POST ) && count( $_POST ) ) // Like for async-upload where $_GET['post_id'] isn't set
1309                 $calling_post_id = $post->post_parent;
1310         if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
1311                 $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
1312                 $thumbnail = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail(\"$attachment_id\", \"$ajax_nonce\");return false;'>" . esc_html__( "Use as featured image" ) . "</a>";
1313         }
1314
1315         if ( ( $send || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) )
1316                 $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $thumbnail $delete</td></tr>\n" );
1317
1318         $hidden_fields = array();
1319
1320         foreach ( $form_fields as $id => $field ) {
1321                 if ( $id[0] == '_' )
1322                         continue;
1323
1324                 if ( !empty( $field['tr'] ) ) {
1325                         $item .= $field['tr'];
1326                         continue;
1327                 }
1328
1329                 $field = array_merge( $defaults, $field );
1330                 $name = "attachments[$attachment_id][$id]";
1331
1332                 if ( $field['input'] == 'hidden' ) {
1333                         $hidden_fields[$name] = $field['value'];
1334                         continue;
1335                 }
1336
1337                 $required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1338                 $aria_required = $field['required'] ? " aria-required='true' " : '';
1339                 $class  = $id;
1340                 $class .= $field['required'] ? ' form-required' : '';
1341
1342                 $item .= "\t\t<tr class='$class'>\n\t\t\t<th valign='top' scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label></th>\n\t\t\t<td class='field'>";
1343                 if ( !empty( $field[ $field['input'] ] ) )
1344                         $item .= $field[ $field['input'] ];
1345                 elseif ( $field['input'] == 'textarea' ) {
1346                         if ( user_can_richedit() ) { // textarea_escaped when user_can_richedit() = false
1347                                 $field['value'] = esc_textarea( $field['value'] );
1348                         }
1349                         $item .= "<textarea type='text' id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1350                 } else {
1351                         $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
1352                 }
1353                 if ( !empty( $field['helps'] ) )
1354                         $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1355                 $item .= "</td>\n\t\t</tr>\n";
1356
1357                 $extra_rows = array();
1358
1359                 if ( !empty( $field['errors'] ) )
1360                         foreach ( array_unique( (array) $field['errors'] ) as $error )
1361                                 $extra_rows['error'][] = $error;
1362
1363                 if ( !empty( $field['extra_rows'] ) )
1364                         foreach ( $field['extra_rows'] as $class => $rows )
1365                                 foreach ( (array) $rows as $html )
1366                                         $extra_rows[$class][] = $html;
1367
1368                 foreach ( $extra_rows as $class => $rows )
1369                         foreach ( $rows as $html )
1370                                 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1371         }
1372
1373         if ( !empty( $form_fields['_final'] ) )
1374                 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1375         $item .= "\t</tbody>\n";
1376         $item .= "\t</table>\n";
1377
1378         foreach ( $hidden_fields as $name => $value )
1379                 $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
1380
1381         if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
1382                 $parent = (int) $_REQUEST['post_id'];
1383                 $parent_name = "attachments[$attachment_id][post_parent]";
1384                 $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
1385         }
1386
1387         return $item;
1388 }
1389
1390 /**
1391  * {@internal Missing Short Description}}
1392  *
1393  * @since 2.5.0
1394  */
1395 function media_upload_header() {
1396         ?>
1397         <script type="text/javascript">post_id = <?php echo intval($_REQUEST['post_id']); ?>;</script>
1398         <div id="media-upload-header">
1399         <?php the_media_upload_tabs(); ?>
1400         </div>
1401         <?php
1402 }
1403
1404 /**
1405  * {@internal Missing Short Description}}
1406  *
1407  * @since 2.5.0
1408  *
1409  * @param unknown_type $errors
1410  */
1411 function media_upload_form( $errors = null ) {
1412         global $type, $tab, $pagenow;
1413
1414         $flash_action_url = admin_url('async-upload.php');
1415
1416         // If Mac and mod_security, no Flash. :(
1417         $flash = true;
1418         if ( false !== stripos($_SERVER['HTTP_USER_AGENT'], 'mac') && apache_mod_loaded('mod_security') )
1419                 $flash = false;
1420
1421         $flash = apply_filters('flash_uploader', $flash);
1422         $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
1423
1424         $upload_size_unit = $max_upload_size =  wp_max_upload_size();
1425         $sizes = array( 'KB', 'MB', 'GB' );
1426         for ( $u = -1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u++ )
1427                 $upload_size_unit /= 1024;
1428         if ( $u < 0 ) {
1429                 $upload_size_unit = 0;
1430                 $u = 0;
1431         } else {
1432                 $upload_size_unit = (int) $upload_size_unit;
1433         }
1434 ?>
1435 <script type="text/javascript">
1436 //<![CDATA[
1437 var uploaderMode = 0;
1438 jQuery(document).ready(function($){
1439         uploaderMode = getUserSetting('uploader');
1440         $('.upload-html-bypass a').click(function(){deleteUserSetting('uploader');uploaderMode=0;swfuploadPreLoad();return false;});
1441         $('.upload-flash-bypass a').click(function(){setUserSetting('uploader', '1');uploaderMode=1;swfuploadPreLoad();return false;});
1442 });
1443 //]]>
1444 </script>
1445 <div id="media-upload-notice">
1446 <?php if (isset($errors['upload_notice']) ) { ?>
1447         <?php echo $errors['upload_notice']; ?>
1448 <?php } ?>
1449 </div>
1450 <div id="media-upload-error">
1451 <?php if (isset($errors['upload_error']) && is_wp_error($errors['upload_error'])) { ?>
1452         <?php echo $errors['upload_error']->get_error_message(); ?>
1453 <?php } ?>
1454 </div>
1455 <?php
1456 // Check quota for this blog if multisite
1457 if ( is_multisite() && !is_upload_space_available() ) {
1458         echo '<p>' . sprintf( __( 'Sorry, you have filled your storage quota (%s MB).' ), get_space_allowed() ) . '</p>';
1459         return;
1460 }
1461
1462 do_action('pre-upload-ui');
1463
1464 if ( $flash ) :
1465
1466 // Set the post params, which SWFUpload will post back with the file, and pass
1467 // them through a filter.
1468 $post_params = array(
1469                 "post_id" => $post_id,
1470                 "auth_cookie" => (is_ssl() ? $_COOKIE[SECURE_AUTH_COOKIE] : $_COOKIE[AUTH_COOKIE]),
1471                 "logged_in_cookie" => $_COOKIE[LOGGED_IN_COOKIE],
1472                 "_wpnonce" => wp_create_nonce('media-form'),
1473                 "type" => $type,
1474                 "tab" => $tab,
1475                 "short" => "1",
1476 );
1477 $post_params = apply_filters( 'swfupload_post_params', $post_params );
1478 $p = array();
1479 foreach ( $post_params as $param => $val )
1480         $p[] = "\t\t'$param' : '$val'";
1481 $post_params_str = implode( ", \n", $p );
1482
1483 // #8545. wmode=transparent cannot be used with SWFUpload
1484 if ( 'media-new.php' == $pagenow ) {
1485         $upload_image_path = get_user_option( 'admin_color' );
1486         if ( 'classic' != $upload_image_path )
1487                 $upload_image_path = 'fresh';
1488         $upload_image_path = admin_url( 'images/upload-' . $upload_image_path . '.png?ver=20101205' );
1489 } else {
1490         $upload_image_path = includes_url( 'images/upload.png?ver=20100531' );
1491 }
1492
1493 ?>
1494 <script type="text/javascript">
1495 //<![CDATA[
1496 var swfu;
1497 SWFUpload.onload = function() {
1498         var settings = {
1499                         button_text: '<span class="button"><?php _e('Select Files'); ?><\/span>',
1500                         button_text_style: '.button { text-align: center; font-weight: bold; font-family:"Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; font-size: 11px; text-shadow: 0 1px 0 #FFFFFF; color:#464646; }',
1501                         button_height: "23",
1502                         button_width: "132",
1503                         button_text_top_padding: 3,
1504                         button_image_url: '<?php echo $upload_image_path; ?>',
1505                         button_placeholder_id: "flash-browse-button",
1506                         upload_url : "<?php echo esc_attr( $flash_action_url ); ?>",
1507                         flash_url : "<?php echo includes_url('js/swfupload/swfupload.swf'); ?>",
1508                         file_post_name: "async-upload",
1509                         file_types: "<?php echo apply_filters('upload_file_glob', '*.*'); ?>",
1510                         post_params : {
1511                                 <?php echo $post_params_str; ?>
1512                         },
1513                         file_size_limit : "<?php echo $max_upload_size; ?>b",
1514                         file_dialog_start_handler : fileDialogStart,
1515                         file_queued_handler : fileQueued,
1516                         upload_start_handler : uploadStart,
1517                         upload_progress_handler : uploadProgress,
1518                         upload_error_handler : uploadError,
1519                         upload_success_handler : <?php echo apply_filters( 'swfupload_success_handler', 'uploadSuccess' ); ?>,
1520                         upload_complete_handler : uploadComplete,
1521                         file_queue_error_handler : fileQueueError,
1522                         file_dialog_complete_handler : fileDialogComplete,
1523                         swfupload_pre_load_handler: swfuploadPreLoad,
1524                         swfupload_load_failed_handler: swfuploadLoadFailed,
1525                         custom_settings : {
1526                                 degraded_element_id : "html-upload-ui", // id of the element displayed when swfupload is unavailable
1527                                 swfupload_element_id : "flash-upload-ui" // id of the element displayed when swfupload is available
1528                         },
1529                         debug: false
1530                 };
1531                 swfu = new SWFUpload(settings);
1532 };
1533 //]]>
1534 </script>
1535
1536 <div id="flash-upload-ui" class="hide-if-no-js">
1537 <?php do_action('pre-flash-upload-ui'); ?>
1538
1539         <div>
1540         <?php _e( 'Choose files to upload' ); ?>
1541         <div id="flash-browse-button"></div>
1542         <span><input id="cancel-upload" disabled="disabled" onclick="cancelUpload()" type="button" value="<?php esc_attr_e('Cancel Upload'); ?>" class="button" /></span>
1543         </div>
1544         <p class="media-upload-size"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[$u] ); ?></p>
1545 <?php do_action('post-flash-upload-ui'); ?>
1546         <p class="howto"><?php _e('After a file has been uploaded, you can add titles and descriptions.'); ?></p>
1547 </div>
1548 <?php endif; // $flash ?>
1549
1550 <div id="html-upload-ui">
1551 <?php do_action('pre-html-upload-ui'); ?>
1552         <p id="async-upload-wrap">
1553                 <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
1554                 <input type="file" name="async-upload" id="async-upload" />
1555                 <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
1556                 <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
1557         </p>
1558         <div class="clear"></div>
1559         <p class="media-upload-size"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[$u] ); ?></p>
1560         <?php if ( is_lighttpd_before_150() ): ?>
1561         <p><?php _e('If you want to use all capabilities of the uploader, like uploading multiple files at once, please update to lighttpd 1.5.'); ?></p>
1562         <?php endif;?>
1563 <?php do_action('post-html-upload-ui', $flash); ?>
1564 </div>
1565 <?php do_action('post-upload-ui'); ?>
1566 <?php
1567 }
1568
1569 /**
1570  * {@internal Missing Short Description}}
1571  *
1572  * @since 2.5.0
1573  *
1574  * @param unknown_type $type
1575  * @param unknown_type $errors
1576  * @param unknown_type $id
1577  */
1578 function media_upload_type_form($type = 'file', $errors = null, $id = null) {
1579         media_upload_header();
1580
1581         $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
1582
1583         $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1584         $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1585 ?>
1586
1587 <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
1588 <?php submit_button( '', 'hidden', 'save', false ); ?>
1589 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1590 <?php wp_nonce_field('media-form'); ?>
1591
1592 <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
1593
1594 <?php media_upload_form( $errors ); ?>
1595
1596 <script type="text/javascript">
1597 //<![CDATA[
1598 jQuery(function($){
1599         var preloaded = $(".media-item.preloaded");
1600         if ( preloaded.length > 0 ) {
1601                 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1602         }
1603         updateMediaForm();
1604 });
1605 //]]>
1606 </script>
1607 <div id="media-items">
1608 <?php
1609 if ( $id ) {
1610         if ( !is_wp_error($id) ) {
1611                 add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
1612                 echo get_media_items( $id, $errors );
1613         } else {
1614                 echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div>';
1615                 exit;
1616         }
1617 }
1618 ?>
1619 </div>
1620 <p class="savebutton ml-submit">
1621 <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
1622 </p>
1623 </form>
1624 <?php
1625 }
1626
1627 /**
1628  * {@internal Missing Short Description}}
1629  *
1630  * @since 2.7.0
1631  *
1632  * @param unknown_type $type
1633  * @param unknown_type $errors
1634  * @param unknown_type $id
1635  */
1636 function media_upload_type_url_form($type = 'file', $errors = null, $id = null) {
1637         media_upload_header();
1638
1639         $post_id = intval($_REQUEST['post_id']);
1640
1641         $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1642         $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1643
1644         $callback = "type_url_form_$type";
1645 ?>
1646
1647 <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
1648 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1649 <?php wp_nonce_field('media-form'); ?>
1650
1651 <?php if ( is_callable($callback) ) { ?>
1652
1653 <h3 class="media-title"><?php _e('Add media file from URL'); ?></h3>
1654
1655 <script type="text/javascript">
1656 //<![CDATA[
1657 var addExtImage = {
1658
1659         width : '',
1660         height : '',
1661         align : 'alignnone',
1662
1663         insert : function() {
1664                 var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
1665
1666                 if ( '' == f.src.value || '' == t.width )
1667                         return false;
1668
1669                 if ( f.title.value ) {
1670                         title = f.title.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1671                         title = ' title="'+title+'"';
1672                 }
1673
1674                 if ( f.alt.value )
1675                         alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1676
1677 <?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
1678                 if ( f.caption.value )
1679                         caption = f.caption.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1680 <?php } ?>
1681
1682                 cls = caption ? '' : ' class="'+t.align+'"';
1683
1684                 html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
1685
1686                 if ( f.url.value )
1687                         html = '<a href="'+f.url.value+'">'+html+'</a>';
1688
1689                 if ( caption )
1690                         html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
1691
1692                 var win = window.dialogArguments || opener || parent || top;
1693                 win.send_to_editor(html);
1694                 return false;
1695         },
1696
1697         resetImageData : function() {
1698                 var t = addExtImage;
1699
1700                 t.width = t.height = '';
1701                 document.getElementById('go_button').style.color = '#bbb';
1702                 if ( ! document.forms[0].src.value )
1703                         document.getElementById('status_img').innerHTML = '*';
1704                 else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
1705         },
1706
1707         updateImageData : function() {
1708                 var t = addExtImage;
1709
1710                 t.width = t.preloadImg.width;
1711                 t.height = t.preloadImg.height;
1712                 document.getElementById('go_button').style.color = '#333';
1713                 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
1714         },
1715
1716         getImageData : function() {
1717                 var t = addExtImage, src = document.forms[0].src.value;
1718
1719                 if ( ! src ) {
1720                         t.resetImageData();
1721                         return false;
1722                 }
1723                 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />';
1724                 t.preloadImg = new Image();
1725                 t.preloadImg.onload = t.updateImageData;
1726                 t.preloadImg.onerror = t.resetImageData;
1727                 t.preloadImg.src = src;
1728         }
1729 }
1730 //]]>
1731 </script>
1732
1733 <div id="media-items">
1734 <div class="media-item media-blank">
1735 <?php echo apply_filters($callback, call_user_func($callback)); ?>
1736 </div>
1737 </div>
1738 </form>
1739 <?php
1740         } else {
1741                 wp_die( __('Unknown action.') );
1742         }
1743 }
1744
1745 /**
1746  * {@internal Missing Short Description}}
1747  *
1748  * @since 2.5.0
1749  *
1750  * @param unknown_type $errors
1751  */
1752 function media_upload_gallery_form($errors) {
1753         global $redir_tab, $type;
1754
1755         $redir_tab = 'gallery';
1756         media_upload_header();
1757
1758         $post_id = intval($_REQUEST['post_id']);
1759         $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
1760         $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1761 ?>
1762
1763 <script type="text/javascript">
1764 <!--
1765 jQuery(function($){
1766         var preloaded = $(".media-item.preloaded");
1767         if ( preloaded.length > 0 ) {
1768                 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1769                 updateMediaForm();
1770         }
1771 });
1772 -->
1773 </script>
1774 <div id="sort-buttons" class="hide-if-no-js">
1775 <span>
1776 <?php _e('All Tabs:'); ?>
1777 <a href="#" id="showall"><?php _e('Show'); ?></a>
1778 <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
1779 </span>
1780 <?php _e('Sort Order:'); ?>
1781 <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
1782 <a href="#" id="desc"><?php _e('Descending'); ?></a> |
1783 <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
1784 </div>
1785 <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="gallery-form">
1786 <?php wp_nonce_field('media-form'); ?>
1787 <?php //media_upload_form( $errors ); ?>
1788 <table class="widefat" cellspacing="0">
1789 <thead><tr>
1790 <th><?php _e('Media'); ?></th>
1791 <th class="order-head"><?php _e('Order'); ?></th>
1792 <th class="actions-head"><?php _e('Actions'); ?></th>
1793 </tr></thead>
1794 </table>
1795 <div id="media-items">
1796 <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
1797 <?php echo get_media_items($post_id, $errors); ?>
1798 </div>
1799
1800 <p class="ml-submit">
1801 <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
1802 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1803 <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
1804 <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
1805 </p>
1806
1807 <div id="gallery-settings" style="display:none;">
1808 <div class="title"><?php _e('Gallery Settings'); ?></div>
1809 <table id="basic" class="describe"><tbody>
1810         <tr>
1811         <th scope="row" class="label">
1812                 <label>
1813                 <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
1814                 </label>
1815         </th>
1816         <td class="field">
1817                 <input type="radio" name="linkto" id="linkto-file" value="file" />
1818                 <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
1819
1820                 <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
1821                 <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
1822         </td>
1823         </tr>
1824
1825         <tr>
1826         <th scope="row" class="label">
1827                 <label>
1828                 <span class="alignleft"><?php _e('Order images by:'); ?></span>
1829                 </label>
1830         </th>
1831         <td class="field">
1832                 <select id="orderby" name="orderby">
1833                         <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
1834                         <option value="title"><?php _e('Title'); ?></option>
1835                         <option value="ID"><?php _e('Date/Time'); ?></option>
1836                         <option value="rand"><?php _e('Random'); ?></option>
1837                 </select>
1838         </td>
1839         </tr>
1840
1841         <tr>
1842         <th scope="row" class="label">
1843                 <label>
1844                 <span class="alignleft"><?php _e('Order:'); ?></span>
1845                 </label>
1846         </th>
1847         <td class="field">
1848                 <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
1849                 <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
1850
1851                 <input type="radio" name="order" id="order-desc" value="desc" />
1852                 <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
1853         </td>
1854         </tr>
1855
1856         <tr>
1857         <th scope="row" class="label">
1858                 <label>
1859                 <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
1860                 </label>
1861         </th>
1862         <td class="field">
1863                 <select id="columns" name="columns">
1864                         <option value="1">1</option>
1865                         <option value="2">2</option>
1866                         <option value="3" selected="selected">3</option>
1867                         <option value="4">4</option>
1868                         <option value="5">5</option>
1869                         <option value="6">6</option>
1870                         <option value="7">7</option>
1871                         <option value="8">8</option>
1872                         <option value="9">9</option>
1873                 </select>
1874         </td>
1875         </tr>
1876 </tbody></table>
1877
1878 <p class="ml-submit">
1879 <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
1880 <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
1881 </p>
1882 </div>
1883 </form>
1884 <?php
1885 }
1886
1887 /**
1888  * {@internal Missing Short Description}}
1889  *
1890  * @since 2.5.0
1891  *
1892  * @param unknown_type $errors
1893  */
1894 function media_upload_library_form($errors) {
1895         global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
1896
1897         media_upload_header();
1898
1899         $post_id = intval($_REQUEST['post_id']);
1900
1901         $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
1902         $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1903
1904         $_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
1905         if ( $_GET['paged'] < 1 )
1906                 $_GET['paged'] = 1;
1907         $start = ( $_GET['paged'] - 1 ) * 10;
1908         if ( $start < 1 )
1909                 $start = 0;
1910         add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
1911
1912         list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
1913
1914 ?>
1915
1916 <form id="filter" action="" method="get">
1917 <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
1918 <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
1919 <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
1920 <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
1921
1922 <p id="media-search" class="search-box">
1923         <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
1924         <input type="text" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
1925         <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
1926 </p>
1927
1928 <ul class="subsubsub">
1929 <?php
1930 $type_links = array();
1931 $_num_posts = (array) wp_count_attachments();
1932 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
1933 foreach ( $matches as $_type => $reals )
1934         foreach ( $reals as $real )
1935                 if ( isset($num_posts[$_type]) )
1936                         $num_posts[$_type] += $_num_posts[$real];
1937                 else
1938                         $num_posts[$_type] = $_num_posts[$real];
1939 // If available type specified by media button clicked, filter by that type
1940 if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
1941         $_GET['post_mime_type'] = $type;
1942         list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
1943 }
1944 if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
1945         $class = ' class="current"';
1946 else
1947         $class = '';
1948 $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
1949 foreach ( $post_mime_types as $mime_type => $label ) {
1950         $class = '';
1951
1952         if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
1953                 continue;
1954
1955         if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
1956                 $class = ' class="current"';
1957
1958         $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
1959 }
1960 echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
1961 unset($type_links);
1962 ?>
1963 </ul>
1964
1965 <div class="tablenav">
1966
1967 <?php
1968 $page_links = paginate_links( array(
1969         'base' => add_query_arg( 'paged', '%#%' ),
1970         'format' => '',
1971         'prev_text' => __('&laquo;'),
1972         'next_text' => __('&raquo;'),
1973         'total' => ceil($wp_query->found_posts / 10),
1974         'current' => $_GET['paged']
1975 ));
1976
1977 if ( $page_links )
1978         echo "<div class='tablenav-pages'>$page_links</div>";
1979 ?>
1980
1981 <div class="alignleft actions">
1982 <?php
1983
1984 $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
1985
1986 $arc_result = $wpdb->get_results( $arc_query );
1987
1988 $month_count = count($arc_result);
1989
1990 if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
1991 <select name='m'>
1992 <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
1993 <?php
1994 foreach ($arc_result as $arc_row) {
1995         if ( $arc_row->yyear == 0 )
1996                 continue;
1997         $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
1998
1999         if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
2000                 $default = ' selected="selected"';
2001         else
2002                 $default = '';
2003
2004         echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
2005         echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
2006         echo "</option>\n";
2007 }
2008 ?>
2009 </select>
2010 <?php } ?>
2011
2012 <?php submit_button( __( 'Filter &#187;' ), 'secondary', 'post-query-submit', false ); ?>
2013
2014 </div>
2015
2016 <br class="clear" />
2017 </div>
2018 </form>
2019
2020 <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="library-form">
2021
2022 <?php wp_nonce_field('media-form'); ?>
2023 <?php //media_upload_form( $errors ); ?>
2024
2025 <script type="text/javascript">
2026 <!--
2027 jQuery(function($){
2028         var preloaded = $(".media-item.preloaded");
2029         if ( preloaded.length > 0 ) {
2030                 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2031                 updateMediaForm();
2032         }
2033 });
2034 -->
2035 </script>
2036
2037 <div id="media-items">
2038 <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2039 <?php echo get_media_items(null, $errors); ?>
2040 </div>
2041 <p class="ml-submit">
2042 <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
2043 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2044 </p>
2045 </form>
2046 <?php
2047 }
2048
2049 /**
2050  * {@internal Missing Short Description}}
2051  *
2052  * @since 2.7.0
2053  *
2054  * @return unknown
2055  */
2056 function type_url_form_image() {
2057         if ( !apply_filters( 'disable_captions', '' ) ) {
2058                 $caption = '
2059                 <tr>
2060                         <th valign="top" scope="row" class="label">
2061                                 <span class="alignleft"><label for="caption">' . __('Image Caption') . '</label></span>
2062                         </th>
2063                         <td class="field"><input id="caption" name="caption" value="" type="text" /></td>
2064                 </tr>
2065 ';
2066         } else {
2067                 $caption = '';
2068         }
2069
2070         $default_align = get_option('image_default_align');
2071         if ( empty($default_align) )
2072                 $default_align = 'none';
2073
2074         return '
2075         <h4 class="media-sub-title">' . __('Insert an image from another web site') . '</h4>
2076         <table class="describe"><tbody>
2077                 <tr>
2078                         <th valign="top" scope="row" class="label" style="width:130px;">
2079                                 <span class="alignleft"><label for="src">' . __('Image URL') . '</label></span>
2080                                 <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
2081                         </th>
2082                         <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
2083                 </tr>
2084
2085                 <tr>
2086                         <th valign="top" scope="row" class="label">
2087                                 <span class="alignleft"><label for="title">' . __('Image Title') . '</label></span>
2088                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2089                         </th>
2090                         <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
2091                 </tr>
2092
2093                 <tr>
2094                         <th valign="top" scope="row" class="label">
2095                                 <span class="alignleft"><label for="alt">' . __('Alternate Text') . '</label></span>
2096                         </th>
2097                         <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
2098                         <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
2099                 </tr>
2100                 ' . $caption . '
2101                 <tr class="align">
2102                         <th valign="top" scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
2103                         <td class="field">
2104                                 <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
2105                                 <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
2106                                 <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
2107                                 <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
2108                                 <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
2109                                 <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
2110                                 <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
2111                                 <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
2112                         </td>
2113                 </tr>
2114
2115                 <tr>
2116                         <th valign="top" scope="row" class="label">
2117                                 <span class="alignleft"><label for="url">' . __('Link Image To:') . '</label></span>
2118                         </th>
2119                         <td class="field"><input id="url" name="url" value="" type="text" /><br />
2120
2121                         <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
2122                         <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
2123                         <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
2124                 </tr>
2125         ' . _insert_into_post_button('image') . '
2126         </tbody></table>
2127 ';
2128
2129 }
2130
2131 /**
2132  * {@internal Missing Short Description}}
2133  *
2134  * @since 2.7.0
2135  *
2136  * @return unknown
2137  */
2138 function type_url_form_audio() {
2139         return '
2140         <table class="describe"><tbody>
2141                 <tr>
2142                         <th valign="top" scope="row" class="label">
2143                                 <span class="alignleft"><label for="insertonly[href]">' . __('Audio File URL') . '</label></span>
2144                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2145                         </th>
2146                         <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
2147                 </tr>
2148                 <tr>
2149                         <th valign="top" scope="row" class="label">
2150                                 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
2151                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2152                         </th>
2153                         <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
2154                 </tr>
2155                 <tr><td></td><td class="help">' . __('Link text, e.g. &#8220;Still Alive by Jonathan Coulton&#8221;') . '</td></tr>
2156         ' . _insert_into_post_button('audio') . '
2157         </tbody></table>
2158 ';
2159 }
2160
2161 /**
2162  * {@internal Missing Short Description}}
2163  *
2164  * @since 2.7.0
2165  *
2166  * @return unknown
2167  */
2168 function type_url_form_video() {
2169         return '
2170         <table class="describe"><tbody>
2171                 <tr>
2172                         <th valign="top" scope="row" class="label">
2173                                 <span class="alignleft"><label for="insertonly[href]">' . __('Video URL') . '</label></span>
2174                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2175                         </th>
2176                         <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
2177                 </tr>
2178                 <tr>
2179                         <th valign="top" scope="row" class="label">
2180                                 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
2181                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2182                         </th>
2183                         <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
2184                 </tr>
2185                 <tr><td></td><td class="help">' . __('Link text, e.g. &#8220;Lucy on YouTube&#8221;') . '</td></tr>
2186         ' . _insert_into_post_button('video') . '
2187         </tbody></table>
2188 ';
2189 }
2190
2191 /**
2192  * {@internal Missing Short Description}}
2193  *
2194  * @since 2.7.0
2195  *
2196  * @return unknown
2197  */
2198 function type_url_form_file() {
2199         return '
2200         <table class="describe"><tbody>
2201                 <tr>
2202                         <th valign="top" scope="row" class="label">
2203                                 <span class="alignleft"><label for="insertonly[href]">' . __('URL') . '</label></span>
2204                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2205                         </th>
2206                         <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
2207                 </tr>
2208                 <tr>
2209                         <th valign="top" scope="row" class="label">
2210                                 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
2211                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2212                         </th>
2213                         <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
2214                 </tr>
2215                 <tr><td></td><td class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</td></tr>
2216         ' . _insert_into_post_button('file') . '
2217         </tbody></table>
2218 ';
2219 }
2220
2221
2222 function _insert_into_post_button($type) {
2223         if ( !post_type_supports(get_post_type($_GET['post_id']), 'editor') )
2224                 return '';
2225
2226         if ( 'image' == $type )
2227         return '
2228                 <tr>
2229                         <td></td>
2230                         <td>
2231                                 <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
2232                         </td>
2233                 </tr>
2234         ';
2235
2236         return '
2237                 <tr>
2238                         <td></td>
2239                         <td>
2240                                 ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
2241                         </td>
2242                 </tr>
2243         ';
2244 }
2245
2246 /**
2247  * {@internal Missing Short Description}}
2248  *
2249  * Support a GET parameter for disabling the flash uploader.
2250  *
2251  * @since 2.6.0
2252  *
2253  * @param unknown_type $flash
2254  * @return unknown
2255  */
2256 function media_upload_use_flash($flash) {
2257         if ( array_key_exists('flash', $_REQUEST) )
2258                 $flash = !empty($_REQUEST['flash']);
2259         return $flash;
2260 }
2261
2262 add_filter('flash_uploader', 'media_upload_use_flash');
2263
2264 /**
2265  * {@internal Missing Short Description}}
2266  *
2267  * @since 2.6.0
2268  */
2269 function media_upload_flash_bypass() {
2270         echo '<p class="upload-flash-bypass">';
2271         printf( __('You are using the Flash uploader.  Problems?  Try the <a href="%s">Browser uploader</a> instead.'), esc_url(add_query_arg('flash', 0)) );
2272         echo '</p>';
2273 }
2274
2275 /**
2276  * {@internal Missing Short Description}}
2277  *
2278  * @since 2.6.0
2279  */
2280 function media_upload_html_bypass($flash = true) {
2281         echo '<p class="upload-html-bypass hide-if-no-js">';
2282         _e('You are using the Browser uploader.');
2283         if ( $flash ) {
2284                 // the user manually selected the browser uploader, so let them switch back to Flash
2285                 echo ' ';
2286                 printf( __('Try the <a href="%s">Flash uploader</a> instead.'), esc_url(add_query_arg('flash', 1)) );
2287         }
2288         echo "</p>\n";
2289 }
2290
2291 add_action('post-flash-upload-ui', 'media_upload_flash_bypass');
2292 add_action('post-html-upload-ui', 'media_upload_html_bypass');
2293
2294 /**
2295  * {@internal Missing Short Description}}
2296  *
2297  * Make sure the GET parameter sticks when we submit a form.
2298  *
2299  * @since 2.6.0
2300  *
2301  * @param unknown_type $url
2302  * @return unknown
2303  */
2304 function media_upload_bypass_url($url) {
2305         if ( array_key_exists('flash', $_REQUEST) )
2306                 $url = add_query_arg('flash', intval($_REQUEST['flash']));
2307         return $url;
2308 }
2309
2310 add_filter('media_upload_form_url', 'media_upload_bypass_url');
2311
2312 add_filter('async_upload_image', 'get_media_item', 10, 2);
2313 add_filter('async_upload_audio', 'get_media_item', 10, 2);
2314 add_filter('async_upload_video', 'get_media_item', 10, 2);
2315 add_filter('async_upload_file', 'get_media_item', 10, 2);
2316
2317 add_action('media_upload_image', 'media_upload_image');
2318 add_action('media_upload_audio', 'media_upload_audio');
2319 add_action('media_upload_video', 'media_upload_video');
2320 add_action('media_upload_file', 'media_upload_file');
2321
2322 add_filter('media_upload_gallery', 'media_upload_gallery');
2323
2324 add_filter('media_upload_library', 'media_upload_library');