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