Skip to content
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

Gallery: Fix block registration hook priority #37409

Merged
merged 1 commit into from
Dec 17, 2021

Conversation

Mamaduka
Copy link
Member

@Mamaduka Mamaduka commented Dec 15, 2021

Description

Fixes #37391.

All core blocks use the default (10) init hook priority for registration. Using higher priority causes gutenberg_reregister_core_block_types (which also uses default priority) to incorrectly re-register blocks.

@gziolo's flow summary:

  • register from block.json with priority 10
  • unregister the block, register from core later at priority 20
  • nothing to unregister, register from plugin later at priority 20

Testing

This needs to be backported to the core for proper testing. Meanwhile, you can temporarily edit the WP core file and match changes in this PR.

The following PHP notice shouldn't be visible:

Notice: WP_Block_Type_Registry::register was called incorrectly. Block type "core/gallery" is already registered.

Types of changes

Bugfix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@Mamaduka Mamaduka requested a review from mkevins as a code owner December 15, 2021 13:34
@Mamaduka Mamaduka self-assigned this Dec 15, 2021
@Mamaduka Mamaduka added [Block] Gallery Affects the Gallery Block - used to display groups of images [Type] Bug An existing feature does not function as intended labels Dec 15, 2021
@Mamaduka
Copy link
Member Author

Pinging @ramonjd in case this priority was intentional.

@gziolo
Copy link
Member

gziolo commented Dec 15, 2021

In b844345 I removed duplicated entries for block registration. If there is a PHP file we use it to register blocks, otherwise we execute register_block_type_from_metadata using the block.json file. It's also worth noting that blocks get registered from block.json with the same priority as gutenberg_reregister_core_block_types which is 10.

Sidenote: PHP logic for the Gallery and Image block got added in #35975.

@gziolo gziolo requested a review from ramonjd December 15, 2021 14:17
@ramonjd
Copy link
Member

ramonjd commented Dec 15, 2021

Pinging @ramonjd in case this priority was intentional.

Thanks for the heads up. No it wasn't intentional, I took the priority argument from existing blocks and forgot (or didn't know) to remove the gallery from the registration list.

Thanks for discovering this one @Mamaduka 🙇

I just retested this change and the gallery behaves as intended. I can't see the PHP notices.

For the record, I tested the navigation block in the editor as well. Links and sub-menus work as I'd expect too.

@Mamaduka
Copy link
Member Author

Thanks for testing, @ramonjd.

I was unsure about changing priorities after seeing this comment. Can you give me more details on how to test this case?

@Mamaduka Mamaduka force-pushed the fix/gallery-registration-priority branch from b844345 to 22f6831 Compare December 16, 2021 04:54
@ramonjd
Copy link
Member

ramonjd commented Dec 16, 2021

I was unsure about changing priorities after seeing this comment.

Sorry, I think my comment there might have stemmed from a misunderstanding on my part about the way we register core blocks in blocks.php.

I see that if a block is in the block_names list, we run it through register_block_type_from_metadata .

In #35975 I was calling register_block_type_from_metadata again from the Gallery and Image index.php files, so it's maybe why adding the priority helped due to double registration (?), which you've noted in the description for this PR.

Can you give me more details on how to test this case?

The outcome we're looking for is to see the data-id attribute for images within galleries only, and on the frontend only.

Screen Shot 2021-12-17 at 7 54 36 am

I just ran this branch again and it's showing for me consistently. 👍

Thanks again for fixing this!!

@ramonjd
Copy link
Member

ramonjd commented Dec 16, 2021

I see a commit that removes duplicated entries for block registrations, including the gallery and image entries I added 😇 : b844345#diff-4167605a44440d5cfb787398fed6a2c0e85f2d83d358d57b9d9dba29ecb50734

But it's not yet on trunk. Should we add it here?

Oh, I just read the latest update to the PR description under flow summary. 🙇

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for looking at this @Mamaduka

This needs to be backported to the core for proper testing. Meanwhile, you can temporarily edit the WP core file and match changes in this PR.

I edited the WP core file to match the changes in this PR and can confirm that the PHP notice Notice: WP_Block_Type_Registry::register was called incorrectly. Block type "core/gallery" is already registered. no longer appears.

The gallery works as expected, that is, the data-id is added to Image Blocks within a Gallery.

👍

@ramonjd ramonjd added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Dec 17, 2021
@Mamaduka Mamaduka merged commit 8b8313d into trunk Dec 17, 2021
@Mamaduka Mamaduka deleted the fix/gallery-registration-priority branch December 17, 2021 06:16
@github-actions github-actions bot added this to the Gutenberg 12.3 milestone Dec 17, 2021
@tellthemachines tellthemachines removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Gallery Affects the Gallery Block - used to display groups of images [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PHP notice when activating Gutenberg trunk on WP trunk (5.9 beta 3)
4 participants