public
Created

Hotfixing bug in wp_save_image() in WordPress 3.5

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<?php
 
add_action( 'wp_ajax_image-editor', 'fix_wp_ajax_image_editor', 0 );
 
function fix_wp_ajax_image_editor() {
$attachment_id = intval( $_POST['postid'] );
 
if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) )
wp_die( -1 );
 
check_ajax_referer( "image_editor-$attachment_id" );
include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
 
$msg = false;
switch ( $_POST['do'] ) {
case 'save' :
$old_meta = wp_get_attachment_metadata( $attachment_id );
 
$msg = wp_save_image( $attachment_id );
$msg = json_encode( $msg );
 
break;
case 'scale' :
$old_meta = wp_get_attachment_metadata( $attachment_id );
 
$msg = wp_save_image( $attachment_id );
 
break;
case 'restore' :
$msg = wp_restore_image( $attachment_id );
break;
}
 
if( isset( $old_meta ) ) {
$new_meta = wp_get_attachment_metadata( $attachment_id );
$new_meta['sizes'] = array_merge( $old_meta['sizes'], $new_meta['sizes'] );
 
wp_update_attachment_metadata( $attachment_id, $new_meta );
 
if( 'save' == $_POST['do'] )
wp_die( $msg );
}
 
wp_image_editor( $attachment_id, $msg );
wp_die();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.