Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image.php

    r42228 r42343  
    3838
    3939    $editor = wp_get_image_editor( $src );
    40     if ( is_wp_error( $editor ) )
     40    if ( is_wp_error( $editor ) ) {
    4141        return $editor;
     42    }
    4243
    4344    $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
    44     if ( is_wp_error( $src ) )
     45    if ( is_wp_error( $src ) ) {
    4546        return $src;
    46 
    47     if ( ! $dst_file )
     47    }
     48
     49    if ( ! $dst_file ) {
    4850        $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
     51    }
    4952
    5053    /*
     
    5760
    5861    $result = $editor->save( $dst_file );
    59     if ( is_wp_error( $result ) )
     62    if ( is_wp_error( $result ) ) {
    6063        return $result;
     64    }
    6165
    6266    return $dst_file;
     
    7579    $attachment = get_post( $attachment_id );
    7680
    77     $metadata = array();
    78     $support = false;
     81    $metadata  = array();
     82    $support   = false;
    7983    $mime_type = get_post_mime_type( $attachment );
    8084
    8185    if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) {
    82         $imagesize = getimagesize( $file );
    83         $metadata['width'] = $imagesize[0];
     86        $imagesize          = getimagesize( $file );
     87        $metadata['width']  = $imagesize[0];
    8488        $metadata['height'] = $imagesize[1];
    8589
    8690        // Make the file path relative to the upload dir.
    87         $metadata['file'] = _wp_relative_upload_path($file);
     91        $metadata['file'] = _wp_relative_upload_path( $file );
    8892
    8993        // Make thumbnails and other intermediate sizes.
     
    9296        $sizes = array();
    9397        foreach ( get_intermediate_image_sizes() as $s ) {
    94             $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
    95             if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) {
     98            $sizes[ $s ] = array(
     99                'width'  => '',
     100                'height' => '',
     101                'crop'   => false,
     102            );
     103            if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) {
    96104                // For theme-added sizes
    97                 $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] );
     105                $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] );
    98106            } else {
    99107                // For default sizes set in options
    100                 $sizes[$s]['width'] = get_option( "{$s}_size_w" );
    101             }
    102 
    103             if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) {
     108                $sizes[ $s ]['width'] = get_option( "{$s}_size_w" );
     109            }
     110
     111            if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) {
    104112                // For theme-added sizes
    105                 $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] );
     113                $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] );
    106114            } else {
    107115                // For default sizes set in options
    108                 $sizes[$s]['height'] = get_option( "{$s}_size_h" );
    109             }
    110 
    111             if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) {
     116                $sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
     117            }
     118
     119            if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) {
    112120                // For theme-added sizes
    113                 $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop'];
     121                $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop'];
    114122            } else {
    115123                // For default sizes set in options
    116                 $sizes[$s]['crop'] = get_option( "{$s}_crop" );
     124                $sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
    117125            }
    118126        }
     
    132140            $editor = wp_get_image_editor( $file );
    133141
    134             if ( ! is_wp_error( $editor ) )
     142            if ( ! is_wp_error( $editor ) ) {
    135143                $metadata['sizes'] = $editor->multi_resize( $sizes );
     144            }
    136145        } else {
    137146            $metadata['sizes'] = array();
     
    140149        // Fetch additional metadata from EXIF/IPTC.
    141150        $image_meta = wp_read_image_metadata( $file );
    142         if ( $image_meta )
     151        if ( $image_meta ) {
    143152            $metadata['image_meta'] = $image_meta;
    144 
     153        }
    145154    } elseif ( wp_attachment_is( 'video', $attachment ) ) {
    146155        $metadata = wp_read_video_metadata( $file );
    147         $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
     156        $support  = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
    148157    } elseif ( wp_attachment_is( 'audio', $attachment ) ) {
    149158        $metadata = wp_read_audio_metadata( $file );
    150         $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
     159        $support  = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
    151160    }
    152161
    153162    if ( $support && ! empty( $metadata['image']['data'] ) ) {
    154163        // Check for existing cover.
    155         $hash = md5( $metadata['image']['data'] );
    156         $posts = get_posts( array(
    157             'fields' => 'ids',
    158             'post_type' => 'attachment',
    159             'post_mime_type' => $metadata['image']['mime'],
    160             'post_status' => 'inherit',
    161             'posts_per_page' => 1,
    162             'meta_key' => '_cover_hash',
    163             'meta_value' => $hash
    164         ) );
     164        $hash   = md5( $metadata['image']['data'] );
     165        $posts  = get_posts(
     166            array(
     167                'fields'         => 'ids',
     168                'post_type'      => 'attachment',
     169                'post_mime_type' => $metadata['image']['mime'],
     170                'post_status'    => 'inherit',
     171                'posts_per_page' => 1,
     172                'meta_key'       => '_cover_hash',
     173                'meta_value'     => $hash,
     174            )
     175        );
    165176        $exists = reset( $posts );
    166177
     
    170181            $ext = '.jpg';
    171182            switch ( $metadata['image']['mime'] ) {
    172             case 'image/gif':
    173                 $ext = '.gif';
    174                 break;
    175             case 'image/png':
    176                 $ext = '.png';
    177                 break;
     183                case 'image/gif':
     184                    $ext = '.gif';
     185                    break;
     186                case 'image/png':
     187                    $ext = '.png';
     188                    break;
    178189            }
    179190            $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
     
    182193                $image_attachment = array(
    183194                    'post_mime_type' => $metadata['image']['mime'],
    184                     'post_type' => 'attachment',
    185                     'post_content' => '',
     195                    'post_type'      => 'attachment',
     196                    'post_content'   => '',
    186197                );
    187198                /**
     
    203214            }
    204215        }
    205     }
    206     // Try to create image thumbnails for PDFs
    207     else if ( 'application/pdf' === $mime_type ) {
     216    } // Try to create image thumbnails for PDFs
     217    elseif ( 'application/pdf' === $mime_type ) {
    208218        $fallback_sizes = array(
    209219            'thumbnail',
     
    222232        $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
    223233
    224         $sizes = array();
     234        $sizes                      = array();
    225235        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    226236
     
    257267                 * Ensure the PDF preview image does not overwrite any JPEG images that already exist.
    258268                 */
    259                 $dirname = dirname( $file ) . '/';
    260                 $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION );
     269                $dirname      = dirname( $file ) . '/';
     270                $ext          = '.' . pathinfo( $file, PATHINFO_EXTENSION );
    261271                $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' );
    262272
     
    270280
    271281                    if ( ! is_wp_error( $editor ) ) {
    272                         $metadata['sizes'] = $editor->multi_resize( $sizes );
     282                        $metadata['sizes']         = $editor->multi_resize( $sizes );
    273283                        $metadata['sizes']['full'] = $uploaded;
    274284                    }
     
    302312 * @return int|float
    303313 */
    304 function wp_exif_frac2dec($str) {
     314function wp_exif_frac2dec( $str ) {
    305315    @list( $n, $d ) = explode( '/', $str );
    306     if ( !empty($d) )
     316    if ( ! empty( $d ) ) {
    307317        return $n / $d;
     318    }
    308319    return $str;
    309320}
     
    317328 * @return int
    318329 */
    319 function wp_exif_date2ts($str) {
    320     @list( $date, $time ) = explode( ' ', trim($str) );
    321     @list( $y, $m, $d ) = explode( ':', $date );
     330function wp_exif_date2ts( $str ) {
     331    @list( $date, $time ) = explode( ' ', trim( $str ) );
     332    @list( $y, $m, $d )   = explode( ':', $date );
    322333
    323334    return strtotime( "{$y}-{$m}-{$d} {$time}" );
     
    341352 */
    342353function wp_read_image_metadata( $file ) {
    343     if ( ! file_exists( $file ) )
     354    if ( ! file_exists( $file ) ) {
    344355        return false;
     356    }
    345357
    346358    list( , , $sourceImageType ) = getimagesize( $file );
     
    353365     */
    354366    $meta = array(
    355         'aperture' => 0,
    356         'credit' => '',
    357         'camera' => '',
    358         'caption' => '',
     367        'aperture'          => 0,
     368        'credit'            => '',
     369        'camera'            => '',
     370        'caption'           => '',
    359371        'created_timestamp' => 0,
    360         'copyright' => '',
    361         'focal_length' => 0,
    362         'iso' => 0,
    363         'shutter_speed' => 0,
    364         'title' => '',
    365         'orientation' => 0,
    366         'keywords' => array(),
     372        'copyright'         => '',
     373        'focal_length'      => 0,
     374        'iso'               => 0,
     375        'shutter_speed'     => 0,
     376        'title'             => '',
     377        'orientation'       => 0,
     378        'keywords'          => array(),
    367379    );
    368380
     
    381393            if ( ! empty( $iptc['2#105'][0] ) ) {
    382394                $meta['title'] = trim( $iptc['2#105'][0] );
    383             /*
    384              * Title, "Many use the Title field to store the filename of the image,
    385              * though the field may be used in many ways."
    386              */
     395                /*
     396                * Title, "Many use the Title field to store the filename of the image,
     397                * though the field may be used in many ways."
     398                */
    387399            } elseif ( ! empty( $iptc['2#005'][0] ) ) {
    388400                $meta['title'] = trim( $iptc['2#005'][0] );
     
    404416            }
    405417
    406             if ( ! empty( $iptc['2#110'][0] ) ) // credit
     418            if ( ! empty( $iptc['2#110'][0] ) ) { // credit
    407419                $meta['credit'] = trim( $iptc['2#110'][0] );
    408             elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
     420            } elseif ( ! empty( $iptc['2#080'][0] ) ) { // creator / legacy byline
    409421                $meta['credit'] = trim( $iptc['2#080'][0] );
    410 
    411             if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time
     422            }
     423
     424            if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) { // created date and time
    412425                $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
    413 
    414             if ( ! empty( $iptc['2#116'][0] ) ) // copyright
     426            }
     427
     428            if ( ! empty( $iptc['2#116'][0] ) ) { // copyright
    415429                $meta['copyright'] = trim( $iptc['2#116'][0] );
     430            }
    416431
    417432            if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array
     
    455470            if ( ! empty( $exif['Artist'] ) ) {
    456471                $meta['credit'] = trim( $exif['Artist'] );
    457             } elseif ( ! empty($exif['Author'] ) ) {
     472            } elseif ( ! empty( $exif['Author'] ) ) {
    458473                $meta['credit'] = trim( $exif['Author'] );
    459474            }
     
    524539 * @return bool True if valid image, false if not valid image.
    525540 */
    526 function file_is_valid_image($path) {
    527     $size = @getimagesize($path);
    528     return !empty($size);
     541function file_is_valid_image( $path ) {
     542    $size = @getimagesize( $path );
     543    return ! empty( $size );
    529544}
    530545
     
    537552 * @return bool True if suitable, false if not suitable.
    538553 */
    539 function file_is_displayable_image($path) {
     554function file_is_displayable_image( $path ) {
    540555    $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP );
    541556
     
    572587function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
    573588    $filepath = _load_image_to_edit_path( $attachment_id, $size );
    574     if ( empty( $filepath ) )
     589    if ( empty( $filepath ) ) {
    575590        return false;
     591    }
    576592
    577593    switch ( $mime_type ) {
    578594        case 'image/jpeg':
    579             $image = imagecreatefromjpeg($filepath);
     595            $image = imagecreatefromjpeg( $filepath );
    580596            break;
    581597        case 'image/png':
    582             $image = imagecreatefrompng($filepath);
     598            $image = imagecreatefrompng( $filepath );
    583599            break;
    584600        case 'image/gif':
    585             $image = imagecreatefromgif($filepath);
     601            $image = imagecreatefromgif( $filepath );
    586602            break;
    587603        default:
     
    589605            break;
    590606    }
    591     if ( is_resource($image) ) {
     607    if ( is_resource( $image ) ) {
    592608        /**
    593609         * Filters the current image being loaded for editing.
     
    600616         */
    601617        $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
    602         if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
    603             imagealphablending($image, false);
    604             imagesavealpha($image, true);
     618        if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
     619            imagealphablending( $image, false );
     620            imagesavealpha( $image, true );
    605621        }
    606622    }
     
    677693function _copy_image_file( $attachment_id ) {
    678694    $dst_file = $src_file = get_attached_file( $attachment_id );
    679     if ( ! file_exists( $src_file ) )
     695    if ( ! file_exists( $src_file ) ) {
    680696        $src_file = _load_image_to_edit_path( $attachment_id );
     697    }
    681698
    682699    if ( $src_file ) {
     
    690707        wp_mkdir_p( dirname( $dst_file ) );
    691708
    692         if ( ! @copy( $src_file, $dst_file ) )
     709        if ( ! @copy( $src_file, $dst_file ) ) {
    693710            $dst_file = false;
     711        }
    694712    } else {
    695713        $dst_file = false;
Note: See TracChangeset for help on using the changeset viewer.