Make WordPress Core


Ignore:
Timestamp:
03/06/2015 08:25:09 PM (9 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.