<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[22510] trunk: WP_Image_Editor improvements.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/22510">22510</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2012-11-09 19:38:54 +0000 (Fri, 09 Nov 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>WP_Image_Editor improvements.

* Make test() and supports_mime_type() static.
* Add required_methods argument to get_instance(). Allows requesting an implementation that has certain methods/capabilities.
* Whitespace cleanup

Props markoheijnen
see <a href="http://core.trac.wordpress.org/ticket/6821">#6821</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesimageeditphp">trunk/wp-admin/includes/image-edit.php</a></li>
<li><a href="#trunkwpincludesclasswpimageeditorgdphp">trunk/wp-includes/class-wp-image-editor-gd.php</a></li>
<li><a href="#trunkwpincludesclasswpimageeditorimagickphp">trunk/wp-includes/class-wp-image-editor-imagick.php</a></li>
<li><a href="#trunkwpincludesclasswpimageeditorphp">trunk/wp-includes/class-wp-image-editor.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesimageeditphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/image-edit.php (22509 => 22510)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/image-edit.php        2012-11-09 12:43:39 UTC (rev 22509)
+++ trunk/wp-admin/includes/image-edit.php        2012-11-09 19:38:54 UTC (rev 22510)
</span><span class="lines">@@ -237,7 +237,6 @@
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * Saves Image to File
</span><del>- * @TODO: Add mime_type support to WP_Image_Editor
</del><span class="cx">  *
</span><span class="cx">  * @param string $filename
</span><span class="cx">  * @param WP_Image_Editor $image
</span></span></pre></div>
<a id="trunkwpincludesclasswpimageeditorgdphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-image-editor-gd.php (22509 => 22510)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-image-editor-gd.php        2012-11-09 12:43:39 UTC (rev 22509)
+++ trunk/wp-includes/class-wp-image-editor-gd.php        2012-11-09 19:38:54 UTC (rev 22510)
</span><span class="lines">@@ -28,11 +28,11 @@
</span><span class="cx">          * Checks to see if current environment supports GD
</span><span class="cx">          *
</span><span class="cx">          * @since 3.5.0
</span><del>-         * @access protected
</del><ins>+         * @access public
</ins><span class="cx">          *
</span><span class="cx">          * @return boolean
</span><span class="cx">          */
</span><del>-        public function test() {
</del><ins>+        public static function test( $args = null ) {
</ins><span class="cx">                 if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -99,7 +99,7 @@
</span><span class="cx">          * @param string $mime_type
</span><span class="cx">          * @return boolean
</span><span class="cx">          */
</span><del>-        public function supports_mime_type( $mime_type ) {
</del><ins>+        public static function supports_mime_type( $mime_type ) {
</ins><span class="cx">                 $allowed_mime_types = array( 'image/gif', 'image/png', 'image/jpeg' );
</span><span class="cx"> 
</span><span class="cx">                 return in_array( $mime_type, $allowed_mime_types );
</span></span></pre></div>
<a id="trunkwpincludesclasswpimageeditorimagickphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-image-editor-imagick.php (22509 => 22510)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-image-editor-imagick.php        2012-11-09 12:43:39 UTC (rev 22509)
+++ trunk/wp-includes/class-wp-image-editor-imagick.php        2012-11-09 19:38:54 UTC (rev 22510)
</span><span class="lines">@@ -29,11 +29,11 @@
</span><span class="cx">          * Checks to see if current environment supports Imagick
</span><span class="cx">          *
</span><span class="cx">          * @since 3.5.0
</span><del>-         * @access protected
</del><ins>+         * @access public
</ins><span class="cx">          *
</span><span class="cx">          * @return boolean
</span><span class="cx">          */
</span><del>-        public function test() {
</del><ins>+        public static function test( $args = null ) {
</ins><span class="cx">                 if ( ! extension_loaded( 'imagick' ) )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="lines">@@ -143,7 +143,7 @@
</span><span class="cx">          * @param string $mime_type
</span><span class="cx">          * @return boolean
</span><span class="cx">          */
</span><del>-        public function supports_mime_type( $mime_type ) {
</del><ins>+        public static function supports_mime_type( $mime_type ) {
</ins><span class="cx">                 if ( ! $mime_type )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesclasswpimageeditorphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-image-editor.php (22509 => 22510)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-image-editor.php        2012-11-09 12:43:39 UTC (rev 22509)
+++ trunk/wp-includes/class-wp-image-editor.php        2012-11-09 19:38:54 UTC (rev 22510)
</span><span class="lines">@@ -17,7 +17,6 @@
</span><span class="cx">         protected $mime_type  = null;
</span><span class="cx">         protected $default_mime_type = 'image/jpeg';
</span><span class="cx">         protected $quality = 90;
</span><del>-        private static $implementation;
</del><span class="cx"> 
</span><span class="cx">         protected function __construct( $filename ) {
</span><span class="cx">                 $this-&gt;file = $filename;
</span><span class="lines">@@ -30,16 +29,17 @@
</span><span class="cx">          * @access public
</span><span class="cx">          *
</span><span class="cx">          * @param string $path Path to File to Load
</span><ins>+         * @param array $required_methods Methods to require in implementation
</ins><span class="cx">          * @return WP_Image_Editor|WP_Error|boolean
</span><span class="cx">          */
</span><del>-        public final static function get_instance( $path = null ) {
-                $implementation = apply_filters( 'image_editor_class', self::choose_implementation(), $path );
</del><ins>+        public final static function get_instance( $path = null, $required_methods = null ) {
+                $implementation = apply_filters( 'wp_image_editor_class', self::choose_implementation( $required_methods ), $path );
</ins><span class="cx"> 
</span><span class="cx">                 if ( $implementation ) {
</span><span class="cx">                         $editor = new $implementation( $path );
</span><span class="cx">                         $loaded = $editor-&gt;load();
</span><span class="cx"> 
</span><del>-                        if ( is_wp_error ( $loaded ) )
</del><ins>+                        if ( is_wp_error( $loaded ) )
</ins><span class="cx">                                 return $loaded;
</span><span class="cx"> 
</span><span class="cx">                         return $editor;
</span><span class="lines">@@ -54,40 +54,78 @@
</span><span class="cx">          * @since 3.5.0
</span><span class="cx">          * @access private
</span><span class="cx">          *
</span><ins>+         * @param $required_methods Array String array of all methods required for implementation returned.
+         *
</ins><span class="cx">          * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
</span><span class="cx">          */
</span><del>-        private final static function choose_implementation() {
</del><ins>+        private final static function choose_implementation( $required_methods = null ) {
+                $request_order = apply_filters( 'wp_image_editors',
+                        array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
</ins><span class="cx"> 
</span><del>-                if ( null === self::$implementation ) {
-                        $request_order = apply_filters( 'wp_editors', array( 'imagick', 'gd' ) );
</del><ins>+                if ( ! $required_methods )
+                        $required_methods = apply_filters( 'wp_image_editor_default_methods',
+                                array( 'resize', 'multi_resize', 'crop', 'rotate', 'flip', 'stream' ) );
</ins><span class="cx"> 
</span><del>-                        // Loop over each editor on each request looking for one which will serve this request's needs
-                        foreach ( $request_order as $editor ) {
-                                $class = 'WP_Image_Editor_' . $editor;
</del><ins>+                // Loop over each editor on each request looking for one which will serve this request's needs
+                foreach ( $request_order as $editor ) {
+                        // Check to see if this editor is a possibility, calls the editor statically
+                        if ( ! call_user_func( array( $editor, 'test' ) ) )
+                                continue;
</ins><span class="cx"> 
</span><del>-                                // Check to see if this editor is a possibility, calls the editor statically
-                                if ( ! call_user_func( array( $class, 'test' ) ) )
-                                        continue;
</del><ins>+                        // Make sure that all methods are supported by editor.
+                        if ( array_diff( $required_methods, get_class_methods( $editor ) ) )
+                                continue;
</ins><span class="cx"> 
</span><del>-                                self::$implementation = $class;
-                                break;
-                        }
</del><ins>+                        return $editor;
</ins><span class="cx">                 }
</span><del>-                return self::$implementation;
</del><ins>+                return false;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        abstract public function test(); // returns bool
</del><span class="cx">         abstract protected function load(); // returns bool|WP_Error
</span><del>-        abstract public function supports_mime_type( $mime_type ); // returns bool
-        abstract public function resize( $max_w, $max_h, $crop = false );
-        abstract public function multi_resize( $sizes );
-        abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
-        abstract public function rotate( $angle );
-        abstract public function flip( $horz, $vert );
</del><span class="cx">         abstract public function save( $destfilename = null, $mime_type = null );
</span><del>-        abstract public function stream( $mime_type = null );
</del><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Implement all of the below to support natively used functions:
+         *
+         * public function resize( $max_w, $max_h, $crop = false )
+         * public function multi_resize( $sizes )
+         * public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false )
+         * public function rotate( $angle )
+         * public function flip( $horz, $vert )
+         * public function stream( $mime_type = null )
+         */
+
+        /**
+         * Checks to see if current environment supports the editor chosen.
+         * Must be overridden in a sub-class.
+         *
+         * @since 3.5.0
+         * @access public
+         * @abstract
+         *
+         * @param $args array
+         * @return boolean
+         */
+        public static function test( $args = null ) {
+                return false;
+        }
+
+        /**
+         * Checks to see if editor supports mime-type specified
+         * Must be overridden in a sub-class.
+         *
+         * @since 3.5.0
+         * @access public
+         * @abstract
+         *
+         * @param string $mime_type
+         * @return boolean
+         */
+        public static function supports_mime_type( $mime_type ) {
+                return false;
+        }
+
+        /**
</ins><span class="cx">          * Gets dimensions of image
</span><span class="cx">          *
</span><span class="cx">          * @since 3.5.0
</span><span class="lines">@@ -258,19 +296,19 @@
</span><span class="cx"> 
</span><span class="cx">                 $result = call_user_func_array( $function, $arguments );
</span><span class="cx"> 
</span><del>-                if( $result &amp;&amp; $stream ) {
</del><ins>+                if ( $result &amp;&amp; $stream ) {
</ins><span class="cx">                         $contents = ob_get_contents();
</span><span class="cx"> 
</span><span class="cx">                         $fp = fopen( $dst_file, 'w' );
</span><span class="cx"> 
</span><del>-                        if( ! $fp )
</del><ins>+                        if ( ! $fp )
</ins><span class="cx">                                 return false;
</span><span class="cx"> 
</span><span class="cx">                         fwrite( $fp, $contents );
</span><span class="cx">                         fclose( $fp );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if( $stream ) {
</del><ins>+                if ( $stream ) {
</ins><span class="cx">                         ob_end_clean();
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="lines">@@ -295,8 +333,8 @@
</span><span class="cx">                 $extensions = array_keys( $mime_types );
</span><span class="cx"> 
</span><span class="cx">                 foreach( $extensions as $_extension ) {
</span><del>-                        if( preg_match(&quot;/{$extension}/i&quot;, $_extension ) ) {
-                                return $mime_types[ $_extension ];
</del><ins>+                        if ( preg_match( &quot;/{$extension}/i&quot;, $_extension ) ) {
+                                return $mime_types[$_extension];
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>