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

Post and Comment Template blocks: Change render_block_context priority to 1 #52364

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Jul 6, 2023

What?

Block context relevant for the Post Template and Comment Template blocks (i.e. post ID and type, and comment ID, respectively) is set via the render_block_context filter. This PR changes that filter's priority from the default 10 to 1 (i.e. as early as possible).

This PR is based on @dhl01's WordPress/wordpress-develop#4780.

Why?

As explained in https://core.trac.wordpress.org/ticket/58699, block context for those blocks was previously directly passed as an argument to the WP_Block constructor. During the 6.3 cycle, we changed that to use the render_block_context filter instead (in #50313 and #50279, later amended by #50883). Overall, the filter-based approach has some benefits that we'd rather keep; however, I missed at the time that any other (3rd-party) render_block_context filter with priority of 10 or less would no longer receive that block context.

How?

This PR remediates that problem by assigning the earliest possible priority to both filters.

Testing Instructions

We have automated test coverage for this:

public function test_rendering_comment_template_sets_comment_id_context() {
$parsed_comment_author_name_block = parse_blocks( '<!-- wp:comment-author-name /-->' )[0];
$comment_author_name_block = new WP_Block(
$parsed_comment_author_name_block,
array(
'commentId' => self::$comment_ids[0],
)
);
$comment_author_name_block_markup = $comment_author_name_block->render();
$this->assertNotEmpty(
$comment_author_name_block_markup,
'Comment Author Name block rendered markup is empty.'
);
$render_block_callback = static function( $block_content, $block ) use ( $parsed_comment_author_name_block ) {
// Insert a Comment Author Name block (which requires `commentId`
// block context to work) after the Comment Content block.
if ( 'core/comment-content' !== $block['blockName'] ) {
return $block_content;
}
$inserted_content = render_block( $parsed_comment_author_name_block );
return $inserted_content . $block_content;
};
add_filter( 'render_block', $render_block_callback, 10, 3 );
$parsed_blocks = parse_blocks(
'<!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template -->'
);
$block = new WP_Block(
$parsed_blocks[0],
array(
'postId' => self::$custom_post->ID,
)
);
$markup = $block->render();
remove_filter( 'render_block', $render_block_callback );
$this->assertStringContainsString(
$comment_author_name_block_markup,
$markup,
"Rendered markup doesn't contain Comment Author Name block."
);
}

Other than that, make sure that the Post Template and Comment Template blocks still work as before on the frontend. (This should be fairly straight-forward to verify in a current Block Theme, e.g. TT3.)

@ockham ockham added Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta [Block] Comment Template Affects the Comment Template Block [Block] Post Template Affects the Post Template Block labels Jul 6, 2023
@ockham ockham self-assigned this Jul 6, 2023
@ockham ockham added the Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) label Jul 6, 2023
@ockham ockham marked this pull request as ready for review July 6, 2023 09:43
@github-actions
Copy link

github-actions bot commented Jul 6, 2023

Flaky tests detected in 10e8d8c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5473873188
📝 Reported issues:

@dlh01
Copy link
Contributor

dlh01 commented Jul 6, 2023

Thanks for picking this up @ockham! Just noting I'm at @dlh01 if I can help with anything, not dhl 😄

@ockham
Copy link
Contributor Author

ockham commented Jul 7, 2023

Thanks for picking this up @ockham! Just noting I'm at @dlh01 if I can help with anything, not dhl 😄

Ahh, my bad! Apologies, I'll try to finally commit your GH handle to memory 😅

Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

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

Work as expected.

@cbravobernal cbravobernal merged commit 6ca7fcb into trunk Jul 7, 2023
@cbravobernal cbravobernal deleted the fix/post-and-comment-template-block-render-block-context-priority branch July 7, 2023 08:54
@github-actions github-actions bot added this to the Gutenberg 16.3 milestone Jul 7, 2023
@cbravobernal cbravobernal modified the milestones: Gutenberg 16.3, 16.1 Jul 7, 2023
@cbravobernal
Copy link
Contributor

I think this would need a cherry pick to Gutenberg 16.1 @tellthemachines @ramonjd

@ramonjd
Copy link
Member

ramonjd commented Jul 9, 2023

I think this would need a cherry pick to Gutenberg 16.1 @tellthemachines @ramonjd

Thanks for the ping. How egregious are these errors? Could they be part of 16.2.0 RC1?

I see a recent point release has been created for 16.1.2 - are you comfortable doing something similar if you think it should be in 16.1?

@tellthemachines
Copy link
Contributor

I just cherry-picked this PR to the update/beta-4-second-round branch to get it included in the next release: 69236dc

@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 Jul 10, 2023
@cbravobernal
Copy link
Contributor

Thanks for the ping. How egregious are these errors? Could they be part of 16.2.0 RC1?

I think they could be. I've seen they are included in beta 4, so is OK with that.

Thanks for everything!

@ockham
Copy link
Contributor Author

ockham commented Jul 10, 2023

Yeah, this doesn't need a 16.1.x IMO. I'll cherry-pick it into 16.2 though!

@ockham
Copy link
Contributor Author

ockham commented Jul 10, 2023

Forgot to say Thank You All!

@ockham
Copy link
Contributor Author

ockham commented Jul 10, 2023

Yeah, this doesn't need a 16.1.x IMO. I'll cherry-pick it into 16.2 though!

Done in 07ef985.

@ockham ockham removed the Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) label Jul 10, 2023
tellthemachines added a commit that referenced this pull request Jul 11, 2023
* Post and Comment Template blocks: Change render_block_context priority to 1 (#52364)

* Footnotes: fix lingering format boundary attr (#52439)

* Footnotes: Fix incorrect anchor position in Firefox (#52425)

* Scope CSS rules for the wp admin reset to js support only. (#52376)

* Fix: Patterns & template parts: remove "apply globally" option from block settings (#52160)

* Advanced styles panel: add an early return

* Update index.js

* Minor styling changes

* Use array of features

---------

Co-authored-by: George Mamadashvili <[email protected]>

* make the body of the editor minimmum viewport height so that smaller contents maintain clickability (#52406)

* Patterns: Add renaming, duplication, and deletion options (#52270)

* Patterns: Update manage pattern links to go to site editor if available (#52403)

* [Patterns] Separate sync status into a filter control (#52303)

Co-authored-by: Saxon Fletcher <[email protected]>
Co-authored-by: Glen Davies <[email protected]>

* Patterns: Add missing decoding entities processing in Patterns and Template/Parts pages (#52449)

* Fix document title icon appearance (#52424)

* Quote block: Add transform to paragraph (#51809)

* Add ungroup transform as transform to p

* Lint

* Update test and snapshot.

---------

Co-authored-by: Juan Aldasoro <[email protected]>

* remove sidebar group descriptions (#52453)

* Patterns:  alternative grid layout to improve keyboard accessibility (#52357)

* add sync tooltip (#52458)

* Patterns: Update section heading levels (#52273)

* Ensure that the unsaved title is not persisted when reopening the modal (#52473)

* Iframe: avoid asset parsing & fix script localisation (#52405)

* Iframe: avoid asset parsing & fix script localisation

* Add e2e test for script localisation

* Update descriptions (#52468)

* Footnotes: show in inserter and placehold (#52445)

* Footnotes: show in inserter and placehold

* Fix placeholder block membrane; fix copy; add icon, label

---------

Co-authored-by: Miguel Fonseca <[email protected]>

* Fix: Focus loss on navigation link label editing on Firefox. (#52428)

* Update tooltip (#52465)

* Refactor, document, and fix image block deprecations (#52081)

* Refactor, document, and fix image block deprecations

* Fix v5 attributes & supports

* Fix v1 & v2 deprecation tests

* Rename deprecated test descriptions

* Respect custom aspect ratio (#52286)

* add image width and height via css inline style, update width and height attrs to be string

* keep width and height as attributes too, keep the attributes as numbers

* updates image fixtures

* RichText/Footnotes: make getRichTextValues work with InnerBlocks.Content (#52241)

* RichText/Footnotes: make getRichTextValues work with InnerBlocks.Content

---------

Co-authored-by: Miguel Fonseca <[email protected]>

* Footnotes: save numbering through the entity provider (#52423)

* Footnotes: save numbering through the entity provider

* Add sup so no styling is needed at all

* Migrate old format

* Restore old styles, fix nested attribute queries

* Fix anchor selection

* Migrate markup in entity provider instead

* Fix tests

* Fix typo

* Fix comment

---------

Co-authored-by: Miguel Fonseca <[email protected]>

* Revert "Post editor: Require confirmation before removing Footnotes (#52277)" (#52486)

This reverts commit e6426ea.

* List block: Fix selected type option (#52472)

* Library - make pattern title clickable (#51898)

* Use button inside title

* Remove href

* Preserve roving tab index

* Fix link colors to match trunk $gray-600

* Remove redundant var

* Amend colors as per review

* remove old files again

---------

Co-authored-by: scruffian <[email protected]>

* remove status icon (#52457)

* Rename block theme activation nonce variable. (#52398)

---------

Co-authored-by: Bernie Reiter <[email protected]>
Co-authored-by: Ella <[email protected]>
Co-authored-by: Aki Hamano <[email protected]>
Co-authored-by: Andrea Fercia <[email protected]>
Co-authored-by: Carolina Nymark <[email protected]>
Co-authored-by: George Mamadashvili <[email protected]>
Co-authored-by: Andrei Draganescu <[email protected]>
Co-authored-by: Aaron Robertshaw <[email protected]>
Co-authored-by: Kai Hao <[email protected]>
Co-authored-by: Saxon Fletcher <[email protected]>
Co-authored-by: Glen Davies <[email protected]>
Co-authored-by: James Koster <[email protected]>
Co-authored-by: Rich Tabor <[email protected]>
Co-authored-by: Juan Aldasoro <[email protected]>
Co-authored-by: Miguel Fonseca <[email protected]>
Co-authored-by: Jorge Costa <[email protected]>
Co-authored-by: Alex Lende <[email protected]>
Co-authored-by: Héctor <[email protected]>
Co-authored-by: Petter Walbø Johnsgård <[email protected]>
Co-authored-by: Dave Smith <[email protected]>
Co-authored-by: scruffian <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Comment Template Affects the Comment Template Block [Block] Post Template Affects the Post Template Block
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

5 participants