+ calculateImageSelectOptions: function( attachment, controller ) {
+ var control = controller.get( 'control' ),
+ flexWidth = !! parseInt( control.params.flex_width, 10 ),
+ flexHeight = !! parseInt( control.params.flex_height, 10 ),
+ realWidth = attachment.get( 'width' ),
+ realHeight = attachment.get( 'height' ),
+ xInit = parseInt( control.params.width, 10 ),
+ yInit = parseInt( control.params.height, 10 ),
+ ratio = xInit / yInit,
+ xImg = xInit,
+ yImg = yInit,
+ x1, y1, imgSelectOptions;
+
+ controller.set( 'canSkipCrop', ! control.mustBeCropped( flexWidth, flexHeight, xInit, yInit, realWidth, realHeight ) );
+
+ if ( realWidth / realHeight > ratio ) {
+ yInit = realHeight;
+ xInit = yInit * ratio;
+ } else {
+ xInit = realWidth;
+ yInit = xInit / ratio;
+ }
+
+ x1 = ( realWidth - xInit ) / 2;
+ y1 = ( realHeight - yInit ) / 2;
+
+ imgSelectOptions = {
+ handles: true,
+ keys: true,
+ instance: true,
+ persistent: true,
+ imageWidth: realWidth,
+ imageHeight: realHeight,
+ minWidth: xImg > xInit ? xInit : xImg,
+ minHeight: yImg > yInit ? yInit : yImg,
+ x1: x1,
+ y1: y1,
+ x2: xInit + x1,
+ y2: yInit + y1
+ };
+
+ if ( flexHeight === false && flexWidth === false ) {
+ imgSelectOptions.aspectRatio = xInit + ':' + yInit;
+ }
+
+ if ( true === flexHeight ) {
+ delete imgSelectOptions.minHeight;
+ imgSelectOptions.maxWidth = realWidth;
+ }
+
+ if ( true === flexWidth ) {
+ delete imgSelectOptions.minWidth;
+ imgSelectOptions.maxHeight = realHeight;
+ }
+
+ return imgSelectOptions;
+ },
+
+ /**
+ * Return whether the image must be cropped, based on required dimensions.
+ *
+ * @param {bool} flexW
+ * @param {bool} flexH
+ * @param {int} dstW
+ * @param {int} dstH
+ * @param {int} imgW
+ * @param {int} imgH
+ * @return {bool}
+ */
+ mustBeCropped: function( flexW, flexH, dstW, dstH, imgW, imgH ) {
+ if ( true === flexW && true === flexH ) {
+ return false;
+ }
+
+ if ( true === flexW && dstH === imgH ) {
+ return false;
+ }
+
+ if ( true === flexH && dstW === imgW ) {
+ return false;
+ }
+
+ if ( dstW === imgW && dstH === imgH ) {
+ return false;
+ }
+
+ if ( imgW <= dstW ) {
+ return false;
+ }
+
+ return true;
+ },
+
+ /**
+ * If cropping was skipped, apply the image data directly to the setting.
+ */
+ onSkippedCrop: function() {
+ var attachment = this.frame.state().get( 'selection' ).first().toJSON();
+ this.setImageFromAttachment( attachment );
+ },
+
+ /**
+ * Updates the setting and re-renders the control UI.
+ *
+ * @param {object} attachment
+ */
+ setImageFromAttachment: function( attachment ) {
+ this.params.attachment = attachment;
+
+ // Set the Customizer setting; the callback takes care of rendering.
+ this.setting( attachment.id );
+ }
+ });
+
+ /**
+ * A control for selecting and cropping Site Icons.
+ *
+ * @class
+ * @augments wp.customize.CroppedImageControl
+ * @augments wp.customize.MediaControl
+ * @augments wp.customize.Control
+ * @augments wp.customize.Class
+ */
+ api.SiteIconControl = api.CroppedImageControl.extend({
+
+ /**
+ * Create a media modal select frame, and store it so the instance can be reused when needed.
+ */
+ initFrame: function() {
+ var l10n = _wpMediaViewsL10n;
+
+ this.frame = wp.media({
+ button: {
+ text: l10n.select,
+ close: false
+ },
+ states: [
+ new wp.media.controller.Library({
+ title: this.params.button_labels.frame_title,
+ library: wp.media.query({ type: 'image' }),
+ multiple: false,
+ date: false,
+ priority: 20,
+ suggestedWidth: this.params.width,
+ suggestedHeight: this.params.height
+ }),
+ new wp.media.controller.SiteIconCropper({
+ imgSelectOptions: this.calculateImageSelectOptions,
+ control: this
+ })
+ ]
+ });
+
+ this.frame.on( 'select', this.onSelect, this );
+ this.frame.on( 'cropped', this.onCropped, this );
+ 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.frame.trigger('content:error:crop');
+ } );
+ } else {
+ this.frame.setState( 'cropper' );
+ }
+ },
+
+ /**
+ * Updates the setting and re-renders the control UI.
+ *
+ * @param {object} attachment
+ */
+ setImageFromAttachment: function( attachment ) {
+ var sizes = [ 'site_icon-32', 'thumbnail', 'full' ], link,
+ icon;
+
+ _.each( sizes, function( size ) {
+ if ( ! icon && ! _.isUndefined ( attachment.sizes[ size ] ) ) {
+ icon = attachment.sizes[ size ];
+ }
+ } );
+
+ this.params.attachment = attachment;
+
+ // Set the Customizer setting; the callback takes care of rendering.
+ this.setting( attachment.id );
+
+ if ( ! icon ) {
+ return;
+ }
+
+ // Update the icon in-browser.
+ link = $( 'link[rel="icon"][sizes="32x32"]' );
+ link.attr( 'href', icon.url );
+ },
+
+ /**
+ * Called when the "Remove" link is clicked. Empties the setting.
+ *
+ * @param {object} event jQuery Event object
+ */
+ removeFile: function( event ) {
+ if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
+ return;
+ }
+ event.preventDefault();
+
+ this.params.attachment = {};
+ this.setting( '' );
+ this.renderContent(); // Not bound to setting change when emptying.
+ $( 'link[rel="icon"][sizes="32x32"]' ).attr( 'href', '/favicon.ico' ); // Set to default.
+ }
+ });
+
+ /**
+ * @class
+ * @augments wp.customize.Control
+ * @augments wp.customize.Class
+ */
+ api.HeaderControl = api.Control.extend({
+ ready: function() {
+ this.btnRemove = $('#customize-control-header_image .actions .remove');
+ this.btnNew = $('#customize-control-header_image .actions .new');
+
+ _.bindAll(this, 'openMedia', 'removeImage');
+
+ this.btnNew.on( 'click', this.openMedia );
+ this.btnRemove.on( 'click', this.removeImage );
+
+ api.HeaderTool.currentHeader = this.getInitialHeaderImage();
+
+ new api.HeaderTool.CurrentView({
+ model: api.HeaderTool.currentHeader,
+ el: '#customize-control-header_image .current .container'
+ });
+
+ new api.HeaderTool.ChoiceListView({
+ collection: api.HeaderTool.UploadsList = new api.HeaderTool.ChoiceList(),
+ el: '#customize-control-header_image .choices .uploaded .list'
+ });
+
+ new api.HeaderTool.ChoiceListView({
+ collection: api.HeaderTool.DefaultsList = new api.HeaderTool.DefaultsList(),
+ el: '#customize-control-header_image .choices .default .list'
+ });
+
+ api.HeaderTool.combinedList = api.HeaderTool.CombinedList = new api.HeaderTool.CombinedList([
+ api.HeaderTool.UploadsList,
+ api.HeaderTool.DefaultsList
+ ]);
+
+ // Ensure custom-header-crop Ajax requests bootstrap the Customizer to activate the previewed theme.
+ wp.media.controller.Cropper.prototype.defaults.doCropArgs.wp_customize = 'on';
+ wp.media.controller.Cropper.prototype.defaults.doCropArgs.customize_theme = api.settings.theme.stylesheet;
+ },
+
+ /**
+ * Returns a new instance of api.HeaderTool.ImageModel based on the currently
+ * saved header image (if any).
+ *
+ * @since 4.2.0
+ *
+ * @returns {Object} Options
+ */
+ getInitialHeaderImage: function() {
+ if ( ! api.get().header_image || ! api.get().header_image_data || _.contains( [ 'remove-header', 'random-default-image', 'random-uploaded-image' ], api.get().header_image ) ) {
+ return new api.HeaderTool.ImageModel();
+ }
+
+ // Get the matching uploaded image object.
+ var currentHeaderObject = _.find( _wpCustomizeHeader.uploads, function( imageObj ) {
+ return ( imageObj.attachment_id === api.get().header_image_data.attachment_id );
+ } );
+ // Fall back to raw current header image.
+ if ( ! currentHeaderObject ) {
+ currentHeaderObject = {
+ url: api.get().header_image,
+ thumbnail_url: api.get().header_image,
+ attachment_id: api.get().header_image_data.attachment_id
+ };
+ }
+
+ return new api.HeaderTool.ImageModel({
+ header: currentHeaderObject,
+ choice: currentHeaderObject.url.split( '/' ).pop()
+ });
+ },
+
+ /**
+ * Returns a set of options, computed from the attached image data and
+ * theme-specific data, to be fed to the imgAreaSelect plugin in
+ * wp.media.view.Cropper.
+ *
+ * @param {wp.media.model.Attachment} attachment
+ * @param {wp.media.controller.Cropper} controller
+ * @returns {Object} Options
+ */
+ calculateImageSelectOptions: function(attachment, controller) {
+ var xInit = parseInt(_wpCustomizeHeader.data.width, 10),