Skip to content

Commit 1136dd3

Browse files
mattwiebepfefferle
andauthored
Header Images: fix cropping for users without the 'customize' capability (#854)
* Header Images: fix cropping for users without the 'customize' capability * only show users’ own posts to lower capaability users --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 9d1decf commit 1136dd3

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

assets/js/activitypub-header-image.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,42 @@
1414
$headerImagePreview = $( '#activitypub-header-image-preview' ),
1515
$hiddenDataField = $( '#activitypub_header_image' ),
1616
$removeButton = $( '#activitypub-remove-header-image' ),
17-
frame;
17+
frame,
18+
ImageCropperNoCustomizer;
19+
20+
/**
21+
* We register our own handler because the Core one invokes the Customizer, which fails the request unnecessarily
22+
* for users who don't have the 'customize' capability.
23+
* See https://github.com/Automattic/wordpress-activitypub/issues/846
24+
*/
25+
ImageCropperNoCustomizer = wp.media.controller.CustomizeImageCropper.extend( {
26+
doCrop: function( attachment ) {
27+
var cropDetails = attachment.get( 'cropDetails' ),
28+
control = this.get( 'control' ),
29+
ratio = cropDetails.width / cropDetails.height;
30+
31+
// Use crop measurements when flexible in both directions.
32+
if ( control.params.flex_width && control.params.flex_height ) {
33+
cropDetails.dst_width = cropDetails.width;
34+
cropDetails.dst_height = cropDetails.height;
35+
36+
// Constrain flexible side based on image ratio and size of the fixed side.
37+
} else {
38+
cropDetails.dst_width = control.params.flex_width ? control.params.height * ratio : control.params.width;
39+
cropDetails.dst_height = control.params.flex_height ? control.params.width / ratio : control.params.height;
40+
}
41+
42+
return wp.ajax.post( 'crop-image', {
43+
// where wp_customize: 'on' would be in Core, for no good reason I understand.
44+
nonce: attachment.get( 'nonces' ).edit,
45+
id: attachment.get( 'id' ),
46+
context: control.id,
47+
cropDetails: cropDetails
48+
} );
49+
}
50+
} );
51+
52+
1853

1954
/**
2055
* Calculate image selection options based on the attachment dimensions.
@@ -73,6 +108,11 @@
73108
*/
74109
$chooseButton.on( 'click', function () {
75110
var $el = $( this );
111+
var userId = $el.data( 'userId' );
112+
var mediaQuery = { type: 'image' };
113+
if ( userId ) {
114+
mediaQuery.author = userId;
115+
}
76116

77117
// Create the media frame.
78118
frame = wp.media( {
@@ -86,12 +126,12 @@
86126
states: [
87127
new wp.media.controller.Library( {
88128
title: $el.data( 'choose-text' ),
89-
library: wp.media.query( { type: 'image' } ),
129+
library: wp.media.query( mediaQuery ),
90130
date: false,
91131
suggestedWidth: $el.data( 'size' ),
92132
suggestedHeight: $el.data( 'size' ),
93133
} ),
94-
new wp.media.controller.CustomizeImageCropper( {
134+
new ImageCropperNoCustomizer( {
95135
control: {
96136
params: {
97137
width: $el.data( 'size' ),

templates/user-settings.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class="<?php echo \esc_attr( $classes_for_button ); ?>"
6565
data-choose-text="<?php \esc_attr_e( 'Choose a Header Image', 'activitypub' ); ?>"
6666
data-update-text="<?php \esc_attr_e( 'Change Header Icon', 'activitypub' ); ?>"
6767
data-update="<?php \esc_attr_e( 'Set as Header Image', 'activitypub' ); ?>"
68+
<?php
69+
// We only need to constrain the user_id for users who can't edit others' posts.
70+
if ( ! \current_user_can( 'edit_others_posts' ) ) {
71+
printf( 'data-user-id="%s"', esc_attr( \get_current_user_id() ) );
72+
}
73+
?>
6874
data-state="<?php echo \esc_attr( (int) $header_image ); ?>">
6975
<?php if ( (int) $header_image ) : ?>
7076
<?php \esc_html_e( 'Change Header Image', 'activitypub' ); ?>

0 commit comments

Comments
 (0)