WordPress.org

Make WordPress Core

Changeset 12155


Ignore:
Timestamp:
11/07/2009 11:20:09 AM (9 years ago)
Author:
azaozz
Message:

Add 'IMAGE_EDIT_OVERWRITE' constant to control edited image save or replace, most useful for setups that have dynamic image resizing, see #10528

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image-edit.php

    r11985 r12155  
    2424    $can_restore = !empty($backup_sizes) && isset($backup_sizes['full-orig'])
    2525        && $backup_sizes['full-orig']['file'] != basename($meta['file']);
    26 
    27 
    28     // temp convert backup sizes
    29     if ( $sub_sizes ) {
    30         $update = false;
    31         foreach ( $meta['sizes'] as $name => $val ) {
    32             if ( strpos($name, 'backup-') === 0 ) {
    33                 $m = array();
    34                 preg_match('/backup-([0-9]+)-(.*)/', $name, $m);
    35                 if ( !isset($backup_sizes["{$m[2]}-orig"]) )
    36                     $n = "{$m[2]}-orig";
    37                 else
    38                     $n = "{$m[2]}-{$m[1]}";
    39                 $backup_sizes[$n] = $val;
    40                 unset($meta['sizes'][$name]);
    41                 $update = true;
    42             }
    43         }
    44         if ( $update ) {
    45             wp_update_attachment_metadata( $post_id, $meta );
    46             update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
    47         }
    48     }
    49     // end temp
    5026
    5127    if ( $msg ) {
     
    452428            $data = $backup_sizes["$default_size-orig"];
    453429            if ( 'full' == $default_size ) {
    454                 if ( $parts['basename'] != $data['file'] )
    455                     $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
     430                if ( $parts['basename'] != $data['file'] ) {
     431                    if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
     432                        // delete only if it's edited image
     433                        if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) {
     434                            $delpath = apply_filters('wp_delete_file', $meta['file']);
     435                            @unlink($delpath);
     436                        }
     437                    } else {
     438                        $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
     439                    }
     440                }
    456441
    457442                $meta['file'] = path_join($parts['dirname'], $data['file']);
     
    463448                $restored = update_attached_file($post_id, $meta['file']);
    464449            } else {
    465                 if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] )
    466                     $backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];
     450                if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] ) {
     451                    if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
     452                        // delete only if it's edited image
     453                        if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) {
     454                            $delpath = apply_filters( 'wp_delete_file', path_join($parts['dirname'], $meta['sizes'][$default_size]['file']) );
     455                            @unlink($delpath);
     456                        }
     457                    } else {
     458                        $backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];
     459                    }
     460                }
    467461
    468462                $meta['sizes'][$default_size] = $data;
     
    548542    $suffix = time() . rand(100, 999);
    549543
    550     while( true ) {
    551         $filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
    552         $filename .= "-e{$suffix}";
    553         $new_filename = "{$filename}.{$path_parts['extension']}";
    554         $new_path = "{$path_parts['dirname']}/$new_filename";
    555         if ( file_exists($new_path) )
    556             $suffix++;
     544    if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE &&
     545        isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) {
     546
     547        if ( 'thumbnail' == $target )
     548            $new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
    557549        else
    558             break;
     550            $new_path = $path;
     551    } else {
     552        while( true ) {
     553            $filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
     554            $filename .= "-e{$suffix}";
     555            $new_filename = "{$filename}.{$path_parts['extension']}";
     556            $new_path = "{$path_parts['dirname']}/$new_filename";
     557            if ( file_exists($new_path) )
     558                $suffix++;
     559            else
     560                break;
     561        }
    559562    }
    560563
     
    568571        $tag = false;
    569572        if ( isset($backup_sizes['full-orig']) ) {
    570             if ( $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
     573            if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
    571574                $tag = "full-$suffix";
    572575        } else {
     
    603606            if ( isset($meta['sizes'][$size]) ) {
    604607                if ( isset($backup_sizes["$size-orig"]) ) {
    605                     if ( $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
     608                    if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
    606609                        $tag = "$size-$suffix";
    607610                } else {
Note: See TracChangeset for help on using the changeset viewer.