<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[12155] trunk/wp-admin/includes/image-edit.php: Add 'IMAGE_EDIT_OVERWRITE'
  constant to control edited image save or replace,
  most useful for setups that have dynamic image resizing, see #10528</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12155">12155</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2009-11-07 11:20:09 +0000 (Sat, 07 Nov 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add 'IMAGE_EDIT_OVERWRITE' constant to control edited image save or replace, most useful for setups that have dynamic image resizing, see <a href="http://trac.wordpress.org/ticket/10528">#10528</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesimageeditphp">trunk/wp-admin/includes/image-edit.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 (12154 => 12155)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/image-edit.php        2009-11-07 00:50:28 UTC (rev 12154)
+++ trunk/wp-admin/includes/image-edit.php        2009-11-07 11:20:09 UTC (rev 12155)
</span><span class="lines">@@ -24,30 +24,6 @@
</span><span class="cx">         $can_restore = !empty($backup_sizes) &amp;&amp; isset($backup_sizes['full-orig'])
</span><span class="cx">                 &amp;&amp; $backup_sizes['full-orig']['file'] != basename($meta['file']);
</span><span class="cx"> 
</span><del>-
-        // temp convert backup sizes
-        if ( $sub_sizes ) {
-                $update = false;
-                foreach ( $meta['sizes'] as $name =&gt; $val ) {
-                        if ( strpos($name, 'backup-') === 0 ) {
-                                $m = array();
-                                preg_match('/backup-([0-9]+)-(.*)/', $name, $m);
-                                if ( !isset($backup_sizes[&quot;{$m[2]}-orig&quot;]) )
-                                        $n = &quot;{$m[2]}-orig&quot;;
-                                else
-                                        $n = &quot;{$m[2]}-{$m[1]}&quot;;
-                                $backup_sizes[$n] = $val;
-                                unset($meta['sizes'][$name]);
-                                $update = true;
-                        }
-                }
-                if ( $update ) {
-                        wp_update_attachment_metadata( $post_id, $meta );
-                        update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
-                }
-        }
-        // end temp
-
</del><span class="cx">         if ( $msg ) {
</span><span class="cx">                 if ( isset($msg-&gt;error) )
</span><span class="cx">                         $note = &quot;&lt;div class='error'&gt;&lt;p&gt;$msg-&gt;error&lt;/p&gt;&lt;/div&gt;&quot;;
</span><span class="lines">@@ -451,8 +427,17 @@
</span><span class="cx">                 if ( isset($backup_sizes[&quot;$default_size-orig&quot;]) ) {
</span><span class="cx">                         $data = $backup_sizes[&quot;$default_size-orig&quot;];
</span><span class="cx">                         if ( 'full' == $default_size ) {
</span><del>-                                if ( $parts['basename'] != $data['file'] )
-                                        $backup_sizes[&quot;full-$suffix&quot;] = array('width' =&gt; $meta['width'], 'height' =&gt; $meta['height'], 'file' =&gt; $parts['basename']);
</del><ins>+                                if ( $parts['basename'] != $data['file'] ) {
+                                        if ( defined('IMAGE_EDIT_OVERWRITE') &amp;&amp; IMAGE_EDIT_OVERWRITE ) {
+                                                // delete only if it's edited image
+                                                if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) {
+                                                        $delpath = apply_filters('wp_delete_file', $meta['file']);
+                                                        @unlink($delpath);
+                                                }
+                                        } else {
+                                                $backup_sizes[&quot;full-$suffix&quot;] = array('width' =&gt; $meta['width'], 'height' =&gt; $meta['height'], 'file' =&gt; $parts['basename']);
+                                        }
+                                }
</ins><span class="cx"> 
</span><span class="cx">                                 $meta['file'] = path_join($parts['dirname'], $data['file']);
</span><span class="cx">                                 $meta['width'] = $data['width'];
</span><span class="lines">@@ -462,8 +447,17 @@
</span><span class="cx">                                 $meta['hwstring_small'] = &quot;height='$uheight' width='$uwidth'&quot;;
</span><span class="cx">                                 $restored = update_attached_file($post_id, $meta['file']);
</span><span class="cx">                         } else {
</span><del>-                                if ( isset($meta['sizes'][$default_size]) &amp;&amp; $meta['sizes'][$default_size]['file'] != $data['file'] )
-                                        $backup_sizes[&quot;$default_size-{$suffix}&quot;] = $meta['sizes'][$default_size];
</del><ins>+                                if ( isset($meta['sizes'][$default_size]) &amp;&amp; $meta['sizes'][$default_size]['file'] != $data['file'] ) {
+                                        if ( defined('IMAGE_EDIT_OVERWRITE') &amp;&amp; IMAGE_EDIT_OVERWRITE ) {
+                                                // delete only if it's edited image
+                                                if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) {
+                                                        $delpath = apply_filters( 'wp_delete_file', path_join($parts['dirname'], $meta['sizes'][$default_size]['file']) );
+                                                        @unlink($delpath);
+                                                }
+                                        } else {
+                                                $backup_sizes[&quot;$default_size-{$suffix}&quot;] = $meta['sizes'][$default_size];
+                                        }
+                                }
</ins><span class="cx"> 
</span><span class="cx">                                 $meta['sizes'][$default_size] = $data;
</span><span class="cx">                         }
</span><span class="lines">@@ -547,15 +541,24 @@
</span><span class="cx">         $filename = $path_parts['filename'];
</span><span class="cx">         $suffix = time() . rand(100, 999);
</span><span class="cx"> 
</span><del>-        while( true ) {
-                $filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
-                $filename .= &quot;-e{$suffix}&quot;;
-                $new_filename = &quot;{$filename}.{$path_parts['extension']}&quot;;
-                $new_path = &quot;{$path_parts['dirname']}/$new_filename&quot;;
-                if ( file_exists($new_path) )
-                        $suffix++;
</del><ins>+        if ( defined('IMAGE_EDIT_OVERWRITE') &amp;&amp; IMAGE_EDIT_OVERWRITE &amp;&amp;
+                isset($backup_sizes['full-orig']) &amp;&amp; $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) {
+
+                if ( 'thumbnail' == $target )
+                        $new_path = &quot;{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}&quot;;
</ins><span class="cx">                 else
</span><del>-                        break;
</del><ins>+                        $new_path = $path;
+        } else {
+                while( true ) {
+                        $filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
+                        $filename .= &quot;-e{$suffix}&quot;;
+                        $new_filename = &quot;{$filename}.{$path_parts['extension']}&quot;;
+                        $new_path = &quot;{$path_parts['dirname']}/$new_filename&quot;;
+                        if ( file_exists($new_path) )
+                                $suffix++;
+                        else
+                                break;
+                }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         // save the full-size file, also needed to create sub-sizes
</span><span class="lines">@@ -567,7 +570,7 @@
</span><span class="cx">         if ( 'nothumb' == $target || 'all' == $target || 'full' == $target || $scaled ) {
</span><span class="cx">                 $tag = false;
</span><span class="cx">                 if ( isset($backup_sizes['full-orig']) ) {
</span><del>-                        if ( $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
</del><ins>+                        if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) &amp;&amp; $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
</ins><span class="cx">                                 $tag = &quot;full-$suffix&quot;;
</span><span class="cx">                 } else {
</span><span class="cx">                         $tag = 'full-orig';
</span><span class="lines">@@ -602,7 +605,7 @@
</span><span class="cx">                         $tag = false;
</span><span class="cx">                         if ( isset($meta['sizes'][$size]) ) {
</span><span class="cx">                                 if ( isset($backup_sizes[&quot;$size-orig&quot;]) ) {
</span><del>-                                        if ( $backup_sizes[&quot;$size-orig&quot;]['file'] != $meta['sizes'][$size]['file'] )
</del><ins>+                                        if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) &amp;&amp; $backup_sizes[&quot;$size-orig&quot;]['file'] != $meta['sizes'][$size]['file'] )
</ins><span class="cx">                                                 $tag = &quot;$size-$suffix&quot;;
</span><span class="cx">                                 } else {
</span><span class="cx">                                         $tag = &quot;$size-orig&quot;;
</span></span></pre>
</div>
</div>

</body>
</html>