Make WordPress Core


Ignore:
Timestamp:
11/22/2012 09:52:16 AM (12 years ago)
Author:
nacin
Message:

WP_Image_Editor: the last stand.

  • Have wp_get_image_editor() rather than WP_Image_Editor::get_instance(). Having static factory methods would be less confusing if there weren't also static methods tied to individual editor implementations.
  • Lazy-load the WP_Image_Editor base class and editor implementations.
  • Have WP_Image_Editor_GD::supports_mime_type() actually check which types it supports.
  • Deprecate gd_edit_image_support() in favor of wp_image_editor_supports().

props DH-Shredder, scribu, markoheijnen. fixes #22356. see #6821.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-image-editor.php

    r22619 r22817  
    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
     
    1515    protected $file = null;
    1616    protected $size = null;
    17     protected $mime_type  = null;
     17    protected $mime_type = null;
    1818    protected $default_mime_type = 'image/jpeg';
    1919    protected $quality = 90;
    2020
    21     protected function __construct( $filename ) {
    22         $this->file = $filename;
    23     }
    24 
    25     /**
    26      * Returns a WP_Image_Editor instance and loads file into it.
    27      *
    28      * @since 3.5.0
    29      * @access public
    30      *
    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
    34      */
    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') );
    49     }
    50 
    51     /**
    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.
    59      */
    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         }
     21    /**
     22     * Each instance handles a single file.
     23     */
     24    public function __construct( $file ) {
     25        $this->file = $file;
     26    }
     27
     28    /**
     29     * Checks to see if current environment supports the editor chosen.
     30     * Must be overridden in a sub-class.
     31     *
     32     * @since 3.5.0
     33     * @access public
     34     * @abstract
     35     *
     36     * @param array $args
     37     * @return boolean
     38     */
     39    public static function test( $args = array() ) {
    7940        return false;
    8041    }
    8142
    8243    /**
     44     * Checks to see if editor supports the mime-type specified.
     45     * Must be overridden in a sub-class.
     46     *
     47     * @since 3.5.0
     48     * @access public
     49     * @abstract
     50     *
     51     * @param string $mime_type
     52     * @return boolean
     53     */
     54    public static function supports_mime_type( $mime_type ) {
     55        return false;
     56    }
     57
     58    /**
    8359     * Loads image from $this->file into editor.
    8460     *
     
    8965     * @return boolean|WP_Error True if loaded; WP_Error on failure.
    9066     */
    91     abstract protected function load();
     67    abstract public function load();
    9268
    9369    /**
     
    169145     * @abstract
    170146     *
    171      * @param boolean $horz Horizonal Flip
     147     * @param boolean $horz Horizontal Flip
    172148     * @param boolean $vert Vertical Flip
    173149     * @return boolean|WP_Error
     
    186162     */
    187163    abstract public function stream( $mime_type = null );
    188 
    189     /**
    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     }
    218164
    219165    /**
     
    452398    }
    453399}
     400
Note: See TracChangeset for help on using the changeset viewer.