From 2533d76203892902f8c31dfc535ef32d946d53ed Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Wed, 22 Jul 2015 02:29:57 +0000 Subject: [PATCH] Site Icon: Create custom sizes for images that don't need cropping. This will duplicate an image that doesn't need cropping once and will skip creating new images and custom sizes if the selected image has the `site-icon` context. Fixes #33011. git-svn-id: https://develop.svn.wordpress.org/trunk@33356 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ajax-actions.php | 11 +++++++++ src/wp-admin/js/customize-controls.js | 34 +++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index b5bcd32d7d..88b6c7965f 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -3080,6 +3080,17 @@ function wp_ajax_crop_image() { require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php'; global $wp_site_icon; + // Skip creating a new attachment if the attachment is a Site Icon. + if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) == $context ) { + + // Delete the temporary cropped file, we don't need it. + wp_delete_file( $cropped ); + + // Additional sizes in wp_prepare_attachment_for_js(). + add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); + break; + } + /** This filter is documented in wp-admin/custom-header.php */ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. $object = $wp_site_icon->create_attachment_object( $cropped, $attachment_id ); diff --git a/src/wp-admin/js/customize-controls.js b/src/wp-admin/js/customize-controls.js index a5ca3bd14e..7aaa477b00 100644 --- a/src/wp-admin/js/customize-controls.js +++ b/src/wp-admin/js/customize-controls.js @@ -2075,6 +2075,38 @@ this.frame.on( 'skippedcrop', this.onSkippedCrop, this ); }, + /** + * After an image is selected in the media modal, switch to the cropper + * state if the image isn't the right size. + */ + onSelect: function() { + var attachment = this.frame.state().get( 'selection' ).first().toJSON(), + controller = this; + + if ( this.params.width === attachment.width && this.params.height === attachment.height && ! this.params.flex_width && ! this.params.flex_height ) { + wp.ajax.post( 'crop-image', { + nonce: attachment.nonces.edit, + id: attachment.id, + context: 'site-icon', + cropDetails: { + x1: 0, + y1: 0, + width: this.params.width, + height: this.params.height, + dst_width: this.params.width, + dst_height: this.params.height + } + } ).done( function( croppedImage ) { + controller.setImageFromAttachment( croppedImage ); + controller.frame.close(); + } ).fail( function() { + controller.trigger('content:error:crop'); + } ); + } else { + this.frame.setState( 'cropper' ); + } + }, + /** * Updates the setting and re-renders the control UI. * @@ -2090,7 +2122,7 @@ // Update the icon in-browser. - $( 'link[rel="icon"]' ).attr( 'href', icon.url ); + $( 'link[sizes="32x32"]' ).attr( 'href', icon.url ); }, /**