diff --git a/src/wp-admin/edit-form-advanced.php b/src/wp-admin/edit-form-advanced.php index 295b96a8f3..d6a8480dcb 100644 --- a/src/wp-admin/edit-form-advanced.php +++ b/src/wp-admin/edit-form-advanced.php @@ -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' ); 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' ); - } 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' ); } } @@ -178,7 +178,7 @@ if ( 'attachment' == $post_type ) { add_meta_box( 'submitdiv', __('Save'), 'attachment_submit_meta_box', null, 'side', 'core' ); 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' ); } } else { diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index b576badc7f..81ba091519 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -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; $id3data = wp_get_attachment_metadata( $post['ID'] ); if ( ! is_array( $id3data ) ) { @@ -2448,7 +2448,7 @@ function wp_ajax_send_attachment_to_editor() { $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : ''; $title = ''; // We no longer insert title tags into tags, as they are redundant. $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'] ); } diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 25a49cef5b..9171247d2f 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -127,10 +127,10 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { if ( $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 ); $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 ); $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); } diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index b08498f9dd..5f6d08c7ab 100644 --- a/src/wp-admin/includes/media.php +++ b/src/wp-admin/includes/media.php @@ -2653,13 +2653,13 @@ function edit_form_image_editor( $post ) { post_mime_type, 'audio/' ) ): + elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ): wp_maybe_generate_attachment_metadata( $post ); 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 ); diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index cb2f25f98d..a9965651f9 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -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' ); 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' ); - } 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' ); } } diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 735bfad032..4d54178ee2 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -1550,7 +1550,7 @@ function prepend_attachment($content) { if ( empty($post->post_type) || $post->post_type != 'attachment' ) return $content; - if ( 0 === strpos( $post->post_mime_type, 'video' ) ) { + if ( wp_attachment_is( 'video', $post ) ) { $meta = wp_get_attachment_metadata( get_the_ID() ); $atts = array( 'src' => wp_get_attachment_url() ); 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() ); } $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() ) ); } else { $p = '

'; diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 6beb39f71d..6a4d62c15d 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -5070,29 +5070,57 @@ function wp_get_attachment_thumb_url( $post_id = 0 ) { 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. * * @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. */ -function wp_attachment_is_image( $post_id = 0 ) { - $post_id = (int) $post_id; - 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; +function wp_attachment_is_image( $post = 0 ) { + return wp_attachment_is( 'image', $post ); } /**