]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/media.php
WordPress 4.0
[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  * Defines the default media upload tabs
11  *
12  * @since 2.5.0
13  *
14  * @return array default tabs
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         /**
25          * Filter the available tabs in the legacy (pre-3.5.0) media popup.
26          *
27          * @since 2.5.0
28          *
29          * @param array $_default_tabs An array of media tabs.
30          */
31         return apply_filters( 'media_upload_tabs', $_default_tabs );
32 }
33
34 /**
35  * Adds the gallery tab back to the tabs array if post has image attachments
36  *
37  * @since 2.5.0
38  *
39  * @param array $tabs
40  * @return array $tabs with gallery if post has image attachment
41  */
42 function update_gallery_tab($tabs) {
43         global $wpdb;
44
45         if ( !isset($_REQUEST['post_id']) ) {
46                 unset($tabs['gallery']);
47                 return $tabs;
48         }
49
50         $post_id = intval($_REQUEST['post_id']);
51
52         if ( $post_id )
53                 $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 ) ) );
54
55         if ( empty($attachments) ) {
56                 unset($tabs['gallery']);
57                 return $tabs;
58         }
59
60         $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
61
62         return $tabs;
63 }
64 add_filter('media_upload_tabs', 'update_gallery_tab');
65
66 /**
67  * {@internal Missing Short Description}}
68  *
69  * @since 2.5.0
70  */
71 function the_media_upload_tabs() {
72         global $redir_tab;
73         $tabs = media_upload_tabs();
74         $default = 'type';
75
76         if ( !empty($tabs) ) {
77                 echo "<ul id='sidemenu'>\n";
78                 if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) ) {
79                         $current = $redir_tab;
80                 } elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) ) {
81                         $current = $_GET['tab'];
82                 } else {
83                         /** This filter is documented in wp-admin/media-upload.php */
84                         $current = apply_filters( 'media_upload_default_tab', $default );
85                 }
86
87                 foreach ( $tabs as $callback => $text ) {
88                         $class = '';
89
90                         if ( $current == $callback )
91                                 $class = " class='current'";
92
93                         $href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
94                         $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
95                         echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
96                 }
97                 echo "</ul>\n";
98         }
99 }
100
101 /**
102  * {@internal Missing Short Description}}
103  *
104  * @since 2.5.0
105  *
106  * @param integer $id image attachment id
107  * @param string $caption image caption
108  * @param string $alt image alt attribute
109  * @param string $title image title attribute
110  * @param string $align image css alignment property
111  * @param string $url image src url
112  * @param string|bool $rel image rel attribute
113  * @param string $size image size (thumbnail, medium, large, full or added  with add_image_size() )
114  * @return string the html to insert into editor
115  */
116 function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
117
118         $html = get_image_tag($id, $alt, '', $align, $size);
119
120         $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
121
122         if ( $url )
123                 $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
124
125         /**
126          * Filter the image HTML markup to send to the editor.
127          *
128          * @since 2.5.0
129          *
130          * @param string $html    The image HTML markup to send.
131          * @param int    $id      The attachment id.
132          * @param string $caption The image caption.
133          * @param string $title   The image title.
134          * @param string $align   The image alignment.
135          * @param string $url     The image source URL.
136          * @param string $size    The image size.
137          * @param string $alt     The image alternative, or alt, text.
138          */
139         $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
140
141         return $html;
142 }
143
144 /**
145  * Adds image shortcode with caption to editor
146  *
147  * @since 2.6.0
148  *
149  * @param string $html
150  * @param integer $id
151  * @param string $caption image caption
152  * @param string $alt image alt attribute
153  * @param string $title image title attribute
154  * @param string $align image css alignment property
155  * @param string $url image src url
156  * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
157  * @return string
158  */
159 function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
160
161         /**
162          * Filter whether to disable captions.
163          *
164          * Prevents image captions from being appended to image HTML when inserted into the editor.
165          *
166          * @since 2.6.0
167          *
168          * @param bool $bool Whether to disable appending captions. Returning true to the filter
169          *                   will disable captions. Default empty string.
170          */
171         if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
172                 return $html;
173
174         $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
175
176         if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
177                 return $html;
178
179         $width = $matches[1];
180
181         $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
182         $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
183
184         // Convert any remaining line breaks to <br>.
185         $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
186
187         $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
188         if ( empty($align) )
189                 $align = 'none';
190
191         $shcode = '[caption id="' . $id . '" align="align' . $align     . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
192
193         /**
194          * Filter the image HTML markup including the caption shortcode.
195          *
196          * @since 2.6.0
197          *
198          * @param string $shcode The image HTML markup with caption shortcode.
199          * @param string $html   The image HTML markup.
200          */
201         return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
202 }
203 add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
204
205 /**
206  * Private preg_replace callback used in image_add_caption()
207  *
208  * @access private
209  * @since 3.4.0
210  */
211 function _cleanup_image_add_caption( $matches ) {
212         // Remove any line breaks from inside the tags.
213         return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
214 }
215
216 /**
217  * Adds image html to editor
218  *
219  * @since 2.5.0
220  *
221  * @param string $html
222  */
223 function media_send_to_editor($html) {
224 ?>
225 <script type="text/javascript">
226 /* <![CDATA[ */
227 var win = window.dialogArguments || opener || parent || top;
228 win.send_to_editor('<?php echo addslashes($html); ?>');
229 /* ]]> */
230 </script>
231 <?php
232         exit;
233 }
234
235 /**
236  * This handles the file upload POST itself, creating the attachment post.
237  *
238  * @since 2.5.0
239  *
240  * @param string $file_id Index into the {@link $_FILES} array of the upload
241  * @param int $post_id The post ID the media is associated with
242  * @param array $post_data allows you to overwrite some of the attachment
243  * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
244  * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
245  */
246 function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
247
248         $time = current_time('mysql');
249         if ( $post = get_post($post_id) ) {
250                 if ( substr( $post->post_date, 0, 4 ) > 0 )
251                         $time = $post->post_date;
252         }
253
254         $name = $_FILES[$file_id]['name'];
255         $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
256
257         if ( isset($file['error']) )
258                 return new WP_Error( 'upload_error', $file['error'] );
259
260         $name_parts = pathinfo($name);
261         $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
262
263         $url = $file['url'];
264         $type = $file['type'];
265         $file = $file['file'];
266         $title = $name;
267         $content = '';
268
269         if ( preg_match( '#^audio#', $type ) ) {
270                 $meta = wp_read_audio_metadata( $file );
271
272                 if ( ! empty( $meta['title'] ) )
273                         $title = $meta['title'];
274
275                 $content = '';
276
277                 if ( ! empty( $title ) ) {
278
279                         if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
280                                 /* translators: 1: audio track title, 2: album title, 3: artist name */
281                                 $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
282                         } else if ( ! empty( $meta['album'] ) ) {
283                                 /* translators: 1: audio track title, 2: album title */
284                                 $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
285                         } else if ( ! empty( $meta['artist'] ) ) {
286                                 /* translators: 1: audio track title, 2: artist name */
287                                 $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
288                         } else {
289                                 $content .= sprintf( __( '"%s".' ), $title );
290                         }
291
292                 } else if ( ! empty( $meta['album'] ) ) {
293
294                         if ( ! empty( $meta['artist'] ) ) {
295                                 /* translators: 1: audio album title, 2: artist name */
296                                 $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
297                         } else {
298                                 $content .= $meta['album'] . '.';
299                         }
300
301                 } else if ( ! empty( $meta['artist'] ) ) {
302
303                         $content .= $meta['artist'] . '.';
304
305                 }
306
307                 if ( ! empty( $meta['year'] ) )
308                         $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
309
310                 if ( ! empty( $meta['track_number'] ) ) {
311                         $track_number = explode( '/', $meta['track_number'] );
312                         if ( isset( $track_number[1] ) )
313                                 $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
314                         else
315                                 $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
316                 }
317
318                 if ( ! empty( $meta['genre'] ) )
319                         $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
320
321         // Use image exif/iptc data for title and caption defaults if possible.
322         } elseif ( $image_meta = @wp_read_image_metadata( $file ) ) {
323                 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
324                         $title = $image_meta['title'];
325                 if ( trim( $image_meta['caption'] ) )
326                         $content = $image_meta['caption'];
327         }
328
329         // Construct the attachment array
330         $attachment = array_merge( array(
331                 'post_mime_type' => $type,
332                 'guid' => $url,
333                 'post_parent' => $post_id,
334                 'post_title' => $title,
335                 'post_content' => $content,
336         ), $post_data );
337
338         // This should never be set as it would then overwrite an existing attachment.
339         if ( isset( $attachment['ID'] ) )
340                 unset( $attachment['ID'] );
341
342         // Save the data
343         $id = wp_insert_attachment($attachment, $file, $post_id);
344         if ( !is_wp_error($id) ) {
345                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
346         }
347
348         return $id;
349
350 }
351
352 /**
353  * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
354  *
355  * @since 2.6.0
356  *
357  * @param array $file_array Array similar to a {@link $_FILES} upload array
358  * @param int $post_id The post ID the media is associated with
359  * @param string $desc Description of the sideloaded file
360  * @param array $post_data allows you to overwrite some of the attachment
361  * @return int|object The ID of the attachment or a WP_Error on failure
362  */
363 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
364         $overrides = array('test_form'=>false);
365
366         $time = current_time( 'mysql' );
367         if ( $post = get_post( $post_id ) ) {
368                 if ( substr( $post->post_date, 0, 4 ) > 0 )
369                         $time = $post->post_date;
370         }
371
372         $file = wp_handle_sideload( $file_array, $overrides, $time );
373         if ( isset($file['error']) )
374                 return new WP_Error( 'upload_error', $file['error'] );
375
376         $url = $file['url'];
377         $type = $file['type'];
378         $file = $file['file'];
379         $title = preg_replace('/\.[^.]+$/', '', basename($file));
380         $content = '';
381
382         // Use image exif/iptc data for title and caption defaults if possible.
383         if ( $image_meta = @wp_read_image_metadata($file) ) {
384                 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
385                         $title = $image_meta['title'];
386                 if ( trim( $image_meta['caption'] ) )
387                         $content = $image_meta['caption'];
388         }
389
390         if ( isset( $desc ) )
391                 $title = $desc;
392
393         // Construct the attachment array.
394         $attachment = array_merge( array(
395                 'post_mime_type' => $type,
396                 'guid' => $url,
397                 'post_parent' => $post_id,
398                 'post_title' => $title,
399                 'post_content' => $content,
400         ), $post_data );
401
402         // This should never be set as it would then overwrite an existing attachment.
403         if ( isset( $attachment['ID'] ) )
404                 unset( $attachment['ID'] );
405
406         // Save the attachment metadata
407         $id = wp_insert_attachment($attachment, $file, $post_id);
408         if ( !is_wp_error($id) )
409                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
410
411         return $id;
412 }
413
414 /**
415  * Adds the iframe to display content for the media upload page
416  *
417  * @since 2.5.0
418  *
419  * @param array $content_func
420  */
421 function wp_iframe($content_func /* ... */) {
422         _wp_admin_html_begin();
423 ?>
424 <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
425 <?php
426
427 wp_enqueue_style( 'colors' );
428 // Check callback name for 'media'
429 if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
430         || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
431         wp_enqueue_style( 'media' );
432 wp_enqueue_style( 'ie' );
433 ?>
434 <script type="text/javascript">
435 //<![CDATA[
436 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();}}};
437 var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
438 isRtl = <?php echo (int) is_rtl(); ?>;
439 //]]>
440 </script>
441 <?php
442         /** This action is documented in wp-admin/admin-header.php */
443         do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
444
445         /**
446          * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
447          *
448          * @since 2.9.0
449          */
450         do_action( 'admin_print_styles-media-upload-popup' );
451
452         /** This action is documented in wp-admin/admin-header.php */
453         do_action( 'admin_print_styles' );
454
455         /**
456          * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
457          *
458          * @since 2.9.0
459          */
460         do_action( 'admin_print_scripts-media-upload-popup' );
461
462         /** This action is documented in wp-admin/admin-header.php */
463         do_action( 'admin_print_scripts' );
464
465         /**
466          * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
467          * media upload popup are printed.
468          *
469          * @since 2.9.0
470          */
471         do_action( 'admin_head-media-upload-popup' );
472
473         /** This action is documented in wp-admin/admin-header.php */
474         do_action( 'admin_head' );
475
476 if ( is_string( $content_func ) ) {
477         /**
478          * Fires in the admin header for each specific form tab in the legacy
479          * (pre-3.5.0) media upload popup.
480          *
481          * The dynamic portion of the hook, $content_func, refers to the form
482          * callback for the media upload type. Possible values include
483          * 'media_upload_type_form', 'media_upload_type_url_form', and
484          * 'media_upload_library_form'.
485          *
486          * @since 2.5.0
487          */
488         do_action( "admin_head_{$content_func}" );
489 }
490 ?>
491 </head>
492 <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
493 <script type="text/javascript">
494 document.body.className = document.body.className.replace('no-js', 'js');
495 </script>
496 <?php
497         $args = func_get_args();
498         $args = array_slice($args, 1);
499         call_user_func_array($content_func, $args);
500
501         /** This action is documented in wp-admin/admin-footer.php */
502         do_action( 'admin_print_footer_scripts' );
503 ?>
504 <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
505 </body>
506 </html>
507 <?php
508 }
509
510 /**
511  * Adds the media button to the editor
512  *
513  * @since 2.5.0
514  *
515  * @param string $editor_id
516  */
517 function media_buttons($editor_id = 'content') {
518         static $instance = 0;
519         $instance++;
520
521         $post = get_post();
522         if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
523                 $post = $GLOBALS['post_ID'];
524
525         wp_enqueue_media( array(
526                 'post' => $post
527         ) );
528
529         $img = '<span class="wp-media-buttons-icon"></span> ';
530
531         $id_attribute = $instance === 1 ? ' id="insert-media-button"' : '';
532         printf( '<a href="#"%s class="button insert-media add_media" data-editor="%s" title="%s">%s</a>',
533                 $id_attribute,
534                 esc_attr( $editor_id ),
535                 esc_attr__( 'Add Media' ),
536                 $img . __( 'Add Media' )
537         );
538         /**
539          * Filter the legacy (pre-3.5.0) media buttons.
540          *
541          * @since 2.5.0
542          * @deprecated 3.5.0 Use 'media_buttons' action instead.
543          *
544          * @param string $string Media buttons context. Default empty.
545          */
546         $legacy_filter = apply_filters( 'media_buttons_context', '' );
547
548         if ( $legacy_filter ) {
549                 // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
550                 if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
551                         $legacy_filter .= '</a>';
552                 echo $legacy_filter;
553         }
554 }
555 add_action( 'media_buttons', 'media_buttons' );
556
557 function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
558         global $post_ID;
559
560         if ( empty( $post_id ) )
561                 $post_id = $post_ID;
562
563         $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
564
565         if ( $type && 'media' != $type )
566                 $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
567
568         if ( ! empty( $tab ) )
569                 $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
570
571         /**
572          * Filter the upload iframe source URL for a specific media type.
573          *
574          * The dynamic portion of the hook name, $type, refers to the type
575          * of media uploaded.
576          *
577          * @since 3.0.0
578          *
579          * @param string $upload_iframe_src The upload iframe source URL by type.
580          */
581         $upload_iframe_src = apply_filters( $type . '_upload_iframe_src', $upload_iframe_src );
582
583         return add_query_arg('TB_iframe', true, $upload_iframe_src);
584 }
585
586 /**
587  * {@internal Missing Short Description}}
588  *
589  * @since 2.5.0
590  *
591  * @return mixed void|object WP_Error on failure
592  */
593 function media_upload_form_handler() {
594         check_admin_referer('media-form');
595
596         $errors = null;
597
598         if ( isset($_POST['send']) ) {
599                 $keys = array_keys($_POST['send']);
600                 $send_id = (int) array_shift($keys);
601         }
602
603         if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
604                 $post = $_post = get_post($attachment_id, ARRAY_A);
605
606                 if ( !current_user_can( 'edit_post', $attachment_id ) )
607                         continue;
608
609                 if ( isset($attachment['post_content']) )
610                         $post['post_content'] = $attachment['post_content'];
611                 if ( isset($attachment['post_title']) )
612                         $post['post_title'] = $attachment['post_title'];
613                 if ( isset($attachment['post_excerpt']) )
614                         $post['post_excerpt'] = $attachment['post_excerpt'];
615                 if ( isset($attachment['menu_order']) )
616                         $post['menu_order'] = $attachment['menu_order'];
617
618                 if ( isset($send_id) && $attachment_id == $send_id ) {
619                         if ( isset($attachment['post_parent']) )
620                                 $post['post_parent'] = $attachment['post_parent'];
621                 }
622
623                 /**
624                  * Filter the attachment fields to be saved.
625                  *
626                  * @since 2.5.0
627                  *
628                  * @see wp_get_attachment_metadata()
629                  *
630                  * @param WP_Post $post       The WP_Post object.
631                  * @param array   $attachment An array of attachment metadata.
632                  */
633                 $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
634
635                 if ( isset($attachment['image_alt']) ) {
636                         $image_alt = wp_unslash( $attachment['image_alt'] );
637                         if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
638                                 $image_alt = wp_strip_all_tags( $image_alt, true );
639
640                                 // Update_meta expects slashed.
641                                 update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
642                         }
643                 }
644
645                 if ( isset($post['errors']) ) {
646                         $errors[$attachment_id] = $post['errors'];
647                         unset($post['errors']);
648                 }
649
650                 if ( $post != $_post )
651                         wp_update_post($post);
652
653                 foreach ( get_attachment_taxonomies($post) as $t ) {
654                         if ( isset($attachment[$t]) )
655                                 wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
656                 }
657         }
658
659         if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
660                 <script type="text/javascript">
661                 /* <![CDATA[ */
662                 var win = window.dialogArguments || opener || parent || top;
663                 win.tb_remove();
664                 /* ]]> */
665                 </script>
666                 <?php
667                 exit;
668         }
669
670         if ( isset($send_id) ) {
671                 $attachment = wp_unslash( $_POST['attachments'][$send_id] );
672
673                 $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
674                 if ( !empty($attachment['url']) ) {
675                         $rel = '';
676                         if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
677                                 $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
678                         $html = "<a href='{$attachment['url']}'$rel>$html</a>";
679                 }
680
681                 /**
682                  * Filter the HTML markup for a media item sent to the editor.
683                  *
684                  * @since 2.5.0
685                  *
686                  * @see wp_get_attachment_metadata()
687                  *
688                  * @param string $html       HTML markup for a media item sent to the editor.
689                  * @param int    $send_id    The first key from the $_POST['send'] data.
690                  * @param array  $attachment Array of attachment metadata.
691                  */
692                 $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
693                 return media_send_to_editor($html);
694         }
695
696         return $errors;
697 }
698
699 /**
700  * {@internal Missing Short Description}}
701  *
702  * @since 2.5.0
703  *
704  * @return mixed
705  */
706 function wp_media_upload_handler() {
707         $errors = array();
708         $id = 0;
709
710         if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
711                 check_admin_referer('media-form');
712                 // Upload File button was clicked
713                 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
714                 unset($_FILES);
715                 if ( is_wp_error($id) ) {
716                         $errors['upload_error'] = $id;
717                         $id = false;
718                 }
719         }
720
721         if ( !empty($_POST['insertonlybutton']) ) {
722                 $src = $_POST['src'];
723                 if ( !empty($src) && !strpos($src, '://') )
724                         $src = "http://$src";
725
726                 if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
727                         $title = esc_html( wp_unslash( $_POST['title'] ) );
728                         if ( empty( $title ) )
729                                 $title = esc_html( basename( $src ) );
730
731                         if ( $title && $src )
732                                 $html = "<a href='" . esc_url($src) . "'>$title</a>";
733
734                         $type = 'file';
735                         if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
736                                 && ( 'audio' == $ext_type || 'video' == $ext_type ) )
737                                         $type = $ext_type;
738
739                         /**
740                          * Filter the URL sent to the editor for a specific media type.
741                          *
742                          * The dynamic portion of the hook name, $type, refers to the type
743                          * of media being sent.
744                          *
745                          * @since 3.3.0
746                          *
747                          * @param string $html  HTML markup sent to the editor.
748                          * @param string $src   Media source URL.
749                          * @param string $title Media title.
750                          */
751                         $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
752                 } else {
753                         $align = '';
754                         $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
755                         if ( isset($_POST['align']) ) {
756                                 $align = esc_attr( wp_unslash( $_POST['align'] ) );
757                                 $class = " class='align$align'";
758                         }
759                         if ( !empty($src) )
760                                 $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
761
762                         /**
763                          * Filter the image URL sent to the editor.
764                          *
765                          * @since 2.8.0
766                          *
767                          * @param string $html  HTML markup sent to the editor for an image.
768                          * @param string $src   Image source URL.
769                          * @param string $alt   Image alternate, or alt, text.
770                          * @param string $align The image alignment. Default 'alignnone'. Possible values include
771                          *                      'alignleft', 'aligncenter', 'alignright', 'alignnone'.
772                          */
773                         $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
774                 }
775
776                 return media_send_to_editor($html);
777         }
778
779         if ( isset( $_POST['save'] ) ) {
780                 $errors['upload_notice'] = __('Saved.');
781                 return media_upload_gallery();
782         } elseif ( ! empty( $_POST ) ) {
783                 $return = media_upload_form_handler();
784
785                 if ( is_string($return) )
786                         return $return;
787                 if ( is_array($return) )
788                         $errors = $return;
789         }
790
791         if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
792                 $type = 'image';
793                 if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
794                         $type = $_GET['type'];
795                 return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
796         }
797
798         return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
799 }
800
801 /**
802  * Download an image from the specified URL and attach it to a post.
803  *
804  * @since 2.6.0
805  *
806  * @param string $file The URL of the image to download
807  * @param int $post_id The post ID the media is to be associated with
808  * @param string $desc Optional. Description of the image
809  * @return string|WP_Error Populated HTML img tag on success
810  */
811 function media_sideload_image( $file, $post_id, $desc = null ) {
812         if ( ! empty( $file ) ) {
813                 // Set variables for storage, fix file filename for query strings.
814                 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
815                 $file_array = array();
816                 $file_array['name'] = basename( $matches[0] );
817
818                 // Download file to temp location.
819                 $file_array['tmp_name'] = download_url( $file );
820
821                 // If error storing temporarily, return the error.
822                 if ( is_wp_error( $file_array['tmp_name'] ) ) {
823                         return $file_array['tmp_name'];
824                 }
825
826                 // Do the validation and storage stuff.
827                 $id = media_handle_sideload( $file_array, $post_id, $desc );
828
829                 // If error storing permanently, unlink.
830                 if ( is_wp_error( $id ) ) {
831                         @unlink( $file_array['tmp_name'] );
832                         return $id;
833                 }
834
835                 $src = wp_get_attachment_url( $id );
836         }
837
838         // Finally check to make sure the file has been saved, then return the HTML.
839         if ( ! empty( $src ) ) {
840                 $alt = isset( $desc ) ? esc_attr( $desc ) : '';
841                 $html = "<img src='$src' alt='$alt' />";
842                 return $html;
843         }
844 }
845
846 /**
847  * {@internal Missing Short Description}}
848  *
849  * @since 2.5.0
850  *
851  * @return unknown
852  */
853 function media_upload_gallery() {
854         $errors = array();
855
856         if ( !empty($_POST) ) {
857                 $return = media_upload_form_handler();
858
859                 if ( is_string($return) )
860                         return $return;
861                 if ( is_array($return) )
862                         $errors = $return;
863         }
864
865         wp_enqueue_script('admin-gallery');
866         return wp_iframe( 'media_upload_gallery_form', $errors );
867 }
868
869 /**
870  * {@internal Missing Short Description}}
871  *
872  * @since 2.5.0
873  *
874  * @return unknown
875  */
876 function media_upload_library() {
877         $errors = array();
878         if ( !empty($_POST) ) {
879                 $return = media_upload_form_handler();
880
881                 if ( is_string($return) )
882                         return $return;
883                 if ( is_array($return) )
884                         $errors = $return;
885         }
886
887         return wp_iframe( 'media_upload_library_form', $errors );
888 }
889
890 /**
891  * Retrieve HTML for the image alignment radio buttons with the specified one checked.
892  *
893  * @since 2.7.0
894  *
895  * @param object $post
896  * @param string $checked
897  * @return string
898  */
899 function image_align_input_fields( $post, $checked = '' ) {
900
901         if ( empty($checked) )
902                 $checked = get_user_setting('align', 'none');
903
904         $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
905         if ( !array_key_exists( (string) $checked, $alignments ) )
906                 $checked = 'none';
907
908         $out = array();
909         foreach ( $alignments as $name => $label ) {
910                 $name = esc_attr($name);
911                 $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
912                         ( $checked == $name ? " checked='checked'" : "" ) .
913                         " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
914         }
915         return join("\n", $out);
916 }
917
918 /**
919  * Retrieve HTML for the size radio buttons with the specified one checked.
920  *
921  * @since 2.7.0
922  *
923  * @param object $post
924  * @param bool|string $check
925  * @return array
926  */
927 function image_size_input_fields( $post, $check = '' ) {
928
929         /**
930          * Filter the names and labels of the default image sizes.
931          *
932          * @since 3.3.0
933          *
934          * @param array $size_names Array of image sizes and their names. Default values
935          *                          include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
936          */
937         $size_names = apply_filters( 'image_size_names_choose', array(
938                 'thumbnail' => __( 'Thumbnail' ),
939                 'medium'    => __( 'Medium' ),
940                 'large'     => __( 'Large' ),
941                 'full'      => __( 'Full Size' )
942         ) );
943
944                 if ( empty($check) )
945                         $check = get_user_setting('imgsize', 'medium');
946
947                 foreach ( $size_names as $size => $label ) {
948                         $downsize = image_downsize($post->ID, $size);
949                         $checked = '';
950
951                         // Is this size selectable?
952                         $enabled = ( $downsize[3] || 'full' == $size );
953                         $css_id = "image-size-{$size}-{$post->ID}";
954
955                         // If this size is the default but that's not available, don't select it.
956                         if ( $size == $check ) {
957                                 if ( $enabled )
958                                         $checked = " checked='checked'";
959                                 else
960                                         $check = '';
961                         } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
962                                 /*
963                                  * If $check is not enabled, default to the first available size
964                                  * that's bigger than a thumbnail.
965                                  */
966                                 $check = $size;
967                                 $checked = " checked='checked'";
968                         }
969
970                         $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 />";
971
972                         $html .= "<label for='{$css_id}'>$label</label>";
973
974                         // Only show the dimensions if that choice is available.
975                         if ( $enabled )
976                                 $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
977
978                         $html .= '</div>';
979
980                         $out[] = $html;
981                 }
982
983                 return array(
984                         'label' => __('Size'),
985                         'input' => 'html',
986                         'html'  => join("\n", $out),
987                 );
988 }
989
990 /**
991  * Retrieve HTML for the Link URL buttons with the default link type as specified.
992  *
993  * @since 2.7.0
994  *
995  * @param object $post
996  * @param string $url_type
997  * @return string
998  */
999 function image_link_input_fields($post, $url_type = '') {
1000
1001         $file = wp_get_attachment_url($post->ID);
1002         $link = get_attachment_link($post->ID);
1003
1004         if ( empty($url_type) )
1005                 $url_type = get_user_setting('urlbutton', 'post');
1006
1007         $url = '';
1008         if ( $url_type == 'file' )
1009                 $url = $file;
1010         elseif ( $url_type == 'post' )
1011                 $url = $link;
1012
1013         return "
1014         <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
1015         <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
1016         <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
1017         <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
1018 ";
1019 }
1020
1021 function wp_caption_input_textarea($edit_post) {
1022         // Post data is already escaped.
1023         $name = "attachments[{$edit_post->ID}][post_excerpt]";
1024
1025         return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
1026 }
1027
1028 /**
1029  * {@internal Missing Short Description}}
1030  *
1031  * @since 2.5.0
1032  *
1033  * @param array $form_fields
1034  * @param object $post
1035  * @return array
1036  */
1037 function image_attachment_fields_to_edit($form_fields, $post) {
1038         return $form_fields;
1039 }
1040
1041 /**
1042  * {@internal Missing Short Description}}
1043  *
1044  * @since 2.5.0
1045  *
1046  * @param array   $form_fields An array of attachment form fields.
1047  * @param WP_Post $post        The WP_Post attachment object.
1048  * @return array Filtered attachment form fields.
1049  */
1050 function media_single_attachment_fields_to_edit( $form_fields, $post ) {
1051         unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
1052         return $form_fields;
1053 }
1054
1055 /**
1056  * {@internal Missing Short Description}}
1057  *
1058  * @since 2.8.0
1059  *
1060  * @param array   $form_fields An array of attachment form fields.
1061  * @param WP_Post $post        The WP_Post attachment object.
1062  * @return array Filtered attachment form fields.
1063  */
1064 function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
1065         unset($form_fields['image_url']);
1066         return $form_fields;
1067 }
1068
1069 /**
1070  * Filters input from media_upload_form_handler() and assigns a default
1071  * post_title from the file name if none supplied.
1072  *
1073  * Illustrates the use of the attachment_fields_to_save filter
1074  * which can be used to add default values to any field before saving to DB.
1075  *
1076  * @since 2.5.0
1077  *
1078  * @param WP_Post $post       The WP_Post attachment object.
1079  * @param array   $attachment An array of attachment metadata.
1080  * @return array Filtered attachment post object.
1081  */
1082 function image_attachment_fields_to_save( $post, $attachment ) {
1083         if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
1084                 if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
1085                         $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
1086                         $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
1087                         $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
1088                 }
1089         }
1090
1091         return $post;
1092 }
1093
1094 add_filter( 'attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2 );
1095
1096 /**
1097  * {@internal Missing Short Description}}
1098  *
1099  * @since 2.5.0
1100  *
1101  * @param string $html
1102  * @param integer $attachment_id
1103  * @param array $attachment
1104  * @return array
1105  */
1106 function image_media_send_to_editor($html, $attachment_id, $attachment) {
1107         $post = get_post($attachment_id);
1108         if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1109                 $url = $attachment['url'];
1110                 $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
1111                 $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
1112                 $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
1113                 $rel = ( $url == get_attachment_link($attachment_id) );
1114
1115                 return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
1116         }
1117
1118         return $html;
1119 }
1120
1121 add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
1122
1123 /**
1124  * {@internal Missing Short Description}}
1125  *
1126  * @since 2.5.0
1127  *
1128  * @param object $post
1129  * @param array $errors
1130  * @return array
1131  */
1132 function get_attachment_fields_to_edit($post, $errors = null) {
1133         if ( is_int($post) )
1134                 $post = get_post($post);
1135         if ( is_array($post) )
1136                 $post = new WP_Post( (object) $post );
1137
1138         $image_url = wp_get_attachment_url($post->ID);
1139
1140         $edit_post = sanitize_post($post, 'edit');
1141
1142         $form_fields = array(
1143                 'post_title'   => array(
1144                         'label'      => __('Title'),
1145                         'value'      => $edit_post->post_title
1146                 ),
1147                 'image_alt'   => array(),
1148                 'post_excerpt' => array(
1149                         'label'      => __('Caption'),
1150                         'input'      => 'html',
1151                         'html'       => wp_caption_input_textarea($edit_post)
1152                 ),
1153                 'post_content' => array(
1154                         'label'      => __('Description'),
1155                         'value'      => $edit_post->post_content,
1156                         'input'      => 'textarea'
1157                 ),
1158                 'url'          => array(
1159                         'label'      => __('Link URL'),
1160                         'input'      => 'html',
1161                         'html'       => image_link_input_fields($post, get_option('image_default_link_type')),
1162                         'helps'      => __('Enter a link URL or click above for presets.')
1163                 ),
1164                 'menu_order'   => array(
1165                         'label'      => __('Order'),
1166                         'value'      => $edit_post->menu_order
1167                 ),
1168                 'image_url'     => array(
1169                         'label'      => __('File URL'),
1170                         'input'      => 'html',
1171                         'html'       => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
1172                         'value'      => wp_get_attachment_url($post->ID),
1173                         'helps'      => __('Location of the uploaded file.')
1174                 )
1175         );
1176
1177         foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1178                 $t = (array) get_taxonomy($taxonomy);
1179                 if ( ! $t['public'] || ! $t['show_ui'] )
1180                         continue;
1181                 if ( empty($t['label']) )
1182                         $t['label'] = $taxonomy;
1183                 if ( empty($t['args']) )
1184                         $t['args'] = array();
1185
1186                 $terms = get_object_term_cache($post->ID, $taxonomy);
1187                 if ( false === $terms )
1188                         $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1189
1190                 $values = array();
1191
1192                 foreach ( $terms as $term )
1193                         $values[] = $term->slug;
1194                 $t['value'] = join(', ', $values);
1195
1196                 $form_fields[$taxonomy] = $t;
1197         }
1198
1199         // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1200         // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1201         $form_fields = array_merge_recursive($form_fields, (array) $errors);
1202
1203         // This was formerly in image_attachment_fields_to_edit().
1204         if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1205                 $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
1206                 if ( empty($alt) )
1207                         $alt = '';
1208
1209                 $form_fields['post_title']['required'] = true;
1210
1211                 $form_fields['image_alt'] = array(
1212                         'value' => $alt,
1213                         'label' => __('Alternative Text'),
1214                         'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
1215                 );
1216
1217                 $form_fields['align'] = array(
1218                         'label' => __('Alignment'),
1219                         'input' => 'html',
1220                         'html'  => image_align_input_fields($post, get_option('image_default_align')),
1221                 );
1222
1223                 $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
1224
1225         } else {
1226                 unset( $form_fields['image_alt'] );
1227         }
1228
1229         /**
1230          * Filter the attachment fields to edit.
1231          *
1232          * @since 2.5.0
1233          *
1234          * @param array   $form_fields An array of attachment form fields.
1235          * @param WP_Post $post        The WP_Post attachment object.
1236          */
1237         $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
1238
1239         return $form_fields;
1240 }
1241
1242 /**
1243  * Retrieve HTML for media items of post gallery.
1244  *
1245  * The HTML markup retrieved will be created for the progress of SWF Upload
1246  * component. Will also create link for showing and hiding the form to modify
1247  * the image attachment.
1248  *
1249  * @since 2.5.0
1250  *
1251  * @param int $post_id Optional. Post ID.
1252  * @param array $errors Errors for attachment, if any.
1253  * @return string
1254  */
1255 function get_media_items( $post_id, $errors ) {
1256         $attachments = array();
1257         if ( $post_id ) {
1258                 $post = get_post($post_id);
1259                 if ( $post && $post->post_type == 'attachment' )
1260                         $attachments = array($post->ID => $post);
1261                 else
1262                         $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
1263         } else {
1264                 if ( is_array($GLOBALS['wp_the_query']->posts) )
1265                         foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
1266                                 $attachments[$attachment->ID] = $attachment;
1267         }
1268
1269         $output = '';
1270         foreach ( (array) $attachments as $id => $attachment ) {
1271                 if ( $attachment->post_status == 'trash' )
1272                         continue;
1273                 if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
1274                         $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress hidden'><div class='bar'></div></div><div id='media-upload-error-$id' class='hidden'></div><div class='filename hidden'></div>$item\n</div>";
1275         }
1276
1277         return $output;
1278 }
1279
1280 /**
1281  * Retrieve HTML form for modifying the image attachment.
1282  *
1283  * @since 2.5.0
1284  *
1285  * @param int $attachment_id Attachment ID for modification.
1286  * @param string|array $args Optional. Override defaults.
1287  * @return string HTML form for attachment.
1288  */
1289 function get_media_item( $attachment_id, $args = null ) {
1290         global $redir_tab;
1291
1292         if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
1293                 $thumb_url = $thumb_url[0];
1294         else
1295                 $thumb_url = false;
1296
1297         $post = get_post( $attachment_id );
1298         $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
1299
1300         $default_args = array(
1301                 'errors' => null,
1302                 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
1303                 'delete' => true,
1304                 'toggle' => true,
1305                 'show_title' => true
1306         );
1307         $args = wp_parse_args( $args, $default_args );
1308
1309         /**
1310          * Filter the arguments used to retrieve an image for the edit image form.
1311          *
1312          * @since 3.1.0
1313          *
1314          * @see get_media_item
1315          *
1316          * @param array $args An array of arguments.
1317          */
1318         $r = apply_filters( 'get_media_item_args', $args );
1319
1320         $toggle_on  = __( 'Show' );
1321         $toggle_off = __( 'Hide' );
1322
1323         $filename = esc_html( wp_basename( $post->guid ) );
1324         $title = esc_attr( $post->post_title );
1325
1326         $post_mime_types = get_post_mime_types();
1327         $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
1328         $type = array_shift( $keys );
1329         $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
1330
1331         $form_fields = get_attachment_fields_to_edit( $post, $r['errors'] );
1332
1333         if ( $r['toggle'] ) {
1334                 $class = empty( $r['errors'] ) ? 'startclosed' : 'startopen';
1335                 $toggle_links = "
1336         <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
1337         <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
1338         } else {
1339                 $class = '';
1340                 $toggle_links = '';
1341         }
1342
1343         $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
1344         $display_title = $r['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . "</span></div>" : '';
1345
1346         $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
1347         $order = '';
1348
1349         foreach ( $form_fields as $key => $val ) {
1350                 if ( 'menu_order' == $key ) {
1351                         if ( $gallery )
1352                                 $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>";
1353                         else
1354                                 $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
1355
1356                         unset( $form_fields['menu_order'] );
1357                         break;
1358                 }
1359         }
1360
1361         $media_dims = '';
1362         $meta = wp_get_attachment_metadata( $post->ID );
1363         if ( isset( $meta['width'], $meta['height'] ) )
1364                 $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
1365
1366         /**
1367          * Filter the media metadata.
1368          *
1369          * @since 2.5.0
1370          *
1371          * @param string  $media_dims The HTML markup containing the media dimensions.
1372          * @param WP_Post $post       The WP_Post attachment object.
1373          */
1374         $media_dims = apply_filters( 'media_meta', $media_dims, $post );
1375
1376         $image_edit_button = '';
1377         if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
1378                 $nonce = wp_create_nonce( "image_editor-$post->ID" );
1379                 $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' ) . "' /> <span class='spinner'></span>";
1380         }
1381
1382         $attachment_url = get_permalink( $attachment_id );
1383
1384         $item = "
1385         $type_html
1386         $toggle_links
1387         $order
1388         $display_title
1389         <table class='slidetoggle describe $class'>
1390                 <thead class='media-item-info' id='media-head-$post->ID'>
1391                 <tr>
1392                         <td class='A1B1' id='thumbnail-head-$post->ID'>
1393                         <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
1394                         <p>$image_edit_button</p>
1395                         </td>
1396                         <td>
1397                         <p><strong>" . __('File name:') . "</strong> $filename</p>
1398                         <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
1399                         <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
1400                         if ( !empty( $media_dims ) )
1401                                 $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
1402
1403                         $item .= "</td></tr>\n";
1404
1405         $item .= "
1406                 </thead>
1407                 <tbody>
1408                 <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
1409                 <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
1410
1411         $defaults = array(
1412                 'input'      => 'text',
1413                 'required'   => false,
1414                 'value'      => '',
1415                 'extra_rows' => array(),
1416         );
1417
1418         if ( $r['send'] ) {
1419                 $r['send'] = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
1420         }
1421
1422         $delete = empty( $r['delete'] ) ? '' : $r['delete'];
1423         if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
1424                 if ( !EMPTY_TRASH_DAYS ) {
1425                         $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete-permanently'>" . __( 'Delete Permanently' ) . '</a>';
1426                 } elseif ( !MEDIA_TRASH ) {
1427                         $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
1428                          <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
1429                          <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
1430                          <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
1431                          </div>";
1432                 } else {
1433                         $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
1434                         <a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-post_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
1435                 }
1436         } else {
1437                 $delete = '';
1438         }
1439
1440         $thumbnail = '';
1441         $calling_post_id = 0;
1442         if ( isset( $_GET['post_id'] ) ) {
1443                 $calling_post_id = absint( $_GET['post_id'] );
1444         } elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set
1445                 $calling_post_id = $post->post_parent;
1446         }
1447         if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
1448                 && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
1449                 $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
1450                 $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>";
1451         }
1452
1453         if ( ( $r['send'] || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) ) {
1454                 $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $r['send'] . " $thumbnail $delete</td></tr>\n" );
1455         }
1456         $hidden_fields = array();
1457
1458         foreach ( $form_fields as $id => $field ) {
1459                 if ( $id[0] == '_' )
1460                         continue;
1461
1462                 if ( !empty( $field['tr'] ) ) {
1463                         $item .= $field['tr'];
1464                         continue;
1465                 }
1466
1467                 $field = array_merge( $defaults, $field );
1468                 $name = "attachments[$attachment_id][$id]";
1469
1470                 if ( $field['input'] == 'hidden' ) {
1471                         $hidden_fields[$name] = $field['value'];
1472                         continue;
1473                 }
1474
1475                 $required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1476                 $aria_required = $field['required'] ? " aria-required='true' " : '';
1477                 $class  = $id;
1478                 $class .= $field['required'] ? ' form-required' : '';
1479
1480                 $item .= "\t\t<tr class='$class'>\n\t\t\t<th 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'>";
1481                 if ( !empty( $field[ $field['input'] ] ) )
1482                         $item .= $field[ $field['input'] ];
1483                 elseif ( $field['input'] == 'textarea' ) {
1484                         if ( 'post_content' == $id && user_can_richedit() ) {
1485                                 // Sanitize_post() skips the post_content when user_can_richedit.
1486                                 $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1487                         }
1488                         // Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
1489                         $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1490                 } else {
1491                         $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
1492                 }
1493                 if ( !empty( $field['helps'] ) )
1494                         $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1495                 $item .= "</td>\n\t\t</tr>\n";
1496
1497                 $extra_rows = array();
1498
1499                 if ( !empty( $field['errors'] ) )
1500                         foreach ( array_unique( (array) $field['errors'] ) as $error )
1501                                 $extra_rows['error'][] = $error;
1502
1503                 if ( !empty( $field['extra_rows'] ) )
1504                         foreach ( $field['extra_rows'] as $class => $rows )
1505                                 foreach ( (array) $rows as $html )
1506                                         $extra_rows[$class][] = $html;
1507
1508                 foreach ( $extra_rows as $class => $rows )
1509                         foreach ( $rows as $html )
1510                                 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1511         }
1512
1513         if ( !empty( $form_fields['_final'] ) )
1514                 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1515         $item .= "\t</tbody>\n";
1516         $item .= "\t</table>\n";
1517
1518         foreach ( $hidden_fields as $name => $value )
1519                 $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
1520
1521         if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
1522                 $parent = (int) $_REQUEST['post_id'];
1523                 $parent_name = "attachments[$attachment_id][post_parent]";
1524                 $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
1525         }
1526
1527         return $item;
1528 }
1529
1530 function get_compat_media_markup( $attachment_id, $args = null ) {
1531         $post = get_post( $attachment_id );
1532
1533         $default_args = array(
1534                 'errors' => null,
1535                 'in_modal' => false,
1536         );
1537
1538         $user_can_edit = current_user_can( 'edit_post', $attachment_id );
1539
1540         $args = wp_parse_args( $args, $default_args );
1541
1542         /** This filter is documented in wp-admin/includes/media.php */
1543         $args = apply_filters( 'get_media_item_args', $args );
1544
1545         $form_fields = array();
1546
1547         if ( $args['in_modal'] ) {
1548                 foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1549                         $t = (array) get_taxonomy($taxonomy);
1550                         if ( ! $t['public'] || ! $t['show_ui'] )
1551                                 continue;
1552                         if ( empty($t['label']) )
1553                                 $t['label'] = $taxonomy;
1554                         if ( empty($t['args']) )
1555                                 $t['args'] = array();
1556
1557                         $terms = get_object_term_cache($post->ID, $taxonomy);
1558                         if ( false === $terms )
1559                                 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1560
1561                         $values = array();
1562
1563                         foreach ( $terms as $term )
1564                                 $values[] = $term->slug;
1565                         $t['value'] = join(', ', $values);
1566                         $t['taxonomy'] = true;
1567
1568                         $form_fields[$taxonomy] = $t;
1569                 }
1570         }
1571
1572         // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1573         // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1574         $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
1575
1576         /** This filter is documented in wp-admin/includes/media.php */
1577         $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
1578
1579         unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
1580                 $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
1581                 $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
1582
1583         /** This filter is documented in wp-admin/includes/media.php */
1584         $media_meta = apply_filters( 'media_meta', '', $post );
1585
1586         $defaults = array(
1587                 'input'         => 'text',
1588                 'required'      => false,
1589                 'value'         => '',
1590                 'extra_rows'    => array(),
1591                 'show_in_edit'  => true,
1592                 'show_in_modal' => true,
1593         );
1594
1595         $hidden_fields = array();
1596
1597         $item = '';
1598         foreach ( $form_fields as $id => $field ) {
1599                 if ( $id[0] == '_' )
1600                         continue;
1601
1602                 $name = "attachments[$attachment_id][$id]";
1603                 $id_attr = "attachments-$attachment_id-$id";
1604
1605                 if ( !empty( $field['tr'] ) ) {
1606                         $item .= $field['tr'];
1607                         continue;
1608                 }
1609
1610                 $field = array_merge( $defaults, $field );
1611
1612                 if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
1613                         continue;
1614
1615                 if ( $field['input'] == 'hidden' ) {
1616                         $hidden_fields[$name] = $field['value'];
1617                         continue;
1618                 }
1619
1620                 $readonly      = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
1621                 $required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1622                 $aria_required = $field['required'] ? " aria-required='true' " : '';
1623                 $class  = 'compat-field-' . $id;
1624                 $class .= $field['required'] ? ' form-required' : '';
1625
1626                 $item .= "\t\t<tr class='$class'>";
1627                 $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
1628                 $item .= "</th>\n\t\t\t<td class='field'>";
1629
1630                 if ( !empty( $field[ $field['input'] ] ) )
1631                         $item .= $field[ $field['input'] ];
1632                 elseif ( $field['input'] == 'textarea' ) {
1633                         if ( 'post_content' == $id && user_can_richedit() ) {
1634                                 // sanitize_post() skips the post_content when user_can_richedit.
1635                                 $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1636                         }
1637                         $item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1638                 } else {
1639                         $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly $aria_required />";
1640                 }
1641                 if ( !empty( $field['helps'] ) )
1642                         $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1643                 $item .= "</td>\n\t\t</tr>\n";
1644
1645                 $extra_rows = array();
1646
1647                 if ( !empty( $field['errors'] ) )
1648                         foreach ( array_unique( (array) $field['errors'] ) as $error )
1649                                 $extra_rows['error'][] = $error;
1650
1651                 if ( !empty( $field['extra_rows'] ) )
1652                         foreach ( $field['extra_rows'] as $class => $rows )
1653                                 foreach ( (array) $rows as $html )
1654                                         $extra_rows[$class][] = $html;
1655
1656                 foreach ( $extra_rows as $class => $rows )
1657                         foreach ( $rows as $html )
1658                                 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1659         }
1660
1661         if ( !empty( $form_fields['_final'] ) )
1662                 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1663         if ( $item )
1664                 $item = '<table class="compat-attachment-fields">' . $item . '</table>';
1665
1666         foreach ( $hidden_fields as $hidden_field => $value ) {
1667                 $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
1668         }
1669
1670         if ( $item )
1671                 $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
1672
1673         return array(
1674                 'item'   => $item,
1675                 'meta'   => $media_meta,
1676         );
1677 }
1678
1679 /**
1680  * {@internal Missing Short Description}}
1681  *
1682  * @since 2.5.0
1683  */
1684 function media_upload_header() {
1685         $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
1686         echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
1687         if ( empty( $_GET['chromeless'] ) ) {
1688                 echo '<div id="media-upload-header">';
1689                 the_media_upload_tabs();
1690                 echo '</div>';
1691         }
1692 }
1693
1694 /**
1695  * {@internal Missing Short Description}}
1696  *
1697  * @since 2.5.0
1698  *
1699  * @param unknown_type $errors
1700  */
1701 function media_upload_form( $errors = null ) {
1702         global $type, $tab, $is_IE, $is_opera;
1703
1704         if ( ! _device_can_upload() ) {
1705                 echo '<p>' . sprintf( __('The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.'), 'http://apps.wordpress.org/' ) . '</p>';
1706                 return;
1707         }
1708
1709         $upload_action_url = admin_url('async-upload.php');
1710         $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
1711         $_type = isset($type) ? $type : '';
1712         $_tab = isset($tab) ? $tab : '';
1713
1714         $max_upload_size = wp_max_upload_size();
1715         if ( ! $max_upload_size ) {
1716                 $max_upload_size = 0;
1717         }
1718 ?>
1719
1720 <div id="media-upload-notice"><?php
1721
1722         if (isset($errors['upload_notice']) )
1723                 echo $errors['upload_notice'];
1724
1725 ?></div>
1726 <div id="media-upload-error"><?php
1727
1728         if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
1729                 echo $errors['upload_error']->get_error_message();
1730
1731 ?></div>
1732 <?php
1733 if ( is_multisite() && !is_upload_space_available() ) {
1734         /**
1735          * Fires when an upload will exceed the defined upload space quota for a network site.
1736          *
1737          * @since 3.5.0
1738          */
1739         do_action( 'upload_ui_over_quota' );
1740         return;
1741 }
1742
1743 /**
1744  * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
1745  *
1746  * @since 2.6.0
1747  */
1748 do_action( 'pre-upload-ui' );
1749
1750 $post_params = array(
1751         "post_id" => $post_id,
1752         "_wpnonce" => wp_create_nonce('media-form'),
1753         "type" => $_type,
1754         "tab" => $_tab,
1755         "short" => "1",
1756 );
1757
1758 /**
1759  * Filter the media upload post parameters.
1760  *
1761  * @since 3.1.0 As 'swfupload_post_params'
1762  * @since 3.3.0
1763  *
1764  * @param array $post_params An array of media upload parameters used by Plupload.
1765  */
1766 $post_params = apply_filters( 'upload_post_params', $post_params );
1767
1768 $plupload_init = array(
1769         'runtimes'            => 'html5,flash,silverlight,html4',
1770         'browse_button'       => 'plupload-browse-button',
1771         'container'           => 'plupload-upload-ui',
1772         'drop_element'        => 'drag-drop-area',
1773         'file_data_name'      => 'async-upload',
1774         'url'                 => $upload_action_url,
1775         'flash_swf_url'       => includes_url( 'js/plupload/plupload.flash.swf' ),
1776         'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
1777         'filters' => array(
1778                 'max_file_size'   => $max_upload_size . 'b',
1779         ),
1780         'multipart_params'    => $post_params,
1781 );
1782
1783 /**
1784  * Filter the default Plupload settings.
1785  *
1786  * @since 3.3.0
1787  *
1788  * @param array $plupload_init An array of default settings used by Plupload.
1789  */
1790 $plupload_init = apply_filters( 'plupload_init', $plupload_init );
1791
1792 ?>
1793
1794 <script type="text/javascript">
1795 <?php
1796 // Verify size is an int. If not return default value.
1797 $large_size_h = absint( get_option('large_size_h') );
1798 if( !$large_size_h )
1799         $large_size_h = 1024;
1800 $large_size_w = absint( get_option('large_size_w') );
1801 if( !$large_size_w )
1802         $large_size_w = 1024;
1803 ?>
1804 var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
1805 wpUploaderInit = <?php echo json_encode($plupload_init); ?>;
1806 </script>
1807
1808 <div id="plupload-upload-ui" class="hide-if-no-js">
1809 <?php
1810 /**
1811  * Fires before the upload interface loads.
1812  *
1813  * @since 2.6.0 As 'pre-flash-upload-ui'
1814  * @since 3.3.0
1815  */
1816 do_action( 'pre-plupload-upload-ui' ); ?>
1817 <div id="drag-drop-area">
1818         <div class="drag-drop-inside">
1819         <p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
1820         <p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
1821         <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
1822         </div>
1823 </div>
1824 <?php
1825 /**
1826  * Fires after the upload interface loads.
1827  *
1828  * @since 2.6.0 As 'post-flash-upload-ui'
1829  * @since 3.3.0
1830  */
1831 do_action( 'post-plupload-upload-ui' ); ?>
1832 </div>
1833
1834 <div id="html-upload-ui" class="hide-if-js">
1835         <?php
1836         /**
1837          * Fires before the upload button in the media upload interface.
1838          *
1839          * @since 2.6.0
1840          */
1841         do_action( 'pre-html-upload-ui' );
1842         ?>
1843         <p id="async-upload-wrap">
1844                 <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
1845                 <input type="file" name="async-upload" id="async-upload" />
1846                 <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
1847                 <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
1848         </p>
1849         <div class="clear"></div>
1850 <?php
1851 /**
1852  * Fires after the upload button in the media upload interface.
1853  *
1854  * @since 2.6.0
1855  */
1856 do_action( 'post-html-upload-ui' );
1857 ?>
1858 </div>
1859
1860 <p class="max-upload-size"><?php printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) ); ?></p>
1861 <?php
1862
1863         /**
1864          * Fires on the post upload UI screen.
1865          *
1866          * Legacy (pre-3.5.0) media workflow hook.
1867          *
1868          * @since 2.6.0
1869          */
1870         do_action( 'post-upload-ui' );
1871 }
1872
1873 /**
1874  * {@internal Missing Short Description}}
1875  *
1876  * @since 2.5.0
1877  *
1878  * @param string $type
1879  * @param object $errors
1880  * @param integer $id
1881  */
1882 function media_upload_type_form($type = 'file', $errors = null, $id = null) {
1883
1884         media_upload_header();
1885
1886         $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
1887
1888         $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1889
1890         /**
1891          * Filter the media upload form action URL.
1892          *
1893          * @since 2.6.0
1894          *
1895          * @param string $form_action_url The media upload form action URL.
1896          * @param string $type            The type of media. Default 'file'.
1897          */
1898         $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
1899         $form_class = 'media-upload-form type-form validate';
1900
1901         if ( get_user_setting('uploader') )
1902                 $form_class .= ' html-uploader';
1903 ?>
1904
1905 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
1906 <?php submit_button( '', 'hidden', 'save', false ); ?>
1907 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1908 <?php wp_nonce_field('media-form'); ?>
1909
1910 <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
1911
1912 <?php media_upload_form( $errors ); ?>
1913
1914 <script type="text/javascript">
1915 //<![CDATA[
1916 jQuery(function($){
1917         var preloaded = $(".media-item.preloaded");
1918         if ( preloaded.length > 0 ) {
1919                 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1920         }
1921         updateMediaForm();
1922 });
1923 //]]>
1924 </script>
1925 <div id="media-items"><?php
1926
1927 if ( $id ) {
1928         if ( !is_wp_error($id) ) {
1929                 add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
1930                 echo get_media_items( $id, $errors );
1931         } else {
1932                 echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
1933                 exit;
1934         }
1935 }
1936 ?></div>
1937
1938 <p class="savebutton ml-submit">
1939 <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
1940 </p>
1941 </form>
1942 <?php
1943 }
1944
1945 /**
1946  * {@internal Missing Short Description}}
1947  *
1948  * @since 2.7.0
1949  *
1950  * @param string $type
1951  * @param object $errors
1952  * @param integer $id
1953  */
1954 function media_upload_type_url_form($type = null, $errors = null, $id = null) {
1955         if ( null === $type )
1956                 $type = 'image';
1957
1958         media_upload_header();
1959
1960         $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
1961
1962         $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1963         /** This filter is documented in wp-admin/includes/media.php */
1964         $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
1965         $form_class = 'media-upload-form type-form validate';
1966
1967         if ( get_user_setting('uploader') )
1968                 $form_class .= ' html-uploader';
1969 ?>
1970
1971 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
1972 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1973 <?php wp_nonce_field('media-form'); ?>
1974
1975 <h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
1976
1977 <script type="text/javascript">
1978 //<![CDATA[
1979 var addExtImage = {
1980
1981         width : '',
1982         height : '',
1983         align : 'alignnone',
1984
1985         insert : function() {
1986                 var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
1987
1988                 if ( '' == f.src.value || '' == t.width )
1989                         return false;
1990
1991                 if ( f.alt.value )
1992                         alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1993
1994 <?php
1995         /** This filter is documented in wp-admin/includes/media.php */
1996         if ( ! apply_filters( 'disable_captions', '' ) ) {
1997                 ?>
1998                 if ( f.caption.value ) {
1999                         caption = f.caption.value.replace(/\r\n|\r/g, '\n');
2000                         caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
2001                                 return a.replace(/[\r\n\t]+/, ' ');
2002                         });
2003
2004                         caption = caption.replace(/\s*\n\s*/g, '<br />');
2005                 }
2006 <?php } ?>
2007
2008                 cls = caption ? '' : ' class="'+t.align+'"';
2009
2010                 html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
2011
2012                 if ( f.url.value ) {
2013                         url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
2014                         html = '<a href="'+url+'">'+html+'</a>';
2015                 }
2016
2017                 if ( caption )
2018                         html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
2019
2020                 var win = window.dialogArguments || opener || parent || top;
2021                 win.send_to_editor(html);
2022                 return false;
2023         },
2024
2025         resetImageData : function() {
2026                 var t = addExtImage;
2027
2028                 t.width = t.height = '';
2029                 document.getElementById('go_button').style.color = '#bbb';
2030                 if ( ! document.forms[0].src.value )
2031                         document.getElementById('status_img').innerHTML = '*';
2032                 else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
2033         },
2034
2035         updateImageData : function() {
2036                 var t = addExtImage;
2037
2038                 t.width = t.preloadImg.width;
2039                 t.height = t.preloadImg.height;
2040                 document.getElementById('go_button').style.color = '#333';
2041                 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
2042         },
2043
2044         getImageData : function() {
2045                 if ( jQuery('table.describe').hasClass('not-image') )
2046                         return;
2047
2048                 var t = addExtImage, src = document.forms[0].src.value;
2049
2050                 if ( ! src ) {
2051                         t.resetImageData();
2052                         return false;
2053                 }
2054
2055                 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" width="16" />';
2056                 t.preloadImg = new Image();
2057                 t.preloadImg.onload = t.updateImageData;
2058                 t.preloadImg.onerror = t.resetImageData;
2059                 t.preloadImg.src = src;
2060         }
2061 }
2062
2063 jQuery(document).ready( function($) {
2064         $('.media-types input').click( function() {
2065                 $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
2066         });
2067 });
2068
2069 //]]>
2070 </script>
2071
2072 <div id="media-items">
2073 <div class="media-item media-blank">
2074 <?php
2075 /**
2076  * Filter the insert media from URL form HTML.
2077  *
2078  * @since 3.3.0
2079  *
2080  * @param string $form_html The insert from URL form HTML.
2081  */
2082 echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
2083 ?>
2084 </div>
2085 </div>
2086 </form>
2087 <?php
2088 }
2089
2090 /**
2091  * Adds gallery form to upload iframe
2092  *
2093  * @since 2.5.0
2094  *
2095  * @param array $errors
2096  */
2097 function media_upload_gallery_form($errors) {
2098         global $redir_tab, $type;
2099
2100         $redir_tab = 'gallery';
2101         media_upload_header();
2102
2103         $post_id = intval($_REQUEST['post_id']);
2104         $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
2105         /** This filter is documented in wp-admin/includes/media.php */
2106         $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
2107         $form_class = 'media-upload-form validate';
2108
2109         if ( get_user_setting('uploader') )
2110                 $form_class .= ' html-uploader';
2111 ?>
2112
2113 <script type="text/javascript">
2114 jQuery(function($){
2115         var preloaded = $(".media-item.preloaded");
2116         if ( preloaded.length > 0 ) {
2117                 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2118                 updateMediaForm();
2119         }
2120 });
2121 </script>
2122 <div id="sort-buttons" class="hide-if-no-js">
2123 <span>
2124 <?php _e('All Tabs:'); ?>
2125 <a href="#" id="showall"><?php _e('Show'); ?></a>
2126 <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
2127 </span>
2128 <?php _e('Sort Order:'); ?>
2129 <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
2130 <a href="#" id="desc"><?php _e('Descending'); ?></a> |
2131 <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
2132 </div>
2133 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
2134 <?php wp_nonce_field('media-form'); ?>
2135 <?php //media_upload_form( $errors ); ?>
2136 <table class="widefat">
2137 <thead><tr>
2138 <th><?php _e('Media'); ?></th>
2139 <th class="order-head"><?php _e('Order'); ?></th>
2140 <th class="actions-head"><?php _e('Actions'); ?></th>
2141 </tr></thead>
2142 </table>
2143 <div id="media-items">
2144 <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2145 <?php echo get_media_items($post_id, $errors); ?>
2146 </div>
2147
2148 <p class="ml-submit">
2149 <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
2150 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2151 <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
2152 <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
2153 </p>
2154
2155 <div id="gallery-settings" style="display:none;">
2156 <div class="title"><?php _e('Gallery Settings'); ?></div>
2157 <table id="basic" class="describe"><tbody>
2158         <tr>
2159         <th scope="row" class="label">
2160                 <label>
2161                 <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
2162                 </label>
2163         </th>
2164         <td class="field">
2165                 <input type="radio" name="linkto" id="linkto-file" value="file" />
2166                 <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
2167
2168                 <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
2169                 <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
2170         </td>
2171         </tr>
2172
2173         <tr>
2174         <th scope="row" class="label">
2175                 <label>
2176                 <span class="alignleft"><?php _e('Order images by:'); ?></span>
2177                 </label>
2178         </th>
2179         <td class="field">
2180                 <select id="orderby" name="orderby">
2181                         <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
2182                         <option value="title"><?php _e('Title'); ?></option>
2183                         <option value="post_date"><?php _e('Date/Time'); ?></option>
2184                         <option value="rand"><?php _e('Random'); ?></option>
2185                 </select>
2186         </td>
2187         </tr>
2188
2189         <tr>
2190         <th scope="row" class="label">
2191                 <label>
2192                 <span class="alignleft"><?php _e('Order:'); ?></span>
2193                 </label>
2194         </th>
2195         <td class="field">
2196                 <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
2197                 <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
2198
2199                 <input type="radio" name="order" id="order-desc" value="desc" />
2200                 <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
2201         </td>
2202         </tr>
2203
2204         <tr>
2205         <th scope="row" class="label">
2206                 <label>
2207                 <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
2208                 </label>
2209         </th>
2210         <td class="field">
2211                 <select id="columns" name="columns">
2212                         <option value="1">1</option>
2213                         <option value="2">2</option>
2214                         <option value="3" selected="selected">3</option>
2215                         <option value="4">4</option>
2216                         <option value="5">5</option>
2217                         <option value="6">6</option>
2218                         <option value="7">7</option>
2219                         <option value="8">8</option>
2220                         <option value="9">9</option>
2221                 </select>
2222         </td>
2223         </tr>
2224 </tbody></table>
2225
2226 <p class="ml-submit">
2227 <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
2228 <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' ); ?>" />
2229 </p>
2230 </div>
2231 </form>
2232 <?php
2233 }
2234
2235 /**
2236  * {@internal Missing Short Description}}
2237  *
2238  * @since 2.5.0
2239  *
2240  * @param array $errors
2241  */
2242 function media_upload_library_form($errors) {
2243         global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
2244
2245         media_upload_header();
2246
2247         $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
2248
2249         $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
2250         /** This filter is documented in wp-admin/includes/media.php */
2251         $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
2252         $form_class = 'media-upload-form validate';
2253
2254         if ( get_user_setting('uploader') )
2255                 $form_class .= ' html-uploader';
2256
2257         $q = $_GET;
2258         $q['posts_per_page'] = 10;
2259         $q['paged'] = isset( $q['paged'] ) ? intval( $q['paged'] ) : 0;
2260         if ( $q['paged'] < 1 ) {
2261                 $q['paged'] = 1;
2262         }
2263         $q['offset'] = ( $q['paged'] - 1 ) * 10;
2264         if ( $q['offset'] < 1 ) {
2265                 $q['offset'] = 0;
2266         }
2267
2268         list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
2269
2270 ?>
2271
2272 <form id="filter" action="" method="get">
2273 <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
2274 <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
2275 <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
2276 <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
2277 <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
2278
2279 <p id="media-search" class="search-box">
2280         <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
2281         <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
2282         <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
2283 </p>
2284
2285 <ul class="subsubsub">
2286 <?php
2287 $type_links = array();
2288 $_num_posts = (array) wp_count_attachments();
2289 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
2290 foreach ( $matches as $_type => $reals )
2291         foreach ( $reals as $real )
2292                 if ( isset($num_posts[$_type]) )
2293                         $num_posts[$_type] += $_num_posts[$real];
2294                 else
2295                         $num_posts[$_type] = $_num_posts[$real];
2296 // If available type specified by media button clicked, filter by that type
2297 if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
2298         $_GET['post_mime_type'] = $type;
2299         list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
2300 }
2301 if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
2302         $class = ' class="current"';
2303 else
2304         $class = '';
2305 $type_links[] = '<li><a href="' . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . '"' . $class . '>' . __('All Types') . '</a>';
2306 foreach ( $post_mime_types as $mime_type => $label ) {
2307         $class = '';
2308
2309         if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
2310                 continue;
2311
2312         if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
2313                 $class = ' class="current"';
2314
2315         $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>';
2316 }
2317 /**
2318  * Filter the media upload mime type list items.
2319  *
2320  * Returned values should begin with an <li> tag.
2321  *
2322  * @since 3.1.0
2323  *
2324  * @param array $type_links An array of list items containing mime type link HTML.
2325  */
2326 echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
2327 unset($type_links);
2328 ?>
2329 </ul>
2330
2331 <div class="tablenav">
2332
2333 <?php
2334 $page_links = paginate_links( array(
2335         'base' => add_query_arg( 'paged', '%#%' ),
2336         'format' => '',
2337         'prev_text' => __('&laquo;'),
2338         'next_text' => __('&raquo;'),
2339         'total' => ceil($wp_query->found_posts / 10),
2340         'current' => $q['paged'],
2341 ));
2342
2343 if ( $page_links )
2344         echo "<div class='tablenav-pages'>$page_links</div>";
2345 ?>
2346
2347 <div class="alignleft actions">
2348 <?php
2349
2350 $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";
2351
2352 $arc_result = $wpdb->get_results( $arc_query );
2353
2354 $month_count = count($arc_result);
2355 $selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
2356
2357 if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
2358 <select name='m'>
2359 <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
2360 <?php
2361 foreach ($arc_result as $arc_row) {
2362         if ( $arc_row->yyear == 0 )
2363                 continue;
2364         $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
2365
2366         if ( $arc_row->yyear . $arc_row->mmonth == $selected_month )
2367                 $default = ' selected="selected"';
2368         else
2369                 $default = '';
2370
2371         echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
2372         echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
2373         echo "</option>\n";
2374 }
2375 ?>
2376 </select>
2377 <?php } ?>
2378
2379 <?php submit_button( __( 'Filter &#187;' ), 'button', 'post-query-submit', false ); ?>
2380
2381 </div>
2382
2383 <br class="clear" />
2384 </div>
2385 </form>
2386
2387 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
2388
2389 <?php wp_nonce_field('media-form'); ?>
2390 <?php //media_upload_form( $errors ); ?>
2391
2392 <script type="text/javascript">
2393 <!--
2394 jQuery(function($){
2395         var preloaded = $(".media-item.preloaded");
2396         if ( preloaded.length > 0 ) {
2397                 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2398                 updateMediaForm();
2399         }
2400 });
2401 -->
2402 </script>
2403
2404 <div id="media-items">
2405 <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2406 <?php echo get_media_items(null, $errors); ?>
2407 </div>
2408 <p class="ml-submit">
2409 <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
2410 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2411 </p>
2412 </form>
2413 <?php
2414 }
2415
2416 /**
2417  * Creates the form for external url
2418  *
2419  * @since 2.7.0
2420  *
2421  * @param string $default_view
2422  * @return string the form html
2423  */
2424 function wp_media_insert_url_form( $default_view = 'image' ) {
2425         /** This filter is documented in wp-admin/includes/media.php */
2426         if ( ! apply_filters( 'disable_captions', '' ) ) {
2427                 $caption = '
2428                 <tr class="image-only">
2429                         <th scope="row" class="label">
2430                                 <label for="caption"><span class="alignleft">' . __('Image Caption') . '</span></label>
2431                         </th>
2432                         <td class="field"><textarea id="caption" name="caption"></textarea></td>
2433                 </tr>
2434 ';
2435         } else {
2436                 $caption = '';
2437         }
2438
2439         $default_align = get_option('image_default_align');
2440         if ( empty($default_align) )
2441                 $default_align = 'none';
2442
2443         if ( 'image' == $default_view ) {
2444                 $view = 'image-only';
2445                 $table_class = '';
2446         } else {
2447                 $view = $table_class = 'not-image';
2448         }
2449
2450         return '
2451         <p class="media-types"><label><input type="radio" name="media_type" value="image" id="image-only"' . checked( 'image-only', $view, false ) . ' /> ' . __( 'Image' ) . '</label> &nbsp; &nbsp; <label><input type="radio" name="media_type" value="generic" id="not-image"' . checked( 'not-image', $view, false ) . ' /> ' . __( 'Audio, Video, or Other File' ) . '</label></p>
2452         <table class="describe ' . $table_class . '"><tbody>
2453                 <tr>
2454                         <th scope="row" class="label" style="width:130px;">
2455                                 <label for="src"><span class="alignleft">' . __('URL') . '</span></label>
2456                                 <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
2457                         </th>
2458                         <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
2459                 </tr>
2460
2461                 <tr>
2462                         <th scope="row" class="label">
2463                                 <label for="title"><span class="alignleft">' . __('Title') . '</span></label>
2464                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2465                         </th>
2466                         <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
2467                 </tr>
2468
2469                 <tr class="not-image"><td></td><td><p class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</p></td></tr>
2470
2471                 <tr class="image-only">
2472                         <th scope="row" class="label">
2473                                 <label for="alt"><span class="alignleft">' . __('Alternative Text') . '</span></label>
2474                         </th>
2475                         <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
2476                         <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
2477                 </tr>
2478                 ' . $caption . '
2479                 <tr class="align image-only">
2480                         <th scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
2481                         <td class="field">
2482                                 <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
2483                                 <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
2484                                 <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
2485                                 <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
2486                                 <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
2487                                 <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
2488                                 <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
2489                                 <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
2490                         </td>
2491                 </tr>
2492
2493                 <tr class="image-only">
2494                         <th scope="row" class="label">
2495                                 <label for="url"><span class="alignleft">' . __('Link Image To:') . '</span></label>
2496                         </th>
2497                         <td class="field"><input id="url" name="url" value="" type="text" /><br />
2498
2499                         <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
2500                         <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
2501                         <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
2502                 </tr>
2503                 <tr class="image-only">
2504                         <td></td>
2505                         <td>
2506                                 <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
2507                         </td>
2508                 </tr>
2509                 <tr class="not-image">
2510                         <td></td>
2511                         <td>
2512                                 ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
2513                         </td>
2514                 </tr>
2515         </tbody></table>
2516 ';
2517
2518 }
2519
2520 /**
2521  * Displays the multi-file uploader message.
2522  *
2523  * @since 2.6.0
2524  */
2525 function media_upload_flash_bypass() {
2526         $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
2527
2528         if ( $post = get_post() )
2529                 $browser_uploader .= '&amp;post_id=' . intval( $post->ID );
2530         elseif ( ! empty( $GLOBALS['post_ID'] ) )
2531                 $browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
2532
2533         ?>
2534         <p class="upload-flash-bypass">
2535         <?php printf( __( 'You are using the multi-file uploader. Problems? Try the <a href="%1$s" target="%2$s">browser uploader</a> instead.' ), $browser_uploader, '_blank' ); ?>
2536         </p>
2537         <?php
2538 }
2539 add_action('post-plupload-upload-ui', 'media_upload_flash_bypass');
2540
2541 /**
2542  * Displays the browser's built-in uploader message.
2543  *
2544  * @since 2.6.0
2545  */
2546 function media_upload_html_bypass() {
2547         ?>
2548         <p class="upload-html-bypass hide-if-no-js">
2549            <?php _e('You are using the browser&#8217;s built-in file uploader. The WordPress uploader includes multiple file selection and drag and drop capability. <a href="#">Switch to the multi-file uploader</a>.'); ?>
2550         </p>
2551         <?php
2552 }
2553 add_action('post-html-upload-ui', 'media_upload_html_bypass');
2554
2555 /**
2556  * Used to display a "After a file has been uploaded..." help message.
2557  *
2558  * @since 3.3.0
2559  */
2560 function media_upload_text_after() {}
2561
2562 /**
2563  * Displays the checkbox to scale images.
2564  *
2565  * @since 3.3.0
2566  */
2567 function media_upload_max_image_resize() {
2568         $checked = get_user_setting('upload_resize') ? ' checked="true"' : '';
2569         $a = $end = '';
2570
2571         if ( current_user_can( 'manage_options' ) ) {
2572                 $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
2573                 $end = '</a>';
2574         }
2575 ?>
2576 <p class="hide-if-no-js"><label>
2577 <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
2578 <?php
2579         /* translators: %1$s is link start tag, %2$s is link end tag, %3$d is width, %4$d is height*/
2580         printf( __( 'Scale images to match the large size selected in %1$simage options%2$s (%3$d &times; %4$d).' ), $a, $end, (int) get_option( 'large_size_w', '1024' ), (int) get_option( 'large_size_h', '1024' ) );
2581 ?>
2582 </label></p>
2583 <?php
2584 }
2585
2586 /**
2587  * Displays the out of storage quota message in Multisite.
2588  *
2589  * @since 3.5.0
2590  */
2591 function multisite_over_quota_message() {
2592         echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
2593 }
2594
2595 /**
2596  * Displays the image and editor in the post editor
2597  *
2598  * @since 3.5.0
2599  */
2600 function edit_form_image_editor( $post ) {
2601         $open = isset( $_GET['image-editor'] );
2602         if ( $open )
2603                 require_once ABSPATH . 'wp-admin/includes/image-edit.php';
2604
2605         $thumb_url = false;
2606         if ( $attachment_id = intval( $post->ID ) )
2607                 $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
2608
2609         $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
2610
2611         $att_url = wp_get_attachment_url( $post->ID ); ?>
2612         <div class="wp_attachment_holder">
2613         <?php
2614         if ( wp_attachment_is_image( $post->ID ) ) :
2615                 $image_edit_button = '';
2616                 if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
2617                         $nonce = wp_create_nonce( "image_editor-$post->ID" );
2618                         $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' ) . "' /> <span class='spinner'></span>";
2619                 }
2620         ?>
2621
2622                 <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
2623
2624                 <div<?php if ( $open ) echo ' style="display:none"'; ?> class="wp_attachment_image" id="media-head-<?php echo $attachment_id; ?>">
2625                         <p id="thumbnail-head-<?php echo $attachment_id; ?>"><img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" /></p>
2626                         <p><?php echo $image_edit_button; ?></p>
2627                 </div>
2628                 <div<?php if ( ! $open ) echo ' style="display:none"'; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
2629                         <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
2630                 </div>
2631         <?php
2632         elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):
2633
2634                 wp_maybe_generate_attachment_metadata( $post );
2635
2636                 echo wp_audio_shortcode( array( 'src' => $att_url ) );
2637
2638         elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):
2639
2640                 wp_maybe_generate_attachment_metadata( $post );
2641
2642                 $meta = wp_get_attachment_metadata( $attachment_id );
2643                 $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
2644                 $h = ! empty( $meta['height'] ) ? $meta['height'] : 0;
2645                 if ( $h && $w < $meta['width'] ) {
2646                         $h = round( ( $meta['height'] * $w ) / $meta['width'] );
2647                 }
2648
2649                 $attr = array( 'src' => $att_url );
2650                 if ( ! empty( $w ) && ! empty( $h ) ) {
2651                         $attr['width'] = $w;
2652                         $attr['height'] = $h;
2653                 }
2654
2655                 $thumb_id = get_post_thumbnail_id( $attachment_id );
2656                 if ( ! empty( $thumb_id ) ) {
2657                         $attr['poster'] = wp_get_attachment_url( $thumb_id );
2658                 }
2659
2660                 echo wp_video_shortcode( $attr );
2661
2662         endif; ?>
2663         </div>
2664         <div class="wp_attachment_details edit-form-section">
2665                 <p>
2666                         <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
2667                         <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
2668                 </p>
2669
2670
2671         <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
2672                 <p>
2673                         <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
2674                         <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" value="<?php echo esc_attr( $alt_text ); ?>" />
2675                 </p>
2676         <?php endif; ?>
2677
2678         <?php
2679                 $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
2680                 $editor_args = array(
2681                         'textarea_name' => 'content',
2682                         'textarea_rows' => 5,
2683                         'media_buttons' => false,
2684                         'tinymce' => false,
2685                         'quicktags' => $quicktags_settings,
2686                 );
2687         ?>
2688
2689         <label for="content"><strong><?php _e( 'Description' ); ?></strong><?php
2690         if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
2691                 echo ': ' . __( 'Displayed on attachment pages.' );
2692         } ?></label>
2693         <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
2694
2695         </div>
2696         <?php
2697         $extras = get_compat_media_markup( $post->ID );
2698         echo $extras['item'];
2699         echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
2700 }
2701
2702 /**
2703  * Displays non-editable attachment metadata in the publish metabox
2704  *
2705  * @since 3.5.0
2706  */
2707 function attachment_submitbox_metadata() {
2708         $post = get_post();
2709
2710         $filename = esc_html( wp_basename( $post->guid ) );
2711
2712         $media_dims = '';
2713         $meta = wp_get_attachment_metadata( $post->ID );
2714         if ( isset( $meta['width'], $meta['height'] ) )
2715                 $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
2716         /** This filter is documented in wp-admin/includes/media.php */
2717         $media_dims = apply_filters( 'media_meta', $media_dims, $post );
2718
2719         $att_url = wp_get_attachment_url( $post->ID );
2720 ?>
2721         <div class="misc-pub-section misc-pub-attachment">
2722                         <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
2723                         <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" value="<?php echo esc_attr($att_url); ?>" />
2724         </div>
2725         <div class="misc-pub-section misc-pub-filename">
2726                 <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
2727         </div>
2728         <div class="misc-pub-section misc-pub-filetype">
2729                 <?php _e( 'File type:' ); ?> <strong><?php
2730                         if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
2731                                 echo esc_html( strtoupper( $matches[1] ) );
2732                                 list( $mime_type ) = explode( '/', $post->post_mime_type );
2733                                 if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
2734                                         if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
2735                                                 echo ' (' . $meta['mime_type'] . ')';
2736                                         }
2737                                 }
2738                         } else {
2739                                 echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
2740                         }
2741                 ?></strong>
2742         </div>
2743
2744         <?php
2745                 $file  = get_attached_file( $post->ID );
2746                 $file_size = false;
2747
2748                 if ( isset( $meta['filesize'] ) )
2749                         $file_size = $meta['filesize'];
2750                 elseif ( file_exists( $file ) )
2751                         $file_size = filesize( $file );
2752
2753                 if ( ! empty( $file_size ) ) : ?>
2754                         <div class="misc-pub-section misc-pub-filesize">
2755                                 <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
2756                         </div>
2757                         <?php
2758                 endif;
2759
2760         if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
2761
2762                 /**
2763                  * Filter the audio and video metadata fields to be shown in the publish meta box.
2764                  *
2765                  * The key for each item in the array should correspond to an attachment
2766                  * metadata key, and the value should be the desired label.
2767                  *
2768                  * @since 3.7.0
2769                  *
2770                  * @param array $fields An array of the attachment metadata keys and labels.
2771                  */
2772                 $fields = apply_filters( 'media_submitbox_misc_sections', array(
2773                         'length_formatted' => __( 'Length:' ),
2774                         'bitrate'          => __( 'Bitrate:' ),
2775                 ) );
2776
2777                 foreach ( $fields as $key => $label ) {
2778                         if ( empty( $meta[ $key ] ) ) {
2779                                 continue;
2780                         }
2781         ?>
2782                 <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
2783                         <?php echo $label ?> <strong><?php
2784                                 switch ( $key ) {
2785                                         case 'bitrate' :
2786                                                 echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
2787                                                 if ( ! empty( $meta['bitrate_mode'] ) ) {
2788                                                         echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
2789                                                 }
2790                                                 break;
2791                                         default:
2792                                                 echo esc_html( $meta[ $key ] );
2793                                                 break;
2794                                 }
2795                         ?></strong>
2796                 </div>
2797         <?php
2798                 }
2799
2800                 /**
2801                  * Filter the audio attachment metadata fields to be shown in the publish meta box.
2802                  *
2803                  * The key for each item in the array should correspond to an attachment
2804                  * metadata key, and the value should be the desired label.
2805                  *
2806                  * @since 3.7.0
2807                  *
2808                  * @param array $fields An array of the attachment metadata keys and labels.
2809                  */
2810                 $audio_fields = apply_filters( 'audio_submitbox_misc_sections', array(
2811                         'dataformat' => __( 'Audio Format:' ),
2812                         'codec'      => __( 'Audio Codec:' )
2813                 ) );
2814
2815                 foreach ( $audio_fields as $key => $label ) {
2816                         if ( empty( $meta['audio'][ $key ] ) ) {
2817                                 continue;
2818                         }
2819         ?>
2820                 <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
2821                         <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
2822                 </div>
2823         <?php
2824                 }
2825
2826         }
2827
2828         if ( $media_dims ) : ?>
2829         <div class="misc-pub-section misc-pub-dimensions">
2830                 <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
2831         </div>
2832 <?php
2833         endif;
2834 }
2835
2836 add_filter( 'async_upload_image', 'get_media_item', 10, 2 );
2837 add_filter( 'async_upload_audio', 'get_media_item', 10, 2 );
2838 add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
2839 add_filter( 'async_upload_file',  'get_media_item', 10, 2 );
2840
2841 add_action( 'media_upload_image', 'wp_media_upload_handler' );
2842 add_action( 'media_upload_audio', 'wp_media_upload_handler' );
2843 add_action( 'media_upload_video', 'wp_media_upload_handler' );
2844 add_action( 'media_upload_file',  'wp_media_upload_handler' );
2845
2846 add_filter( 'media_upload_gallery', 'media_upload_gallery' );
2847 add_filter( 'media_upload_library', 'media_upload_library' );
2848
2849 add_action( 'attachment_submitbox_misc_actions', 'attachment_submitbox_metadata' );
2850
2851 /**
2852  * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
2853  *
2854  * @since 3.6.0
2855  *
2856  * @param array $metadata An existing array with data
2857  * @param array $data Data supplied by ID3 tags
2858  */
2859 function wp_add_id3_tag_data( &$metadata, $data ) {
2860         foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
2861                 if ( ! empty( $data[$version]['comments'] ) ) {
2862                         foreach ( $data[$version]['comments'] as $key => $list ) {
2863                                 if ( ! empty( $list ) ) {
2864                                         $metadata[$key] = reset( $list );
2865                                         // Fix bug in byte stream analysis.
2866                                         if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
2867                                                 $metadata[$key] = 'Cop' . $metadata[$key];
2868                                 }
2869                         }
2870                         break;
2871                 }
2872         }
2873
2874         if ( ! empty( $data['id3v2']['APIC'] ) ) {
2875                 $image = reset( $data['id3v2']['APIC']);
2876                 if ( ! empty( $image['data'] ) ) {
2877                         $metadata['image'] = array(
2878                                 'data' => $image['data'],
2879                                 'mime' => $image['image_mime'],
2880                                 'width' => $image['image_width'],
2881                                 'height' => $image['image_height']
2882                         );
2883                 }
2884         } elseif ( ! empty( $data['comments']['picture'] ) ) {
2885                 $image = reset( $data['comments']['picture'] );
2886                 if ( ! empty( $image['data'] ) ) {
2887                         $metadata['image'] = array(
2888                                 'data' => $image['data'],
2889                                 'mime' => $image['image_mime']
2890                         );
2891                 }
2892         }
2893 }
2894
2895 /**
2896  * Retrieve metadata from a video file's ID3 tags
2897  *
2898  * @since 3.6.0
2899  *
2900  * @param string $file Path to file.
2901  * @return array|boolean Returns array of metadata, if found.
2902  */
2903 function wp_read_video_metadata( $file ) {
2904         if ( ! file_exists( $file ) )
2905                 return false;
2906
2907         $metadata = array();
2908
2909         if ( ! class_exists( 'getID3' ) )
2910                 require( ABSPATH . WPINC . '/ID3/getid3.php' );
2911         $id3 = new getID3();
2912         $data = $id3->analyze( $file );
2913
2914         if ( isset( $data['video']['lossless'] ) )
2915                 $metadata['lossless'] = $data['video']['lossless'];
2916         if ( ! empty( $data['video']['bitrate'] ) )
2917                 $metadata['bitrate'] = (int) $data['video']['bitrate'];
2918         if ( ! empty( $data['video']['bitrate_mode'] ) )
2919                 $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
2920         if ( ! empty( $data['filesize'] ) )
2921                 $metadata['filesize'] = (int) $data['filesize'];
2922         if ( ! empty( $data['mime_type'] ) )
2923                 $metadata['mime_type'] = $data['mime_type'];
2924         if ( ! empty( $data['playtime_seconds'] ) )
2925                 $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
2926         if ( ! empty( $data['playtime_string'] ) )
2927                 $metadata['length_formatted'] = $data['playtime_string'];
2928         if ( ! empty( $data['video']['resolution_x'] ) )
2929                 $metadata['width'] = (int) $data['video']['resolution_x'];
2930         if ( ! empty( $data['video']['resolution_y'] ) )
2931                 $metadata['height'] = (int) $data['video']['resolution_y'];
2932         if ( ! empty( $data['fileformat'] ) )
2933                 $metadata['fileformat'] = $data['fileformat'];
2934         if ( ! empty( $data['video']['dataformat'] ) )
2935                 $metadata['dataformat'] = $data['video']['dataformat'];
2936         if ( ! empty( $data['video']['encoder'] ) )
2937                 $metadata['encoder'] = $data['video']['encoder'];
2938         if ( ! empty( $data['video']['codec'] ) )
2939                 $metadata['codec'] = $data['video']['codec'];
2940
2941         if ( ! empty( $data['audio'] ) ) {
2942                 unset( $data['audio']['streams'] );
2943                 $metadata['audio'] = $data['audio'];
2944         }
2945
2946         wp_add_id3_tag_data( $metadata, $data );
2947
2948         return $metadata;
2949 }
2950
2951 /**
2952  * Retrieve metadata from a audio file's ID3 tags
2953  *
2954  * @since 3.6.0
2955  *
2956  * @param string $file Path to file.
2957  * @return array|boolean Returns array of metadata, if found.
2958  */
2959 function wp_read_audio_metadata( $file ) {
2960         if ( ! file_exists( $file ) )
2961                 return false;
2962         $metadata = array();
2963
2964         if ( ! class_exists( 'getID3' ) )
2965                 require( ABSPATH . WPINC . '/ID3/getid3.php' );
2966         $id3 = new getID3();
2967         $data = $id3->analyze( $file );
2968
2969         if ( ! empty( $data['audio'] ) ) {
2970                 unset( $data['audio']['streams'] );
2971                 $metadata = $data['audio'];
2972         }
2973
2974         if ( ! empty( $data['fileformat'] ) )
2975                 $metadata['fileformat'] = $data['fileformat'];
2976         if ( ! empty( $data['filesize'] ) )
2977                 $metadata['filesize'] = (int) $data['filesize'];
2978         if ( ! empty( $data['mime_type'] ) )
2979                 $metadata['mime_type'] = $data['mime_type'];
2980         if ( ! empty( $data['playtime_seconds'] ) )
2981                 $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
2982         if ( ! empty( $data['playtime_string'] ) )
2983                 $metadata['length_formatted'] = $data['playtime_string'];
2984
2985         wp_add_id3_tag_data( $metadata, $data );
2986
2987         return $metadata;
2988 }