ID ) ) {
return $form_fields;
}
if ( ! current_user_can( 'upload_files' ) ) {
return $form_fields;
}
$form_fields['nano_banana_edit'] = array(
'label' => __( 'AI Edit', '45squared-ai-powered-image-generator' ),
'input' => 'html',
'html' => sprintf(
'
'
. '
'
. '
'
. '
'
. '
'
. '
',
absint( $post->ID ),
esc_attr__( 'Describe the edit (e.g., "Remove the background")', '45squared-ai-powered-image-generator' ),
esc_html__( 'Edit with Nano Banana', '45squared-ai-powered-image-generator' )
),
);
return $form_fields;
}
/**
* AJAX: Edit an image.
*/
public function ajax_edit_image() {
check_ajax_referer( 'nano_banana_nonce', 'nonce' );
if ( ! current_user_can( 'upload_files' ) ) {
wp_send_json_error( __( 'Unauthorized.', '45squared-ai-powered-image-generator' ) );
}
$attachment_id = absint( $_POST['attachment_id'] ?? 0 );
$prompt = sanitize_textarea_field( wp_unslash( $_POST['prompt'] ?? '' ) );
$save_mode = sanitize_text_field( wp_unslash( $_POST['save_mode'] ?? 'new' ) );
$save_mode = 'replace' === $save_mode ? 'replace' : 'new';
if ( ! $attachment_id || ! wp_attachment_is_image( $attachment_id ) ) {
wp_send_json_error( __( 'Invalid attachment.', '45squared-ai-powered-image-generator' ) );
}
// Per-attachment authorization: upload_files alone is not enough to
// read or overwrite someone else's media.
if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
wp_send_json_error( __( 'You do not have permission to edit this attachment.', '45squared-ai-powered-image-generator' ) );
}
if ( empty( $prompt ) ) {
wp_send_json_error( __( 'Please enter an edit prompt.', '45squared-ai-powered-image-generator' ) );
}
$file_path = get_attached_file( $attachment_id );
if ( ! $file_path || ! file_exists( $file_path ) ) {
wp_send_json_error( __( 'Source image file not found.', '45squared-ai-powered-image-generator' ) );
}
$api = new Nano_Banana_API();
$result = $api->edit_image( $file_path, $prompt );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result->get_error_message() );
}
$model = get_option( 'nano_banana_model', 'gemini-3.1-flash-image-preview' );
if ( 'replace' === $save_mode ) {
// Replace the original file, but only with data that is genuinely
// an image in the attachment's existing format.
$info = function_exists( 'getimagesizefromstring' ) ? @getimagesizefromstring( $result['data'] ) : false; // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
if ( ! is_array( $info ) || empty( $info['mime'] ) || get_post_mime_type( $attachment_id ) !== $info['mime'] ) {
wp_send_json_error( __( 'The edited image could not be written back in the original format.', '45squared-ai-powered-image-generator' ) );
}
file_put_contents( $file_path, $result['data'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
require_once ABSPATH . 'wp-admin/includes/image.php';
$metadata = wp_generate_attachment_metadata( $attachment_id, $file_path );
wp_update_attachment_metadata( $attachment_id, $metadata );
update_post_meta( $attachment_id, '_nano_banana_edit_prompt', sanitize_textarea_field( $prompt ) );
$new_id = $attachment_id;
} else {
$new_id = $api->save_to_media_library( $result, $prompt . ' (edited)', $model );
if ( is_wp_error( $new_id ) ) {
wp_send_json_error( $new_id->get_error_message() );
}
update_post_meta( $new_id, '_nano_banana_source_attachment', $attachment_id );
}
Nano_Banana_History::log_generation( $prompt, $model, 1, 'edit' );
wp_send_json_success(
array(
'attachmentId' => $new_id,
'url' => wp_get_attachment_url( $new_id ),
'editUrl' => get_edit_post_link( $new_id, 'raw' ),
)
);
}
}