From 8f8f0b90e89d53c63a65edb6cb468e24797aabbd Mon Sep 17 00:00:00 2001 From: Brian DiChiara <122309362+bd-viget@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:51:35 -0500 Subject: [PATCH] [#114] Allow Skip Cropping (#115) --- .../viget-wp/src/assets/js/skip-cropping.js | 40 +++++++++++++++++++ .../viget-wp/src/classes/Admin/Assets.php | 24 ++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 wp-content/mu-plugins/viget-wp/src/assets/js/skip-cropping.js diff --git a/wp-content/mu-plugins/viget-wp/src/assets/js/skip-cropping.js b/wp-content/mu-plugins/viget-wp/src/assets/js/skip-cropping.js new file mode 100644 index 00000000..2aceff8d --- /dev/null +++ b/wp-content/mu-plugins/viget-wp/src/assets/js/skip-cropping.js @@ -0,0 +1,40 @@ +/** + * This allows the user to skip the cropping step when uploading a site icon. + * + * @package VigetWP + */ + +(function($, wp) { + $(document).ready(function() { + if (!wp.media) { + return; + } + + const originalController = wp.media.controller.SiteIconCropper; + + wp.media.controller.SiteIconCropper = originalController.extend({ + activate: function() { + originalController.prototype.activate.apply(this, arguments); + this.set('canSkipCrop', true); + } + }); + + const originalButton = wp.media.view.Button; + + wp.media.view.Button = originalButton.extend({ + click: function( e ) { + if (!this.options.classes.includes('media-button-skip')) { + originalButton.prototype.click.apply(this, arguments); + return; + } + + e.preventDefault(); + + const controller = this.controller; + const attachment = controller.state().get('selection').first().toJSON(); + controller.trigger('cropped', attachment); + controller.close(); + } + }); + }); +})(jQuery, wp); diff --git a/wp-content/mu-plugins/viget-wp/src/classes/Admin/Assets.php b/wp-content/mu-plugins/viget-wp/src/classes/Admin/Assets.php index f89d9432..f537e9c8 100644 --- a/wp-content/mu-plugins/viget-wp/src/classes/Admin/Assets.php +++ b/wp-content/mu-plugins/viget-wp/src/classes/Admin/Assets.php @@ -28,11 +28,33 @@ private function load_assets(): void { add_action( 'admin_enqueue_scripts', function () { + $version = vigetwp()->get_version(); + + if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { + $js_path = VIGETWP_PLUGIN_PATH . 'src/assets/js/skip-cropping.js'; + $version = filemtime( $js_path ); + } + + wp_enqueue_script( + 'vigetwp-admin-skip-cropping', + VIGETWP_PLUGIN_URL . 'src/assets/js/skip-cropping.js', + [ 'jquery', 'media-views' ], + $version, + [ + 'in_footer' => true, + ] + ); + + if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { + $css_path = VIGETWP_PLUGIN_PATH . 'src/assets/css/admin.css'; + $version = filemtime( $css_path ); + } + wp_enqueue_style( 'vigetwp-admin-styles', VIGETWP_PLUGIN_URL . 'src/assets/css/admin.css', [], // dependencies. - vigetwp()->get_version() + $version ); } );