-
Notifications
You must be signed in to change notification settings - Fork 8
Handle deletion of attachment associated with media widget #27
Conversation
*/ | ||
public function display_media_state( $states, $post ) { | ||
$settings = $this->get_settings(); | ||
$attachment_id = empty( $settings[ $this->number ]['attachment_id'] ) ? 0 : $settings[ $this->number ]['attachment_id']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only going to work if you have one image widget. If you have multiple image widgets, then you'd have to iterate over all $settings
to check each one to see if it has attachment_id
that is the same as $post->ID
. As it is right now, it's only looking at the current $this->number
widget instance, which is probably going to be the last one registered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in db98115.
* @param WP_Post $post The current attachment object. | ||
* @return array | ||
*/ | ||
public function display_media_state( $states, $post ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this maybe be moved to the base WP_Widget_Media
class? Naturally some of the hard-coded values here would need to change, like replace 'image/'
with $this->widget_options['mime_type']
and use $this->name
instead of 'Image Widget'
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use $this->name instead of 'Image Widget'
Duh! I didn't think of that. Yes it should be in WP_Widget_Media
, the only reason it's not is because I didn't want a hard coded array of names. So yeah, that solves it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something else related to the behavior of a widget upon deletion of its associated attachment is what happens when you try deleting the image used for the image widget you're currently editing. See https://cloudup.com/cJs4gRGYnXE
Per https://github.com/xwp/wp-core-media-widgets/pull/27/files#r106510104 I think this should result in the selected image being removed from the widget, and a notification shown that the image was removed.
*/ | ||
public function delete_attachment_data( $post_id ) { | ||
$settings = $this->get_settings(); | ||
$attachment_id = empty( $settings[ $this->number ]['attachment_id'] ) ? 0 : $settings[ $this->number ]['attachment_id']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As done in display_media_state
, this will need to loop over all $settings
not just $settings[ $this->number ]
.
* | ||
* @param int $post_id Attachment ID. | ||
*/ | ||
public function delete_attachment_data( $post_id ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleting the widget instances when the associated attachment is deleted makes me nervous. I think it would be better to instead cause the widget to not render anything, and show a notification in the widget control that the attachment has been removed. For example, I think it should behave like an RSS widget pointing to a 404 feed URL.
By showing a notice that the attachment has been deleted, there would be less user mystery when they find a widget disappeared. Deleting the widget would also cause the widget's title to be dropped, resulting in data loss. I think it would be better if a user discovers they deleted an image used by an image widget, and then picks another image for that existing widget, rather than having to create a whole new widget from scratch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
➕ I was thinking the same thing - when the media item is deleted, perhaps just not render anything - or perhaps if the user has a certain role, and is viewing the site, then show a notice in the widget area that the attachment is missing.
Message needs more work. See #27 (comment) 04
7d566e9 introduced an error message when an attachment can't be loaded: @melchoyce What should the message look like and say? EDIT: We should probably not display the Edit button in case of an error either. |
7d566e9
to
15f66db
Compare
Message needs more work. See #27 (comment) 04
How about:
We'll also want to make sure the text is left-aligned. (Props @michelleweber) |
Thank you! Updated in c5bc6d5 |
@westonruter: Using the widget name doesn't work too well: I'm not sure how I feel about |
@obenland oh I forgot that “widget” wasn't actually part of the name. Two options I can think of:
I think 2 would be a better choice. |
Agreed |
@@ -33,6 +33,10 @@ public function __construct() { | |||
'edit_media' => __( 'Edit Image' ), | |||
'change_media' => __( 'Change Image' ), | |||
'select_media' => __( 'Select Image' ), | |||
'error' => sprintf( | |||
__( 'We can’t find that image. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Backbone view for should have a new event handler for clicking on this link to have the same result as clicking the "Select Media" or "Change Media" buttons. The underlying href
can remain there so they can right-click to open the media library in a new tab, but otherwise we should keep them inside the customizer and open the select frame for the media modal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 43e5c93.
I decided to go with a new selector that is a little broader than I'd like it to be, in order to not give translators the chance to break functionality by accidentally omitting the .select-media
class.
30bfe96
to
a5848c3
Compare
Message needs more work. See #27 (comment) 04
43e5c93
to
77735b2
Compare
|
||
if ( $use_count > 1 ) { | ||
/* translators: %d is widget count */ | ||
$states[] = sprintf( __( 'Media Widgets (%d)' ), $use_count ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be changed to reference $this->l10n['media_library_state']
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd want a singular and a plural version? Or just the singular?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plural conditional was an enhancement I came up with. Maybe it wouldn't be useful in practice, but I can imagine that it would be helpful for users to know how many widgets are using the image.
If we have both, then perhaps we'd need media_library_state_singular_widget
and media_library_state_multiple_widgets
'change_media' => __( 'Change Media' ), | ||
'edit_media' => __( 'Edit Media' ), | ||
'error' => sprintf( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There could be additional errors in the future, so I think this should be attachment_missing_error
instead.
wp-admin/js/widgets/media-widgets.js
Outdated
@@ -73,6 +73,7 @@ wp.mediaWidgets = ( function( $ ) { | |||
* @type {Object} | |||
*/ | |||
events: { | |||
'click .notice a': 'selectMedia', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the link should have a class name on it. If a subclass introduces a different error with a link in it, we'd want to make sure this event handler only handles clicks on the media library link. So either the container . notice
element have a class like missing-attachment-error
, or the link itself can have a media-library
class that is then used in the selector here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I prefer the former, I'd like to avoid a situation where translators accidentally break functionality.
wp-admin/js/widgets/media-widgets.js
Outdated
} ); | ||
attachment.fetch() | ||
.done( function() { | ||
control.selectedAttachment.set( attachment.attributes ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also ensure that the error
flag is cleared for good measure?
control.selectedAttachment.set( _.extend( {}, attachment.attributes, { error: false } ) );
wp-admin/js/widgets/media-widgets.js
Outdated
control.selectedAttachment.set( attachment.attributes ); | ||
} ) | ||
.fail( function() { | ||
control.selectedAttachment.set( { error: true } ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the error
here should be a code like missing_attachment
instead of a boolean to anticipate additional error types in the future.
See #27 (comment) 28, #27 (comment) 78, #27 (comment) 07, #27 (comment) 45
Deletes the widget when its corresponding attachment gets deleted. Also adds a label to the attachment in the media list, when its used in a widget. Fixes #22.
Message needs more work. See #27 (comment) 04
See #27 (comment) 28, #27 (comment) 78, #27 (comment) 07, #27 (comment) 45
0b7def3
to
b760046
Compare
Should have all your change requests addressed? |
@obenland I saved an image widget that had an assigned attachment. I then deleted the underlying attachment and reloaded the customizer. When I did, I saw: I would have expected there to be an error message. It seems the order of the conditionals wasn't right? I changed them in 7f32d28 and ac1239a, and now I see as expected: |
Something to note is that the |
esc_url( admin_url( 'upload.php' ) ) | ||
), | ||
/* translators: %s is widget count */ | ||
'media_library_state' => _n_noop( 'Image Widget', 'Image Widget (%s)' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@obenland PHP_CodeSniffer is complaining here:
Missing singular placeholder, needed for some languages. See https://codex.wordpress.org/I18n_for_WordPress_Developers#Plurals (WordPress.WP.I18n.MissingSingularPlaceholder)
Is this valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe these should actually be two separate strings.
For more about this, see WordPress/WordPress-Coding-Standards#567 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's a valid concern in our case. We're not saying One Widget
, 2 Widgets
, the label's singular/plural version is of no concern here really.
IMO we should rather skip the rule for that line than to introduce a language workaround (which in German at least reads quite awkwardly), what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #39.
Shows an error message in the media widget when its associated attachment is deleted.
Also adds a label to the attachment in the media list, when its used in a widget.
Fixes #22.
/cc @timmyc