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