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