WordPress.org

Make WordPress Core

Ticket #6821: 6821.10.diff

File 6821.10.diff, 8.8 KB (added by markoheijnen, 7 years ago)

No caching and always run test

  • wp-admin/includes/image-edit.php

    diff --git wp-admin/includes/image-edit.php wp-admin/includes/image-edit.php
    index 0b06926..a1e429f 100644
    function wp_stream_image( $image, $mime_type, $post_id ) { 
    237237
    238238/**
    239239 * Saves Image to File
    240  * @TODO: Add mime_type support to WP_Image_Editor
    241240 *
    242241 * @param string $filename
    243242 * @param WP_Image_Editor $image
  • 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 9248c7e..dcd1644 100644
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    2828         * Checks to see if current environment supports GD
    2929         *
    3030         * @since 3.5.0
    31          * @access protected
     31         * @access public
    3232         *
    3333         * @return boolean
    3434         */
    35         public function test() {
     35        public static function test( $args = null ) {
    3636                if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
    3737                        return false;
    3838
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    5151                if ( $this->image )
    5252                        return true;
    5353
    54                 if ( ! file_exists( $this->file ) )
     54                if ( ! is_file( $this->file ) )
    5555                        return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
    5656
    5757                // Set artificially high because GD uses uncompressed images in memory
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    9999         * @param string $mime_type
    100100         * @return boolean
    101101         */
    102         public function supports_mime_type( $mime_type ) {
     102        public static function supports_mime_type( $mime_type ) {
    103103                $allowed_mime_types = array( 'image/gif', 'image/png', 'image/jpeg' );
    104104
    105105                return in_array( $mime_type, $allowed_mime_types );
  • 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 9d2c822..c3372d3 100644
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    2929         * Checks to see if current environment supports Imagick
    3030         *
    3131         * @since 3.5.0
    32          * @access protected
     32         * @access public
    3333         *
    3434         * @return boolean
    3535         */
    36         public function test() {
     36        public static function test( $args = null ) {
    3737                if ( ! extension_loaded( 'imagick' ) )
    3838                        return false;
    3939
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    5252                if ( $this->image )
    5353                        return true;
    5454
    55                 if ( ! file_exists( $this->file ) )
     55                if ( ! is_file( $this->file ) )
    5656                        return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
    5757
    5858                try {
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    143143         * @param string $mime_type
    144144         * @return boolean
    145145         */
    146         public function supports_mime_type( $mime_type ) {
     146        public static function supports_mime_type( $mime_type ) {
    147147                if ( ! $mime_type )
    148148                        return false;
    149149
  • wp-includes/class-wp-image-editor.php

    diff --git wp-includes/class-wp-image-editor.php wp-includes/class-wp-image-editor.php
    index 6ee94a2..05665b6 100644
    abstract class WP_Image_Editor { 
    1717        protected $mime_type  = null;
    1818        protected $default_mime_type = 'image/jpeg';
    1919        protected $quality = 90;
    20         private static $implementation;
    2120
    2221        protected function __construct( $filename ) {
    2322                $this->file = $filename;
    abstract class WP_Image_Editor { 
    3029         * @access public
    3130         *
    3231         * @param string $path Path to File to Load
     32         * @param array $required_methods Methods to require in implementation
    3333         * @return WP_Image_Editor|WP_Error|boolean
    3434         */
    35         public final static function get_instance( $path = null ) {
    36                 $implementation = apply_filters( 'image_editor_class', self::choose_implementation(), $path );
     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 );
    3737
    3838                if ( $implementation ) {
    3939                        $editor = new $implementation( $path );
    4040                        $loaded = $editor->load();
    4141
    42                         if ( is_wp_error ( $loaded ) )
     42                        if ( is_wp_error( $loaded ) )
    4343                                return $loaded;
    4444
    4545                        return $editor;
    abstract class WP_Image_Editor { 
    5454         * @since 3.5.0
    5555         * @access private
    5656         *
     57         * @param $required_methods Array String array of all methods required for implementation returned.
     58         *
    5759         * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
    5860         */
    59         private final static function choose_implementation() {
     61        private final static function choose_implementation( $required_methods = null ) {
     62                $request_order = apply_filters( 'wp_image_editors',
     63                        array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
    6064
    61                 if ( null === self::$implementation ) {
    62                         $request_order = apply_filters( 'wp_editors', array( 'imagick', 'gd' ) );
     65                if ( ! $required_methods )
     66                        $required_methods = apply_filters( 'wp_image_editor_default_methods',
     67                                array( 'resize', 'multi_resize', 'crop', 'rotate', 'flip', 'stream' ) );
    6368
    64                         // Loop over each editor on each request looking for one which will serve this request's needs
    65                         foreach ( $request_order as $editor ) {
    66                                 $class = 'WP_Image_Editor_' . $editor;
     69                // Loop over each editor on each request looking for one which will serve this request's needs
     70                foreach ( $request_order as $editor ) {
     71                        // Check to see if this editor is a possibility, calls the editor statically
     72                        if ( ! call_user_func( array( $editor, 'test' ) ) )
     73                                continue;
    6774
    68                                 // Check to see if this editor is a possibility, calls the editor statically
    69                                 if ( ! call_user_func( array( $class, 'test' ) ) )
    70                                         continue;
     75                        // Make sure that all methods are supported by editor.
     76                        if ( array_diff( $required_methods, get_class_methods( $editor ) ) )
     77                                continue;
    7178
    72                                 self::$implementation = $class;
    73                                 break;
    74                         }
     79                        return $editor;
    7580                }
    76                 return self::$implementation;
     81                return false;
    7782        }
    7883
    79         abstract public function test(); // returns bool
    8084        abstract protected function load(); // returns bool|WP_Error
    81         abstract public function supports_mime_type( $mime_type ); // returns bool
    82         abstract public function resize( $max_w, $max_h, $crop = false );
    83         abstract public function multi_resize( $sizes );
    84         abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
    85         abstract public function rotate( $angle );
    86         abstract public function flip( $horz, $vert );
    8785        abstract public function save( $destfilename = null, $mime_type = null );
    88         abstract public function stream( $mime_type = null );
     86
     87        /**
     88         * Implement all of the below to support natively used functions:
     89         *
     90         * public function resize( $max_w, $max_h, $crop = false )
     91         * public function multi_resize( $sizes )
     92         * public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false )
     93         * public function rotate( $angle )
     94         * public function flip( $horz, $vert )
     95         * public function stream( $mime_type = null )
     96         */
     97
     98        /**
     99         * Checks to see if current environment supports the editor chosen.
     100         * Must be overridden in a sub-class.
     101         *
     102         * @since 3.5.0
     103         * @access public
     104         * @abstract
     105         *
     106         * @param $args array
     107         * @return boolean
     108         */
     109        public static function test( $args = null ) {
     110                return false;
     111        }
     112
     113        /**
     114         * Checks to see if editor supports mime-type specified
     115         * Must be overridden in a sub-class.
     116         *
     117         * @since 3.5.0
     118         * @access public
     119         * @abstract
     120         *
     121         * @param string $mime_type
     122         * @return boolean
     123         */
     124        public static function supports_mime_type( $mime_type ) {
     125                return false;
     126        }
    89127
    90128        /**
    91129         * Gets dimensions of image
    abstract class WP_Image_Editor { 
    258296
    259297                $result = call_user_func_array( $function, $arguments );
    260298
    261                 if( $result && $stream ) {
     299                if ( $result && $stream ) {
    262300                        $contents = ob_get_contents();
    263301
    264302                        $fp = fopen( $dst_file, 'w' );
    265303
    266                         if( ! $fp )
     304                        if ( ! $fp )
    267305                                return false;
    268306
    269307                        fwrite( $fp, $contents );
    270308                        fclose( $fp );
    271309                }
    272310
    273                 if( $stream ) {
     311                if ( $stream ) {
    274312                        ob_end_clean();
    275313                }
    276314
    abstract class WP_Image_Editor { 
    295333                $extensions = array_keys( $mime_types );
    296334
    297335                foreach( $extensions as $_extension ) {
    298                         if( preg_match("/{$extension}/i", $_extension ) ) {
    299                                 return $mime_types[ $_extension ];
     336                        if ( preg_match( "/{$extension}/i", $_extension ) ) {
     337                                return $mime_types[$_extension];
    300338                        }
    301339                }
    302340
  • wp-includes/deprecated.php

    diff --git wp-includes/deprecated.php wp-includes/deprecated.php
    index a9744e8..101bc10 100644
    function wp_load_image( $file ) { 
    32213221        if ( is_numeric( $file ) )
    32223222                $file = get_attached_file( $file );
    32233223
    3224         if ( ! file_exists( $file ) )
     3224        if ( ! is_file( $file ) )
    32253225                return sprintf(__('File “%s” doesn’t exist?'), $file);
    32263226
    32273227        if ( ! function_exists('imagecreatefromstring') )