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

Block Hooks: Not properly working with Classic block in Post Content (first/last child insertion) #68988

Closed
2 of 6 tasks
ockham opened this issue Jan 31, 2025 · 3 comments · Fixed by #69241
Closed
2 of 6 tasks
Assignees
Labels
[Feature] Block hooks [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@ockham
Copy link
Contributor

ockham commented Jan 31, 2025

Description

As reported by @gziolo here:

One thing that might need closer look is when using the Classic block in the post content. I couldn't make it work with inserting the hooked block as the last or first child in the core/post-content block.

Step-by-step reproduction instructions

TBD

Screenshots, screen recording, code snippet

Screenshot 2025-01-30 at 14 33 13 Screenshot 2025-01-30 at 14 33 43

Environment info

No response

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@ockham ockham added [Feature] Block hooks [Type] Bug An existing feature does not function as intended labels Jan 31, 2025
@t-hamano
Copy link
Contributor

t-hamano commented Feb 2, 2025

Is this a recent issue? If so, we can add this issue to the 6.8 project board.

@ockham
Copy link
Contributor Author

ockham commented Feb 3, 2025

@t-hamano Yes, this was found by @gziolo when reviewing my other bugfix. I think we should add it to the 6.8 board 👍

I won't be able to work on a fix this week, but I'll look into it next week.

@ockham
Copy link
Contributor Author

ockham commented Feb 17, 2025

I think I see what's going on here.

This behavior is a result of how we insert blocks in the first_child and last_child position. In short, that insertion doesn't happen when we're visiting the anchor block (i.e. the future parent block of the hooked child block that's about to be inserted), but rather when visiting that anchor's current first and last child, respectively. (Here's the relevant code for first child and last child insertion.)

AFAIR, we chose this way as it fit in better with the chosen function signatures of the visitor functions (and thus, the information available to the visitors). IIRC, it also corresponded better to user expectations, as it allowed us to eschew an edge case, but my memory is a bit fuzzy here.

However, this meant that child insertion would only work if the anchor (parent) block had at least one child. This was deemed a reasonable drawback at the time, as it also aligned with the use cases we envisioned.

The problem with the Classic block it isn't really a block -- it is stored without block delimiters, e.g.

<h1>Hello</h1>
<p>nice</p>

When the Block Hooks algorithms runs on post content like the above (and creates an ad-hoc "virtual" core/post-content wrapper block), it ends up with something like the following;

<!-- wp:post-content -->
<h1>Hello</h1>
<p>nice</p>
<!-- /wp:post-content -->

There's no child block for the visitor function to run on and insert the hooked first of last child block.


I'll try to think of a solution. One option would be to wrap the classic content in an ad-hoc virtual classic block wrapper, akin to the virtual Post Content block. I need to look up our handling of the Classic block on the server and if we have any precedent for a virtual core/freeform block wrapper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block hooks [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
Development

Successfully merging a pull request may close this issue.

2 participants