WordPress.org

Make WordPress Core

Ticket #22356: 22356.diff

File 22356.diff, 6.7 KB (added by DH-Shredder, 17 months ago)

Introduce WP_Image_Editor::supports(); Harden supports_mime_type in Imagick and GD; Check mime while choosing implementation; DOC Fixes. (created in collaboration with markoheijnen)

  • wp-admin/includes/media.php

    diff --git wp-admin/includes/media.php wp-admin/includes/media.php
    index 478dfdd..3aeda34 100644
    function get_media_item( $attachment_id, $args = null ) { 
    11221122        $media_dims = apply_filters( 'media_meta', $media_dims, $post ); 
    11231123 
    11241124        $image_edit_button = ''; 
    1125         if ( gd_edit_image_support( $post->post_mime_type ) ) { 
     1125        if ( WP_Image_Editor::supports( $post->post_mime_type ) ) { 
    11261126                $nonce = wp_create_nonce( "image_editor-$post->ID" ); 
    11271127                $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>"; 
    11281128        } 
    function edit_form_image_editor() { 
    22592259        $att_url = wp_get_attachment_url( $post->ID ); 
    22602260 
    22612261        $image_edit_button = ''; 
    2262         if ( gd_edit_image_support( $post->post_mime_type ) ) { 
     2262        if ( WP_Image_Editor::supports( $post->post_mime_type ) ) { 
    22632263                $nonce = wp_create_nonce( "image_editor-$post->ID" ); 
    22642264                $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>"; 
    22652265        } 
  • wp-includes/class-wp-image-editor-gd.php

    diff --git wp-includes/class-wp-image-editor-gd.php wp-includes/class-wp-image-editor-gd.php
    index ca76006..c52735a 100644
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    100100         * @return boolean 
    101101         */ 
    102102        public static function supports_mime_type( $mime_type ) { 
    103                 $allowed_mime_types = array( 'image/gif', 'image/png', 'image/jpeg' ); 
     103                $image_types = imagetypes(); 
     104                switch( $mime_type ) { 
     105                        case 'image/jpeg': 
     106                                return ($image_types & IMG_JPG) != 0; 
     107                        case 'image/png': 
     108                                return ($image_types & IMG_PNG) != 0; 
     109                        case 'image/gif': 
     110                                return ($image_types & IMG_GIF) != 0; 
     111                } 
    104112 
    105                 return in_array( $mime_type, $allowed_mime_types ); 
     113                return false; 
    106114        } 
    107115 
    108116        /** 
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    261269         * @since 3.5.0 
    262270         * @access public 
    263271         * 
    264          * @param boolean $horz Horizonal Flip 
     272         * @param boolean $horz Horizontal Flip 
    265273         * @param boolean $vert Vertical Flip 
    266274         * @returns boolean|WP_Error 
    267275         */ 
  • wp-includes/class-wp-image-editor-imagick.php

    diff --git wp-includes/class-wp-image-editor-imagick.php wp-includes/class-wp-image-editor-imagick.php
    index 601b99b..0edcde0 100644
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    147147         * @return boolean 
    148148         */ 
    149149        public static function supports_mime_type( $mime_type ) { 
    150                 if ( ! $mime_type ) 
    151                         return false; 
    152  
    153150                $imagick_extension = strtoupper( self::get_extension( $mime_type ) ); 
    154151 
     152                if ( ! $imagick_extension ) 
     153                        return false; 
     154 
    155155                try { 
    156156                        return ( (bool) Imagick::queryFormats( $imagick_extension ) ); 
    157157                } 
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    312312         * @since 3.5.0 
    313313         * @access public 
    314314         * 
    315          * @param boolean $horz Horizonal Flip 
     315         * @param boolean $horz Horizontal Flip 
    316316         * @param boolean $vert Vertical Flip 
    317317         * @returns boolean|WP_Error 
    318318         */ 
  • wp-includes/class-wp-image-editor.php

    diff --git wp-includes/class-wp-image-editor.php wp-includes/class-wp-image-editor.php
    index 920e4a4..83b214f 100644
    abstract class WP_Image_Editor { 
    3232         * @param array $required_methods Methods to require in implementation 
    3333         * @return WP_Image_Editor|WP_Error 
    3434         */ 
    35         public final static function get_instance( $path = null, $required_methods = null ) { 
    36                 $implementation = apply_filters( 'wp_image_editor_class', self::choose_implementation( $required_methods ), $path ); 
     35        public final static function get_instance( $path = null, $required_methods = array() ) { 
     36                $implementation = apply_filters( 'wp_image_editor_class', 
     37                        self::choose_implementation( 
     38                                $required_methods, 
     39                                array( 'path' => $path ) 
     40                        ) 
     41                ); 
    3742 
    3843                if ( $implementation ) { 
    3944                        $editor = new $implementation( $path ); 
    abstract class WP_Image_Editor { 
    5762         * @param array $required_methods String array of all methods required for implementation returned. 
    5863         * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request. 
    5964         */ 
    60         private final static function choose_implementation( $required_methods = null ) { 
     65        private final static function choose_implementation( $required_methods = array(), $args = array() ) { 
    6166                $request_order = apply_filters( 'wp_image_editors', 
    6267                        array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) ); 
    6368 
    64                 if ( ! $required_methods ) 
    65                         $required_methods = array(); 
     69                if ( ! isset( $args['mime_type'] ) && isset( $args['path'] ) ) { 
     70                        $file_info  = wp_check_filetype( $args['path'] ); 
     71 
     72                        // If $file_info['type'] is false, then we let the editor attempt to 
     73                        // figure out the file type, rather than forcing a failure based on extension. 
     74                        if ( isset( $file_info ) && $file_info['type'] ) 
     75                                $args['mime_type'] = $file_info['type']; 
     76                } 
    6677 
    6778                // Loop over each editor on each request looking for one which will serve this request's needs 
    6879                foreach ( $request_order as $editor ) { 
    6980                        // Check to see if this editor is a possibility, calls the editor statically 
    70                         if ( ! call_user_func( array( $editor, 'test' ) ) ) 
     81                        if ( ! call_user_func( array( $editor, 'test' ), $args ) ) 
     82                                continue; 
     83 
     84                        if ( isset( $args['mime_type'] ) && 
     85                                ! call_user_func( 
     86                                        array( $editor, 'supports_mime_type' ), 
     87                                        $args['mime_type'] ) ) { 
    7188                                continue; 
     89                        } 
    7290 
    7391                        // Make sure that all methods are supported by editor. 
    7492                        if ( array_diff( $required_methods, get_class_methods( $editor ) ) ) 
    abstract class WP_Image_Editor { 
    7694 
    7795                        return $editor; 
    7896                } 
     97 
    7998                return false; 
    8099        } 
    81100 
    82101        /** 
     102         * Tests whether there is an editor that supports a given mime type or methods. 
     103         * 
     104         * @since 3.5.0 
     105         * @access public 
     106         * 
     107         * @param string $mime_type Mime type to check for compatibility 
     108         * @param array $required_methods String array of all methods required 
     109         * @return boolean true if an eligible editor is found; false otherwise 
     110         */ 
     111        public final static function supports( $mime_type = null, $required_methods = array() ) { 
     112                return ( (bool) self::choose_implementation( $required_methods, array( 'mime_type' => $mime_type ) ) ); 
     113        } 
     114 
     115        /** 
    83116         * Loads image from $this->file into editor. 
    84117         * 
    85118         * @since 3.5.0 
    abstract class WP_Image_Editor { 
    168201         * @access public 
    169202         * @abstract 
    170203         * 
    171          * @param boolean $horz Horizonal Flip 
     204         * @param boolean $horz Horizontal Flip 
    172205         * @param boolean $vert Vertical Flip 
    173206         * @return boolean|WP_Error 
    174207         */