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