Cleanup up the display, escaping, and handling of ID3 data for media. Rename `wp_get_relevant_id3_keys()` to `wp_get_attachment_id3_keys()`.

Props nacin.
See #27574.


Built from https://develop.svn.wordpress.org/trunk@27869


git-svn-id: http://core.svn.wordpress.org/trunk@27700 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2014-03-31 05:16:16 +00:00
parent 98ceb17972
commit 01d2062af3
4 changed files with 68 additions and 68 deletions

View File

@ -2661,10 +2661,7 @@ function edit_form_image_editor( $post ) {
</div>
<div class="wp_attachment_details edit-form-section">
<p>
<label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong><?php
if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ): ?>: <?php
_e( "Custom label for item in a playlist. If empty, the file's available data is used." );
endif ?></label><br />
<label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
<textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
</p>
@ -2688,9 +2685,9 @@ function edit_form_image_editor( $post ) {
?>
<label for="content"><strong><?php _e( 'Description' ); ?></strong><?php
if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ): ?>: <?php
_e( 'Displayed on attachment pages.' );
endif ?></label>
if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
echo ': ' . __( 'Displayed on attachment pages.' );
} ?></label>
<?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
</div>
@ -2728,10 +2725,17 @@ function attachment_submitbox_metadata() {
</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 ) )
if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
echo esc_html( strtoupper( $matches[1] ) );
else
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>
@ -2751,7 +2755,7 @@ function attachment_submitbox_metadata() {
<?php
endif;
if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ):
if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
/**
* Filter the audio and video metadata fields to be shown in the publish meta box.
@ -2761,41 +2765,35 @@ function attachment_submitbox_metadata() {
*
* @since 3.7.0
*
* @param array $fields {
* An array of the attachment metadata keys and labels.
*
* @type string $mime_type Label to be shown before the field mime_type.
* @type string $year Label to be shown before the field year.
* @type string $genre Label to be shown before the field genre.
* @type string $length_formatted Label to be shown before the field length_formatted.
* }
* @param array $fields An array of the attachment metadata keys and labels.
*/
$fields = apply_filters( 'media_submitbox_misc_sections', array(
'mime_type' => __( 'Mime-type:' ),
'length_formatted' => __( 'Length:' ),
'bitrate' => __( 'Bitrate:' ),
) );
foreach ( $fields as $key => $label ):
if ( ! empty( $meta[$key] ) ) : ?>
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 echo esc_html( $meta[$key] ); ?></strong>
</div>
<?php
endif;
endforeach;
if ( ! empty( $meta['bitrate'] ) ) : ?>
<div class="misc-pub-section misc-pub-bitrate">
<?php _e( 'Bitrate:' ); ?> <strong><?php
echo round( $meta['bitrate'] / 1000 ), 'kb/s';
if ( ! empty( $meta['bitrate_mode'] ) )
echo ' ', strtoupper( $meta['bitrate_mode'] );
<?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
endif;
}
/**
* Filter the audio attachment metadata fields to be shown in the publish meta box.
@ -2805,28 +2803,25 @@ function attachment_submitbox_metadata() {
*
* @since 3.7.0
*
* @param array $fields {
* An array of the attachment metadata keys and labels.
*
* @type string $dataformat Label to be shown before the field dataformat.
* @type string $codec Label to be shown before the field codec.
* }
* @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] ) ) : ?>
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
endif;
endforeach;
}
endif;
}
if ( $media_dims ) : ?>
<div class="misc-pub-section misc-pub-dimensions">

View File

@ -1071,14 +1071,15 @@ function attachment_id3_data_meta_box( $post ) {
$meta = wp_get_attachment_metadata( $post->ID );
}
foreach ( wp_get_relevant_id3_keys( $post ) as $key => $label ): ?>
foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) : ?>
<p>
<label for="title"><?php echo $label ?></label>
<input type="text" name="id3_<?php echo esc_attr( $key ) ?>" id="id3_<?php echo esc_attr( $key ) ?>" class="widefat" value="<?php
<label for="title"><?php echo $label ?></label><br />
<input type="text" name="id3_<?php echo esc_attr( $key ) ?>" id="id3_<?php echo esc_attr( $key ) ?>" class="large-text" value="<?php
if ( ! empty( $meta[ $key ] ) ) {
echo esc_attr( $meta[ $key ] );
}
?>" />
</p>
<?php endforeach;
<?php
endforeach;
}

View File

@ -243,7 +243,7 @@ function edit_post( $post_data = null ) {
$id3data = array();
}
foreach ( wp_get_relevant_id3_keys( $post ) as $key => $label ) {
foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
if ( isset( $post_data[ 'id3_' . $key ] ) ) {
$id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
}

View File

@ -1019,7 +1019,7 @@ function wp_underscore_playlist_templates() {
<img src="{{ data.thumb.src }}"/>
<# } #>
<div class="wp-playlist-caption">
<span class="wp-playlist-item-meta wp-playlist-item-title">&#8220;{{{ data.title }}}&#8221;</span>
<span class="wp-playlist-item-meta wp-playlist-item-title">&#8220;{{ data.title }}&#8221;</span>
<# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #>
<# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #>
</div>
@ -1029,16 +1029,16 @@ function wp_underscore_playlist_templates() {
<div class="wp-playlist-caption">
{{ data.index ? ( data.index + '. ' ) : '' }}
<# if ( data.caption ) { #>
{{{ data.caption }}}
{{ data.caption }}
<# } else { #>
<span class="wp-playlist-item-title">&#8220;{{{ data.title }}}&#8221;</span>
<span class="wp-playlist-item-title">&#8220;{{ data.title }}&#8221;</span>
<# if ( data.artists && data.meta.artist ) { #>
<span class="wp-playlist-item-artist"> &mdash; {{ data.meta.artist }}</span>
<# } #>
<# } #>
</div>
<# if ( data.meta.length_formatted ) { #>
<div class="wp-playlist-item-length">{{{ data.meta.length_formatted }}}</div>
<div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div>
<# } #>
</div>
</script>
@ -1202,7 +1202,7 @@ function wp_playlist_shortcode( $attr ) {
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( ! empty( $meta ) ) {
foreach ( wp_get_relevant_id3_keys( $attachment ) as $key => $label ) {
foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
if ( ! empty( $meta[ $key ] ) ) {
$track['meta'][ $key ] = $meta[ $key ];
}
@ -1312,31 +1312,35 @@ function wp_get_audio_extensions() {
}
/**
* Return useful keys to use to lookup data from an attachment's stored metadata
* Return useful keys to use to lookup data from an attachment's stored metadata.
*
* @since 3.9.0
*
* @param WP_Post $post The post in question, provided for context.
* @param WP_Post $attachment The attachment in question, provided for context.
* @param string $context The context. Accepts 'edit', 'display'. Default 'display'.
* @return array
*/
function wp_get_relevant_id3_keys( $post ) {
function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) {
$fields = array(
'artist' => __( 'Artist' ),
'album' => __( 'Album' ),
'genre' => __( 'Genre' ),
'year' => __( 'Year' ),
'length_formatted' => __( 'Formatted Length' )
);
if ( 'display' === $context ) {
$fields['genre'] = __( 'Genre' );
$fields['year'] = __( 'Year' );
$fields['length_formatted'] = _x( 'Length', 'video or audio' );
}
/**
* Filter the editable list of keys to lookup data from an attachment's metadata.
*
* @since 3.9.0
*
* @param array $fields Key/value pairs of field keys to labels.
* @param WP_Post $post Post object.
* @param array $fields Key/value pairs of field keys to labels.
* @param WP_Post $attachment Attachment object.
*/
return apply_filters( 'wp_get_relevant_id3_keys', $fields, $post );
return apply_filters( 'wp_get_attachment_id3_keys', $fields, $attachment, $context );
}
/**
* The Audio shortcode.
@ -2333,7 +2337,7 @@ function wp_prepare_attachment_for_js( $attachment ) {
$response['fileLength'] = $meta['length_formatted'];
$response['meta'] = array();
foreach ( wp_get_relevant_id3_keys( $attachment ) as $key => $label ) {
foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
if ( ! empty( $meta[ $key ] ) ) {
$response['meta'][ $key ] = $meta[ $key ];
}