+ $media_dims = '';
+ $meta = wp_get_attachment_metadata( $post->ID );
+ if ( isset( $meta['width'], $meta['height'] ) )
+ $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']} × {$meta['height']}</span> ";
+ /** This filter is documented in wp-admin/includes/media.php */
+ $media_dims = apply_filters( 'media_meta', $media_dims, $post );
+
+ $att_url = wp_get_attachment_url( $post->ID );
+?>
+ <div class="misc-pub-section misc-pub-attachment">
+ <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
+ <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" />
+ </div>
+ <div class="misc-pub-section misc-pub-filename">
+ <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
+ </div>
+ <div class="misc-pub-section misc-pub-filetype">
+ <?php _e( 'File type:' ); ?> <strong><?php
+ if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
+ echo esc_html( strtoupper( $matches[1] ) );
+ list( $mime_type ) = explode( '/', $post->post_mime_type );
+ if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
+ if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
+ echo ' (' . $meta['mime_type'] . ')';
+ }
+ }
+ } else {
+ echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
+ }
+ ?></strong>
+ </div>
+
+ <?php
+ $file_size = false;
+
+ if ( isset( $meta['filesize'] ) )
+ $file_size = $meta['filesize'];
+ elseif ( file_exists( $file ) )
+ $file_size = filesize( $file );
+
+ if ( ! empty( $file_size ) ) : ?>
+ <div class="misc-pub-section misc-pub-filesize">
+ <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
+ </div>
+ <?php
+ endif;
+
+ if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
+
+ /**
+ * Filter the audio and video metadata fields to be shown in the publish meta box.
+ *
+ * The key for each item in the array should correspond to an attachment
+ * metadata key, and the value should be the desired label.
+ *
+ * @since 3.7.0
+ *
+ * @param array $fields An array of the attachment metadata keys and labels.
+ */
+ $fields = apply_filters( 'media_submitbox_misc_sections', array(
+ 'length_formatted' => __( 'Length:' ),
+ 'bitrate' => __( 'Bitrate:' ),
+ ) );
+
+ foreach ( $fields as $key => $label ) {
+ if ( empty( $meta[ $key ] ) ) {
+ continue;
+ }
+ ?>
+ <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
+ <?php echo $label ?> <strong><?php
+ switch ( $key ) {
+ case 'bitrate' :
+ echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
+ if ( ! empty( $meta['bitrate_mode'] ) ) {
+ echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
+ }
+ break;
+ default:
+ echo esc_html( $meta[ $key ] );
+ break;
+ }
+ ?></strong>
+ </div>
+ <?php
+ }
+
+ /**
+ * Filter the audio attachment metadata fields to be shown in the publish meta box.
+ *
+ * The key for each item in the array should correspond to an attachment
+ * metadata key, and the value should be the desired label.
+ *
+ * @since 3.7.0
+ *
+ * @param array $fields An array of the attachment metadata keys and labels.
+ */
+ $audio_fields = apply_filters( 'audio_submitbox_misc_sections', array(
+ 'dataformat' => __( 'Audio Format:' ),
+ 'codec' => __( 'Audio Codec:' )
+ ) );
+
+ foreach ( $audio_fields as $key => $label ) {
+ if ( empty( $meta['audio'][ $key ] ) ) {
+ continue;
+ }
+ ?>
+ <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
+ <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
+ </div>
+ <?php
+ }
+
+ }
+
+ if ( $media_dims ) : ?>
+ <div class="misc-pub-section misc-pub-dimensions">
+ <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
+ </div>
+<?php
+ endif;
+}
+
+/**
+ * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
+ *
+ * @since 3.6.0
+ *
+ * @param array $metadata An existing array with data
+ * @param array $data Data supplied by ID3 tags
+ */
+function wp_add_id3_tag_data( &$metadata, $data ) {
+ foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
+ if ( ! empty( $data[$version]['comments'] ) ) {
+ foreach ( $data[$version]['comments'] as $key => $list ) {
+ if ( 'length' !== $key && ! empty( $list ) ) {
+ $metadata[$key] = reset( $list );
+ // Fix bug in byte stream analysis.
+ if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
+ $metadata[$key] = 'Cop' . $metadata[$key];
+ }
+ }
+ break;
+ }
+ }
+
+ if ( ! empty( $data['id3v2']['APIC'] ) ) {
+ $image = reset( $data['id3v2']['APIC']);
+ if ( ! empty( $image['data'] ) ) {
+ $metadata['image'] = array(
+ 'data' => $image['data'],
+ 'mime' => $image['image_mime'],
+ 'width' => $image['image_width'],
+ 'height' => $image['image_height']
+ );
+ }
+ } elseif ( ! empty( $data['comments']['picture'] ) ) {
+ $image = reset( $data['comments']['picture'] );
+ if ( ! empty( $image['data'] ) ) {
+ $metadata['image'] = array(
+ 'data' => $image['data'],
+ 'mime' => $image['image_mime']
+ );
+ }
+ }
+}
+
+/**
+ * Retrieve metadata from a video file's ID3 tags
+ *
+ * @since 3.6.0
+ *
+ * @param string $file Path to file.
+ * @return array|bool Returns array of metadata, if found.
+ */
+function wp_read_video_metadata( $file ) {
+ if ( ! file_exists( $file ) ) {
+ return false;
+ }
+
+ $metadata = array();
+
+ if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
+ define( 'GETID3_TEMP_DIR', get_temp_dir() );
+ }
+
+ if ( ! class_exists( 'getID3', false ) ) {
+ require( ABSPATH . WPINC . '/ID3/getid3.php' );
+ }
+ $id3 = new getID3();
+ $data = $id3->analyze( $file );
+
+ if ( isset( $data['video']['lossless'] ) )
+ $metadata['lossless'] = $data['video']['lossless'];
+ if ( ! empty( $data['video']['bitrate'] ) )
+ $metadata['bitrate'] = (int) $data['video']['bitrate'];
+ if ( ! empty( $data['video']['bitrate_mode'] ) )
+ $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
+ if ( ! empty( $data['filesize'] ) )
+ $metadata['filesize'] = (int) $data['filesize'];
+ if ( ! empty( $data['mime_type'] ) )
+ $metadata['mime_type'] = $data['mime_type'];
+ if ( ! empty( $data['playtime_seconds'] ) )
+ $metadata['length'] = (int) round( $data['playtime_seconds'] );
+ if ( ! empty( $data['playtime_string'] ) )
+ $metadata['length_formatted'] = $data['playtime_string'];
+ if ( ! empty( $data['video']['resolution_x'] ) )
+ $metadata['width'] = (int) $data['video']['resolution_x'];
+ if ( ! empty( $data['video']['resolution_y'] ) )
+ $metadata['height'] = (int) $data['video']['resolution_y'];
+ if ( ! empty( $data['fileformat'] ) )
+ $metadata['fileformat'] = $data['fileformat'];
+ if ( ! empty( $data['video']['dataformat'] ) )
+ $metadata['dataformat'] = $data['video']['dataformat'];
+ if ( ! empty( $data['video']['encoder'] ) )
+ $metadata['encoder'] = $data['video']['encoder'];
+ if ( ! empty( $data['video']['codec'] ) )
+ $metadata['codec'] = $data['video']['codec'];
+
+ if ( ! empty( $data['audio'] ) ) {
+ unset( $data['audio']['streams'] );
+ $metadata['audio'] = $data['audio'];
+ }
+
+ wp_add_id3_tag_data( $metadata, $data );
+
+ return $metadata;
+}
+
+/**
+ * Retrieve metadata from a audio file's ID3 tags
+ *
+ * @since 3.6.0
+ *
+ * @param string $file Path to file.
+ * @return array|bool Returns array of metadata, if found.
+ */
+function wp_read_audio_metadata( $file ) {
+ if ( ! file_exists( $file ) ) {
+ return false;
+ }
+ $metadata = array();
+
+ if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
+ define( 'GETID3_TEMP_DIR', get_temp_dir() );
+ }
+
+ if ( ! class_exists( 'getID3', false ) ) {
+ require( ABSPATH . WPINC . '/ID3/getid3.php' );
+ }
+ $id3 = new getID3();
+ $data = $id3->analyze( $file );
+
+ if ( ! empty( $data['audio'] ) ) {
+ unset( $data['audio']['streams'] );
+ $metadata = $data['audio'];
+ }
+
+ if ( ! empty( $data['fileformat'] ) )
+ $metadata['fileformat'] = $data['fileformat'];
+ if ( ! empty( $data['filesize'] ) )
+ $metadata['filesize'] = (int) $data['filesize'];
+ if ( ! empty( $data['mime_type'] ) )
+ $metadata['mime_type'] = $data['mime_type'];
+ if ( ! empty( $data['playtime_seconds'] ) )
+ $metadata['length'] = (int) round( $data['playtime_seconds'] );
+ if ( ! empty( $data['playtime_string'] ) )
+ $metadata['length_formatted'] = $data['playtime_string'];
+
+ wp_add_id3_tag_data( $metadata, $data );
+
+ return $metadata;
+}
+
+/**
+ * Encapsulate logic for Attach/Detach actions
+ *
+ * @since 4.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int $parent_id Attachment parent ID.
+ * @param string $action Optional. Attach/detach action. Accepts 'attach' or 'detach'.
+ * Default 'attach'.
+ */
+function wp_media_attach_action( $parent_id, $action = 'attach' ) {
+ global $wpdb;
+
+ if ( ! $parent_id ) {
+ return;
+ }
+
+ if ( ! current_user_can( 'edit_post', $parent_id ) ) {
+ wp_die( __( 'You are not allowed to edit this post.' ) );
+ }
+ $ids = array();
+ foreach ( (array) $_REQUEST['media'] as $att_id ) {
+ $att_id = (int) $att_id;
+
+ if ( ! current_user_can( 'edit_post', $att_id ) ) {
+ continue;
+ }
+
+ $ids[] = $att_id;
+ }
+
+ if ( ! empty( $ids ) ) {
+ $ids_string = implode( ',', $ids );
+ if ( 'attach' === $action ) {
+ $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
+ } else {
+ $result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
+ }
+
+ foreach ( $ids as $att_id ) {
+ clean_attachment_cache( $att_id );
+ }
+ }
+
+ if ( isset( $result ) ) {
+ $location = 'upload.php';
+ if ( $referer = wp_get_referer() ) {
+ if ( false !== strpos( $referer, 'upload.php' ) ) {
+ $location = remove_query_arg( array( 'attached', 'detach' ), $referer );
+ }
+ }
+
+ $key = 'attach' === $action ? 'attached' : 'detach';
+ $location = add_query_arg( array( $key => $result ), $location );
+ wp_redirect( $location );
+ exit;
+ }
+}