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: Implement block insertion functions #5240

Closed
wants to merge 1 commit into from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Sep 18, 2023

See https://core.trac.wordpress.org/ticket/59313 and #5158.

Trac ticket: https://core.trac.wordpress.org/ticket/59385


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ockham ockham self-assigned this Sep 18, 2023
@ockham ockham changed the title Add/insert hooked blocks Implement Hooked Blocks insertion Sep 18, 2023
@ockham ockham force-pushed the add/insert-hooked-blocks branch 2 times, most recently from a665872 to dd8f187 Compare September 18, 2023 13:27
@ockham ockham changed the title Implement Hooked Blocks insertion Block: Implement Hooked Blocks insertion functions Sep 18, 2023
@ockham
Copy link
Contributor Author

ockham commented Sep 18, 2023

@gziolo I've decided to only implement insert_block_hook and inser_block_hooks here (plus unit test coverage). We'll only wire them to block templates and patterns to actually insert blocks in a follow-up 😄

Edit: I've updated the tracking ticket accordingly.

<!-- /wp:paragraph -->
</div>
<!-- /wp:tests/group -->
HTML
Copy link
Contributor Author

Choose a reason for hiding this comment

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

PHP 7.0 didn't like the trailing comma 😕

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that is a bit annoying. Usually, you need to put the string in its variable to use Heredoc:

Warning
Prior to PHP 7.3.0, it is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.

So it's only the semicolon allowed which won't work here 😞

@ockham
Copy link
Contributor Author

ockham commented Sep 18, 2023

@gziolo There's another fix we might have to backport to GB: e9931e7.

@ockham
Copy link
Contributor Author

ockham commented Sep 18, 2023

@gziolo This needs test coverage for insert_hooked_blocks (plural) (see #5241 (comment)); otherwise, it might be about ready 🤞

@ockham
Copy link
Contributor Author

ockham commented Sep 18, 2023

Will need #5242 to eventually pass the parent arg to insert_hooked_blocks.

src/wp-includes/blocks.php Outdated Show resolved Hide resolved
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

insert_hooked_block seems ready to go.

src/wp-includes/blocks.php Show resolved Hide resolved
src/wp-includes/blocks.php Outdated Show resolved Hide resolved
<!-- /wp:paragraph -->
</div>
<!-- /wp:tests/group -->
HTML
Copy link
Member

Choose a reason for hiding this comment

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

Yes, that is a bit annoying. Usually, you need to put the string in its variable to use Heredoc:

Warning
Prior to PHP 7.3.0, it is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.

So it's only the semicolon allowed which won't work here 😞

src/wp-includes/blocks.php Outdated Show resolved Hide resolved
@ockham
Copy link
Contributor Author

ockham commented Sep 19, 2023

I was considering doing one more thing: Split insert_hooked_block into insert_hooked_child_block and insert_hooked_sibling_block, since they're pretty different. I also might adjust the function signature of the latter a bit more to align with this.

@ockham
Copy link
Contributor Author

ockham commented Sep 19, 2023

I was considering doing one more thing: Split insert_hooked_block into insert_hooked_child_block and insert_hooked_sibling_block, since they're pretty different. I also might adjust the function signature of the latter a bit more to align with this.

Might actually rename to insert_child_block and insert_sibling_block, since they're arguably not even that specific to hooked blocks.

@ockham ockham force-pushed the add/insert-hooked-blocks branch from a3ebe61 to 257e1a0 Compare September 19, 2023 08:39
@ockham
Copy link
Contributor Author

ockham commented Sep 19, 2023

@gziolo Should be ready for review! I've changed it a lot, but I'm really happy with it now. It's much less specific to hooked blocks now, yet the functions are exactly the way we'll need them 😄

@ockham ockham marked this pull request as ready for review September 19, 2023 09:51
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

I had a minor note about the return values that seem obsolete for the helper functions because they all mutate the first param, which is then returned. Anyway, I don't have strong feelings about it so if there is any reason to keep them, feel free to leave it as is.

src/wp-includes/blocks.php Outdated Show resolved Hide resolved
@gziolo
Copy link
Member

gziolo commented Sep 19, 2023

Test failures seem unrelated, so maybe there is a need to rebase the PR.

I like how the code is split now into smaller helper functions that are specialized and nicely encapsulate their task. One final note, they will be exposed in the dev docs portal, which I'm fine with.

@ockham
Copy link
Contributor Author

ockham commented Sep 19, 2023

Test failures seem unrelated, so maybe there is a need to rebase the PR.

Yeah, looks like unit tests have been failing for all recent PRs 😕

I like how the code is split now into smaller helper functions that are specialized and nicely encapsulate their task. One final note, they will be exposed in the dev docs portal, which I'm fine with.

Yeah, I think that's perfectly okay -- the functions are now generic enough that they might even come in handy for different purposes.

@ockham ockham force-pushed the add/insert-hooked-blocks branch from 3b18937 to 4812c96 Compare September 19, 2023 11:34
@ockham ockham changed the title Block: Implement Hooked Blocks insertion functions Block: Implement block insertion functions Sep 19, 2023
@ockham ockham force-pushed the add/insert-hooked-blocks branch from 099ad1b to 1c5c63e Compare September 19, 2023 12:23
For #59313, we need to implement functions to insert a given parsed block into another parsed block's inner blocks, and to prepend and append to that array, respectively.

We will use those functions in combination with `traverse_and_serialize_blocks` (see #59327) to implement automatic insertion of hooked blocks into block templates and patterns.

Props gziolo.
Fixes #59385.
@ockham ockham force-pushed the add/insert-hooked-blocks branch from 1c5c63e to 13aa0cf Compare September 19, 2023 12:33
@ockham
Copy link
Contributor Author

ockham commented Sep 19, 2023

Committed to Core in https://core.trac.wordpress.org/changeset/56618.

@ockham ockham closed this Sep 19, 2023
@ockham ockham deleted the add/insert-hooked-blocks branch September 19, 2023 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants