-
Notifications
You must be signed in to change notification settings - Fork 384
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
Rework how and whether galleries are rendered as carousels #4775
Conversation
@pierlon Could you finish this one out? |
Sure will do. |
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
@googlebot I consent. |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
# Conflicts: # tests/php/test-class-amp-youtube-embed-handler.php
The PR description needs to be updated, but the PR is ready for review nonetheless. |
Plugin builds for 9d7260f are ready 🛎️!
|
if ( 'core/gallery' === name ) { | ||
settings.attributes.ampCarousel.default = ! select( 'amp/block-editor' ).hasThemeSupport(); // @todo We could just default this to false now even in Reader mode since block styles are loaded. | ||
} |
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 understand how this works. The default
is changed here, but when is this getting read? It's not part of the below code that does cloneElement()
.
This seems like the not ideal place to put this code as it is causing side effects. Something makes me nervous, like is this expected to work properly if old unmigrated posts are pasted into the editor? I just tried it and it seemed to work, but why?
Humm, and actually, when in legacy Reader mode and I edit a post that originally had:
<!-- wp:gallery {"ids":[1869,770,760],"ampLightbox":true} -->
It gets migrated as:
<!-- wp:gallery {"ids":[1869,770,760],"ampCarousel":false,"ampLightbox":true} -->
Why is ampCarousel
false? The call to select( 'amp/block-editor' ).hasThemeSupport()
is returning false
so then the default value of the ampCarousel
attribute should rather be true, right?
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 isn't taking effect because the save
callback containing this line runs some time after settings.attributes
is read, so I think some of this should be abstracted out and run in the filter callback rather than in the save
function.
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.
But it does seem to work. What filter callback could be used?
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 am not seeing a good way to do it entirely on the JS side, so I proposed a possible workaround here: #4775 (review)
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.
Re #4775 (comment):
I don't understand how this works. The default is changed here, but when is this getting read? It's not part of the below code that does
cloneElement()
.
Previously, the ampCarousel
attribute was being defaulted to true
when using legacy reader mode. So if the ampCarousel
attribute was not set on the block, that meant blockAttributes
in the save()
method will have it set to true
, and it would then be appended to the deprecatedProps
object, which should not be the case.
So the reasoning behind the change was to set the ampCarousel
attribute to always be false
, and then update the default value we know that the attribute did not have a truthy value set on the block. As I understand, the save()
method is called when the block is to be parsed and serialized, and the parsing of the block is what we want to filter so it can be updated accordingly.
The change I made in 6687931 is indeed hacky and warrants being nervous about; I haven't done much testing with it outside of the bug I was trying to fix. I'll try to see if there is any robust alternative to replicate the fix, but for now I defer to the workaround you provided in #4775 (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'm pushing up a commit to introduce a new Service
to formalize that. Let's see how it works…
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.
Please check b2901f0. I tried to explain as best I could the reason for the service, trying to recall the history of these 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.
Tested this a bit, and the only issue I'm finding is the one Weston pointed out here: #4775 (comment) It's going to be hard to find the best place on the JS side to set that default conditionally. Perhaps a boolean could be passed from the backend (with wp_add_inline_script
) indicating whether legacy reader is active. Then in addAMPAttributes
that value could be used to determine ampCarousel
's default.
As for the deprecation approach, I kinda like the (albeit hacky) backend approach that Weston proposed earlier. Since the data attributes are originally provided on the backend via a PHP filter -- in a way that Gutenberg isn't designed to be aware of -- it makes sense to me for the backend to take responsibility for cleaning it up. But if the solution implemented here is working, then no need to change it.
I think this is what we already have now with We basically need the reverse of this now-deleted code: addFilter( 'blocks.getSaveContent.extraProps', 'ampEditorBlocks/addExtraAttributes', addAMPExtraProps ); If not using PHP for this, and if not using |
Unfortunately as far as I know the last filter that runs on post content before it reaches the editor is in the REST handler.
|
@johnwatkins0 Actually, the data attributes were not originally added via PHP filter. They were added in JS via the |
To surface the PhpDoc comment from
|
I notice in legacy Reader mode that if you have a post with: <!-- wp:gallery {"ids":[752,586],"ampCarousel":true} --> it's migrated as: <!-- wp:gallery {"ids":[752,586]} --> The |
@westonruter Otherwise from that, the |
Yeah, this threw me a bit as well. However, I think it is expected. It's because the default value of the attribute is So I think this is OK. The only area where I see it being a problem is if you keep switching back and forth between legacy Reader mode and a non-legacy mode, which should not be normal. |
Ah, I see now. Yeah, in the absence of a JS filter to modify a block's raw HTML on the way into the editor, I do think handling in the REST endpoint is the most solid approach. The deprecation approach was working, but it does make me a little nervous to apply deprecation logic to a block we don't control. |
If nothing else, through this process we've had a good education to some block internals! |
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.
What a journey! Much has been learnt from working on this PR and it will definitely help in the future. I'm comfortable with how the obsolete attributes are removed from the post so it's a 👍 from me.
Commit 6eb20db prevents propagating the block attributes as HTML data attributes for shortcodes, preventing this from happening: There's no need to do such propagation since the shortcode attributes area already present and we're not stripping them out like we are the data attributes for block root elements. |
Merging in spite of unrelated TikTok oEmbed external HTTP request failure. |
Summary
This was co-authored with @pierlon.
Fixes #4774
gallery
shortcode to use carousel only if in legacy Reader mode; otherwise, render shortcode as normal in Standard/Transitional modes. In any mode, allow the presence of theamp-carousel
shortcode attribute to override the default behavior.amp-carousel
andamp-lightbox
attributes into thegallery
shortcode block. Remove the toggles from the block settings. Adding galleries via the shortcode block should not need special block settings toggles, as these attributes should be added directly by the author. Galleries normally will only be added via the Gallery block or the Classic block.data-
attributes into the block markup when layout, no-loading, lightbox, or carousel are enabled. Inject these rather via therender_block
filter. These are added merely as processing instructions for the sanitizers, and there is no need to duplicate the settings in both the block attributes and the attributes of the block's container element saved to the DB. This will need block deprecation logic added.propTypes
for AMP attributes.rest_sanitize_bool()
rather thanfilter_var()
.amp-carousel
and also specifies the lightbox attribute, only theamp-carousel
will possess said attribute. This means the gallery would use it's own lightbox; it is not shared with the other galleries and images on the page.large
size, if none is specified.Todo
Screenshots of Classic Block
Checklist