Skip to content

Commit 6ae5afc

Browse files
committed
Editor: svn add new files missed in [55285].
Follow-up to [55285]. See #57618. git-svn-id: https://develop.svn.wordpress.org/trunk@55286 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f7c2299 commit 6ae5afc

File tree

2 files changed

+337
-0
lines changed

2 files changed

+337
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Position block support flag.
4+
*
5+
* @package WordPress
6+
* @since 6.2.0
7+
*/
8+
9+
/**
10+
* Registers the style block attribute for block types that support it.
11+
*
12+
* @since 6.2.0
13+
* @access private
14+
*
15+
* @param WP_Block_Type $block_type Block Type.
16+
*/
17+
function wp_register_position_support( $block_type ) {
18+
$has_position_support = block_has_support( $block_type, array( 'position' ), false );
19+
20+
// Set up attributes and styles within that if needed.
21+
if ( ! $block_type->attributes ) {
22+
$block_type->attributes = array();
23+
}
24+
25+
if ( $has_position_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
26+
$block_type->attributes['style'] = array(
27+
'type' => 'object',
28+
);
29+
}
30+
}
31+
32+
/**
33+
* Renders position styles to the block wrapper.
34+
*
35+
* @since 6.2.0
36+
* @access private
37+
*
38+
* @param string $block_content Rendered block content.
39+
* @param array $block Block object.
40+
* @return string Filtered block content.
41+
*/
42+
function wp_render_position_support( $block_content, $block ) {
43+
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
44+
$has_position_support = block_has_support( $block_type, array( 'position' ), false );
45+
46+
if (
47+
! $has_position_support ||
48+
empty( $block['attrs']['style']['position'] )
49+
) {
50+
return $block_content;
51+
}
52+
53+
$global_settings = wp_get_global_settings();
54+
$theme_has_sticky_support = _wp_array_get( $global_settings, array( 'position', 'sticky' ), false );
55+
$theme_has_fixed_support = _wp_array_get( $global_settings, array( 'position', 'fixed' ), false );
56+
57+
// Only allow output for position types that the theme supports.
58+
$allowed_position_types = array();
59+
if ( true === $theme_has_sticky_support ) {
60+
$allowed_position_types[] = 'sticky';
61+
}
62+
if ( true === $theme_has_fixed_support ) {
63+
$allowed_position_types[] = 'fixed';
64+
}
65+
66+
$style_attribute = _wp_array_get( $block, array( 'attrs', 'style' ), null );
67+
$class_name = wp_unique_id( 'wp-container-' );
68+
$selector = ".$class_name";
69+
$position_styles = array();
70+
$position_type = _wp_array_get( $style_attribute, array( 'position', 'type' ), '' );
71+
$wrapper_classes = array();
72+
73+
if (
74+
in_array( $position_type, $allowed_position_types, true )
75+
) {
76+
$wrapper_classes[] = $class_name;
77+
$wrapper_classes[] = 'is-position-' . $position_type;
78+
$sides = array( 'top', 'right', 'bottom', 'left' );
79+
80+
foreach ( $sides as $side ) {
81+
$side_value = _wp_array_get( $style_attribute, array( 'position', $side ) );
82+
if ( null !== $side_value ) {
83+
/*
84+
* For fixed or sticky top positions,
85+
* ensure the value includes an offset for the logged in admin bar.
86+
*/
87+
if (
88+
'top' === $side &&
89+
( 'fixed' === $position_type || 'sticky' === $position_type )
90+
) {
91+
// Ensure 0 values can be used in `calc()` calculations.
92+
if ( '0' === $side_value || 0 === $side_value ) {
93+
$side_value = '0px';
94+
}
95+
96+
// Ensure current side value also factors in the height of the logged in admin bar.
97+
$side_value = "calc($side_value + var(--wp-admin--admin-bar--position-offset, 0px))";
98+
}
99+
100+
$position_styles[] =
101+
array(
102+
'selector' => $selector,
103+
'declarations' => array(
104+
$side => $side_value,
105+
),
106+
);
107+
}
108+
}
109+
110+
$position_styles[] =
111+
array(
112+
'selector' => $selector,
113+
'declarations' => array(
114+
'position' => $position_type,
115+
'z-index' => '10',
116+
),
117+
);
118+
}
119+
120+
if ( ! empty( $position_styles ) ) {
121+
/*
122+
* Add to the style engine store to enqueue and render position styles.
123+
*/
124+
wp_style_engine_get_stylesheet_from_css_rules(
125+
$position_styles,
126+
array(
127+
'context' => 'block-supports',
128+
'prettify' => false,
129+
)
130+
);
131+
132+
// Inject class name to block container markup.
133+
$content = new WP_HTML_Tag_Processor( $block_content );
134+
$content->next_tag();
135+
foreach ( $wrapper_classes as $class ) {
136+
$content->add_class( $class );
137+
}
138+
return (string) $content;
139+
}
140+
141+
return $block_content;
142+
}
143+
144+
// Register the block support.
145+
WP_Block_Supports::get_instance()->register(
146+
'position',
147+
array(
148+
'register_attribute' => 'wp_register_position_support',
149+
)
150+
);
151+
add_filter( 'render_block', 'wp_render_position_support', 10, 2 );
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
/**
4+
* @group block-supports
5+
*
6+
* @covers ::wp_render_position_support
7+
*/
8+
class Tests_Block_Supports_WpRenderPositionSupport extends WP_UnitTestCase {
9+
/**
10+
* @var string|null
11+
*/
12+
private $test_block_name;
13+
14+
/**
15+
* Theme root directory.
16+
*
17+
* @var string
18+
*/
19+
private $theme_root;
20+
21+
/**
22+
* Original theme directory.
23+
*
24+
* @var string
25+
*/
26+
private $orig_theme_dir;
27+
28+
public function set_up() {
29+
parent::set_up();
30+
$this->test_block_name = null;
31+
$this->theme_root = realpath( DIR_TESTDATA . '/themedir1' );
32+
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
33+
34+
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
35+
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
36+
37+
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
38+
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
39+
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
40+
41+
// Clear caches.
42+
wp_clean_themes_cache();
43+
unset( $GLOBALS['wp_themes'] );
44+
WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
45+
}
46+
47+
public function tear_down() {
48+
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
49+
50+
// Clear up the filters to modify the theme root.
51+
remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
52+
remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
53+
remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
54+
55+
wp_clean_themes_cache();
56+
unset( $GLOBALS['wp_themes'] );
57+
WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
58+
unregister_block_type( $this->test_block_name );
59+
$this->test_block_name = null;
60+
parent::tear_down();
61+
}
62+
63+
public function filter_set_theme_root() {
64+
return $this->theme_root;
65+
}
66+
67+
/**
68+
* Tests that position block support works as expected.
69+
*
70+
* @ticket 57618
71+
*
72+
* @covers ::wp_render_position_support
73+
*
74+
* @dataProvider data_position_block_support
75+
*
76+
* @param string $theme_name The theme to switch to.
77+
* @param string $block_name The test block name to register.
78+
* @param mixed $position_settings The position block support settings.
79+
* @param mixed $position_style The position styles within the block attributes.
80+
* @param string $expected_wrapper Expected markup for the block wrapper.
81+
* @param string $expected_styles Expected styles enqueued by the style engine.
82+
*/
83+
public function test_position_block_support( $theme_name, $block_name, $position_settings, $position_style, $expected_wrapper, $expected_styles ) {
84+
switch_theme( $theme_name );
85+
$this->test_block_name = $block_name;
86+
87+
register_block_type(
88+
$this->test_block_name,
89+
array(
90+
'api_version' => 2,
91+
'attributes' => array(
92+
'style' => array(
93+
'type' => 'object',
94+
),
95+
),
96+
'supports' => array(
97+
'position' => $position_settings,
98+
),
99+
)
100+
);
101+
102+
$block = array(
103+
'blockName' => 'test/position-rules-are-output',
104+
'attrs' => array(
105+
'style' => array(
106+
'position' => $position_style,
107+
),
108+
),
109+
);
110+
111+
$actual = wp_render_position_support( '<div>Content</div>', $block );
112+
113+
$this->assertMatchesRegularExpression(
114+
$expected_wrapper,
115+
$actual,
116+
'Position block wrapper markup should be correct'
117+
);
118+
119+
$actual_stylesheet = wp_style_engine_get_stylesheet_from_context(
120+
'block-supports',
121+
array(
122+
'prettify' => false,
123+
)
124+
);
125+
126+
$this->assertMatchesRegularExpression(
127+
$expected_styles,
128+
$actual_stylesheet,
129+
'Position style rules output should be correct'
130+
);
131+
}
132+
133+
/**
134+
* Data provider.
135+
*
136+
* @return array
137+
*/
138+
public function data_position_block_support() {
139+
return array(
140+
'sticky position style is applied' => array(
141+
'theme_name' => 'block-theme-child-with-fluid-typography',
142+
'block_name' => 'test/position-rules-are-output',
143+
'position_settings' => true,
144+
'position_style' => array(
145+
'type' => 'sticky',
146+
'top' => '0px',
147+
),
148+
'expected_wrapper' => '/^<div class="wp-container-\d+ is-position-sticky">Content<\/div>$/',
149+
'expected_styles' => '/^.wp-container-\d+' . preg_quote( '{top:calc(0px + var(--wp-admin--admin-bar--position-offset, 0px));position:sticky;z-index:10;}' ) . '$/',
150+
),
151+
'sticky position style is not applied if theme does not support it' => array(
152+
'theme_name' => 'default',
153+
'block_name' => 'test/position-rules-without-theme-support',
154+
'position_settings' => true,
155+
'position_style' => array(
156+
'type' => 'sticky',
157+
'top' => '0px',
158+
),
159+
'expected_wrapper' => '/^<div>Content<\/div>$/',
160+
'expected_styles' => '/^$/',
161+
),
162+
'sticky position style is not applied if block does not support it' => array(
163+
'theme_name' => 'block-theme-child-with-fluid-typography',
164+
'block_name' => 'test/position-rules-without-block-support',
165+
'position_settings' => false,
166+
'position_style' => array(
167+
'type' => 'sticky',
168+
'top' => '0px',
169+
),
170+
'expected_wrapper' => '/^<div>Content<\/div>$/',
171+
'expected_styles' => '/^$/',
172+
),
173+
'sticky position style is not applied if type is not valid' => array(
174+
'theme_name' => 'block-theme-child-with-fluid-typography',
175+
'block_name' => 'test/position-rules-with-valid-type',
176+
'position_settings' => true,
177+
'position_style' => array(
178+
'type' => 'illegal-type',
179+
'top' => '0px',
180+
),
181+
'expected_wrapper' => '/^<div>Content<\/div>$/',
182+
'expected_styles' => '/^$/',
183+
),
184+
);
185+
}
186+
}

0 commit comments

Comments
 (0)