Introduce a function, `wp_attachment_is( $type, $post = 0 )`, to collapse the logic for determining whether an attachment is an `image`, `audio`, or `video`.

This is admittedly a first pass. There needs to be a generic handler for when any other type is passed, but for now it accepts the whitelist.

See #25275.


git-svn-id: https://develop.svn.wordpress.org/trunk@31645 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-03-06 20:25:09 +00:00
parent ff67402710
commit 42bcec5061
7 changed files with 57 additions and 29 deletions

View File

@ -52,9 +52,9 @@ if ( $post_ID == get_option( 'page_for_posts' ) && empty( $post->post_content )
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ); $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
if ( ! $thumbnail_support && 'attachment' === $post_type && $post->post_mime_type ) { if ( ! $thumbnail_support && 'attachment' === $post_type && $post->post_mime_type ) {
if ( 0 === strpos( $post->post_mime_type, 'audio/' ) ) { if ( wp_attachment_is( 'audio', $post ) ) {
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
} elseif ( 0 === strpos( $post->post_mime_type, 'video/' ) ) { } elseif ( wp_attachment_is( 'video', $post ) ) {
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
} }
} }
@ -178,7 +178,7 @@ if ( 'attachment' == $post_type ) {
add_meta_box( 'submitdiv', __('Save'), 'attachment_submit_meta_box', null, 'side', 'core' ); add_meta_box( 'submitdiv', __('Save'), 'attachment_submit_meta_box', null, 'side', 'core' );
add_action( 'edit_form_after_title', 'edit_form_image_editor' ); add_action( 'edit_form_after_title', 'edit_form_image_editor' );
if ( 0 === strpos( $post->post_mime_type, 'audio/' ) ) { if ( wp_attachment_is( 'audio', $post ) ) {
add_meta_box( 'attachment-id3', __( 'Metadata' ), 'attachment_id3_data_meta_box', null, 'normal', 'core' ); add_meta_box( 'attachment-id3', __( 'Metadata' ), 'attachment_id3_data_meta_box', null, 'normal', 'core' );
} }
} else { } else {

View File

@ -2290,7 +2290,7 @@ function wp_ajax_save_attachment() {
} }
} }
if ( 0 === strpos( $post['post_mime_type'], 'audio/' ) ) { if ( wp_attachment_is( 'audio', $post['ID'] ) ) {
$changed = false; $changed = false;
$id3data = wp_get_attachment_metadata( $post['ID'] ); $id3data = wp_get_attachment_metadata( $post['ID'] );
if ( ! is_array( $id3data ) ) { if ( ! is_array( $id3data ) ) {
@ -2448,7 +2448,7 @@ function wp_ajax_send_attachment_to_editor() {
$caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : ''; $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
$title = ''; // We no longer insert title tags into <img> tags, as they are redundant. $title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
$html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt ); $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt );
} elseif ( 'video' === substr( $post->post_mime_type, 0, 5 ) || 'audio' === substr( $post->post_mime_type, 0, 5 ) ) { } elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post ) ) {
$html = stripslashes_deep( $_POST['html'] ); $html = stripslashes_deep( $_POST['html'] );
} }

View File

@ -127,10 +127,10 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
if ( $image_meta ) if ( $image_meta )
$metadata['image_meta'] = $image_meta; $metadata['image_meta'] = $image_meta;
} elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) { } elseif ( wp_attachment_is( 'video', $attachment ) ) {
$metadata = wp_read_video_metadata( $file ); $metadata = wp_read_video_metadata( $file );
$support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
} elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) { } elseif ( wp_attachment_is( 'audio', $attachment ) ) {
$metadata = wp_read_audio_metadata( $file ); $metadata = wp_read_audio_metadata( $file );
$support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
} }

View File

@ -2653,13 +2653,13 @@ function edit_form_image_editor( $post ) {
<?php if ( $open ) wp_image_editor( $attachment_id ); ?> <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
</div> </div>
<?php <?php
elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ): elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ):
wp_maybe_generate_attachment_metadata( $post ); wp_maybe_generate_attachment_metadata( $post );
echo wp_audio_shortcode( array( 'src' => $att_url ) ); echo wp_audio_shortcode( array( 'src' => $att_url ) );
elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ): elseif ( $attachment_id && wp_attachment_is( 'video', $post ) ):
wp_maybe_generate_attachment_metadata( $post ); wp_maybe_generate_attachment_metadata( $post );

View File

@ -2976,9 +2976,9 @@ function wp_enqueue_media( $args = array() ) {
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' ); $thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' );
if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) { if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) {
if ( 0 === strpos( $post->post_mime_type, 'audio/' ) ) { if ( wp_attachment_is( 'audio', $post ) ) {
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
} elseif ( 0 === strpos( $post->post_mime_type, 'video/' ) ) { } elseif ( wp_attachment_is( 'video', $post ) ) {
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
} }
} }

View File

@ -1550,7 +1550,7 @@ function prepend_attachment($content) {
if ( empty($post->post_type) || $post->post_type != 'attachment' ) if ( empty($post->post_type) || $post->post_type != 'attachment' )
return $content; return $content;
if ( 0 === strpos( $post->post_mime_type, 'video' ) ) { if ( wp_attachment_is( 'video', $post ) ) {
$meta = wp_get_attachment_metadata( get_the_ID() ); $meta = wp_get_attachment_metadata( get_the_ID() );
$atts = array( 'src' => wp_get_attachment_url() ); $atts = array( 'src' => wp_get_attachment_url() );
if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
@ -1561,7 +1561,7 @@ function prepend_attachment($content) {
$atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() ); $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
} }
$p = wp_video_shortcode( $atts ); $p = wp_video_shortcode( $atts );
} elseif ( 0 === strpos( $post->post_mime_type, 'audio' ) ) { } elseif ( wp_attachment_is( 'audio', $post ) ) {
$p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) ); $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
} else { } else {
$p = '<p class="attachment">'; $p = '<p class="attachment">';

View File

@ -5070,29 +5070,57 @@ function wp_get_attachment_thumb_url( $post_id = 0 ) {
return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
} }
/**
* Verfify the attachment as being of a specific type
*
* @param string $type Type: image, audio, or video.
* @param int|WP_Post $post_id Optional. Default 0.
* @return bool
*/
function wp_attachment_is( $type, $post_id = 0 ) {
if ( ! $post = get_post( $post_id ) ) {
return false;
}
if ( ! $file = get_attached_file( $post->ID ) ) {
return false;
}
if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
return true;
}
$check = wp_check_filetype( $file );
if ( empty( $check['ext'] ) || 'import' !== $post->post_mime_type ) {
return false;
}
$ext = $check['ext'];
switch ( $type ) {
case 'image':
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
return in_array( $ext, $image_exts );
case 'audio':
return in_array( $ext, wp_get_audio_extensions() );
case 'video':
return in_array( $ext, wp_get_video_extensions() );
}
return false;
}
/** /**
* Check if the attachment is an image. * Check if the attachment is an image.
* *
* @since 2.1.0 * @since 2.1.0
* *
* @param int $post_id Optional. Attachment ID. Default 0. * @param int|WP_Post $post Optional. Attachment ID. Default 0.
* @return bool Whether the attachment is an image. * @return bool Whether the attachment is an image.
*/ */
function wp_attachment_is_image( $post_id = 0 ) { function wp_attachment_is_image( $post = 0 ) {
$post_id = (int) $post_id; return wp_attachment_is( 'image', $post );
if ( !$post = get_post( $post_id ) )
return false;
if ( !$file = get_attached_file( $post->ID ) )
return false;
$ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
return true;
return false;
} }
/** /**