-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Widget Editor: Fix allow adding same image twice #32951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Widget Editor: Fix allow adding same image twice #32951
Conversation
|
@thisissandip I understand that removing filtering solves the "duplicate" block problem, but won't it break the duplicate widget case it was originally meant to address? |
|
I wanted to test this patch with function-based widgets and then I ran into an even larger issue: #32960 |
|
@adamziel Thank you for taking a look! 😄
No, it won't. The filtering was blocking the blocks in the widget area with the same |
IIRC the function-based blocks do and this code is there to prevent adding more than one instance of these (because WordPress doesn't support that).
Fully agreed! I'm only saying that this check is there for a reason and removing it will break something else. It would be cool to rethink it somehow to both prevent adding multiple instances of the same function-based widgets AND support multiple similar images at the same time. I'm not sure how to approach that off the top of my head though |
@adamziel Okay! I'll try some other approach then! |
getdave
left a 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.
TBH I'm not clear from the code what the purpose of this removing widgets which have duplicate ID attribute. There's not a lot of context in the code and the comments don't illuminate much beyond the fact that we're doing filtering.
Given that not all blocks have an id attribute I wonder what we're trying to achieve here?
Perhaps @noisysocks or @talldan can advise?
|
@getdave IIRC we filter out the widgets with duplicate IDs to prevent adding more than one instance of a widget implemented using a function (e.g. marquee_greetings). WordPress doesn't support having more than one instance of these, if you try to save multiple |
|
@adamziel I have a question. If I am not wrong the function-based widgets can be added with legacy widget block right? So wrapping the filter with a block type check can solve the issue. |
How about #32951 (review) ? |
| } | ||
| return true; | ||
| } ); | ||
| const widgetsBlocks = post.blocks; |
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.
// Remove all duplicate reference widget instances for legacy widgets.
// Why? We filter out the widgets with duplicate IDs to prevent adding more than one instance of a widget
// implemented using a function. WordPress doesn't support having more than one instance of these, if you try to
// save multiple instances of these in different sidebars you will run into undefined behaviors.
const usedReferenceWidgets = [];
const widgetsBlocks = post.blocks.filter( ( block ) => {
const { id } = block.attributes;
if ( block.name === 'core/legacy-widget' && block.attributes?.id ) {
if ( usedReferenceWidgets.includes( id ) ) {
return false;
}
usedReferenceWidgets.push( id );
}
return 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.
I gave this a spin and it seemed to work.
I'd say we'd need some confirmation that the widget id guard is still correct as even with Adam's comment I (personally) don't have the full context/confidence that I've done this correctly.
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.
Screen.Capture.on.2021-06-24.at.17-12-27.mov
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.
Yes, it works! I tested with file and cover block too! 🎉
|
@getdave Yes! This is exactly what I was thinking! |
|
@getdave I have updated the code! Thanks for the snippet! |
Co-authored-by: Kai Hao <[email protected]>
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 makes sense to me, the id attribute of the legacy widget is fundamentally different from other block's id attributes, so shouldn't be confused.
Thanks so much for your contribution @thisissandip!
|
This looks great, thank you @thisissandip and @getdave ! |
* Widget Editor: Fix allow adding same image twice * remove duplicate reference widget instances only for legacy widgets * update id check Co-authored-by: Kai Hao <[email protected]> Co-authored-by: Kai Hao <[email protected]>
* Widget Editor: Fix allow adding same image twice * remove duplicate reference widget instances only for legacy widgets * update id check Co-authored-by: Kai Hao <[email protected]> Co-authored-by: Kai Hao <[email protected]>
Description
Fixes #32901
Adding same image twice in the widgets editor only saves one block. This PR fixes that.
This was happening because while saving/updating the blocks in the widget editor, duplicate widget instances were filtered. Whereas when multiple blocks (cover block/image block) use the same image, the
idattribute of the blocks becomes identical and only the first block was saved.How has this been tested?
Types of changes
bug fix
Checklist: