From c47569912d534db15a76f7fa3d812b8421f30261 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Wed, 18 Dec 2019 15:36:09 -0300 Subject: [PATCH] Custom cropper class to remove wp_customize. --- src/admin/js/wp-media-frames.js | 39 +++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/admin/js/wp-media-frames.js b/src/admin/js/wp-media-frames.js index e4426782d..6e450f4b4 100644 --- a/src/admin/js/wp-media-frames.js +++ b/src/admin/js/wp-media-frames.js @@ -6,7 +6,8 @@ export default { initFrame: function() { wp.media.view.settings.post = { - id: this.params.relatedPostId + id: this.params.relatedPostId, + wp_customize: 'off' } this.frame = wp.media({ @@ -57,11 +58,41 @@ export default { } }); } - }), + }), + // CroppedImageControl, with presets for thumbnail dimensions thumbnailControl: wp.customize.CroppedImageControl.extend({ initFrame: function() { + + var MyCustomizeImageCropper = wp.media.controller.Cropper.extend({ + + doCrop: function( attachment ) { + var cropDetails = attachment.get( 'cropDetails' ), + control = this.get( 'control' ), + ratio = cropDetails.width / cropDetails.height; + + // Use crop measurements when flexible in both directions. + if ( control.params.flex_width && control.params.flex_height ) { + cropDetails.dst_width = cropDetails.width; + cropDetails.dst_height = cropDetails.height; + + // Constrain flexible side based on image ratio and size of the fixed side. + } else { + cropDetails.dst_width = control.params.flex_width ? control.params.height * ratio : control.params.width; + cropDetails.dst_height = control.params.flex_height ? control.params.width / ratio : control.params.height; + } + + return wp.ajax.post( 'crop-image', { + wp_customize: 'off', + nonce: attachment.get( 'nonces' ).edit, + id: attachment.get( 'id' ), + context: control.id, + cropDetails: cropDetails + }); + } + }); + var l10n = _wpMediaViewsL10n; wp.media.view.settings.post = { @@ -96,7 +127,7 @@ export default { suggestedHeight: this.params.height, uploadedTo: this.params.relatedPostId }), - new wp.media.controller.CustomizeImageCropper({ + new MyCustomizeImageCropper({ imgSelectOptions: this.calculateImageSelectOptions, control: this }) @@ -193,7 +224,7 @@ export default { initFrame: function() { wp.media.view.settings.post = { - id: this.params.relatedPostId + id: this.params.relatedPostId } this.frame = wp.media({