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