Skip to content

Commit

Permalink
[#114] Allow Skip Cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
bd-viget committed Jun 21, 2024
1 parent 4f14163 commit 291c39b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
40 changes: 40 additions & 0 deletions wp-content/mu-plugins/viget-wp/src/assets/js/skip-cropping.js
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 23 additions & 1 deletion wp-content/mu-plugins/viget-wp/src/classes/Admin/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
);
Expand Down

0 comments on commit 291c39b

Please sign in to comment.