Make WordPress Core

Changeset 31645


Ignore:
Timestamp:
03/06/2015 08:25:09 PM (12 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-advanced.php

    r31550 r31645  
    5353$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
    5454if ( ! $thumbnail_support && 'attachment' === $post_type && $post->post_mime_type ) {
    55         if ( 0 === strpos( $post->post_mime_type, 'audio/' ) ) {
     55        if ( wp_attachment_is( 'audio', $post ) ) {
    5656                $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
    57         } elseif ( 0 === strpos( $post->post_mime_type, 'video/' ) ) {
     57        } elseif ( wp_attachment_is( 'video', $post ) ) {
    5858                $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
    5959        }
     
    179179        add_action( 'edit_form_after_title', 'edit_form_image_editor' );
    180180
    181         if ( 0 === strpos( $post->post_mime_type, 'audio/' ) ) {
     181        if ( wp_attachment_is( 'audio', $post ) ) {
    182182                add_meta_box( 'attachment-id3', __( 'Metadata' ), 'attachment_id3_data_meta_box', null, 'normal', 'core' );
    183183        }
  • trunk/src/wp-admin/includes/ajax-actions.php

    r31626 r31645  
    22912291        }
    22922292
    2293         if ( 0 === strpos( $post['post_mime_type'], 'audio/' ) ) {
     2293        if ( wp_attachment_is( 'audio', $post['ID'] ) ) {
    22942294                $changed = false;
    22952295                $id3data = wp_get_attachment_metadata( $post['ID'] );
     
    24492449                $title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
    24502450                $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt );
    2451         } elseif ( 'video' === substr( $post->post_mime_type, 0, 5 ) || 'audio' === substr( $post->post_mime_type, 0, 5 )  ) {
     2451        } elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post )  ) {
    24522452                $html = stripslashes_deep( $_POST['html'] );
    24532453        }
  • trunk/src/wp-admin/includes/image.php

    r31462 r31645  
    128128                        $metadata['image_meta'] = $image_meta;
    129129
    130         } elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) {
     130        } elseif ( wp_attachment_is( 'video', $attachment ) ) {
    131131                $metadata = wp_read_video_metadata( $file );
    132132                $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
    133         } elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) {
     133        } elseif ( wp_attachment_is( 'audio', $attachment ) ) {
    134134                $metadata = wp_read_audio_metadata( $file );
    135135                $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
  • trunk/src/wp-admin/includes/media.php

    r31624 r31645  
    26542654                </div>
    26552655        <?php
    2656         elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):
     2656        elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ):
    26572657
    26582658                wp_maybe_generate_attachment_metadata( $post );
     
    26602660                echo wp_audio_shortcode( array( 'src' => $att_url ) );
    26612661
    2662         elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):
     2662        elseif ( $attachment_id && wp_attachment_is( 'video', $post ) ):
    26632663
    26642664                wp_maybe_generate_attachment_metadata( $post );
  • trunk/src/wp-includes/media.php

    r31574 r31645  
    29772977                $thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' );
    29782978                if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) {
    2979                         if ( 0 === strpos( $post->post_mime_type, 'audio/' ) ) {
     2979                        if ( wp_attachment_is( 'audio', $post ) ) {
    29802980                                $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
    2981                         } elseif ( 0 === strpos( $post->post_mime_type, 'video/' ) ) {
     2981                        } elseif ( wp_attachment_is( 'video', $post ) ) {
    29822982                                $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
    29832983                        }
  • trunk/src/wp-includes/post-template.php

    r31600 r31645  
    15511551                return $content;
    15521552
    1553         if ( 0 === strpos( $post->post_mime_type, 'video' ) ) {
     1553        if ( wp_attachment_is( 'video', $post ) ) {
    15541554                $meta = wp_get_attachment_metadata( get_the_ID() );
    15551555                $atts = array( 'src' => wp_get_attachment_url() );
     
    15621562                }
    15631563                $p = wp_video_shortcode( $atts );
    1564         } elseif ( 0 === strpos( $post->post_mime_type, 'audio' ) ) {
     1564        } elseif ( wp_attachment_is( 'audio', $post ) ) {
    15651565                $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
    15661566        } else {
  • trunk/src/wp-includes/post.php

    r31614 r31645  
    50725072
    50735073/**
     5074 * Verfify the attachment as being of a specific type
     5075 *
     5076 * @param string      $type    Type: image, audio, or video.
     5077 * @param int|WP_Post $post_id Optional. Default 0.
     5078 * @return bool
     5079 */
     5080function wp_attachment_is( $type, $post_id = 0 ) {
     5081        if ( ! $post = get_post( $post_id ) ) {
     5082                return false;
     5083        }
     5084
     5085        if ( ! $file = get_attached_file( $post->ID ) ) {
     5086                return false;
     5087        }
     5088
     5089        if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
     5090                return true;
     5091        }
     5092
     5093        $check = wp_check_filetype( $file );
     5094        if ( empty( $check['ext'] ) || 'import' !== $post->post_mime_type ) {
     5095                return false;
     5096        }
     5097        $ext = $check['ext'];
     5098
     5099        switch ( $type ) {
     5100        case 'image':
     5101                $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
     5102                return in_array( $ext, $image_exts );
     5103
     5104        case 'audio':
     5105                return in_array( $ext, wp_get_audio_extensions() );
     5106
     5107        case 'video':
     5108                return in_array( $ext, wp_get_video_extensions() );
     5109        }
     5110
     5111        return false;
     5112}
     5113
     5114/**
    50745115 * Check if the attachment is an image.
    50755116 *
    50765117 * @since 2.1.0
    50775118 *
    5078  * @param int $post_id Optional. Attachment ID. Default 0.
     5119 * @param int|WP_Post $post Optional. Attachment ID. Default 0.
    50795120 * @return bool Whether the attachment is an image.
    50805121 */
    5081 function wp_attachment_is_image( $post_id = 0 ) {
    5082         $post_id = (int) $post_id;
    5083         if ( !$post = get_post( $post_id ) )
    5084                 return false;
    5085 
    5086         if ( !$file = get_attached_file( $post->ID ) )
    5087                 return false;
    5088 
    5089         $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
    5090 
    5091         $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
    5092 
    5093         if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
    5094                 return true;
    5095         return false;
     5122function wp_attachment_is_image( $post = 0 ) {
     5123        return wp_attachment_is( 'image', $post );
    50965124}
    50975125
Note: See TracChangeset for help on using the changeset viewer.