Make WordPress Core

Ticket #22356: 22356.8.diff

File 22356.8.diff, 18.2 KB (added by scribu, 11 years ago)

wp_get_image_editor()

  • wp-admin/includes/media.php

    diff --git wp-admin/includes/media.php wp-admin/includes/media.php
    index 6cc1b63..4b921b1 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( array( 'mime_type' => $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() { 
    22542254        $att_url = wp_get_attachment_url( $post->ID );
    22552255
    22562256        $image_edit_button = '';
    2257         if ( gd_edit_image_support( $post->post_mime_type ) ) {
     2257        if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
    22582258                $nonce = wp_create_nonce( "image_editor-$post->ID" );
    22592259                $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>";
    22602260        }
  • 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..78e186e 100644
     
    1212 * @since 3.5.0
    1313 * @package WordPress
    1414 * @subpackage Image_Editor
    15  * @uses WP_Image_Editor Extends class
    1615 */
    1716class WP_Image_Editor_GD extends WP_Image_Editor {
     17
    1818        protected $image = false; // GD Resource
    1919
    2020        function __destruct() {
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    3232         *
    3333         * @return boolean
    3434         */
    35         public static function test( $args = null ) {
     35        public static function test( $args = array() ) {
    3636                if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
    3737                        return false;
    3838
     39                if ( isset( $args['mime_type'] ) && !self::supports_mime_type( $args['mime_type'] ) )
     40                        return false;
     41
    3942                return true;
    4043        }
    4144
    4245        /**
     46         * Checks to see if editor supports the mime-type specified.
     47         *
     48         * @since 3.5.0
     49         * @access public
     50         *
     51         * @param string $mime_type
     52         * @return boolean
     53         */
     54        protected static function supports_mime_type( $mime_type ) {
     55                $image_types = imagetypes();
     56                switch( $mime_type ) {
     57                        case 'image/jpeg':
     58                                return ($image_types & IMG_JPG) != 0;
     59                        case 'image/png':
     60                                return ($image_types & IMG_PNG) != 0;
     61                        case 'image/gif':
     62                                return ($image_types & IMG_GIF) != 0;
     63                }
     64
     65                return false;
     66        }
     67
     68        /**
    4369         * Loads image from $this->file into new GD Resource.
    4470         *
    4571         * @since 3.5.0
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    4773         *
    4874         * @return boolean|\WP_Error
    4975         */
    50         protected function load() {
     76        public function load() {
    5177                if ( $this->image )
    5278                        return true;
    5379
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    91117        }
    92118
    93119        /**
    94          * Checks to see if editor supports the mime-type specified.
    95          *
    96          * @since 3.5.0
    97          * @access public
    98          *
    99          * @param string $mime_type
    100          * @return boolean
    101          */
    102         public static function supports_mime_type( $mime_type ) {
    103                 $allowed_mime_types = array( 'image/gif', 'image/png', 'image/jpeg' );
    104 
    105                 return in_array( $mime_type, $allowed_mime_types );
    106         }
    107 
    108         /**
    109120         * Resizes current image.
    110121         * Wraps _resize, since _resize returns a GD Resource.
    111122         *
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    261272         * @since 3.5.0
    262273         * @access public
    263274         *
    264          * @param boolean $horz Horizonal Flip
     275         * @param boolean $horz Horizontal Flip
    265276         * @param boolean $vert Vertical Flip
    266277         * @returns boolean|WP_Error
    267278         */
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    369380                                return imagejpeg( $this->image, null, $this->quality );
    370381                }
    371382        }
    372 }
    373  No newline at end of file
     383}
  • 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..8f6feef 100644
     
    1212 * @since 3.5.0
    1313 * @package WordPress
    1414 * @subpackage Image_Editor
    15  * @uses WP_Image_Editor Extends class
    1615 */
    1716class WP_Image_Editor_Imagick extends WP_Image_Editor {
     17
    1818        protected $image = null; // Imagick Object
    1919
    2020        function __destruct() {
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    3636         *
    3737         * @return boolean
    3838         */
    39         public static function test( $args = null ) {
     39        public static function test( $args = array() ) {
    4040                if ( ! extension_loaded( 'imagick' ) || ! is_callable( 'Imagick', 'queryFormats' ) )
    4141                        return false;
    4242
     43                if ( isset( $args['mime_type'] ) && !self::supports_mime_type( $args['mime_type'] ) )
     44                        return false;
     45
    4346                return true;
    4447        }
    4548
    4649        /**
     50         * Checks to see if editor supports the mime-type specified.
     51         *
     52         * @since 3.5.0
     53         * @access public
     54         *
     55         * @param string $mime_type
     56         * @return boolean
     57         */
     58        protected static function supports_mime_type( $mime_type ) {
     59                $imagick_extension = strtoupper( self::get_extension( $mime_type ) );
     60
     61                if ( ! $imagick_extension )
     62                        return false;
     63
     64                try {
     65                        return ( (bool) Imagick::queryFormats( $imagick_extension ) );
     66                }
     67                catch ( Exception $e ) {
     68                        return false;
     69                }
     70        }
     71
     72        /**
    4773         * Loads image from $this->file into new Imagick Object.
    4874         *
    4975         * @since 3.5.0
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    5177         *
    5278         * @return boolean|WP_Error True if loaded; WP_Error on failure.
    5379         */
    54         protected function load() {
     80        public function load() {
    5581                if ( $this->image )
    5682                        return true;
    5783
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    138164        }
    139165
    140166        /**
    141          * Checks to see if editor supports the mime-type specified.
    142          *
    143          * @since 3.5.0
    144          * @access public
    145          *
    146          * @param string $mime_type
    147          * @return boolean
    148          */
    149         public static function supports_mime_type( $mime_type ) {
    150                 if ( ! $mime_type )
    151                         return false;
    152 
    153                 $imagick_extension = strtoupper( self::get_extension( $mime_type ) );
    154 
    155                 try {
    156                         return ( (bool) Imagick::queryFormats( $imagick_extension ) );
    157                 }
    158                 catch ( Exception $e ) {
    159                         return false;
    160                 }
    161         }
    162 
    163         /**
    164167         * Resizes current image.
    165168         *
    166169         * @since 3.5.0
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    312315         * @since 3.5.0
    313316         * @access public
    314317         *
    315          * @param boolean $horz Horizonal Flip
     318         * @param boolean $horz Horizontal Flip
    316319         * @param boolean $vert Vertical Flip
    317320         * @returns boolean|WP_Error
    318321         */
  • 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..695d02a 100644
     
    77 */
    88
    99/**
    10  * Base WordPress Image Editor class for which Editor implementations extend
     10 * Base image editor class from which implementations extend
    1111 *
    1212 * @since 3.5.0
    1313 */
    1414abstract class WP_Image_Editor {
     15
    1516        protected $file = null;
    1617        protected $size = null;
    17         protected $mime_type  = null;
     18        protected $mime_type = null;
    1819        protected $default_mime_type = 'image/jpeg';
    1920        protected $quality = 90;
    2021
    21         protected function __construct( $filename ) {
    22                 $this->file = $filename;
    23         }
    24 
    2522        /**
    26          * Returns a WP_Image_Editor instance and loads file into it.
     23         * Checks to see if current environment supports the editor chosen.
     24         * Must be overridden in a sub-class.
    2725         *
    2826         * @since 3.5.0
    2927         * @access public
     28         * @abstract
    3029         *
    31          * @param string $path Path to File to Load
    32          * @param array $required_methods Methods to require in implementation
    33          * @return WP_Image_Editor|WP_Error
     30         * @param array $args
     31         * @return boolean
    3432         */
    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 );
    37 
    38                 if ( $implementation ) {
    39                         $editor = new $implementation( $path );
    40                         $loaded = $editor->load();
    41 
    42                         if ( is_wp_error( $loaded ) )
    43                                 return $loaded;
    44 
    45                         return $editor;
    46                 }
    47 
    48                 return new WP_Error( 'no_editor', __('No editor could be selected') );
     33        public static function test( $args = array() ) {
     34                return false;
    4935        }
    5036
    5137        /**
    52          * Tests which editors are capable of supporting the request.
    53          *
    54          * @since 3.5.0
    55          * @access private
    56          *
    57          * @param array $required_methods String array of all methods required for implementation returned.
    58          * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
     38         * Each instance handles a single file.
    5939         */
    60         private final static function choose_implementation( $required_methods = null ) {
    61                 $request_order = apply_filters( 'wp_image_editors',
    62                         array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
    63 
    64                 if ( ! $required_methods )
    65                         $required_methods = array();
    66 
    67                 // Loop over each editor on each request looking for one which will serve this request's needs
    68                 foreach ( $request_order as $editor ) {
    69                         // Check to see if this editor is a possibility, calls the editor statically
    70                         if ( ! call_user_func( array( $editor, 'test' ) ) )
    71                                 continue;
    72 
    73                         // Make sure that all methods are supported by editor.
    74                         if ( array_diff( $required_methods, get_class_methods( $editor ) ) )
    75                                 continue;
    76 
    77                         return $editor;
    78                 }
    79                 return false;
     40        public function __construct( $file ) {
     41                $this->file = $file;
    8042        }
    8143
    8244        /**
    abstract class WP_Image_Editor { 
    8850         *
    8951         * @return boolean|WP_Error True if loaded; WP_Error on failure.
    9052         */
    91         abstract protected function load();
     53        abstract public function load();
    9254
    9355        /**
    9456         * Saves current image to file.
    abstract class WP_Image_Editor { 
    168130         * @access public
    169131         * @abstract
    170132         *
    171          * @param boolean $horz Horizonal Flip
     133         * @param boolean $horz Horizontal Flip
    172134         * @param boolean $vert Vertical Flip
    173135         * @return boolean|WP_Error
    174136         */
    abstract class WP_Image_Editor { 
    187149        abstract public function stream( $mime_type = null );
    188150
    189151        /**
    190          * Checks to see if current environment supports the editor chosen.
    191          * Must be overridden in a sub-class.
    192          *
    193          * @since 3.5.0
    194          * @access public
    195          * @abstract
    196          *
    197          * @param array $args
    198          * @return boolean
    199          */
    200         public static function test( $args = null ) {
    201                 return false;
    202         }
    203 
    204         /**
    205          * Checks to see if editor supports the mime-type specified.
    206          * Must be overridden in a sub-class.
    207          *
    208          * @since 3.5.0
    209          * @access public
    210          * @abstract
    211          *
    212          * @param string $mime_type
    213          * @return boolean
    214          */
    215         public static function supports_mime_type( $mime_type ) {
    216                 return false;
    217         }
    218 
    219         /**
    220152         * Gets dimensions of image.
    221153         *
    222154         * @since 3.5.0
    abstract class WP_Image_Editor { 
    451383                return $extensions[0];
    452384        }
    453385}
     386
  • wp-includes/deprecated.php

    diff --git wp-includes/deprecated.php wp-includes/deprecated.php
    index 9e99614..6751458 100644
    function _get_post_ancestors( &$post ) { 
    32103210 *
    32113211 * @since 2.1.0
    32123212 * @deprecated 3.5.0
    3213  * @see WP_Image_Editor
     3213 * see wp_get_image_editor()
    32143214 *
    32153215 * @param string $file Filename of the image to load.
    32163216 * @return resource The resulting image resource on success, Error string on failure.
    32173217 */
    32183218function wp_load_image( $file ) {
    3219         _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' );
     3219        _deprecated_function( __FUNCTION__, '3.5', 'wp_get_image_editor()' );
    32203220
    32213221        if ( is_numeric( $file ) )
    32223222                $file = get_attached_file( $file );
    function wp_load_image( $file ) { 
    32503250 *
    32513251 * @since 2.5.0
    32523252 * @deprecated 3.5.0
    3253  * @see WP_Image_Editor
     3253 * see wp_get_image_editor()
    32543254 *
    32553255 * @param string $file Image file path.
    32563256 * @param int $max_w Maximum width to resize to.
    function wp_load_image( $file ) { 
    32623262 * @return mixed WP_Error on failure. String with new destination path.
    32633263 */
    32643264function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
    3265         _deprecated_function( __FUNCTION__, '3.5', 'WP_Image_Editor' );
     3265        _deprecated_function( __FUNCTION__, '3.5', 'wp_get_image_editor()' );
    32663266
    3267         $editor = WP_Image_Editor::get_instance( $file );
     3267        $editor = wp_get_image_editor( $file );
    32683268        if ( is_wp_error( $editor ) )
    32693269                return $editor;
    32703270        $editor->set_quality( $jpeg_quality );
    function user_pass_ok($user_login, $user_pass) { 
    33283328 * @since 2.3.0
    33293329 * @deprecated 3.5.0
    33303330 */
    3331 function _save_post_hook() {}
    3332  No newline at end of file
     3331function _save_post_hook() {}
     3332
     3333/**
     3334 * Check if the installed version of GD supports particular image type
     3335 *
     3336 * @since 2.9.0
     3337 * @deprecated 3.5.0
     3338 * see wp_image_editor_supports()
     3339 *
     3340 * @param string $mime_type
     3341 * @return bool
     3342 */
     3343function gd_edit_image_support($mime_type) {
     3344        _deprecated_function( __FUNCTION__, '3.5', 'wp_image_editor_supports()' );
     3345
     3346        if ( function_exists('imagetypes') ) {
     3347                switch( $mime_type ) {
     3348                        case 'image/jpeg':
     3349                                return (imagetypes() & IMG_JPG) != 0;
     3350                        case 'image/png':
     3351                                return (imagetypes() & IMG_PNG) != 0;
     3352                        case 'image/gif':
     3353                                return (imagetypes() & IMG_GIF) != 0;
     3354                }
     3355        } else {
     3356                switch( $mime_type ) {
     3357                        case 'image/jpeg':
     3358                                return function_exists('imagecreatefromjpeg');
     3359                        case 'image/png':
     3360                                return function_exists('imagecreatefrompng');
     3361                        case 'image/gif':
     3362                                return function_exists('imagecreatefromgif');
     3363                }
     3364        }
     3365        return false;
     3366}
  • wp-includes/media.php

    diff --git wp-includes/media.php wp-includes/media.php
    index d8475f1..3a1268f 100644
    function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal 
    383383 */
    384384function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
    385385        if ( $width || $height ) {
    386                 $editor = WP_Image_Editor::get_instance( $file );
     386                $editor = wp_get_image_editor( $file );
    387387
    388388                if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
    389389                        return false;
    function get_taxonomies_for_attachments( $output = 'names' ) { 
    904904}
    905905
    906906/**
    907  * Check if the installed version of GD supports particular image type
    908  *
    909  * @since 2.9.0
    910  *
    911  * @param string $mime_type
    912  * @return bool
    913  */
    914 function gd_edit_image_support($mime_type) {
    915         if ( function_exists('imagetypes') ) {
    916                 switch( $mime_type ) {
    917                         case 'image/jpeg':
    918                                 return (imagetypes() & IMG_JPG) != 0;
    919                         case 'image/png':
    920                                 return (imagetypes() & IMG_PNG) != 0;
    921                         case 'image/gif':
    922                                 return (imagetypes() & IMG_GIF) != 0;
    923                 }
    924         } else {
    925                 switch( $mime_type ) {
    926                         case 'image/jpeg':
    927                                 return function_exists('imagecreatefromjpeg');
    928                         case 'image/png':
    929                                 return function_exists('imagecreatefrompng');
    930                         case 'image/gif':
    931                                 return function_exists('imagecreatefromgif');
    932                 }
    933         }
    934         return false;
    935 }
    936 
    937 /**
    938907 * Create new GD image resource with transparency support
    939908 * @TODO: Deprecate if possible.
    940909 *
    function wp_max_upload_size() { 
    11711140}
    11721141
    11731142/**
     1143 * Returns a WP_Image_Editor instance and loads file into it.
     1144 *
     1145 * @since 3.5.0
     1146 * @access public
     1147 *
     1148 * @param string $path Path to file to load
     1149 * @param array $args Additional data. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
     1150 * @return WP_Image_Editor|WP_Error
     1151 */
     1152function wp_get_image_editor( $path, $args = array() ) {
     1153        $args['path'] = $path;
     1154
     1155        $implementation = apply_filters( 'wp_image_editor_class', _wp_image_editor_choose( $args ) );
     1156
     1157        if ( $implementation ) {
     1158                $editor = new $implementation( $path );
     1159                $loaded = $editor->load();
     1160
     1161                if ( is_wp_error( $loaded ) )
     1162                        return $loaded;
     1163
     1164                return $editor;
     1165        }
     1166
     1167        return new WP_Error( 'image_no_editor', __('No editor could be selected.') );
     1168}
     1169
     1170/**
     1171 * Tests whether there is an editor that supports a given mime type or methods.
     1172 *
     1173 * @since 3.5.0
     1174 * @access public
     1175 *
     1176 * @param string|array $args String path to image to check for support, or array of arguments.  Accepts { 'path'=>string, 'mime_type'=>string, 'methods'=>{string, string, ...} }
     1177 * @return boolean true if an eligible editor is found; false otherwise
     1178 */
     1179function wp_image_editor_supports( $args = array() ) {
     1180        return (bool) _wp_image_editor_choose( $args );
     1181}
     1182
     1183/**
     1184 * Tests which editors are capable of supporting the request.
     1185 *
     1186 * @since 3.5.0
     1187 * @access private
     1188 *
     1189 * @param array $args Additional data. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
     1190 * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
     1191 */
     1192function _wp_image_editor_choose( $args = array() ) {
     1193        require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
     1194        require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
     1195        require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
     1196
     1197        if ( !isset( $args['mime_type'] ) && isset( $args['path'] ) ) {
     1198                $file_info  = wp_check_filetype( $args['path'] );
     1199
     1200                // If $file_info['type'] is false, then we let the editor attempt to
     1201                // figure out the file type, rather than forcing a failure based on extension.
     1202                if ( isset( $file_info ) && $file_info['type'] )
     1203                        $args['mime_type'] = $file_info['type'];
     1204        }
     1205
     1206        $implementations = apply_filters( 'wp_image_editors',
     1207                array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
     1208
     1209        foreach ( $implementations as $implementation ) {
     1210                if ( ! call_user_func( array( $implementation, 'test' ), $args ) )
     1211                        continue;
     1212
     1213                if ( isset( $args['methods'] ) &&
     1214                         array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
     1215                        continue;
     1216                }
     1217
     1218                return $implementation;
     1219        }
     1220
     1221        return false;
     1222}
     1223
     1224/**
    11741225 * Prints default plupload arguments.
    11751226 *
    11761227 * @since 3.4.0
  • wp-settings.php

    diff --git wp-settings.php wp-settings.php
    index ac331c2..65485a8 100644
    require( ABSPATH . WPINC . '/nav-menu.php' ); 
    143143require( ABSPATH . WPINC . '/nav-menu-template.php' );
    144144require( ABSPATH . WPINC . '/admin-bar.php' );
    145145
    146 require( ABSPATH . WPINC . '/class-wp-image-editor.php' );
    147 require( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' );
    148 require( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );
    149 
    150146// Load multisite-specific files.
    151147if ( is_multisite() ) {
    152148        require( ABSPATH . WPINC . '/ms-functions.php' );