3 * WordPress Administration Media API.
6 * @subpackage Administration
10 * Defines the default media upload tabs
14 * @return array default tabs
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')
25 * Filter the available tabs in the legacy (pre-3.5.0) media popup.
29 * @param array $_default_tabs An array of media tabs.
31 return apply_filters( 'media_upload_tabs', $_default_tabs );
35 * Adds the gallery tab back to the tabs array if post has image attachments
40 * @return array $tabs with gallery if post has image attachment
42 function update_gallery_tab($tabs) {
45 if ( !isset($_REQUEST['post_id']) ) {
46 unset($tabs['gallery']);
50 $post_id = intval($_REQUEST['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 ) ) );
55 if ( empty($attachments) ) {
56 unset($tabs['gallery']);
60 $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
64 add_filter('media_upload_tabs', 'update_gallery_tab');
67 * {@internal Missing Short Description}}
71 function the_media_upload_tabs() {
73 $tabs = media_upload_tabs();
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'];
83 /** This filter is documented in wp-admin/media-upload.php */
84 $current = apply_filters( 'media_upload_default_tab', $default );
87 foreach ( $tabs as $callback => $text ) {
90 if ( $current == $callback )
91 $class = " class='current'";
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";
102 * {@internal Missing Short Description}}
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
116 function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
118 $html = get_image_tag($id, $alt, '', $align, $size);
120 $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
123 $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
126 * Filter the image HTML markup to send to the editor.
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.
139 $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
145 * Adds image shortcode with caption to editor
149 * @param string $html
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() )
159 function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
162 * Filter whether to disable captions.
164 * Prevents image captions from being appended to image HTML when inserted into the editor.
168 * @param bool $bool Whether to disable appending captions. Returning true to the filter
169 * will disable captions. Default empty string.
171 if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
174 $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
176 if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
179 $width = $matches[1];
181 $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
182 $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
183 // convert any remaining line breaks to <br>
184 $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
186 $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
190 $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
193 * Filter the image HTML markup including the caption shortcode.
197 * @param string $shcode The image HTML markup with caption shortcode.
198 * @param string $html The image HTML markup.
200 return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
202 add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
205 * Private preg_replace callback used in image_add_caption()
210 function _cleanup_image_add_caption( $matches ) {
211 // remove any line breaks from inside the tags
212 return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
216 * Adds image html to editor
220 * @param string $html
222 function media_send_to_editor($html) {
224 <script type="text/javascript">
226 var win = window.dialogArguments || opener || parent || top;
227 win.send_to_editor('<?php echo addslashes($html); ?>');
235 * This handles the file upload POST itself, creating the attachment post.
239 * @param string $file_id Index into the {@link $_FILES} array of the upload
240 * @param int $post_id The post ID the media is associated with
241 * @param array $post_data allows you to overwrite some of the attachment
242 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
243 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
245 function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
247 $time = current_time('mysql');
248 if ( $post = get_post($post_id) ) {
249 if ( substr( $post->post_date, 0, 4 ) > 0 )
250 $time = $post->post_date;
253 $name = $_FILES[$file_id]['name'];
254 $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
256 if ( isset($file['error']) )
257 return new WP_Error( 'upload_error', $file['error'] );
259 $name_parts = pathinfo($name);
260 $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
263 $type = $file['type'];
264 $file = $file['file'];
268 if ( preg_match( '#^audio#', $type ) ) {
269 $meta = wp_read_audio_metadata( $file );
271 if ( ! empty( $meta['title'] ) )
272 $title = $meta['title'];
276 if ( ! empty( $title ) ) {
278 if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
279 /* translators: 1: audio track title, 2: album title, 3: artist name */
280 $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
281 } else if ( ! empty( $meta['album'] ) ) {
282 /* translators: 1: audio track title, 2: album title */
283 $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
284 } else if ( ! empty( $meta['artist'] ) ) {
285 /* translators: 1: audio track title, 2: artist name */
286 $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
288 $content .= sprintf( __( '"%s".' ), $title );
291 } else if ( ! empty( $meta['album'] ) ) {
293 if ( ! empty( $meta['artist'] ) ) {
294 /* translators: 1: audio album title, 2: artist name */
295 $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
297 $content .= $meta['album'] . '.';
300 } else if ( ! empty( $meta['artist'] ) ) {
302 $content .= $meta['artist'] . '.';
306 if ( ! empty( $meta['year'] ) )
307 $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
309 if ( ! empty( $meta['track_number'] ) ) {
310 $track_number = explode( '/', $meta['track_number'] );
311 if ( isset( $track_number[1] ) )
312 $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
314 $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
317 if ( ! empty( $meta['genre'] ) )
318 $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
320 // use image exif/iptc data for title and caption defaults if possible
321 } elseif ( $image_meta = @wp_read_image_metadata( $file ) ) {
322 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
323 $title = $image_meta['title'];
324 if ( trim( $image_meta['caption'] ) )
325 $content = $image_meta['caption'];
328 // Construct the attachment array
329 $attachment = array_merge( array(
330 'post_mime_type' => $type,
332 'post_parent' => $post_id,
333 'post_title' => $title,
334 'post_content' => $content,
337 // This should never be set as it would then overwrite an existing attachment.
338 if ( isset( $attachment['ID'] ) )
339 unset( $attachment['ID'] );
342 $id = wp_insert_attachment($attachment, $file, $post_id);
343 if ( !is_wp_error($id) ) {
344 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
352 * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
356 * @param array $file_array Array similar to a {@link $_FILES} upload array
357 * @param int $post_id The post ID the media is associated with
358 * @param string $desc Description of the sideloaded file
359 * @param array $post_data allows you to overwrite some of the attachment
360 * @return int|object The ID of the attachment or a WP_Error on failure
362 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
363 $overrides = array('test_form'=>false);
365 $time = current_time( 'mysql' );
366 if ( $post = get_post( $post_id ) ) {
367 if ( substr( $post->post_date, 0, 4 ) > 0 )
368 $time = $post->post_date;
371 $file = wp_handle_sideload( $file_array, $overrides, $time );
372 if ( isset($file['error']) )
373 return new WP_Error( 'upload_error', $file['error'] );
376 $type = $file['type'];
377 $file = $file['file'];
378 $title = preg_replace('/\.[^.]+$/', '', basename($file));
381 // use image exif/iptc data for title and caption defaults if possible
382 if ( $image_meta = @wp_read_image_metadata($file) ) {
383 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
384 $title = $image_meta['title'];
385 if ( trim( $image_meta['caption'] ) )
386 $content = $image_meta['caption'];
389 if ( isset( $desc ) )
392 // Construct the attachment array
393 $attachment = array_merge( array(
394 'post_mime_type' => $type,
396 'post_parent' => $post_id,
397 'post_title' => $title,
398 'post_content' => $content,
401 // This should never be set as it would then overwrite an existing attachment.
402 if ( isset( $attachment['ID'] ) )
403 unset( $attachment['ID'] );
405 // Save the attachment metadata
406 $id = wp_insert_attachment($attachment, $file, $post_id);
407 if ( !is_wp_error($id) )
408 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
414 * Adds the iframe to display content for the media upload page
418 * @param array $content_func
420 function wp_iframe($content_func /* ... */) {
421 _wp_admin_html_begin();
423 <title><?php bloginfo('name') ?> › <?php _e('Uploads'); ?> — <?php _e('WordPress'); ?></title>
426 wp_enqueue_style( 'colors' );
427 // Check callback name for 'media'
428 if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
429 || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
430 wp_enqueue_style( 'media' );
431 wp_enqueue_style( 'ie' );
433 <script type="text/javascript">
435 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();}}};
436 var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
437 isRtl = <?php echo (int) is_rtl(); ?>;
441 /** This action is documented in wp-admin/admin-header.php */
442 do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
445 * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
449 do_action( 'admin_print_styles-media-upload-popup' );
451 /** This action is documented in wp-admin/admin-header.php */
452 do_action( 'admin_print_styles' );
455 * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
459 do_action( 'admin_print_scripts-media-upload-popup' );
461 /** This action is documented in wp-admin/admin-header.php */
462 do_action( 'admin_print_scripts' );
465 * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
466 * media upload popup are printed.
470 do_action( 'admin_head-media-upload-popup' );
472 /** This action is documented in wp-admin/admin-header.php */
473 do_action( 'admin_head' );
475 if ( is_string( $content_func ) ) {
477 * Fires in the admin header for each specific form tab in the legacy
478 * (pre-3.5.0) media upload popup.
480 * The dynamic portion of the hook, $content_func, refers to the form
481 * callback for the media upload type. Possible values include
482 * 'media_upload_type_form', 'media_upload_type_url_form', and
483 * 'media_upload_library_form'.
487 do_action( "admin_head_{$content_func}" );
491 <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
492 <script type="text/javascript">
493 document.body.className = document.body.className.replace('no-js', 'js');
496 $args = func_get_args();
497 $args = array_slice($args, 1);
498 call_user_func_array($content_func, $args);
500 /** This action is documented in wp-admin/admin-footer.php */
501 do_action( 'admin_print_footer_scripts' );
503 <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
510 * Adds the media button to the editor
514 * @param string $editor_id
516 function media_buttons($editor_id = 'content') {
518 if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
519 $post = $GLOBALS['post_ID'];
521 wp_enqueue_media( array(
525 $img = '<span class="wp-media-buttons-icon"></span> ';
527 echo '<a href="#" id="insert-media-button" class="button insert-media add_media" data-editor="' . esc_attr( $editor_id ) . '" title="' . esc_attr__( 'Add Media' ) . '">' . $img . __( 'Add Media' ) . '</a>';
530 * Filter the legacy (pre-3.5.0) media buttons.
533 * @deprecated 3.5.0 Use 'media_buttons' action instead.
535 * @param string $string Media buttons context. Default empty.
537 $legacy_filter = apply_filters( 'media_buttons_context', '' );
539 if ( $legacy_filter ) {
540 // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
541 if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
542 $legacy_filter .= '</a>';
546 add_action( 'media_buttons', 'media_buttons' );
548 function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
551 if ( empty( $post_id ) )
554 $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
556 if ( $type && 'media' != $type )
557 $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
559 if ( ! empty( $tab ) )
560 $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
563 * Filter the upload iframe source URL for a specific media type.
565 * The dynamic portion of the hook name, $type, refers to the type
570 * @param string $upload_iframe_src The upload iframe source URL by type.
572 $upload_iframe_src = apply_filters( $type . '_upload_iframe_src', $upload_iframe_src );
574 return add_query_arg('TB_iframe', true, $upload_iframe_src);
578 * {@internal Missing Short Description}}
582 * @return mixed void|object WP_Error on failure
584 function media_upload_form_handler() {
585 check_admin_referer('media-form');
589 if ( isset($_POST['send']) ) {
590 $keys = array_keys($_POST['send']);
591 $send_id = (int) array_shift($keys);
594 if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
595 $post = $_post = get_post($attachment_id, ARRAY_A);
597 if ( !current_user_can( 'edit_post', $attachment_id ) )
600 if ( isset($attachment['post_content']) )
601 $post['post_content'] = $attachment['post_content'];
602 if ( isset($attachment['post_title']) )
603 $post['post_title'] = $attachment['post_title'];
604 if ( isset($attachment['post_excerpt']) )
605 $post['post_excerpt'] = $attachment['post_excerpt'];
606 if ( isset($attachment['menu_order']) )
607 $post['menu_order'] = $attachment['menu_order'];
609 if ( isset($send_id) && $attachment_id == $send_id ) {
610 if ( isset($attachment['post_parent']) )
611 $post['post_parent'] = $attachment['post_parent'];
615 * Filter the attachment fields to be saved.
619 * @see wp_get_attachment_metadata()
621 * @param WP_Post $post The WP_Post object.
622 * @param array $attachment An array of attachment metadata.
624 $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
626 if ( isset($attachment['image_alt']) ) {
627 $image_alt = wp_unslash( $attachment['image_alt'] );
628 if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
629 $image_alt = wp_strip_all_tags( $image_alt, true );
630 // update_meta expects slashed
631 update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
635 if ( isset($post['errors']) ) {
636 $errors[$attachment_id] = $post['errors'];
637 unset($post['errors']);
640 if ( $post != $_post )
641 wp_update_post($post);
643 foreach ( get_attachment_taxonomies($post) as $t ) {
644 if ( isset($attachment[$t]) )
645 wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
649 if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
650 <script type="text/javascript">
652 var win = window.dialogArguments || opener || parent || top;
660 if ( isset($send_id) ) {
661 $attachment = wp_unslash( $_POST['attachments'][$send_id] );
663 $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
664 if ( !empty($attachment['url']) ) {
666 if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
667 $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
668 $html = "<a href='{$attachment['url']}'$rel>$html</a>";
672 * Filter the HTML markup for a media item sent to the editor.
676 * @see wp_get_attachment_metadata()
678 * @param string $html HTML markup for a media item sent to the editor.
679 * @param int $send_id The first key from the $_POST['send'] data.
680 * @param array $attachment Array of attachment metadata.
682 $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
683 return media_send_to_editor($html);
690 * {@internal Missing Short Description}}
696 function wp_media_upload_handler() {
700 if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
701 check_admin_referer('media-form');
702 // Upload File button was clicked
703 $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
705 if ( is_wp_error($id) ) {
706 $errors['upload_error'] = $id;
711 if ( !empty($_POST['insertonlybutton']) ) {
712 $src = $_POST['src'];
713 if ( !empty($src) && !strpos($src, '://') )
714 $src = "http://$src";
716 if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
717 $title = esc_html( wp_unslash( $_POST['title'] ) );
718 if ( empty( $title ) )
719 $title = esc_html( basename( $src ) );
721 if ( $title && $src )
722 $html = "<a href='" . esc_url($src) . "'>$title</a>";
725 if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
726 && ( 'audio' == $ext_type || 'video' == $ext_type ) )
730 * Filter the URL sent to the editor for a specific media type.
732 * The dynamic portion of the hook name, $type, refers to the type
733 * of media being sent.
737 * @param string $html HTML markup sent to the editor.
738 * @param string $src Media source URL.
739 * @param string $title Media title.
741 $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
744 $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
745 if ( isset($_POST['align']) ) {
746 $align = esc_attr( wp_unslash( $_POST['align'] ) );
747 $class = " class='align$align'";
750 $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
753 * Filter the image URL sent to the editor.
757 * @param string $html HTML markup sent to the editor for an image.
758 * @param string $src Image source URL.
759 * @param string $alt Image alternate, or alt, text.
760 * @param string $align The image alignment. Default 'alignnone'. Possible values include
761 * 'alignleft', 'aligncenter', 'alignright', 'alignnone'.
763 $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
766 return media_send_to_editor($html);
769 if ( !empty($_POST) ) {
770 $return = media_upload_form_handler();
772 if ( is_string($return) )
774 if ( is_array($return) )
778 if ( isset($_POST['save']) ) {
779 $errors['upload_notice'] = __('Saved.');
780 return media_upload_gallery();
783 if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
785 if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
786 $type = $_GET['type'];
787 return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
790 return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
794 * Download an image from the specified URL and attach it to a post.
798 * @param string $file The URL of the image to download
799 * @param int $post_id The post ID the media is to be associated with
800 * @param string $desc Optional. Description of the image
801 * @return string|WP_Error Populated HTML img tag on success
803 function media_sideload_image($file, $post_id, $desc = null) {
804 if ( ! empty($file) ) {
805 // Download file to temp location
806 $tmp = download_url( $file );
808 // Set variables for storage
809 // fix file filename for query strings
810 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
811 $file_array['name'] = basename($matches[0]);
812 $file_array['tmp_name'] = $tmp;
814 // If error storing temporarily, unlink
815 if ( is_wp_error( $tmp ) ) {
816 @unlink($file_array['tmp_name']);
817 $file_array['tmp_name'] = '';
820 // do the validation and storage stuff
821 $id = media_handle_sideload( $file_array, $post_id, $desc );
822 // If error storing permanently, unlink
823 if ( is_wp_error($id) ) {
824 @unlink($file_array['tmp_name']);
828 $src = wp_get_attachment_url( $id );
831 // Finally check to make sure the file has been saved, then return the html
832 if ( ! empty($src) ) {
833 $alt = isset($desc) ? esc_attr($desc) : '';
834 $html = "<img src='$src' alt='$alt' />";
840 * {@internal Missing Short Description}}
846 function media_upload_gallery() {
849 if ( !empty($_POST) ) {
850 $return = media_upload_form_handler();
852 if ( is_string($return) )
854 if ( is_array($return) )
858 wp_enqueue_script('admin-gallery');
859 return wp_iframe( 'media_upload_gallery_form', $errors );
863 * {@internal Missing Short Description}}
869 function media_upload_library() {
871 if ( !empty($_POST) ) {
872 $return = media_upload_form_handler();
874 if ( is_string($return) )
876 if ( is_array($return) )
880 return wp_iframe( 'media_upload_library_form', $errors );
884 * Retrieve HTML for the image alignment radio buttons with the specified one checked.
888 * @param object $post
889 * @param string $checked
892 function image_align_input_fields( $post, $checked = '' ) {
894 if ( empty($checked) )
895 $checked = get_user_setting('align', 'none');
897 $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
898 if ( !array_key_exists( (string) $checked, $alignments ) )
902 foreach ( $alignments as $name => $label ) {
903 $name = esc_attr($name);
904 $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
905 ( $checked == $name ? " checked='checked'" : "" ) .
906 " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
908 return join("\n", $out);
912 * Retrieve HTML for the size radio buttons with the specified one checked.
916 * @param object $post
917 * @param bool|string $check
920 function image_size_input_fields( $post, $check = '' ) {
923 * Filter the names and labels of the default image sizes.
927 * @param array $size_names Array of image sizes and their names. Default values
928 * include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
930 $size_names = apply_filters( 'image_size_names_choose', array(
931 'thumbnail' => __( 'Thumbnail' ),
932 'medium' => __( 'Medium' ),
933 'large' => __( 'Large' ),
934 'full' => __( 'Full Size' )
938 $check = get_user_setting('imgsize', 'medium');
940 foreach ( $size_names as $size => $label ) {
941 $downsize = image_downsize($post->ID, $size);
944 // is this size selectable?
945 $enabled = ( $downsize[3] || 'full' == $size );
946 $css_id = "image-size-{$size}-{$post->ID}";
947 // if this size is the default but that's not available, don't select it
948 if ( $size == $check ) {
950 $checked = " checked='checked'";
953 } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
954 // if $check is not enabled, default to the first available size that's bigger than a thumbnail
956 $checked = " checked='checked'";
959 $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 />";
961 $html .= "<label for='{$css_id}'>$label</label>";
962 // only show the dimensions if that choice is available
964 $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d × %d)", $downsize[1], $downsize[2] ). "</label>";
972 'label' => __('Size'),
974 'html' => join("\n", $out),
979 * Retrieve HTML for the Link URL buttons with the default link type as specified.
983 * @param object $post
984 * @param string $url_type
987 function image_link_input_fields($post, $url_type = '') {
989 $file = wp_get_attachment_url($post->ID);
990 $link = get_attachment_link($post->ID);
992 if ( empty($url_type) )
993 $url_type = get_user_setting('urlbutton', 'post');
996 if ( $url_type == 'file' )
998 elseif ( $url_type == 'post' )
1002 <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
1003 <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
1004 <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
1005 <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
1009 function wp_caption_input_textarea($edit_post) {
1010 // post data is already escaped
1011 $name = "attachments[{$edit_post->ID}][post_excerpt]";
1013 return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
1017 * {@internal Missing Short Description}}
1021 * @param array $form_fields
1022 * @param object $post
1025 function image_attachment_fields_to_edit($form_fields, $post) {
1026 return $form_fields;
1030 * {@internal Missing Short Description}}
1034 * @param array $form_fields
1035 * @param object $post {@internal $post not used}}
1038 function media_single_attachment_fields_to_edit( $form_fields, $post ) {
1039 unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
1040 return $form_fields;
1044 * {@internal Missing Short Description}}
1048 * @param array $form_fields
1049 * @param object $post {@internal $post not used}}
1052 function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
1053 unset($form_fields['image_url']);
1054 return $form_fields;
1058 * Filters input from media_upload_form_handler() and assigns a default
1059 * post_title from the file name if none supplied.
1061 * Illustrates the use of the attachment_fields_to_save filter
1062 * which can be used to add default values to any field before saving to DB.
1066 * @param object $post
1067 * @param array $attachment {@internal $attachment not used}}
1070 function image_attachment_fields_to_save( $post, $attachment ) {
1071 if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
1072 if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
1073 $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
1074 $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
1075 $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
1082 add_filter( 'attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2 );
1085 * {@internal Missing Short Description}}
1089 * @param string $html
1090 * @param integer $attachment_id
1091 * @param array $attachment
1094 function image_media_send_to_editor($html, $attachment_id, $attachment) {
1095 $post = get_post($attachment_id);
1096 if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1097 $url = $attachment['url'];
1098 $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
1099 $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
1100 $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
1101 $rel = ( $url == get_attachment_link($attachment_id) );
1103 return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
1109 add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
1112 * {@internal Missing Short Description}}
1116 * @param object $post
1117 * @param array $errors
1120 function get_attachment_fields_to_edit($post, $errors = null) {
1121 if ( is_int($post) )
1122 $post = get_post($post);
1123 if ( is_array($post) )
1124 $post = new WP_Post( (object) $post );
1126 $image_url = wp_get_attachment_url($post->ID);
1128 $edit_post = sanitize_post($post, 'edit');
1130 $form_fields = array(
1131 'post_title' => array(
1132 'label' => __('Title'),
1133 'value' => $edit_post->post_title
1135 'image_alt' => array(),
1136 'post_excerpt' => array(
1137 'label' => __('Caption'),
1139 'html' => wp_caption_input_textarea($edit_post)
1141 'post_content' => array(
1142 'label' => __('Description'),
1143 'value' => $edit_post->post_content,
1144 'input' => 'textarea'
1147 'label' => __('Link URL'),
1149 'html' => image_link_input_fields($post, get_option('image_default_link_type')),
1150 'helps' => __('Enter a link URL or click above for presets.')
1152 'menu_order' => array(
1153 'label' => __('Order'),
1154 'value' => $edit_post->menu_order
1156 'image_url' => array(
1157 'label' => __('File URL'),
1159 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
1160 'value' => wp_get_attachment_url($post->ID),
1161 'helps' => __('Location of the uploaded file.')
1165 foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1166 $t = (array) get_taxonomy($taxonomy);
1167 if ( ! $t['public'] || ! $t['show_ui'] )
1169 if ( empty($t['label']) )
1170 $t['label'] = $taxonomy;
1171 if ( empty($t['args']) )
1172 $t['args'] = array();
1174 $terms = get_object_term_cache($post->ID, $taxonomy);
1175 if ( false === $terms )
1176 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1180 foreach ( $terms as $term )
1181 $values[] = $term->slug;
1182 $t['value'] = join(', ', $values);
1184 $form_fields[$taxonomy] = $t;
1187 // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1188 // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1189 $form_fields = array_merge_recursive($form_fields, (array) $errors);
1191 // This was formerly in image_attachment_fields_to_edit().
1192 if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1193 $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
1197 $form_fields['post_title']['required'] = true;
1199 $form_fields['image_alt'] = array(
1201 'label' => __('Alternative Text'),
1202 'helps' => __('Alt text for the image, e.g. “The Mona Lisa”')
1205 $form_fields['align'] = array(
1206 'label' => __('Alignment'),
1208 'html' => image_align_input_fields($post, get_option('image_default_align')),
1211 $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
1214 unset( $form_fields['image_alt'] );
1218 * Filter the attachment fields to edit.
1222 * @param array $form_fields An array of attachment form fields.
1223 * @param WP_Post $post The WP_Post attachment object.
1225 $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
1227 return $form_fields;
1231 * Retrieve HTML for media items of post gallery.
1233 * The HTML markup retrieved will be created for the progress of SWF Upload
1234 * component. Will also create link for showing and hiding the form to modify
1235 * the image attachment.
1239 * @param int $post_id Optional. Post ID.
1240 * @param array $errors Errors for attachment, if any.
1243 function get_media_items( $post_id, $errors ) {
1244 $attachments = array();
1246 $post = get_post($post_id);
1247 if ( $post && $post->post_type == 'attachment' )
1248 $attachments = array($post->ID => $post);
1250 $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
1252 if ( is_array($GLOBALS['wp_the_query']->posts) )
1253 foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
1254 $attachments[$attachment->ID] = $attachment;
1258 foreach ( (array) $attachments as $id => $attachment ) {
1259 if ( $attachment->post_status == 'trash' )
1261 if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
1262 $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>";
1269 * Retrieve HTML form for modifying the image attachment.
1273 * @param int $attachment_id Attachment ID for modification.
1274 * @param string|array $args Optional. Override defaults.
1275 * @return string HTML form for attachment.
1277 function get_media_item( $attachment_id, $args = null ) {
1280 if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
1281 $thumb_url = $thumb_url[0];
1285 $post = get_post( $attachment_id );
1286 $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
1288 $default_args = array( 'errors' => null, 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true, 'delete' => true, 'toggle' => true, 'show_title' => true );
1289 $args = wp_parse_args( $args, $default_args );
1292 * Filter the arguments used to retrieve an image for the edit image form.
1296 * @see get_media_item
1298 * @param array $args An array of arguments.
1300 $args = apply_filters( 'get_media_item_args', $args );
1301 extract( $args, EXTR_SKIP );
1303 $toggle_on = __( 'Show' );
1304 $toggle_off = __( 'Hide' );
1306 $filename = esc_html( wp_basename( $post->guid ) );
1307 $title = esc_attr( $post->post_title );
1309 if ( $_tags = get_the_tags( $attachment_id ) ) {
1310 foreach ( $_tags as $tag )
1311 $tags[] = $tag->name;
1312 $tags = esc_attr( join( ', ', $tags ) );
1315 $post_mime_types = get_post_mime_types();
1316 $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
1317 $type = array_shift( $keys );
1318 $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
1320 $form_fields = get_attachment_fields_to_edit( $post, $errors );
1323 $class = empty( $errors ) ? 'startclosed' : 'startopen';
1325 <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
1326 <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
1332 $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
1333 $display_title = $show_title ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '…' ) . "</span></div>" : '';
1335 $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
1338 foreach ( $form_fields as $key => $val ) {
1339 if ( 'menu_order' == $key ) {
1341 $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>";
1343 $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
1345 unset( $form_fields['menu_order'] );
1351 $meta = wp_get_attachment_metadata( $post->ID );
1352 if ( isset( $meta['width'], $meta['height'] ) )
1353 $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']} × {$meta['height']}</span> ";
1356 * Filter the media metadata.
1360 * @param string $media_dims The HTML markup containing the media dimensions.
1361 * @param WP_Post $post The WP_Post attachment object.
1363 $media_dims = apply_filters( 'media_meta', $media_dims, $post );
1365 $image_edit_button = '';
1366 if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
1367 $nonce = wp_create_nonce( "image_editor-$post->ID" );
1368 $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>";
1371 $attachment_url = get_permalink( $attachment_id );
1378 <table class='slidetoggle describe $class'>
1379 <thead class='media-item-info' id='media-head-$post->ID'>
1381 <td class='A1B1' id='thumbnail-head-$post->ID'>
1382 <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
1383 <p>$image_edit_button</p>
1386 <p><strong>" . __('File name:') . "</strong> $filename</p>
1387 <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
1388 <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
1389 if ( !empty( $media_dims ) )
1390 $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
1392 $item .= "</td></tr>\n";
1397 <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
1398 <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
1402 'required' => false,
1404 'extra_rows' => array(),
1408 $send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
1409 if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
1410 if ( !EMPTY_TRASH_DAYS ) {
1411 $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete-permanently'>" . __( 'Delete Permanently' ) . '</a>';
1412 } elseif ( !MEDIA_TRASH ) {
1413 $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
1414 <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
1415 <a href='" . wp_nonce_url( "post.php?action=delete&post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
1416 <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
1419 $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&post=$attachment_id", 'trash-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
1420 <a href='" . wp_nonce_url( "post.php?action=untrash&post=$attachment_id", 'untrash-post_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
1427 $calling_post_id = 0;
1428 if ( isset( $_GET['post_id'] ) )
1429 $calling_post_id = absint( $_GET['post_id'] );
1430 elseif ( isset( $_POST ) && count( $_POST ) ) // Like for async-upload where $_GET['post_id'] isn't set
1431 $calling_post_id = $post->post_parent;
1432 if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
1433 && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
1434 $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
1435 $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>";
1438 if ( ( $send || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) )
1439 $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $thumbnail $delete</td></tr>\n" );
1441 $hidden_fields = array();
1443 foreach ( $form_fields as $id => $field ) {
1444 if ( $id[0] == '_' )
1447 if ( !empty( $field['tr'] ) ) {
1448 $item .= $field['tr'];
1452 $field = array_merge( $defaults, $field );
1453 $name = "attachments[$attachment_id][$id]";
1455 if ( $field['input'] == 'hidden' ) {
1456 $hidden_fields[$name] = $field['value'];
1460 $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1461 $aria_required = $field['required'] ? " aria-required='true' " : '';
1463 $class .= $field['required'] ? ' form-required' : '';
1465 $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'>";
1466 if ( !empty( $field[ $field['input'] ] ) )
1467 $item .= $field[ $field['input'] ];
1468 elseif ( $field['input'] == 'textarea' ) {
1469 if ( 'post_content' == $id && user_can_richedit() ) {
1470 // sanitize_post() skips the post_content when user_can_richedit
1471 $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1473 // post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit()
1474 $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1476 $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
1478 if ( !empty( $field['helps'] ) )
1479 $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1480 $item .= "</td>\n\t\t</tr>\n";
1482 $extra_rows = array();
1484 if ( !empty( $field['errors'] ) )
1485 foreach ( array_unique( (array) $field['errors'] ) as $error )
1486 $extra_rows['error'][] = $error;
1488 if ( !empty( $field['extra_rows'] ) )
1489 foreach ( $field['extra_rows'] as $class => $rows )
1490 foreach ( (array) $rows as $html )
1491 $extra_rows[$class][] = $html;
1493 foreach ( $extra_rows as $class => $rows )
1494 foreach ( $rows as $html )
1495 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1498 if ( !empty( $form_fields['_final'] ) )
1499 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1500 $item .= "\t</tbody>\n";
1501 $item .= "\t</table>\n";
1503 foreach ( $hidden_fields as $name => $value )
1504 $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
1506 if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
1507 $parent = (int) $_REQUEST['post_id'];
1508 $parent_name = "attachments[$attachment_id][post_parent]";
1509 $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
1515 function get_compat_media_markup( $attachment_id, $args = null ) {
1516 $post = get_post( $attachment_id );
1518 $default_args = array(
1520 'in_modal' => false,
1523 $user_can_edit = current_user_can( 'edit_post', $attachment_id );
1525 $args = wp_parse_args( $args, $default_args );
1527 /** This filter is documented in wp-admin/includes/media.php */
1528 $args = apply_filters( 'get_media_item_args', $args );
1530 $form_fields = array();
1532 if ( $args['in_modal'] ) {
1533 foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1534 $t = (array) get_taxonomy($taxonomy);
1535 if ( ! $t['public'] || ! $t['show_ui'] )
1537 if ( empty($t['label']) )
1538 $t['label'] = $taxonomy;
1539 if ( empty($t['args']) )
1540 $t['args'] = array();
1542 $terms = get_object_term_cache($post->ID, $taxonomy);
1543 if ( false === $terms )
1544 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1548 foreach ( $terms as $term )
1549 $values[] = $term->slug;
1550 $t['value'] = join(', ', $values);
1551 $t['taxonomy'] = true;
1553 $form_fields[$taxonomy] = $t;
1557 // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1558 // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1559 $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
1561 /** This filter is documented in wp-admin/includes/media.php */
1562 $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
1564 unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
1565 $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
1566 $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
1568 /** This filter is documented in wp-admin/includes/media.php */
1569 $media_meta = apply_filters( 'media_meta', '', $post );
1573 'required' => false,
1575 'extra_rows' => array(),
1576 'show_in_edit' => true,
1577 'show_in_modal' => true,
1580 $hidden_fields = array();
1583 foreach ( $form_fields as $id => $field ) {
1584 if ( $id[0] == '_' )
1587 $name = "attachments[$attachment_id][$id]";
1588 $id_attr = "attachments-$attachment_id-$id";
1590 if ( !empty( $field['tr'] ) ) {
1591 $item .= $field['tr'];
1595 $field = array_merge( $defaults, $field );
1597 if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
1600 if ( $field['input'] == 'hidden' ) {
1601 $hidden_fields[$name] = $field['value'];
1605 $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
1606 $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1607 $aria_required = $field['required'] ? " aria-required='true' " : '';
1608 $class = 'compat-field-' . $id;
1609 $class .= $field['required'] ? ' form-required' : '';
1611 $item .= "\t\t<tr class='$class'>";
1612 $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
1613 $item .= "</th>\n\t\t\t<td class='field'>";
1615 if ( !empty( $field[ $field['input'] ] ) )
1616 $item .= $field[ $field['input'] ];
1617 elseif ( $field['input'] == 'textarea' ) {
1618 if ( 'post_content' == $id && user_can_richedit() ) {
1619 // sanitize_post() skips the post_content when user_can_richedit
1620 $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1622 $item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1624 $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly $aria_required />";
1626 if ( !empty( $field['helps'] ) )
1627 $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1628 $item .= "</td>\n\t\t</tr>\n";
1630 $extra_rows = array();
1632 if ( !empty( $field['errors'] ) )
1633 foreach ( array_unique( (array) $field['errors'] ) as $error )
1634 $extra_rows['error'][] = $error;
1636 if ( !empty( $field['extra_rows'] ) )
1637 foreach ( $field['extra_rows'] as $class => $rows )
1638 foreach ( (array) $rows as $html )
1639 $extra_rows[$class][] = $html;
1641 foreach ( $extra_rows as $class => $rows )
1642 foreach ( $rows as $html )
1643 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1646 if ( !empty( $form_fields['_final'] ) )
1647 $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1649 $item = '<table class="compat-attachment-fields">' . $item . '</table>';
1651 foreach ( $hidden_fields as $hidden_field => $value ) {
1652 $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
1656 $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
1660 'meta' => $media_meta,
1665 * {@internal Missing Short Description}}
1669 function media_upload_header() {
1670 $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
1671 echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
1672 if ( empty( $_GET['chromeless'] ) ) {
1673 echo '<div id="media-upload-header">';
1674 the_media_upload_tabs();
1680 * {@internal Missing Short Description}}
1684 * @param unknown_type $errors
1686 function media_upload_form( $errors = null ) {
1687 global $type, $tab, $is_IE, $is_opera;
1689 if ( ! _device_can_upload() ) {
1690 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://wordpress.org/mobile/' ) . '</p>';
1694 $upload_action_url = admin_url('async-upload.php');
1695 $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
1696 $_type = isset($type) ? $type : '';
1697 $_tab = isset($tab) ? $tab : '';
1699 $upload_size_unit = $max_upload_size = wp_max_upload_size();
1700 $sizes = array( 'KB', 'MB', 'GB' );
1702 for ( $u = -1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u++ ) {
1703 $upload_size_unit /= 1024;
1707 $upload_size_unit = 0;
1710 $upload_size_unit = (int) $upload_size_unit;
1714 <div id="media-upload-notice"><?php
1716 if (isset($errors['upload_notice']) )
1717 echo $errors['upload_notice'];
1720 <div id="media-upload-error"><?php
1722 if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
1723 echo $errors['upload_error']->get_error_message();
1727 if ( is_multisite() && !is_upload_space_available() ) {
1729 * Fires when an upload will exceed the defined upload space quota for a network site.
1733 do_action( 'upload_ui_over_quota' );
1738 * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
1742 do_action( 'pre-upload-ui' );
1744 $post_params = array(
1745 "post_id" => $post_id,
1746 "_wpnonce" => wp_create_nonce('media-form'),
1753 * Filter the media upload post parameters.
1755 * @since 3.1.0 As 'swfupload_post_params'
1758 * @param array $post_params An array of media upload parameters used by Plupload.
1760 $post_params = apply_filters( 'upload_post_params', $post_params );
1762 $plupload_init = array(
1763 'runtimes' => 'html5,flash,silverlight,html4',
1764 'browse_button' => 'plupload-browse-button',
1765 'container' => 'plupload-upload-ui',
1766 'drop_element' => 'drag-drop-area',
1767 'file_data_name' => 'async-upload',
1768 'url' => $upload_action_url,
1769 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
1770 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
1772 'max_file_size' => $max_upload_size . 'b',
1774 'multipart_params' => $post_params,
1777 // Multi-file uploading doesn't currently work in iOS Safari,
1778 // single-file allows the built-in camera to be used as source for images
1779 if ( wp_is_mobile() )
1780 $plupload_init['multi_selection'] = false;
1783 * Filter the default Plupload settings.
1787 * @param array $plupload_init An array of default settings used by Plupload.
1789 $plupload_init = apply_filters( 'plupload_init', $plupload_init );
1793 <script type="text/javascript">
1795 // Verify size is an int. If not return default value.
1796 $large_size_h = absint( get_option('large_size_h') );
1797 if( !$large_size_h )
1798 $large_size_h = 1024;
1799 $large_size_w = absint( get_option('large_size_w') );
1800 if( !$large_size_w )
1801 $large_size_w = 1024;
1803 var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
1804 wpUploaderInit = <?php echo json_encode($plupload_init); ?>;
1807 <div id="plupload-upload-ui" class="hide-if-no-js">
1810 * Fires before the upload interface loads.
1812 * @since 2.6.0 As 'pre-flash-upload-ui'
1815 do_action( 'pre-plupload-upload-ui' ); ?>
1816 <div id="drag-drop-area">
1817 <div class="drag-drop-inside">
1818 <p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
1819 <p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
1820 <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
1825 * Fires after the upload interface loads.
1827 * @since 2.6.0 As 'post-flash-upload-ui'
1830 do_action( 'post-plupload-upload-ui' ); ?>
1833 <div id="html-upload-ui" class="hide-if-js">
1836 * Fires before the upload button in the media upload interface.
1840 do_action( 'pre-html-upload-ui' );
1842 <p id="async-upload-wrap">
1843 <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
1844 <input type="file" name="async-upload" id="async-upload" />
1845 <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
1846 <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
1848 <div class="clear"></div>
1851 * Fires after the upload button in the media upload interface.
1855 do_action( 'post-html-upload-ui' );
1859 <span class="max-upload-size"><?php printf( __( 'Maximum upload file size: %d%s.' ), esc_html($upload_size_unit), esc_html($sizes[$u]) ); ?></span>
1863 * Fires on the post upload UI screen.
1865 * Legacy (pre-3.5.0) media workflow hook.
1869 do_action( 'post-upload-ui' );
1873 * {@internal Missing Short Description}}
1877 * @param string $type
1878 * @param object $errors
1879 * @param integer $id
1881 function media_upload_type_form($type = 'file', $errors = null, $id = null) {
1883 media_upload_header();
1885 $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
1887 $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1890 * Filter the media upload form action URL.
1894 * @param string $form_action_url The media upload form action URL.
1895 * @param string $type The type of media. Default 'file'.
1897 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
1898 $form_class = 'media-upload-form type-form validate';
1900 if ( get_user_setting('uploader') )
1901 $form_class .= ' html-uploader';
1904 <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">
1905 <?php submit_button( '', 'hidden', 'save', false ); ?>
1906 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1907 <?php wp_nonce_field('media-form'); ?>
1909 <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
1911 <?php media_upload_form( $errors ); ?>
1913 <script type="text/javascript">
1916 var preloaded = $(".media-item.preloaded");
1917 if ( preloaded.length > 0 ) {
1918 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1924 <div id="media-items"><?php
1927 if ( !is_wp_error($id) ) {
1928 add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
1929 echo get_media_items( $id, $errors );
1931 echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
1937 <p class="savebutton ml-submit">
1938 <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
1945 * {@internal Missing Short Description}}
1949 * @param string $type
1950 * @param object $errors
1951 * @param integer $id
1953 function media_upload_type_url_form($type = null, $errors = null, $id = null) {
1954 if ( null === $type )
1957 media_upload_header();
1959 $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
1961 $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1962 /** This filter is documented in wp-admin/includes/media.php */
1963 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
1964 $form_class = 'media-upload-form type-form validate';
1966 if ( get_user_setting('uploader') )
1967 $form_class .= ' html-uploader';
1970 <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">
1971 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1972 <?php wp_nonce_field('media-form'); ?>
1974 <h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
1976 <script type="text/javascript">
1982 align : 'alignnone',
1984 insert : function() {
1985 var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
1987 if ( '' == f.src.value || '' == t.width )
1991 alt = f.alt.value.replace(/'/g, ''').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
1994 /** This filter is documented in wp-admin/includes/media.php */
1995 if ( ! apply_filters( 'disable_captions', '' ) ) {
1997 if ( f.caption.value ) {
1998 caption = f.caption.value.replace(/\r\n|\r/g, '\n');
1999 caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
2000 return a.replace(/[\r\n\t]+/, ' ');
2003 caption = caption.replace(/\s*\n\s*/g, '<br />');
2007 cls = caption ? '' : ' class="'+t.align+'"';
2009 html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
2011 if ( f.url.value ) {
2012 url = f.url.value.replace(/'/g, ''').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
2013 html = '<a href="'+url+'">'+html+'</a>';
2017 html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
2019 var win = window.dialogArguments || opener || parent || top;
2020 win.send_to_editor(html);
2024 resetImageData : function() {
2025 var t = addExtImage;
2027 t.width = t.height = '';
2028 document.getElementById('go_button').style.color = '#bbb';
2029 if ( ! document.forms[0].src.value )
2030 document.getElementById('status_img').innerHTML = '*';
2031 else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
2034 updateImageData : function() {
2035 var t = addExtImage;
2037 t.width = t.preloadImg.width;
2038 t.height = t.preloadImg.height;
2039 document.getElementById('go_button').style.color = '#333';
2040 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
2043 getImageData : function() {
2044 if ( jQuery('table.describe').hasClass('not-image') )
2047 var t = addExtImage, src = document.forms[0].src.value;
2054 document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" width="16" />';
2055 t.preloadImg = new Image();
2056 t.preloadImg.onload = t.updateImageData;
2057 t.preloadImg.onerror = t.resetImageData;
2058 t.preloadImg.src = src;
2062 jQuery(document).ready( function($) {
2063 $('.media-types input').click( function() {
2064 $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
2071 <div id="media-items">
2072 <div class="media-item media-blank">
2075 * Filter the insert media from URL form HTML.
2079 * @param string $form_html The insert from URL form HTML.
2081 echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
2090 * Adds gallery form to upload iframe
2094 * @param array $errors
2096 function media_upload_gallery_form($errors) {
2097 global $redir_tab, $type;
2099 $redir_tab = 'gallery';
2100 media_upload_header();
2102 $post_id = intval($_REQUEST['post_id']);
2103 $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
2104 /** This filter is documented in wp-admin/includes/media.php */
2105 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
2106 $form_class = 'media-upload-form validate';
2108 if ( get_user_setting('uploader') )
2109 $form_class .= ' html-uploader';
2112 <script type="text/javascript">
2115 var preloaded = $(".media-item.preloaded");
2116 if ( preloaded.length > 0 ) {
2117 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2123 <div id="sort-buttons" class="hide-if-no-js">
2125 <?php _e('All Tabs:'); ?>
2126 <a href="#" id="showall"><?php _e('Show'); ?></a>
2127 <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
2129 <?php _e('Sort Order:'); ?>
2130 <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
2131 <a href="#" id="desc"><?php _e('Descending'); ?></a> |
2132 <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
2134 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
2135 <?php wp_nonce_field('media-form'); ?>
2136 <?php //media_upload_form( $errors ); ?>
2137 <table class="widefat">
2139 <th><?php _e('Media'); ?></th>
2140 <th class="order-head"><?php _e('Order'); ?></th>
2141 <th class="actions-head"><?php _e('Actions'); ?></th>
2144 <div id="media-items">
2145 <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2146 <?php echo get_media_items($post_id, $errors); ?>
2149 <p class="ml-submit">
2150 <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
2151 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2152 <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
2153 <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
2156 <div id="gallery-settings" style="display:none;">
2157 <div class="title"><?php _e('Gallery Settings'); ?></div>
2158 <table id="basic" class="describe"><tbody>
2160 <th scope="row" class="label">
2162 <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
2166 <input type="radio" name="linkto" id="linkto-file" value="file" />
2167 <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
2169 <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
2170 <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
2175 <th scope="row" class="label">
2177 <span class="alignleft"><?php _e('Order images by:'); ?></span>
2181 <select id="orderby" name="orderby">
2182 <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
2183 <option value="title"><?php _e('Title'); ?></option>
2184 <option value="post_date"><?php _e('Date/Time'); ?></option>
2185 <option value="rand"><?php _e('Random'); ?></option>
2191 <th scope="row" class="label">
2193 <span class="alignleft"><?php _e('Order:'); ?></span>
2197 <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
2198 <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
2200 <input type="radio" name="order" id="order-desc" value="desc" />
2201 <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
2206 <th scope="row" class="label">
2208 <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
2212 <select id="columns" name="columns">
2213 <option value="1">1</option>
2214 <option value="2">2</option>
2215 <option value="3" selected="selected">3</option>
2216 <option value="4">4</option>
2217 <option value="5">5</option>
2218 <option value="6">6</option>
2219 <option value="7">7</option>
2220 <option value="8">8</option>
2221 <option value="9">9</option>
2227 <p class="ml-submit">
2228 <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
2229 <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' ); ?>" />
2237 * {@internal Missing Short Description}}
2241 * @param array $errors
2243 function media_upload_library_form($errors) {
2244 global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
2246 media_upload_header();
2248 $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
2250 $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
2251 /** This filter is documented in wp-admin/includes/media.php */
2252 $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
2253 $form_class = 'media-upload-form validate';
2255 if ( get_user_setting('uploader') )
2256 $form_class .= ' html-uploader';
2259 $q['posts_per_page'] = 10;
2260 $q['paged'] = isset( $q['paged'] ) ? intval( $q['paged'] ) : 0;
2261 if ( $q['paged'] < 1 ) {
2264 $q['offset'] = ( $q['paged'] - 1 ) * 10;
2265 if ( $q['offset'] < 1 ) {
2269 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
2273 <form id="filter" action="" method="get">
2274 <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
2275 <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
2276 <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
2277 <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
2278 <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
2280 <p id="media-search" class="search-box">
2281 <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
2282 <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
2283 <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
2286 <ul class="subsubsub">
2288 $type_links = array();
2289 $_num_posts = (array) wp_count_attachments();
2290 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
2291 foreach ( $matches as $_type => $reals )
2292 foreach ( $reals as $real )
2293 if ( isset($num_posts[$_type]) )
2294 $num_posts[$_type] += $_num_posts[$real];
2296 $num_posts[$_type] = $_num_posts[$real];
2297 // If available type specified by media button clicked, filter by that type
2298 if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
2299 $_GET['post_mime_type'] = $type;
2300 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
2302 if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
2303 $class = ' class="current"';
2306 $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
2307 foreach ( $post_mime_types as $mime_type => $label ) {
2310 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
2313 if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
2314 $class = ' class="current"';
2316 $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>';
2319 * Filter the media upload mime type list items.
2321 * Returned values should begin with an <li> tag.
2325 * @param array $type_links An array of list items containing mime type link HTML.
2327 echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
2332 <div class="tablenav">
2335 $page_links = paginate_links( array(
2336 'base' => add_query_arg( 'paged', '%#%' ),
2338 'prev_text' => __('«'),
2339 'next_text' => __('»'),
2340 'total' => ceil($wp_query->found_posts / 10),
2341 'current' => $q['paged'],
2345 echo "<div class='tablenav-pages'>$page_links</div>";
2348 <div class="alignleft actions">
2351 $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";
2353 $arc_result = $wpdb->get_results( $arc_query );
2355 $month_count = count($arc_result);
2356 $selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
2358 if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
2360 <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
2362 foreach ($arc_result as $arc_row) {
2363 if ( $arc_row->yyear == 0 )
2365 $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
2367 if ( $arc_row->yyear . $arc_row->mmonth == $selected_month )
2368 $default = ' selected="selected"';
2372 echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
2373 echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
2380 <?php submit_button( __( 'Filter »' ), 'button', 'post-query-submit', false ); ?>
2384 <br class="clear" />
2388 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
2390 <?php wp_nonce_field('media-form'); ?>
2391 <?php //media_upload_form( $errors ); ?>
2393 <script type="text/javascript">
2396 var preloaded = $(".media-item.preloaded");
2397 if ( preloaded.length > 0 ) {
2398 preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2405 <div id="media-items">
2406 <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2407 <?php echo get_media_items(null, $errors); ?>
2409 <p class="ml-submit">
2410 <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
2411 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2418 * Creates the form for external url
2422 * @param string $default_view
2423 * @return string the form html
2425 function wp_media_insert_url_form( $default_view = 'image' ) {
2426 /** This filter is documented in wp-admin/includes/media.php */
2427 if ( ! apply_filters( 'disable_captions', '' ) ) {
2429 <tr class="image-only">
2430 <th scope="row" class="label">
2431 <label for="caption"><span class="alignleft">' . __('Image Caption') . '</span></label>
2433 <td class="field"><textarea id="caption" name="caption"></textarea></td>
2440 $default_align = get_option('image_default_align');
2441 if ( empty($default_align) )
2442 $default_align = 'none';
2444 if ( 'image' == $default_view ) {
2445 $view = 'image-only';
2448 $view = $table_class = 'not-image';
2452 <p class="media-types"><label><input type="radio" name="media_type" value="image" id="image-only"' . checked( 'image-only', $view, false ) . ' /> ' . __( 'Image' ) . '</label> <label><input type="radio" name="media_type" value="generic" id="not-image"' . checked( 'not-image', $view, false ) . ' /> ' . __( 'Audio, Video, or Other File' ) . '</label></p>
2453 <table class="describe ' . $table_class . '"><tbody>
2455 <th scope="row" class="label" style="width:130px;">
2456 <label for="src"><span class="alignleft">' . __('URL') . '</span></label>
2457 <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
2459 <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
2463 <th scope="row" class="label">
2464 <label for="title"><span class="alignleft">' . __('Title') . '</span></label>
2465 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2467 <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
2470 <tr class="not-image"><td></td><td><p class="help">' . __('Link text, e.g. “Ransom Demands (PDF)”') . '</p></td></tr>
2472 <tr class="image-only">
2473 <th scope="row" class="label">
2474 <label for="alt"><span class="alignleft">' . __('Alternative Text') . '</span></label>
2476 <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
2477 <p class="help">' . __('Alt text for the image, e.g. “The Mona Lisa”') . '</p></td>
2480 <tr class="align image-only">
2481 <th scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
2483 <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
2484 <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
2485 <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
2486 <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
2487 <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
2488 <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
2489 <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
2490 <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
2494 <tr class="image-only">
2495 <th scope="row" class="label">
2496 <label for="url"><span class="alignleft">' . __('Link Image To:') . '</span></label>
2498 <td class="field"><input id="url" name="url" value="" type="text" /><br />
2500 <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
2501 <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
2502 <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
2504 <tr class="image-only">
2507 <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
2510 <tr class="not-image">
2513 ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
2522 * Displays the multi-file uploader message.
2526 function media_upload_flash_bypass() {
2527 $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
2529 if ( $post = get_post() )
2530 $browser_uploader .= '&post_id=' . intval( $post->ID );
2531 elseif ( ! empty( $GLOBALS['post_ID'] ) )
2532 $browser_uploader .= '&post_id=' . intval( $GLOBALS['post_ID'] );
2535 <p class="upload-flash-bypass">
2536 <?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' ); ?>
2540 add_action('post-plupload-upload-ui', 'media_upload_flash_bypass');
2543 * Displays the browser's built-in uploader message.
2547 function media_upload_html_bypass() {
2549 <p class="upload-html-bypass hide-if-no-js">
2550 <?php _e('You are using the browser’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>.'); ?>
2554 add_action('post-html-upload-ui', 'media_upload_html_bypass');
2557 * Used to display a "After a file has been uploaded..." help message.
2561 function media_upload_text_after() {}
2564 * Displays the checkbox to scale images.
2568 function media_upload_max_image_resize() {
2569 $checked = get_user_setting('upload_resize') ? ' checked="true"' : '';
2572 if ( current_user_can( 'manage_options' ) ) {
2573 $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
2577 <p class="hide-if-no-js"><label>
2578 <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
2580 /* translators: %1$s is link start tag, %2$s is link end tag, %3$d is width, %4$d is height*/
2581 printf( __( 'Scale images to match the large size selected in %1$simage options%2$s (%3$d × %4$d).' ), $a, $end, (int) get_option( 'large_size_w', '1024' ), (int) get_option( 'large_size_h', '1024' ) );
2588 * Displays the out of storage quota message in Multisite.
2592 function multisite_over_quota_message() {
2593 echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
2597 * Displays the image and editor in the post editor
2601 function edit_form_image_editor( $post ) {
2602 $open = isset( $_GET['image-editor'] );
2604 require_once ABSPATH . 'wp-admin/includes/image-edit.php';
2607 if ( $attachment_id = intval( $post->ID ) )
2608 $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
2610 $filename = esc_html( basename( $post->guid ) );
2611 $title = esc_attr( $post->post_title );
2612 $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
2614 $att_url = wp_get_attachment_url( $post->ID ); ?>
2615 <div class="wp_attachment_holder">
2617 if ( wp_attachment_is_image( $post->ID ) ) :
2618 $image_edit_button = '';
2619 if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
2620 $nonce = wp_create_nonce( "image_editor-$post->ID" );
2621 $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>";
2625 <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
2627 <div<?php if ( $open ) echo ' style="display:none"'; ?> class="wp_attachment_image" id="media-head-<?php echo $attachment_id; ?>">
2628 <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>
2629 <p><?php echo $image_edit_button; ?></p>
2631 <div<?php if ( ! $open ) echo ' style="display:none"'; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
2632 <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
2635 elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):
2637 wp_maybe_generate_attachment_metadata( $post );
2639 echo wp_audio_shortcode( array( 'src' => $att_url ) );
2641 elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):
2643 wp_maybe_generate_attachment_metadata( $post );
2645 $meta = wp_get_attachment_metadata( $attachment_id );
2646 $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
2647 $h = ! empty( $meta['height'] ) ? $meta['height'] : 0;
2648 if ( $h && $w < $meta['width'] ) {
2649 $h = round( ( $meta['height'] * $w ) / $meta['width'] );
2652 $attr = array( 'src' => $att_url );
2653 if ( ! empty( $w ) && ! empty( $h ) ) {
2654 $attr['width'] = $w;
2655 $attr['height'] = $h;
2658 echo wp_video_shortcode( $attr );
2662 <div class="wp_attachment_details edit-form-section">
2664 <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
2665 <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
2669 <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
2671 <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
2672 <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" value="<?php echo esc_attr( $alt_text ); ?>" />
2677 $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
2678 $editor_args = array(
2679 'textarea_name' => 'content',
2680 'textarea_rows' => 5,
2681 'media_buttons' => false,
2683 'quicktags' => $quicktags_settings,
2687 <label for="content"><strong><?php _e( 'Description' ); ?></strong><?php
2688 if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
2689 echo ': ' . __( 'Displayed on attachment pages.' );
2691 <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
2695 $extras = get_compat_media_markup( $post->ID );
2696 echo $extras['item'];
2697 echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
2701 * Displays non-editable attachment metadata in the publish metabox
2705 function attachment_submitbox_metadata() {
2708 $filename = esc_html( wp_basename( $post->guid ) );
2711 $meta = wp_get_attachment_metadata( $post->ID );
2712 if ( isset( $meta['width'], $meta['height'] ) )
2713 $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']} × {$meta['height']}</span> ";
2714 /** This filter is documented in wp-admin/includes/media.php */
2715 $media_dims = apply_filters( 'media_meta', $media_dims, $post );
2717 $att_url = wp_get_attachment_url( $post->ID );
2719 <div class="misc-pub-section misc-pub-attachment">
2720 <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
2721 <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" value="<?php echo esc_attr($att_url); ?>" />
2723 <div class="misc-pub-section misc-pub-filename">
2724 <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
2726 <div class="misc-pub-section misc-pub-filetype">
2727 <?php _e( 'File type:' ); ?> <strong><?php
2728 if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
2729 echo esc_html( strtoupper( $matches[1] ) );
2730 list( $mime_type ) = explode( '/', $post->post_mime_type );
2731 if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
2732 if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
2733 echo ' (' . $meta['mime_type'] . ')';
2737 echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
2743 $file = get_attached_file( $post->ID );
2746 if ( isset( $meta['filesize'] ) )
2747 $file_size = $meta['filesize'];
2748 elseif ( file_exists( $file ) )
2749 $file_size = filesize( $file );
2751 if ( ! empty( $file_size ) ) : ?>
2752 <div class="misc-pub-section misc-pub-filesize">
2753 <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
2758 if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
2761 * Filter the audio and video metadata fields to be shown in the publish meta box.
2763 * The key for each item in the array should correspond to an attachment
2764 * metadata key, and the value should be the desired label.
2768 * @param array $fields An array of the attachment metadata keys and labels.
2770 $fields = apply_filters( 'media_submitbox_misc_sections', array(
2771 'length_formatted' => __( 'Length:' ),
2772 'bitrate' => __( 'Bitrate:' ),
2775 foreach ( $fields as $key => $label ) {
2776 if ( empty( $meta[ $key ] ) ) {
2780 <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
2781 <?php echo $label ?> <strong><?php
2784 echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
2785 if ( ! empty( $meta['bitrate_mode'] ) ) {
2786 echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
2790 echo esc_html( $meta[ $key ] );
2799 * Filter the audio attachment metadata fields to be shown in the publish meta box.
2801 * The key for each item in the array should correspond to an attachment
2802 * metadata key, and the value should be the desired label.
2806 * @param array $fields An array of the attachment metadata keys and labels.
2808 $audio_fields = apply_filters( 'audio_submitbox_misc_sections', array(
2809 'dataformat' => __( 'Audio Format:' ),
2810 'codec' => __( 'Audio Codec:' )
2813 foreach ( $audio_fields as $key => $label ) {
2814 if ( empty( $meta['audio'][ $key ] ) ) {
2818 <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
2819 <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
2826 if ( $media_dims ) : ?>
2827 <div class="misc-pub-section misc-pub-dimensions">
2828 <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
2834 add_filter( 'async_upload_image', 'get_media_item', 10, 2 );
2835 add_filter( 'async_upload_audio', 'get_media_item', 10, 2 );
2836 add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
2837 add_filter( 'async_upload_file', 'get_media_item', 10, 2 );
2839 add_action( 'media_upload_image', 'wp_media_upload_handler' );
2840 add_action( 'media_upload_audio', 'wp_media_upload_handler' );
2841 add_action( 'media_upload_video', 'wp_media_upload_handler' );
2842 add_action( 'media_upload_file', 'wp_media_upload_handler' );
2844 add_filter( 'media_upload_gallery', 'media_upload_gallery' );
2845 add_filter( 'media_upload_library', 'media_upload_library' );
2847 add_action( 'attachment_submitbox_misc_actions', 'attachment_submitbox_metadata' );
2850 * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
2854 * @param array $metadata An existing array with data
2855 * @param array $data Data supplied by ID3 tags
2857 function wp_add_id3_tag_data( &$metadata, $data ) {
2858 foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
2859 if ( ! empty( $data[$version]['comments'] ) ) {
2860 foreach ( $data[$version]['comments'] as $key => $list ) {
2861 if ( ! empty( $list ) ) {
2862 $metadata[$key] = reset( $list );
2863 // fix bug in byte stream analysis
2864 if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
2865 $metadata[$key] = 'Cop' . $metadata[$key];
2872 if ( ! empty( $data['id3v2']['APIC'] ) ) {
2873 $image = reset( $data['id3v2']['APIC']);
2874 if ( ! empty( $image['data'] ) ) {
2875 $metadata['image'] = array(
2876 'data' => $image['data'],
2877 'mime' => $image['image_mime'],
2878 'width' => $image['image_width'],
2879 'height' => $image['image_height']
2882 } elseif ( ! empty( $data['comments']['picture'] ) ) {
2883 $image = reset( $data['comments']['picture'] );
2884 if ( ! empty( $image['data'] ) ) {
2885 $metadata['image'] = array(
2886 'data' => $image['data'],
2887 'mime' => $image['image_mime']
2894 * Retrieve metadata from a video file's ID3 tags
2898 * @param string $file Path to file.
2899 * @return array|boolean Returns array of metadata, if found.
2901 function wp_read_video_metadata( $file ) {
2902 if ( ! file_exists( $file ) )
2905 $metadata = array();
2907 if ( ! class_exists( 'getID3' ) )
2908 require( ABSPATH . WPINC . '/ID3/getid3.php' );
2909 $id3 = new getID3();
2910 $data = $id3->analyze( $file );
2912 if ( isset( $data['video']['lossless'] ) )
2913 $metadata['lossless'] = $data['video']['lossless'];
2914 if ( ! empty( $data['video']['bitrate'] ) )
2915 $metadata['bitrate'] = (int) $data['video']['bitrate'];
2916 if ( ! empty( $data['video']['bitrate_mode'] ) )
2917 $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
2918 if ( ! empty( $data['filesize'] ) )
2919 $metadata['filesize'] = (int) $data['filesize'];
2920 if ( ! empty( $data['mime_type'] ) )
2921 $metadata['mime_type'] = $data['mime_type'];
2922 if ( ! empty( $data['playtime_seconds'] ) )
2923 $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
2924 if ( ! empty( $data['playtime_string'] ) )
2925 $metadata['length_formatted'] = $data['playtime_string'];
2926 if ( ! empty( $data['video']['resolution_x'] ) )
2927 $metadata['width'] = (int) $data['video']['resolution_x'];
2928 if ( ! empty( $data['video']['resolution_y'] ) )
2929 $metadata['height'] = (int) $data['video']['resolution_y'];
2930 if ( ! empty( $data['fileformat'] ) )
2931 $metadata['fileformat'] = $data['fileformat'];
2932 if ( ! empty( $data['video']['dataformat'] ) )
2933 $metadata['dataformat'] = $data['video']['dataformat'];
2934 if ( ! empty( $data['video']['encoder'] ) )
2935 $metadata['encoder'] = $data['video']['encoder'];
2936 if ( ! empty( $data['video']['codec'] ) )
2937 $metadata['codec'] = $data['video']['codec'];
2939 if ( ! empty( $data['audio'] ) ) {
2940 unset( $data['audio']['streams'] );
2941 $metadata['audio'] = $data['audio'];
2944 wp_add_id3_tag_data( $metadata, $data );
2950 * Retrieve metadata from a audio file's ID3 tags
2954 * @param string $file Path to file.
2955 * @return array|boolean Returns array of metadata, if found.
2957 function wp_read_audio_metadata( $file ) {
2958 if ( ! file_exists( $file ) )
2960 $metadata = array();
2962 if ( ! class_exists( 'getID3' ) )
2963 require( ABSPATH . WPINC . '/ID3/getid3.php' );
2964 $id3 = new getID3();
2965 $data = $id3->analyze( $file );
2967 if ( ! empty( $data['audio'] ) ) {
2968 unset( $data['audio']['streams'] );
2969 $metadata = $data['audio'];
2972 if ( ! empty( $data['fileformat'] ) )
2973 $metadata['fileformat'] = $data['fileformat'];
2974 if ( ! empty( $data['filesize'] ) )
2975 $metadata['filesize'] = (int) $data['filesize'];
2976 if ( ! empty( $data['mime_type'] ) )
2977 $metadata['mime_type'] = $data['mime_type'];
2978 if ( ! empty( $data['playtime_seconds'] ) )
2979 $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
2980 if ( ! empty( $data['playtime_string'] ) )
2981 $metadata['length_formatted'] = $data['playtime_string'];
2983 wp_add_id3_tag_data( $metadata, $data );