mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 3k
Patterns: Don't inject theme attr on frontend #5455
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
Closed
ockham
wants to merge
10
commits into
WordPress:trunk
from
ockham:try/fixing-theme-attr-injection-perf-regression
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
909ec6b
Patterns: Don't inject theme attr on frontend
ockham 8fb2f79
Remove _inject_theme_attribute_in_template_part_block() and all its u…
felixarntz 9751e1c
Revert "Remove _inject_theme_attribute_in_template_part_block() and a…
felixarntz 9315aa3
Only call _inject_theme_attribute_in_template_part_block() if a REST …
felixarntz 5932fba
Only call _inject_theme_attribute_in_template_part_block() if a REST …
felixarntz 5c27373
Avoid duplicate code.
felixarntz 44d7eab
Remove tests which are no longer applicable.
felixarntz 14f6306
Implement test coverage to ensure template part block works correctly…
felixarntz 2af8248
Use `!== null` equality checks
ockham 17ca37a
Use `!== null` equality checks once more
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
tests/phpunit/tests/blocks/renderBlockCoreTemplatePart.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
/** | ||
* Tests for `render_block_core_template_part()`. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* | ||
* @group blocks | ||
*/ | ||
class Tests_Blocks_RenderBlockCoreTemplatePart extends WP_UnitTestCase { | ||
|
||
/** | ||
* Temporary storage for original block to render. | ||
* | ||
* @var array|null | ||
*/ | ||
private $original_block_to_render = null; | ||
|
||
/** | ||
* Flag to indicate whether to switch back to the default theme at tear down. | ||
* | ||
* @var bool | ||
*/ | ||
private $switch_to_default_theme_at_teardown = false; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
|
||
$this->original_block_to_render = WP_Block_Supports::$block_to_render; | ||
} | ||
|
||
public function tear_down() { | ||
WP_Block_Supports::$block_to_render = $this->original_block_to_render; | ||
$this->original_block_to_render = null; | ||
|
||
if ( $this->switch_to_default_theme_at_teardown ) { | ||
$this->switch_to_default_theme_at_teardown = false; | ||
switch_theme( WP_DEFAULT_THEME ); | ||
} | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Tests that the core template part block assumes the current theme if no theme attribute provided. | ||
* | ||
* @ticket 59583 | ||
*/ | ||
public function test_render_block_core_template_part_without_theme_attribute() { | ||
$this->maybe_switch_theme( 'block-theme' ); | ||
|
||
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/template-part' ); | ||
|
||
$content = render_block_core_template_part( array( 'slug' => 'small-header' ) ); | ||
|
||
$expected = '<header class="wp-block-template-part">' . "\n"; | ||
$expected .= '<p>Small Header Template Part</p>' . "\n"; | ||
$expected .= '</header>'; | ||
|
||
$this->assertSame( $expected, $content ); | ||
} | ||
|
||
/** | ||
* Tests that the core template part block returns the relevant part if current theme attribute provided. | ||
* | ||
* @ticket 59583 | ||
*/ | ||
public function test_render_block_core_template_part_with_current_theme_attribute() { | ||
$this->maybe_switch_theme( 'block-theme' ); | ||
|
||
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/template-part' ); | ||
|
||
$content = render_block_core_template_part( | ||
array( | ||
'slug' => 'small-header', | ||
'theme' => 'block-theme', | ||
) | ||
); | ||
|
||
$expected = '<header class="wp-block-template-part">' . "\n"; | ||
$expected .= '<p>Small Header Template Part</p>' . "\n"; | ||
$expected .= '</header>'; | ||
|
||
$this->assertSame( $expected, $content ); | ||
} | ||
|
||
/** | ||
* Tests that the core template part block returns nothing if theme attribute for a different theme provided. | ||
* | ||
* @ticket 59583 | ||
*/ | ||
public function test_render_block_core_template_part_with_another_theme_attribute() { | ||
$this->maybe_switch_theme( 'block-theme' ); | ||
|
||
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/template-part' ); | ||
|
||
$content = render_block_core_template_part( | ||
array( | ||
'slug' => 'small-header', | ||
'theme' => WP_DEFAULT_THEME, | ||
) | ||
); | ||
|
||
$expected = 'Template part has been deleted or is unavailable: small-header'; | ||
$this->assertSame( $expected, $content ); | ||
} | ||
|
||
/** | ||
* Switches the theme when not the default theme. | ||
* | ||
* @param string $theme Theme name to switch to. | ||
*/ | ||
private function maybe_switch_theme( $theme ) { | ||
if ( WP_DEFAULT_THEME === $theme ) { | ||
return; | ||
} | ||
|
||
switch_theme( $theme ); | ||
$this->switch_to_default_theme_at_teardown = true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We might consider moving the code that injects the
theme
attr to the patterns controller instead. Probably okay to leave as-is for now, but might be a nice follow-up post-6.4.(Noting that if we still insert hooked blocks here, we'd be running
traverse_and_serialize_blocks
twice, which is suboptimal; so we'd also need to move hooked blocks insertion to the controller so we can remove it here; which would then require us to make sure it's run on the frontend. We can probably achieve that through running it in the Pattern block'srender_block
. This means that we'd need to make quite a few changes, which we most likely don't want to do before 6.4.)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.
Can you explain why
traverse_and_serialize_blocks
would get called twice?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.
Assuming that we inject the
theme
attr in the patterns controller, but keep hooked blocks insertion here in the patterns registry (since it's needed both for the frontend and editor (i.e. REST API)) -- both of these calltraverse_and_serialize_blocks
😅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 see what you mean here - If we were to run the
theme
attribute injection in the REST API controller. Yes, good point that it would get called twice with different processing 👍🏻